diff --git a/src/my-calendar-styles.php b/src/my-calendar-styles.php index 8b0345364..190e50955 100644 --- a/src/my-calendar-styles.php +++ b/src/my-calendar-styles.php @@ -13,210 +13,56 @@ exit; } -/** - * Migrate CSS file to custom. - */ -function mc_migrate_css() { - if ( isset( $_GET['migrate'] ) ) { - if ( 'false' === $_GET['migrate'] ) { - mc_update_option( 'migrated', 'true' ); - mc_show_notice( __( 'Thank you! Your site will be updated to the latest version of core CSS at every update.', 'my-calendar' ) ); - } else { - $verify = wp_verify_nonce( $_GET['migrate'], 'mc-migrate-css' ); - if ( ! $verify ) { - wp_die( 'My Calendar: Permissions not granted to migrate CSS.', 'my-calendar' ); - } else { - global $wp_filesystem; - WP_Filesystem(); - $style = mc_get_option( 'css_file' ); - $stylefile = mc_get_style_path(); - $newfileroot = str_replace( '/my-calendar/', '/my-calendar-custom/', plugin_dir_path( __FILE__ ) ); - $newfiledir = trailingslashit( $newfileroot ) . 'styles/'; - $newfilepath = trailingslashit( $newfiledir ) . $style; - if ( ! $wp_filesystem->exists( $newfileroot ) ) { - $wp_filesystem->mkdir( $newfileroot ); - } - if ( ! $wp_filesystem->exists( $newfiledir ) ) { - $wp_filesystem->mkdir( $newfiledir ); - } - $wrote_migration = $wp_filesystem->copy( $stylefile, $newfilepath, true ); - if ( $wrote_migration ) { - $new = 'mc_custom_' . $style; - mc_update_option( 'css_file', $new ); - mc_update_option( 'migrated', 'true' ); - mc_show_notice( __( 'CSS migrated to custom directory.', 'my-calendar' ) ); - } else { - mc_show_error( __( 'CSS migration failed. You may need to migrate your file via FTP.', 'my-calendar' ) ); - } - } - } - } -} - - -/** - * Re-migrate CSS file to custom location from invalid location. - */ -function mc_remigrate_css() { - if ( isset( $_GET['remigrate'] ) ) { - if ( 'false' === $_GET['remigrate'] ) { - mc_update_option( 'remigrated', 'true' ); - mc_show_notice( __( 'All right! Leaving your CSS file where it is.', 'my-calendar' ) ); - } else { - $verify = wp_verify_nonce( $_GET['remigrate'], 'mc-remigrate-css' ); - if ( ! $verify ) { - wp_die( 'My Calendar: Permissions not granted to migrate CSS.', 'my-calendar' ); - } else { - global $wp_filesystem; - WP_Filesystem(); - $path = str_replace( '/my-calendar', '', plugin_dir_path( __DIR__ ) ) . 'styles/'; - $files = mc_css_list( $path ); - if ( ! empty( $files ) ) { - $style = $files[0]; - } - $stylefile = trailingslashit( $path ) . $style; - $newfileroot = str_replace( '/my-calendar/', '/my-calendar-custom/', plugin_dir_path( __FILE__ ) ); - $newfiledir = trailingslashit( $newfileroot ) . 'styles/'; - $newfilepath = trailingslashit( $newfiledir ) . $style; - if ( ! $wp_filesystem->exists( $newfileroot ) ) { - $wp_filesystem->mkdir( $newfileroot ); - } - if ( ! $wp_filesystem->exists( $newfiledir ) ) { - $wp_filesystem->mkdir( $newfiledir ); - } - $wrote_migration = $wp_filesystem->copy( $stylefile, $newfilepath, true ); - if ( $wrote_migration ) { - $new = 'mc_custom_' . $style; - mc_update_option( 'css_file', $new ); - mc_update_option( 'remigrated', 'true' ); - mc_show_notice( __( 'CSS migrated to the proper custom directory.', 'my-calendar' ) ); - } else { - mc_show_error( __( 'CSS migration failed. You may need to migrate your file via FTP.', 'my-calendar' ) ); - } - } - } - } -} - -/** - * Show CSS migration notice. - */ -function mc_migrate_notice() { - if ( ! ( 'true' === mc_get_option( 'migrated' ) ) && current_user_can( 'mc_edit_styles' ) ) { - if ( ! mc_is_custom_style( mc_get_option( 'css_file' ) ) ) { - $nonce = wp_create_nonce( 'mc-migrate-css' ); - $migrate_url = add_query_arg( 'migrate', $nonce, admin_url( 'admin.php?page=my-calendar-design' ) ); - $dismiss_url = add_query_arg( 'migrate', 'false', admin_url( 'admin.php?page=my-calendar-design' ) ); - // Translators: 1) URL for link to migrate styles. 2) URL to dismiss message and use existing styles. 3) Help link. - mc_show_notice( sprintf( __( 'The CSS Style editor will be removed in My Calendar 3.5. Migrate custom CSS into the My Calendar custom directory at /wp-content/plugins/my-calendar-custom/. Migrate CSS now Keep My Calendar\'s styles %3$s', 'my-calendar' ), $migrate_url, $dismiss_url, mc_help_link( __( 'Learn more', 'my-calendar' ), __( 'Migrating to Custom CSS', 'my-calendar' ), 'Custom CSS', 7, false ) ) ); - } else { - mc_show_notice( __( 'The CSS Style editor will be removed in My Calendar 3.5. You are already using custom CSS, and no changes are required.', 'my-calendar' ) ); - } - } -} - -/** - * Show CSS migration apology. - * - * @return void - */ -function mc_remigrate_notice() { - if ( ! ( 'true' === mc_get_option( 'remigrated' ) ) ) { - $path = str_replace( '/my-calendar', '', plugin_dir_path( __FILE__ ) ) . 'styles/'; - $files = mc_css_list( $path ); - if ( ! empty( $files ) ) { - $migrations = implode( ',', $files ); - $nonce = wp_create_nonce( 'mc-remigrate-css' ); - $migrate_url = add_query_arg( 'remigrate', $nonce, admin_url( 'admin.php?page=my-calendar-design' ) ); - $dismiss_url = add_query_arg( 'remigrate', 'false', admin_url( 'admin.php?page=my-calendar-design' ) ); - // Translators: 1) URL for link to remigrate styles. 2) URL to dismiss message. 3) Help link. - mc_show_notice( sprintf( __( 'Your previously migrated CSS file (%1$s) got put in the wrong directory. Move it to the right place? No, thanks.', 'my-calendar' ), $migrations, $migrate_url, $dismiss_url ) ); - } - } -} - /** * Generate stylesheet editor */ function my_calendar_style_edit() { - $edit_files = true; $message = ''; if ( ! current_user_can( 'mc_edit_styles' ) ) { echo wp_kses_post( '

' . __( 'You do not have permission to customize styles on this site.', 'my-calendar' ) . '

' ); + return; } - if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT === true ) { - $edit_files = false; - mc_show_error( __( 'File editing is disallowed in your WordPress installation. Edit your stylesheets offline.', 'my-calendar' ) ); - } - mc_migrate_css(); - mc_migrate_notice(); - mc_remigrate_css(); - mc_remigrate_notice(); - if ( isset( $_POST['mc_edit_style'] ) || isset( $_POST['mc_reset_style'] ) ) { + + if ( isset( $_POST['mc_edit_style'] ) ) { $nonce = $_REQUEST['_wpnonce']; if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) { wp_die( 'My Calendar: Security check failed' ); } - if ( isset( $_POST['mc_reset_style'] ) ) { - if ( ! empty( $_POST['reset_styles'] ) ) { - $stylefile = mc_get_style_path(); - $styles = mc_default_style(); - $wrote_old_styles = mc_write_styles( $stylefile, $styles ); - if ( $wrote_old_styles ) { - $message = '

' . __( 'Stylesheet updated to match core version.', 'my-calendar' ) . '

'; - } - } - } else { - $my_calendar_style = ( isset( $_POST['style'] ) ) ? stripcslashes( $_POST['style'] ) : false; - $mc_css_file = stripcslashes( sanitize_file_name( $_POST['mc_css_file'] ) ); - - if ( $edit_files ) { - $stylefile = mc_get_style_path( $mc_css_file ); - $wrote_styles = ( false !== $my_calendar_style ) ? mc_write_styles( $stylefile, $my_calendar_style ) : 'disabled'; - } else { - $wrote_styles = false; - } + + $mc_show_css = ( empty( $_POST['mc_show_css'] ) ) ? '' : stripcslashes( sanitize_text_field( $_POST['mc_show_css'] ) ); + mc_update_option( 'show_css', $mc_show_css ); + $use_styles = ( empty( $_POST['use_styles'] ) ) ? '' : 'true'; + mc_update_option( 'use_styles', $use_styles ); - if ( 'disabled' === $wrote_styles ) { - $message = '

' . __( 'Styles are disabled, and were not edited.', 'my-calendar' ) . '

'; - } else { - $message = ( true === $wrote_styles ) ? '

' . __( 'The stylesheet has been updated.', 'my-calendar' ) . ' ' . __( 'Migrate your CSS to the Custom CSS Directory', 'my-calendar' ) . '

' : '

' . __( 'Write Error! Please verify write permissions on the style file.', 'my-calendar' ) . '

'; - } - - $mc_show_css = ( empty( $_POST['mc_show_css'] ) ) ? '' : stripcslashes( sanitize_text_field( $_POST['mc_show_css'] ) ); - mc_update_option( 'show_css', $mc_show_css ); - $use_styles = ( empty( $_POST['use_styles'] ) ) ? '' : 'true'; - mc_update_option( 'use_styles', $use_styles ); - - if ( ! empty( $_POST['style_vars'] ) ) { - $styles = mc_get_option( 'style_vars' ); - if ( isset( $_POST['new_style_var'] ) ) { - $key = sanitize_text_field( $_POST['new_style_var']['key'] ); - $val = sanitize_text_field( $_POST['new_style_var']['val'] ); - if ( $key && $val ) { - if ( 0 !== strpos( $key, '--' ) ) { - $key = '--' . $key; - } - $styles[ $key ] = $val; + if ( ! empty( $_POST['style_vars'] ) ) { + $styles = mc_get_option( 'style_vars' ); + if ( isset( $_POST['new_style_var'] ) ) { + $key = sanitize_text_field( $_POST['new_style_var']['key'] ); + $val = sanitize_text_field( $_POST['new_style_var']['val'] ); + if ( $key && $val ) { + if ( 0 !== strpos( $key, '--' ) ) { + $key = '--' . $key; } + $styles[ $key ] = $val; } - foreach ( $_POST['style_vars'] as $key => $value ) { - if ( '' !== trim( $value ) ) { - $styles[ $key ] = sanitize_text_field( $value ); - } + } + foreach ( $_POST['style_vars'] as $key => $value ) { + if ( '' !== trim( $value ) ) { + $styles[ $key ] = sanitize_text_field( $value ); } - if ( isset( $_POST['delete_var'] ) ) { - $delete = sanitize_text_field( $_POST['delete_var'] ); - foreach ( $delete as $del ) { - unset( $styles[ $del ] ); - } + } + if ( isset( $_POST['delete_var'] ) ) { + $delete = sanitize_text_field( $_POST['delete_var'] ); + foreach ( $delete as $del ) { + unset( $styles[ $del ] ); } - mc_update_option( 'style_vars', $styles ); } - - $message .= ' ' . __( 'Style Settings Saved', 'my-calendar' ) . '.'; + mc_update_option( 'style_vars', $styles ); } + + $message .= ' ' . __( 'Style Settings Saved', 'my-calendar' ) . '.'; + mc_show_notice( $message ); } if ( isset( $_POST['mc_choose_style'] ) ) { @@ -238,52 +84,13 @@ function my_calendar_style_edit() { WP_Filesystem(); $file = $wp_filesystem->get_contents( $stylefile ); $my_calendar_style = $file; - $mc_current_style = mc_default_style(); } else { - $mc_current_style = ''; $my_calendar_style = __( 'Sorry. The file you are looking for doesn\'t appear to exist. Please check your file name and location!', 'my-calendar' ); } - $left_string = normalize_whitespace( $my_calendar_style ); - $right_string = normalize_whitespace( $mc_current_style ); - if ( $right_string ) { // If right string is blank, there is no default. - if ( isset( $_GET['diff'] ) ) { - ?> -
-
-
-
- - - - /> - -
- -
-
- __( 'Comparing Your Style with latest installed version of My Calendar', 'my-calendar' ), - 'title_right' => __( 'Latest (from plugin)', 'my-calendar' ), - 'title_left' => __( 'Current (in use)', 'my-calendar' ), - ) - ); - echo wp_kses_post( $diff ); - ?> -
- ' . __( 'Compare Your Stylesheet with latest installed version of My Calendar.', 'my-calendar' ) . '' ); - } - } + echo mc_stylesheet_selector(); - if ( ! isset( $_GET['diff'] ) ) { - $file = mc_get_option( 'css_file' ); - ?> + $file = mc_get_option( 'css_file' ); + ?>
@@ -325,49 +132,8 @@ function my_calendar_style_edit() {

-
- -

' . __( 'Edit your custom CSS using the WordPress file editor.', 'my-calendar' ) . '

' ); - } else { - echo wp_kses_post( '

' . __( 'The editor is not available for custom CSS files. Edit your custom CSS locally, then upload your changes.', 'my-calendar' ) . '

' ); - } - } else { - $nonce = wp_create_nonce( 'mc-migrate-css' ); - $migrate_url = '' . __( 'Migrate to custom CSS', 'my-calendar' ) . ''; - $disabled = ( $edit_files || mc_get_option( 'use_styles' ) === 'true' ) ? '' : ' disabled="disabled"'; - ?> -

- -

- -

-
- exists( $mc_current_file ) ) { - $file = $wp_filesystem->get_contents( $mc_current_file ); - $mc_current_style = $file; - switch ( $return ) { - case 'content': - return $mc_current_style; - case 'path': - return $mc_current_file; - case 'both': - return array( $mc_current_file, $mc_current_style ); - } - } - - return ''; -} - /** * List CSS files in a directory * @@ -577,76 +310,6 @@ function mc_css_list( $directory ) { return $results; } -/** - * Write updated styles to file - * - * @param string $file File to write to. - * @param string $style New styles to write. - * - * @return boolean; - */ -function mc_write_styles( $file, $style ) { - if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT === true ) { - return false; - } - - $standard = dirname( __FILE__ ) . '/styles/'; - $files = mc_css_list( $standard ); - $accepted_styles = array(); - foreach ( $files as $f ) { - $filepath = mc_get_style_path( $f ); - $path = pathinfo( $filepath ); - if ( 'css' === $path['extension'] ) { - $accepted_styles[] = $filepath; - } - } - - if ( in_array( $file, $accepted_styles, true ) ) { - $is_writable = wp_is_writable( $file ); - if ( $is_writable ) { - global $wp_filesystem; - WP_Filesystem(); - $saved = $wp_filesystem->put_contents( $file, $style ); - - return $saved; - } else { - return false; - } - } - - return false; -} - -add_action( - 'admin_enqueue_scripts', - function() { - if ( ! function_exists( 'wp_enqueue_code_editor' ) ) { - return; - } - - if ( sanitize_title( __( 'My Calendar', 'my-calendar' ) ) . '_page_my-calendar-design' !== get_current_screen()->id ) { - return; - } - - // Enqueue code editor and settings for manipulating HTML. - $settings = wp_enqueue_code_editor( array( 'type' => 'text/css' ) ); - - // Bail if user disabled CodeMirror. - if ( false === $settings ) { - return; - } - - wp_add_inline_script( - 'code-editor', - sprintf( - 'jQuery( function() { wp.codeEditor.initialize( "style", %s ); } );', - wp_json_encode( $settings ) - ) - ); - } -); - - /** * Measure the relative luminosity between two RGB values. *