diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index e7459eb2..a3449d02 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -231,6 +231,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook | `after_setup_theme` | `fictioneer_theme_setup` | `comment_post` | `fictioneer_comment_post`, `fictioneer_post_comment_to_discord` | `current_screen` | `fictioneer_restrict_admin_only_pages`, `fictioneer_restrict_comment_edit` +| `customize_controls_enqueue_scripts` | `fictioneer_enqueue_customizer_scripts` | `customize_register` | `fictioneer_add_customizers` | `customize_save_after` | `fictioneer_watch_for_customer_updates` | `delete_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients` @@ -259,7 +260,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook | `trashed_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients`, `fictioneer_remove_chapter_from_story` | `untrash_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients` | `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries`, `fictioneer_update_option_disable_extended_story_list_meta_queries` -| `wp_ajax_*` | `fictioneer_ajax_clear_my_checkmarks`, `fictioneer_ajax_clear_my_comments`, `fictioneer_ajax_clear_my_comment_subscriptions`, `fictioneer_ajax_clear_my_follows`, `fictioneer_ajax_clear_my_reminders`, `fictioneer_ajax_delete_epub`, `fictioneer_ajax_delete_my_account`, `fictioneer_ajax_delete_my_comment`, `fictioneer_ajax_edit_comment`, `fictioneer_ajax_get_avatar`, `fictioneer_ajax_get_comment_form`, `fictioneer_ajax_get_comment_section`, `fictioneer_ajax_get_finished_checkmarks_list`, `fictioneer_ajax_get_follows_list`, `fictioneer_ajax_get_follows_notifications`, `fictioneer_ajax_get_reminders_list`, `fictioneer_ajax_mark_follows_read`, `fictioneer_ajax_moderate_comment`, `fictioneer_ajax_report_comment`, `fictioneer_ajax_save_bookmarks`, `fictioneer_ajax_set_checkmark`, `fictioneer_ajax_submit_comment`, `fictioneer_ajax_toggle_follow`, `fictioneer_ajax_toggle_reminder`, `fictioneer_ajax_unset_my_oauth`, `fictioneer_ajax_get_user_data`, `fictioneer_ajax_get_auth`, `fictioneer_ajax_purge_schema`, `fictioneer_ajax_purge_all_schemas` +| `wp_ajax_*` | `fictioneer_ajax_clear_my_checkmarks`, `fictioneer_ajax_clear_my_comments`, `fictioneer_ajax_clear_my_comment_subscriptions`, `fictioneer_ajax_clear_my_follows`, `fictioneer_ajax_clear_my_reminders`, `fictioneer_ajax_delete_epub`, `fictioneer_ajax_delete_my_account`, `fictioneer_ajax_delete_my_comment`, `fictioneer_ajax_edit_comment`, `fictioneer_ajax_get_avatar`, `fictioneer_ajax_get_comment_form`, `fictioneer_ajax_get_comment_section`, `fictioneer_ajax_get_finished_checkmarks_list`, `fictioneer_ajax_get_follows_list`, `fictioneer_ajax_get_follows_notifications`, `fictioneer_ajax_get_reminders_list`, `fictioneer_ajax_mark_follows_read`, `fictioneer_ajax_moderate_comment`, `fictioneer_ajax_report_comment`, `fictioneer_ajax_save_bookmarks`, `fictioneer_ajax_set_checkmark`, `fictioneer_ajax_submit_comment`, `fictioneer_ajax_toggle_follow`, `fictioneer_ajax_toggle_reminder`, `fictioneer_ajax_unset_my_oauth`, `fictioneer_ajax_get_user_data`, `fictioneer_ajax_get_auth`, `fictioneer_ajax_purge_schema`, `fictioneer_ajax_purge_all_schemas`, `fictioneer_ajax_reset_theme_colors` | `wp_ajax_nopriv_*` | `fictioneer_ajax_get_comment_form`, `fictioneer_ajax_get_comment_section`, `fictioneer_ajax_submit_comment`, `fictioneer_ajax_get_auth` | `wp_before_admin_bar_render` | `fictioneer_remove_admin_bar_links`, `fictioneer_remove_dashboard_from_admin_bar`, `fictioneer_remove_comments_from_admin_bar` | `wp_dashboard_setup` | `fictioneer_remove_dashboard_widgets` @@ -285,6 +286,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook | `comment_reply_link` | `fictioneer_comment_login_to_reply` | `comment_row_actions` | `fictioneer_remove_quick_edit` | `comment_text` | `fictioneer_bbcodes` +| `customize_refresh_nonces` | `fictioneer_add_customizer_refresh_nonces` | `excerpt_length` | `fictioneer_custom_excerpt_length` | `excerpt_more` | `fictioneer_excerpt_ellipsis` | `get_avatar` | `fictioneer_avatar_fallback` diff --git a/includes/functions/_admin.php b/includes/functions/_admin.php index 6b5218f7..fbbd79fe 100644 --- a/includes/functions/_admin.php +++ b/includes/functions/_admin.php @@ -547,4 +547,49 @@ function fictioneer_ajax_get_chapter_groups() { } add_action( 'wp_ajax_fictioneer_ajax_get_chapter_groups', 'fictioneer_ajax_get_chapter_groups' ); +// ============================================================================= +// AJAX: RESET THEME COLORS +// ============================================================================= + +/** + * AJAX: Reset custom theme colors + * + * @since 5.12.0 + */ + +function fictioneer_ajax_reset_theme_colors() { + // Validate + check_ajax_referer( 'fictioneer-reset-colors', 'fictioneer_nonce' ); + + if ( ! current_user_can( 'edit_theme_options' ) ) { + wp_send_json_error( array( 'failure' => __( 'Error: Insufficient permissions.', 'fictioneer' ) ) ); + } + + // Setup + $mods = get_theme_mods(); + $theme = get_option( 'stylesheet' ); + $json_path = get_template_directory() . '/includes/functions/colors.json'; + $fictioneer_colors = json_decode( file_get_contents( $json_path ), true ); + + // Abort if... + if ( ! is_array( $fictioneer_colors ) || empty( $fictioneer_colors ) ) { + wp_send_json_error( array( 'failure' => __( 'Error: Colors not found.', 'fictioneer' ) ) ); + } + + // Unset colors to reset them to default + foreach ( array_keys( $fictioneer_colors ) as $mod ) { + unset( $mods[ $mod ] ); + } + + // Save to database + update_option( "theme_mods_{$theme}", $mods ); + + // Refresh custom CSS file + fictioneer_build_customize_css(); + + // Finish + wp_send_json_success( array( 'success' => true ) ); +} +add_action( 'wp_ajax_fictioneer_ajax_reset_theme_colors', 'fictioneer_ajax_reset_theme_colors' ); + ?> diff --git a/includes/functions/_customizer-settings.php b/includes/functions/_customizer-settings.php index dee30d06..8bf25e1a 100644 --- a/includes/functions/_customizer-settings.php +++ b/includes/functions/_customizer-settings.php @@ -98,6 +98,43 @@ function fictioneer_add_color_theme_option( $manager, $args ) { ); } +// ============================================================================= +// RESETS +// ============================================================================= + +/** + * Add section with reset options + * + * @since 5.12.0 + * + * @param WP_Customize_Manager $manager The customizer instance. + */ + +function fictioneer_add_reset_options( $manager ) { + $manager->add_section( + 'reset', + array( + 'title' => __( 'Reset Options', 'fictioneer' ), + 'description' => __( 'This allows you to quickly reset options to their default. However, this is irreversible!', 'fictioneer' ), + 'priority' => 160, + ) + ); + + $manager->add_control( + 'reset_all_colors', + array( + 'type' => 'button', + 'settings' => [], + 'priority' => 10, + 'section' => 'reset', + 'input_attrs' => array( + 'value' => __( 'Reset Colors', 'fictioneer' ), + 'class' => 'button button-primary' + ) + ) + ); +} + // ============================================================================= // LIGHT MODE SETTINGS // ============================================================================= @@ -2331,6 +2368,9 @@ function fictioneer_add_customizers( $manager ) { // Fonts fictioneer_add_fonts_customizer_settings( $manager ); + // Resets + fictioneer_add_reset_options( $manager ); + // Open Graph image $manager->add_setting( 'og_image', diff --git a/includes/functions/_theme_setup.php b/includes/functions/_theme_setup.php index 16b451e9..6a98fee9 100644 --- a/includes/functions/_theme_setup.php +++ b/includes/functions/_theme_setup.php @@ -1000,6 +1000,44 @@ function fictioneer_enqueue_block_editor_scripts() { } add_action( 'enqueue_block_editor_assets', 'fictioneer_enqueue_block_editor_scripts' ); +/** + * Enqueue customizer scripts + * + * @since 5.12.0 + */ + +function fictioneer_enqueue_customizer_scripts() { + wp_enqueue_script( + 'fictioneer-customizer-scripts', + get_template_directory_uri() . '/js/customizer.min.js', + ['jquery', 'customize-preview'], + FICTIONEER_VERSION, + true + ); + + wp_localize_script( 'fictioneer-customizer-scripts', 'fictioneerData', array( + 'confirmationDialog' => __( 'Are you sure?', 'fictioneer' ) + )); +} +add_action( 'customize_controls_enqueue_scripts', 'fictioneer_enqueue_customizer_scripts' ); + +/** + * Add nonce for Customizer actions + * + * @since 5.12.0 + * + * @param array $nonces Array of refreshed nonces for save and preview actions. + * + * @return array Updated array of nonces. + */ + +function fictioneer_add_customizer_refresh_nonces( $nonces ) { + $nonces['fictioneer-reset-colors'] = wp_create_nonce( 'fictioneer-reset-colors' ); + + return $nonces; +} +add_filter( 'customize_refresh_nonces', 'fictioneer_add_customizer_refresh_nonces' ); + // ============================================================================= // ADD SCRIPTS TO LOGIN HEAD // ============================================================================= diff --git a/js/customizer.min.js b/js/customizer.min.js new file mode 100644 index 00000000..91dcaecf --- /dev/null +++ b/js/customizer.min.js @@ -0,0 +1 @@ +wp.customize.control("reset_all_colors",(o=>{o.container.find(".button").on("click",(()=>{1==confirm(fictioneerData.confirmationDialog??"Are you sure?")&&wp.ajax.post("fictioneer_ajax_reset_theme_colors",{fictioneer_nonce:wp.customize.settings.nonce["fictioneer-reset-colors"]}).done((o=>{o.success&&location.reload()}))}))})); \ No newline at end of file diff --git a/src/js/customizer.js b/src/js/customizer.js new file mode 100644 index 00000000..8c444186 --- /dev/null +++ b/src/js/customizer.js @@ -0,0 +1,20 @@ +// ============================================================================= +// RESET ALL COLORS +// ============================================================================= + +wp.customize.control( 'reset_all_colors', control => { + control.container.find( '.button' ).on( 'click', () => { + if (confirm(fictioneerData.confirmationDialog ?? 'Are you sure?') == true) { + wp.ajax.post( + 'fictioneer_ajax_reset_theme_colors', + { + fictioneer_nonce: wp.customize.settings.nonce['fictioneer-reset-colors'] + } + ).done(response => { + if (response.success) { + location.reload(); + } + }); + } + }); +});