| 1 |
<?php |
| 2 |
/** |
| 3 |
* Additional PHP for blocks. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* GhostKit_Settings |
| 14 |
*/ |
| 15 |
class GhostKit_Settings { |
| 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' ) ); |
| 22 |
|
| 23 |
// Sometimes redirect is not working on page opens. Admin Init is a solution. |
| 24 |
add_action( 'admin_init', array( $this, 'go_pro_redirect' ) ); |
| 25 |
|
| 26 |
// Add the options page and menu item. |
| 27 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register and enqueue admin-specific style sheet. |
| 32 |
*/ |
| 33 |
public function admin_enqueue_scripts() { |
| 34 |
$screen = get_current_screen(); |
| 35 |
|
| 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' ); |
| 43 |
wp_style_add_data( 'ghostkit-admin', 'suffix', '.min' ); |
| 44 |
|
| 45 |
GhostKit_Assets::enqueue_style( |
| 46 |
'ghostkit-settings', |
| 47 |
'build/settings/style', |
| 48 |
array(), |
| 49 |
filemtime( ghostkit()->plugin_path . 'build/settings/style.css' ) |
| 50 |
); |
| 51 |
wp_style_add_data( 'ghostkit-settings', 'rtl', 'replace' ); |
| 52 |
wp_style_add_data( 'ghostkit-settings', 'suffix', '.min' ); |
| 53 |
|
| 54 |
if ( 'toplevel_page_ghostkit' !== $screen->id ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$block_editor_context = new WP_Block_Editor_Context(); |
| 59 |
|
| 60 |
// Preload server-registered block schemas. |
| 61 |
wp_add_inline_script( |
| 62 |
'wp-blocks', |
| 63 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 64 |
); |
| 65 |
|
| 66 |
wp_add_inline_script( |
| 67 |
'wp-blocks', |
| 68 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), |
| 69 |
'after' |
| 70 |
); |
| 71 |
|
| 72 |
// The settings page is not a block editor screen, but it boots the block editor |
| 73 |
// scripts, so callbacks that check this flag have to see the editor context. |
| 74 |
add_filter( 'should_load_block_editor_scripts_and_styles', '__return_true' ); |
| 75 |
|
| 76 |
// phpcs:ignore |
| 77 |
do_action( 'enqueue_block_editor_assets' ); |
| 78 |
|
| 79 |
// phpcs:ignore |
| 80 |
do_action( 'enqueue_block_assets' ); |
| 81 |
|
| 82 |
remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_true' ); |
| 83 |
|
| 84 |
// Ghost Kit Settings. |
| 85 |
GhostKit_Assets::enqueue_script( |
| 86 |
'ghostkit-settings', |
| 87 |
'build/settings/index', |
| 88 |
array( 'ghostkit-helper' ) |
| 89 |
); |
| 90 |
|
| 91 |
wp_localize_script( |
| 92 |
'ghostkit-settings', |
| 93 |
'ghostkitSettingsData', |
| 94 |
array( |
| 95 |
'api_nonce' => wp_create_nonce( 'wp_rest' ), |
| 96 |
'api_url' => rest_url( 'ghostkit/v1/' ), |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
wp_enqueue_style( 'wp-components' ); |
| 101 |
|
| 102 |
if ( function_exists( 'wp_enqueue_media' ) ) { |
| 103 |
wp_enqueue_media(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Go Pro. |
| 109 |
* Redirect to the Pro purchase page. |
| 110 |
*/ |
| 111 |
public function go_pro_redirect() { |
| 112 |
// phpcs:ignore |
| 113 |
if ( ! isset( $_GET['page'] ) || empty( $_GET['page'] ) ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// phpcs:ignore |
| 118 |
if ( 'ghostkit_go_pro' === $_GET['page'] ) { |
| 119 |
$medium = 'admin_menu'; |
| 120 |
|
| 121 |
// phpcs:ignore |
| 122 |
if ( isset( $_GET['utm_medium'] ) ) { |
| 123 |
// phpcs:ignore |
| 124 |
$medium = $_GET['utm_medium']; |
| 125 |
} |
| 126 |
|
| 127 |
// phpcs:ignore |
| 128 |
wp_redirect( ghostkit()->go_pro_link() . '?utm_source=plugin&utm_medium=' . esc_attr( $medium ) . '&utm_campaign=go_pro&utm_content=' . GHOSTKIT_VERSION ); |
| 129 |
exit(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 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' ), |
| 143 |
// phpcs:ignore |
| 144 |
'data:image/svg+xml;base64,' . base64_encode( file_get_contents( ghostkit()->plugin_path . 'assets/images/admin-icon.svg' ) ), |
| 145 |
56.9 |
| 146 |
); |
| 147 |
|
| 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 |
|
| 191 |
if ( GhostKit_Templates::is_allowed() ) { |
| 192 |
add_submenu_page( |
| 193 |
'ghostkit', |
| 194 |
'', |
| 195 |
esc_html__( 'Templates', 'ghostkit' ), |
| 196 |
'manage_options', |
| 197 |
'edit.php?post_type=ghostkit_template' |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
add_submenu_page( |
| 202 |
'ghostkit', |
| 203 |
'', |
| 204 |
'<span class="dashicons dashicons-star-filled" style="font-size: 17px"></span> ' . esc_html__( 'Go Pro', 'ghostkit' ), |
| 205 |
'manage_options', |
| 206 |
'ghostkit_go_pro', |
| 207 |
array( $this, 'go_pro_redirect' ) |
| 208 |
); |
| 209 |
|
| 210 |
global $wp_version; |
| 211 |
|
| 212 |
// Since WP 6.3 user have an ability to open the reusable blocks page |
| 213 |
// From the Appearance -> Editor -> Patterns. |
| 214 |
if ( ! version_compare( $wp_version, '6.3', '>=' ) ) { |
| 215 |
add_menu_page( |
| 216 |
esc_html__( 'Reusable Blocks', 'ghostkit' ), |
| 217 |
esc_html__( 'Reusable Blocks', 'ghostkit' ), |
| 218 |
'read', |
| 219 |
'edit.php?post_type=wp_block', |
| 220 |
'', |
| 221 |
'dashicons-editor-table', |
| 222 |
57 |
| 223 |
); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Render the admin page. |
| 229 |
*/ |
| 230 |
public function display_admin_page() { |
| 231 |
?> |
| 232 |
<div class="ghostkit-admin-page"></div> |
| 233 |
<?php |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
new GhostKit_Settings(); |
| 238 |
|