| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | * @package ghostkit |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; | |
| 9 | + exit; | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * GhostKit_Settings |
| @@ -12,209 +12,224 @@ | ||
| 12 | 12 | /** |
| 13 | 13 | * GhostKit_Settings |
| 14 | 14 | */ |
| 15 | 15 | class GhostKit_Settings { |
| 16 | - /** | |
| 17 | - * GhostKit_Settings constructor. | |
| 18 | - */ | |
| 19 | - public function __construct() { | |
| 20 | - // work only if Gutenberg available. | |
| 21 | - if ( ! function_exists( 'register_block_type' ) ) { | |
| 22 | - return; | |
| 23 | - } | |
| 16 | + /** | |
| 17 | + * GhostKit_Settings constructor. | |
| 18 | + */ | |
| 19 | + public function __construct() { | |
| 20 | + // Load admin style sheet and JavaScript. | |
| 21 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | |
| 24 | 22 | |
| 25 | - // Load admin style sheet and JavaScript. | |
| 26 | - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | |
| 23 | + // Sometimes redirect is not working on page opens. Admin Init is a solution. | |
| 24 | + add_action( 'admin_init', array( $this, 'go_pro_redirect' ) ); | |
| 27 | 25 | |
| 28 | - // Sometimes redirect is not working on page opens. Admin Init is a solution. | |
| 29 | - add_action( 'admin_init', array( $this, 'go_pro_redirect' ) ); | |
| 26 | + // Add the options page and menu item. | |
| 27 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
| 28 | + } | |
| 30 | 29 | |
| 31 | - // Add the options page and menu item. | |
| 32 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
| 33 | - } | |
| 30 | + /** | |
| 31 | + * Register and enqueue admin-specific style sheet. | |
| 32 | + */ | |
| 33 | + public function admin_enqueue_scripts() { | |
| 34 | + $screen = get_current_screen(); | |
| 34 | 35 | |
| 35 | - /** | |
| 36 | - * Register and enqueue admin-specific style sheet. | |
| 37 | - */ | |
| 38 | - public function admin_enqueue_scripts() { | |
| 39 | - $screen = get_current_screen(); | |
| 36 | + GhostKit_Assets::enqueue_style( | |
| 37 | + 'ghostkit-admin', | |
| 38 | + 'build/assets/admin/css/admin', | |
| 39 | + array(), | |
| 40 | + filemtime( ghostkit()->plugin_path . 'build/assets/admin/css/admin.css' ) | |
| 41 | + ); | |
| 42 | + wp_style_add_data( 'ghostkit-admin', 'rtl', 'replace' ); | |
| 40 | 43 | |
| 41 | - wp_enqueue_style( | |
| 42 | - 'ghostkit-admin', | |
| 43 | - ghostkit()->plugin_url . 'assets/admin/css/admin.min.css', | |
| 44 | - array(), | |
| 45 | - filemtime( ghostkit()->plugin_path . 'assets/admin/css/admin.min.css' ) | |
| 46 | - ); | |
| 47 | - wp_style_add_data( 'ghostkit-admin', 'rtl', 'replace' ); | |
| 48 | - wp_style_add_data( 'ghostkit-admin', 'suffix', '.min' ); | |
| 44 | + GhostKit_Assets::enqueue_style( | |
| 45 | + 'ghostkit-settings', | |
| 46 | + 'build/settings/style', | |
| 47 | + array(), | |
| 48 | + filemtime( ghostkit()->plugin_path . 'build/settings/style.css' ) | |
| 49 | + ); | |
| 50 | + wp_style_add_data( 'ghostkit-settings', 'rtl', 'replace' ); | |
| 49 | 51 | |
| 50 | - wp_enqueue_style( | |
| 51 | - 'ghostkit-settings', | |
| 52 | - ghostkit()->plugin_url . 'settings/style.min.css', | |
| 53 | - array(), | |
| 54 | - filemtime( ghostkit()->plugin_path . 'settings/style.min.css' ) | |
| 55 | - ); | |
| 56 | - wp_style_add_data( 'ghostkit-settings', 'rtl', 'replace' ); | |
| 57 | - wp_style_add_data( 'ghostkit-settings', 'suffix', '.min' ); | |
| 52 | + if ( 'toplevel_page_ghostkit' !== $screen->id ) { | |
| 53 | + return; | |
| 54 | + } | |
| 58 | 55 | |
| 59 | - if ( 'toplevel_page_ghostkit' !== $screen->id ) { | |
| 60 | - return; | |
| 61 | - } | |
| 56 | + $block_editor_context = new WP_Block_Editor_Context(); | |
| 62 | 57 | |
| 63 | - $block_categories = array(); | |
| 64 | - if ( function_exists( 'get_block_categories' ) ) { | |
| 65 | - $block_categories = get_block_categories( get_post() ); | |
| 66 | - } elseif ( function_exists( 'gutenberg_get_block_categories' ) ) { | |
| 67 | - $block_categories = gutenberg_get_block_categories( get_post() ); | |
| 68 | - } | |
| 58 | + // Preload server-registered block schemas. | |
| 59 | + wp_add_inline_script( | |
| 60 | + 'wp-blocks', | |
| 61 | + 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' | |
| 62 | + ); | |
| 69 | 63 | |
| 70 | - // enqueue blocks library. | |
| 71 | - wp_enqueue_script( 'wp-block-library' ); | |
| 64 | + wp_add_inline_script( | |
| 65 | + 'wp-blocks', | |
| 66 | + sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), | |
| 67 | + 'after' | |
| 68 | + ); | |
| 72 | 69 | |
| 73 | - wp_add_inline_script( | |
| 74 | - 'wp-blocks', | |
| 75 | - sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $block_categories ) ), | |
| 76 | - 'after' | |
| 77 | - ); | |
| 70 | + // The settings page is not a block editor screen, but it boots the block editor | |
| 71 | + // scripts, so callbacks that check this flag have to see the editor context. | |
| 72 | + add_filter( 'should_load_block_editor_scripts_and_styles', '__return_true' ); | |
| 78 | 73 | |
| 79 | - // Ghost Kit Settings. | |
| 80 | - wp_enqueue_script( | |
| 81 | - 'ghostkit-settings', | |
| 82 | - ghostkit()->plugin_url . 'settings/index.min.js', | |
| 83 | - array( 'ghostkit-helper', 'wp-data', 'wp-element', 'wp-components', 'wp-api', 'wp-api-request', 'wp-i18n' ), | |
| 84 | - filemtime( ghostkit()->plugin_path . 'settings/index.min.js' ), | |
| 85 | - true | |
| 86 | - ); | |
| 74 | + // phpcs:ignore | |
| 75 | + do_action( 'enqueue_block_editor_assets' ); | |
| 87 | 76 | |
| 88 | - wp_localize_script( | |
| 89 | - 'ghostkit-settings', | |
| 90 | - 'ghostkitSettingsData', | |
| 91 | - array( | |
| 92 | - 'api_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 93 | - 'api_url' => rest_url( 'ghostkit/v1/' ), | |
| 94 | - ) | |
| 95 | - ); | |
| 77 | + // phpcs:ignore | |
| 78 | + do_action( 'enqueue_block_assets' ); | |
| 96 | 79 | |
| 97 | - // phpcs:ignore | |
| 98 | - do_action( 'enqueue_block_editor_assets' ); | |
| 80 | + remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_true' ); | |
| 99 | 81 | |
| 100 | - wp_enqueue_style( 'wp-components' ); | |
| 82 | + // Ghost Kit Settings. | |
| 83 | + GhostKit_Assets::enqueue_script( | |
| 84 | + 'ghostkit-settings', | |
| 85 | + 'build/settings/index', | |
| 86 | + array( 'ghostkit-helper' ) | |
| 87 | + ); | |
| 101 | 88 | |
| 102 | - if ( function_exists( 'wp_enqueue_media' ) ) { | |
| 103 | - wp_enqueue_media(); | |
| 104 | - } | |
| 105 | - } | |
| 89 | + wp_localize_script( | |
| 90 | + 'ghostkit-settings', | |
| 91 | + 'ghostkitSettingsData', | |
| 92 | + array( | |
| 93 | + 'api_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 94 | + 'api_url' => rest_url( 'ghostkit/v1/' ), | |
| 95 | + ) | |
| 96 | + ); | |
| 106 | 97 | |
| 107 | - /** | |
| 108 | - * Go Pro. | |
| 109 | - * Redirect to the Pro purchase page. | |
| 110 | - */ | |
| 111 | - public function go_pro_redirect() { | |
| 98 | + wp_enqueue_style( 'wp-components' ); | |
| 99 | + | |
| 100 | + if ( function_exists( 'wp_enqueue_media' ) ) { | |
| 101 | + wp_enqueue_media(); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Go Pro. | |
| 107 | + * Redirect to the Pro purchase page. | |
| 108 | + */ | |
| 109 | + public function go_pro_redirect() { | |
| 112 | 110 | // phpcs:ignore |
| 113 | 111 | if ( ! isset( $_GET['page'] ) || empty( $_GET['page'] ) ) { |
| 114 | - return; | |
| 115 | - } | |
| 112 | + return; | |
| 113 | + } | |
| 116 | 114 | |
| 117 | 115 | // phpcs:ignore |
| 118 | 116 | if ( 'ghostkit_go_pro' === $_GET['page'] ) { |
| 119 | - $medium = 'admin_menu'; | |
| 117 | + $medium = 'admin_menu'; | |
| 120 | 118 | |
| 121 | 119 | // phpcs:ignore |
| 122 | 120 | if ( isset( $_GET['utm_medium'] ) ) { |
| 123 | 121 | // phpcs:ignore |
| 124 | 122 | $medium = $_GET['utm_medium']; |
| 125 | - } | |
| 123 | + } | |
| 126 | 124 | |
| 127 | 125 | // phpcs:ignore |
| 128 | - wp_redirect( ghostkit()->go_pro_link() . '?utm_source=plugin&utm_medium=' . esc_attr( $medium ) . '&utm_campaign=go_pro&utm_content=2.25.0' ); | |
| 129 | - exit(); | |
| 130 | - } | |
| 131 | - } | |
| 126 | + wp_redirect( ghostkit()->go_pro_link() . '?utm_source=plugin&utm_medium=' . esc_attr( $medium ) . '&utm_campaign=go_pro&utm_content=' . GHOSTKIT_VERSION ); | |
| 127 | + exit(); | |
| 128 | + } | |
| 129 | + } | |
| 132 | 130 | |
| 133 | - /** | |
| 134 | - * Add admin menu. | |
| 135 | - */ | |
| 136 | - public function admin_menu() { | |
| 137 | - add_menu_page( | |
| 138 | - esc_html__( 'Ghost Kit', 'ghostkit' ), | |
| 139 | - esc_html__( 'Ghost Kit', 'ghostkit' ), | |
| 140 | - 'manage_options', | |
| 141 | - 'ghostkit', | |
| 142 | - array( $this, 'display_admin_page' ), | |
| 131 | + /** | |
| 132 | + * Add admin menu. | |
| 133 | + */ | |
| 134 | + public function admin_menu() { | |
| 135 | + add_menu_page( | |
| 136 | + esc_html__( 'Ghost Kit', 'ghostkit' ), | |
| 137 | + esc_html__( 'Ghost Kit', 'ghostkit' ), | |
| 138 | + 'manage_options', | |
| 139 | + 'ghostkit', | |
| 140 | + array( $this, 'display_admin_page' ), | |
| 143 | 141 | // phpcs:ignore |
| 144 | 142 | 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( ghostkit()->plugin_path . 'assets/images/admin-icon.svg' ) ), |
| 145 | - 56.9 | |
| 146 | - ); | |
| 143 | + 56.9 | |
| 144 | + ); | |
| 147 | 145 | |
| 148 | - add_submenu_page( | |
| 149 | - 'ghostkit', | |
| 150 | - '', | |
| 151 | - esc_html__( 'Blocks', 'ghostkit' ), | |
| 152 | - 'manage_options', | |
| 153 | - 'ghostkit' | |
| 154 | - ); | |
| 155 | - add_submenu_page( | |
| 156 | - 'ghostkit', | |
| 157 | - '', | |
| 158 | - esc_html__( 'Icons', 'ghostkit' ), | |
| 159 | - 'manage_options', | |
| 160 | - 'admin.php?page=ghostkit&sub_page=icons' | |
| 161 | - ); | |
| 162 | - add_submenu_page( | |
| 163 | - 'ghostkit', | |
| 164 | - '', | |
| 165 | - esc_html__( 'Typography', 'ghostkit' ), | |
| 166 | - 'manage_options', | |
| 167 | - 'admin.php?page=ghostkit&sub_page=typography' | |
| 168 | - ); | |
| 169 | - add_submenu_page( | |
| 170 | - 'ghostkit', | |
| 171 | - '', | |
| 172 | - esc_html__( 'Fonts', 'ghostkit' ), | |
| 173 | - 'manage_options', | |
| 174 | - 'admin.php?page=ghostkit&sub_page=fonts' | |
| 175 | - ); | |
| 176 | - add_submenu_page( | |
| 177 | - 'ghostkit', | |
| 178 | - '', | |
| 179 | - esc_html__( 'Breakpoints', 'ghostkit' ), | |
| 180 | - 'manage_options', | |
| 181 | - 'admin.php?page=ghostkit&sub_page=breakpoints' | |
| 182 | - ); | |
| 183 | - add_submenu_page( | |
| 184 | - 'ghostkit', | |
| 185 | - '', | |
| 186 | - esc_html__( 'CSS & JavaScript', 'ghostkit' ), | |
| 187 | - 'manage_options', | |
| 188 | - 'admin.php?page=ghostkit&sub_page=css_js' | |
| 189 | - ); | |
| 190 | - add_submenu_page( | |
| 191 | - 'ghostkit', | |
| 192 | - '', | |
| 193 | - '<span class="dashicons dashicons-star-filled" style="font-size: 17px"></span> ' . esc_html__( 'Go Pro', 'ghostkit' ), | |
| 194 | - 'manage_options', | |
| 195 | - 'ghostkit_go_pro', | |
| 196 | - array( $this, 'go_pro_redirect' ) | |
| 197 | - ); | |
| 146 | + add_submenu_page( | |
| 147 | + 'ghostkit', | |
| 148 | + '', | |
| 149 | + esc_html__( 'Blocks', 'ghostkit' ), | |
| 150 | + 'manage_options', | |
| 151 | + 'ghostkit' | |
| 152 | + ); | |
| 153 | + add_submenu_page( | |
| 154 | + 'ghostkit', | |
| 155 | + '', | |
| 156 | + esc_html__( 'Icons', 'ghostkit' ), | |
| 157 | + 'manage_options', | |
| 158 | + 'admin.php?page=ghostkit&sub_page=icons' | |
| 159 | + ); | |
| 160 | + add_submenu_page( | |
| 161 | + 'ghostkit', | |
| 162 | + '', | |
| 163 | + esc_html__( 'Typography', 'ghostkit' ), | |
| 164 | + 'manage_options', | |
| 165 | + 'admin.php?page=ghostkit&sub_page=typography' | |
| 166 | + ); | |
| 167 | + add_submenu_page( | |
| 168 | + 'ghostkit', | |
| 169 | + '', | |
| 170 | + esc_html__( 'Fonts', 'ghostkit' ), | |
| 171 | + 'manage_options', | |
| 172 | + 'admin.php?page=ghostkit&sub_page=fonts' | |
| 173 | + ); | |
| 174 | + add_submenu_page( | |
| 175 | + 'ghostkit', | |
| 176 | + '', | |
| 177 | + esc_html__( 'Breakpoints', 'ghostkit' ), | |
| 178 | + 'manage_options', | |
| 179 | + 'admin.php?page=ghostkit&sub_page=breakpoints' | |
| 180 | + ); | |
| 181 | + add_submenu_page( | |
| 182 | + 'ghostkit', | |
| 183 | + '', | |
| 184 | + esc_html__( 'CSS & JavaScript', 'ghostkit' ), | |
| 185 | + 'manage_options', | |
| 186 | + 'admin.php?page=ghostkit&sub_page=css_js' | |
| 187 | + ); | |
| 198 | 188 | |
| 199 | - add_menu_page( | |
| 200 | - esc_html__( 'Reusable Blocks', 'ghostkit' ), | |
| 201 | - esc_html__( 'Reusable Blocks', 'ghostkit' ), | |
| 202 | - 'read', | |
| 203 | - 'edit.php?post_type=wp_block', | |
| 204 | - '', | |
| 205 | - 'dashicons-editor-table', | |
| 206 | - 57 | |
| 207 | - ); | |
| 208 | - } | |
| 189 | + if ( GhostKit_Templates::is_allowed() ) { | |
| 190 | + add_submenu_page( | |
| 191 | + 'ghostkit', | |
| 192 | + '', | |
| 193 | + esc_html__( 'Templates', 'ghostkit' ), | |
| 194 | + 'manage_options', | |
| 195 | + 'edit.php?post_type=ghostkit_template' | |
| 196 | + ); | |
| 197 | + } | |
| 209 | 198 | |
| 210 | - /** | |
| 211 | - * Render the admin page. | |
| 212 | - */ | |
| 213 | - public function display_admin_page() { | |
| 214 | - ?> | |
| 215 | - <div class="ghostkit-admin-page"></div> | |
| 216 | - <?php | |
| 217 | - } | |
| 199 | + add_submenu_page( | |
| 200 | + 'ghostkit', | |
| 201 | + '', | |
| 202 | + '<span class="dashicons dashicons-star-filled" style="font-size: 17px"></span> ' . esc_html__( 'Go Pro', 'ghostkit' ), | |
| 203 | + 'manage_options', | |
| 204 | + 'ghostkit_go_pro', | |
| 205 | + array( $this, 'go_pro_redirect' ) | |
| 206 | + ); | |
| 207 | + | |
| 208 | + global $wp_version; | |
| 209 | + | |
| 210 | + // Since WP 6.3 user have an ability to open the reusable blocks page | |
| 211 | + // From the Appearance -> Editor -> Patterns. | |
| 212 | + if ( ! version_compare( $wp_version, '6.3', '>=' ) ) { | |
| 213 | + add_menu_page( | |
| 214 | + esc_html__( 'Reusable Blocks', 'ghostkit' ), | |
| 215 | + esc_html__( 'Reusable Blocks', 'ghostkit' ), | |
| 216 | + 'read', | |
| 217 | + 'edit.php?post_type=wp_block', | |
| 218 | + '', | |
| 219 | + 'dashicons-editor-table', | |
| 220 | + 57 | |
| 221 | + ); | |
| 222 | + } | |
| 223 | + } | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * Render the admin page. | |
| 227 | + */ | |
| 228 | + public function display_admin_page() { | |
| 229 | + ?> | |
| 230 | + <div class="ghostkit-admin-page"></div> | |
| 231 | + <?php | |
| 232 | + } | |
| 218 | 233 | } |
| 219 | 234 | |
| 220 | 235 | new GhostKit_Settings(); |