blocks
1 month ago
dynamic-tags
1 week ago
pattern-library
1 month ago
utils
2 years ago
class-do-css.php
3 years ago
class-dynamic-content.php
1 week ago
class-dynamic-tag-security.php
1 week ago
class-enqueue-css.php
1 week ago
class-legacy-attributes.php
4 years ago
class-map-deprecated-attributes.php
3 years ago
class-meta-handler.php
1 week ago
class-plugin-update.php
1 year ago
class-query-loop.php
2 years ago
class-query-utils.php
1 week ago
class-render-blocks.php
1 week ago
class-rest.php
1 year ago
class-save-gate.php
1 week ago
class-settings.php
1 year ago
dashboard.php
1 week ago
defaults.php
1 year ago
deprecated.php
1 year ago
functions.php
1 week ago
general.php
1 week ago
dashboard.php
398 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Our admin Dashboard. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | add_action( 'admin_menu', 'generateblocks_register_dashboard', 9 ); |
| 13 | /** |
| 14 | * Register our Dashboard page. |
| 15 | * |
| 16 | * @since 0.1 |
| 17 | */ |
| 18 | function generateblocks_register_dashboard() { |
| 19 | $dashboard = add_menu_page( |
| 20 | __( 'GenerateBlocks', 'generateblocks' ), |
| 21 | __( 'GenerateBlocks', 'generateblocks' ), |
| 22 | 'manage_options', |
| 23 | 'generateblocks', |
| 24 | 'generateblocks_do_dashboard' |
| 25 | ); |
| 26 | |
| 27 | $show_upgrade_menu = apply_filters( |
| 28 | 'generateblocks_show_upgrade_menu', |
| 29 | ! defined( 'GENERATEBLOCKS_PRO_VERSION' ) |
| 30 | ); |
| 31 | |
| 32 | if ( $show_upgrade_menu ) { |
| 33 | add_submenu_page( |
| 34 | 'generateblocks', |
| 35 | __( 'Upgrade', 'generateblocks' ), |
| 36 | __( 'Upgrade', 'generateblocks' ), |
| 37 | 'manage_options', |
| 38 | 'generateblocks-upgrade', |
| 39 | 'generateblocks_do_dashboard' |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | add_action( "admin_print_styles-$dashboard", 'generateblocks_enqueue_dashboard_scripts' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add our Dashboard-specific scripts. |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | function generateblocks_enqueue_dashboard_scripts() { |
| 52 | $assets_file = GENERATEBLOCKS_DIR . 'dist/dashboard.asset.php'; |
| 53 | $compiled_assets = file_exists( $assets_file ) |
| 54 | ? require $assets_file |
| 55 | : false; |
| 56 | |
| 57 | $assets = |
| 58 | isset( $compiled_assets['dependencies'] ) && |
| 59 | isset( $compiled_assets['version'] ) |
| 60 | ? $compiled_assets |
| 61 | : [ |
| 62 | 'dependencies' => [], |
| 63 | 'version' => filemtime( GENERATEBLOCKS_DIR . 'dist/dashboard.js' ), |
| 64 | ]; |
| 65 | |
| 66 | wp_enqueue_script( |
| 67 | 'generateblocks-dashboard', |
| 68 | GENERATEBLOCKS_DIR_URL . 'dist/dashboard.js', |
| 69 | $assets['dependencies'], |
| 70 | $assets['version'], |
| 71 | true |
| 72 | ); |
| 73 | |
| 74 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 75 | wp_set_script_translations( 'generateblocks-dashboard', 'generateblocks' ); |
| 76 | } |
| 77 | |
| 78 | wp_localize_script( |
| 79 | 'generateblocks-dashboard', |
| 80 | 'generateblocksDashboard', |
| 81 | array( |
| 82 | 'gpImage' => esc_url( GENERATEBLOCKS_DIR_URL ) . 'assets/images/generatepress-sites.png', |
| 83 | 'gpLogo' => esc_url( GENERATEBLOCKS_DIR_URL ) . 'assets/images/generatepress.svg', |
| 84 | 'gbVersion' => GENERATEBLOCKS_VERSION, |
| 85 | 'gbpVersion' => defined( 'GENERATEBLOCKS_PRO_VERSION' ) |
| 86 | ? GENERATEBLOCKS_PRO_VERSION |
| 87 | : false, |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | wp_enqueue_style( |
| 92 | 'generateblocks-dashboard', |
| 93 | GENERATEBLOCKS_DIR_URL . 'dist/dashboard.css', |
| 94 | array( 'wp-components' ), |
| 95 | GENERATEBLOCKS_VERSION |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get a list of our Dashboard pages. |
| 101 | * |
| 102 | * @since 1.2.0 |
| 103 | */ |
| 104 | function generateblocks_get_dashboard_pages() { |
| 105 | return apply_filters( |
| 106 | 'generateblocks_dashboard_screens', |
| 107 | array( |
| 108 | 'generateblocks_page_generateblocks-settings', |
| 109 | ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | add_filter( 'admin_body_class', 'generateblocks_set_admin_body_classes' ); |
| 114 | /** |
| 115 | * Add admin body classes when needed. |
| 116 | * |
| 117 | * @since 1.2.0 |
| 118 | * @param string $classes The existing classes. |
| 119 | */ |
| 120 | function generateblocks_set_admin_body_classes( $classes ) { |
| 121 | $dashboard_pages = generateblocks_get_dashboard_pages(); |
| 122 | $current_screen = get_current_screen(); |
| 123 | |
| 124 | if ( in_array( $current_screen->id, $dashboard_pages ) ) { |
| 125 | $classes .= ' generateblocks-dashboard-page'; |
| 126 | } |
| 127 | |
| 128 | return $classes; |
| 129 | } |
| 130 | |
| 131 | add_action( 'in_admin_header', 'generateblocks_do_dashboard_headers' ); |
| 132 | /** |
| 133 | * Add our Dashboard headers. |
| 134 | * |
| 135 | * @since 1.3.0 |
| 136 | */ |
| 137 | function generateblocks_do_dashboard_headers() { |
| 138 | $dashboard_pages = generateblocks_get_dashboard_pages(); |
| 139 | $current_screen = get_current_screen(); |
| 140 | |
| 141 | if ( in_array( $current_screen->id, $dashboard_pages ) ) { |
| 142 | generateblocks_do_dashboard_header(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | add_action( 'admin_enqueue_scripts', 'generateblocks_enqueue_global_dashboard_scripts' ); |
| 147 | /** |
| 148 | * Add our scripts to the page. |
| 149 | * |
| 150 | * @since 0.1 |
| 151 | */ |
| 152 | function generateblocks_enqueue_global_dashboard_scripts() { |
| 153 | wp_enqueue_style( |
| 154 | 'generateblocks-dashboard-global', |
| 155 | GENERATEBLOCKS_DIR_URL . 'assets/css/dashboard-global.css', |
| 156 | array(), |
| 157 | GENERATEBLOCKS_VERSION |
| 158 | ); |
| 159 | |
| 160 | $dashboard_pages = generateblocks_get_dashboard_pages(); |
| 161 | $current_screen = get_current_screen(); |
| 162 | |
| 163 | if ( in_array( $current_screen->id, $dashboard_pages ) ) { |
| 164 | wp_enqueue_style( |
| 165 | 'generateblocks-settings-build', |
| 166 | GENERATEBLOCKS_DIR_URL . 'dist/settings.css', |
| 167 | array( 'wp-components' ), |
| 168 | GENERATEBLOCKS_VERSION |
| 169 | ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Build our Dashboard menu. |
| 175 | * |
| 176 | * Retained for backwards compatibility and the `generateblocks_dashboard_tabs` |
| 177 | * filter. The dashboard header no longer renders in-header page navigation — |
| 178 | * those links live in the WordPress admin sidebar. |
| 179 | */ |
| 180 | function generateblocks_dashboard_navigation() { |
| 181 | $screen = get_current_screen(); |
| 182 | |
| 183 | $tabs = apply_filters( |
| 184 | 'generateblocks_dashboard_tabs', |
| 185 | array( |
| 186 | 'settings' => array( |
| 187 | 'name' => __( 'Settings', 'generateblocks' ), |
| 188 | 'url' => admin_url( 'admin.php?page=generateblocks-settings' ), |
| 189 | 'class' => 'generateblocks_page_generateblocks-settings' === $screen->id ? 'active' : '', |
| 190 | ), |
| 191 | ) |
| 192 | ); |
| 193 | |
| 194 | if ( ! defined( 'GENERATEBLOCKS_PRO_VERSION' ) ) { |
| 195 | $tabs['pro'] = array( |
| 196 | 'name' => __( 'Get Pro', 'generateblocks' ), |
| 197 | 'url' => 'https://generatepress.com/blocks/', |
| 198 | 'class' => '', |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | // Don't print any markup if we only have one tab. |
| 203 | if ( count( $tabs ) === 1 ) { |
| 204 | return; |
| 205 | } |
| 206 | ?> |
| 207 | <div class="gblocks-navigation"> |
| 208 | <?php |
| 209 | foreach ( $tabs as $tab ) { |
| 210 | printf( |
| 211 | '<a href="%1$s" class="%2$s">%3$s</a>', |
| 212 | esc_url( $tab['url'] ), |
| 213 | esc_attr( $tab['class'] ), |
| 214 | esc_html( $tab['name'] ) |
| 215 | ); |
| 216 | } |
| 217 | ?> |
| 218 | </div> |
| 219 | <?php |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the HTML tags and attributes allowed in the dashboard header icon. |
| 224 | * |
| 225 | * @since 2.4.0 |
| 226 | * |
| 227 | * @return array Allowed HTML for wp_kses(). |
| 228 | */ |
| 229 | function generateblocks_get_dashboard_header_icon_allowed_html() { |
| 230 | // Shared stroke attributes so outline icons (matching the React studio icons) |
| 231 | // survive sanitization alongside filled glyphs like the GenerateBlocks logo. |
| 232 | $stroke = array( |
| 233 | 'stroke' => true, |
| 234 | 'stroke-width' => true, |
| 235 | 'stroke-linecap' => true, |
| 236 | 'stroke-linejoin' => true, |
| 237 | 'stroke-dasharray' => true, |
| 238 | ); |
| 239 | |
| 240 | return array( |
| 241 | 'svg' => array_merge( |
| 242 | array( |
| 243 | 'xmlns' => true, |
| 244 | 'viewbox' => true, |
| 245 | 'width' => true, |
| 246 | 'height' => true, |
| 247 | 'fill' => true, |
| 248 | 'role' => true, |
| 249 | 'aria-hidden' => true, |
| 250 | 'focusable' => true, |
| 251 | 'class' => true, |
| 252 | ), |
| 253 | $stroke |
| 254 | ), |
| 255 | 'g' => array_merge( |
| 256 | array( |
| 257 | 'fill' => true, |
| 258 | 'class' => true, |
| 259 | 'transform' => true, |
| 260 | ), |
| 261 | $stroke |
| 262 | ), |
| 263 | 'path' => array_merge( |
| 264 | array( |
| 265 | 'd' => true, |
| 266 | 'fill' => true, |
| 267 | 'fill-rule' => true, |
| 268 | 'clip-rule' => true, |
| 269 | 'class' => true, |
| 270 | ), |
| 271 | $stroke |
| 272 | ), |
| 273 | 'rect' => array_merge( |
| 274 | array( |
| 275 | 'x' => true, |
| 276 | 'y' => true, |
| 277 | 'width' => true, |
| 278 | 'height' => true, |
| 279 | 'rx' => true, |
| 280 | 'ry' => true, |
| 281 | 'fill' => true, |
| 282 | 'class' => true, |
| 283 | ), |
| 284 | $stroke |
| 285 | ), |
| 286 | 'circle' => array_merge( |
| 287 | array( |
| 288 | 'cx' => true, |
| 289 | 'cy' => true, |
| 290 | 'r' => true, |
| 291 | 'fill' => true, |
| 292 | 'class' => true, |
| 293 | ), |
| 294 | $stroke |
| 295 | ), |
| 296 | 'line' => array_merge( |
| 297 | array( |
| 298 | 'x1' => true, |
| 299 | 'y1' => true, |
| 300 | 'x2' => true, |
| 301 | 'y2' => true, |
| 302 | 'class' => true, |
| 303 | ), |
| 304 | $stroke |
| 305 | ), |
| 306 | 'polyline' => array_merge( |
| 307 | array( |
| 308 | 'points' => true, |
| 309 | 'fill' => true, |
| 310 | 'class' => true, |
| 311 | ), |
| 312 | $stroke |
| 313 | ), |
| 314 | 'polygon' => array_merge( |
| 315 | array( |
| 316 | 'points' => true, |
| 317 | 'fill' => true, |
| 318 | 'class' => true, |
| 319 | ), |
| 320 | $stroke |
| 321 | ), |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Build our Dashboard header. |
| 327 | * |
| 328 | * @since 1.2.0 |
| 329 | */ |
| 330 | function generateblocks_do_dashboard_header() { |
| 331 | $screen = get_current_screen(); |
| 332 | $screen_id = $screen ? $screen->id : ''; |
| 333 | |
| 334 | // The GenerateBlocks logo is the default icon. Plugins can swap in their own |
| 335 | // SVG for a given screen via the filter below (e.g. GenerateCloud uses a |
| 336 | // cloud glyph on its page). The header CSS renders it white on an accent tile. |
| 337 | $default_icon = '<svg aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" x="0" y="0" viewBox="0 0 50 60.12" xml:space="preserve"><path class="st0" d="M6.686 31.622V18.918a.077.077 0 01.05-.072l6.5-2.313 6.5-2.313 9.682-3.445L39.1 7.33a.067.067 0 00.036-.028.074.074 0 00.014-.044V.076a.077.077 0 00-.032-.062.076.076 0 00-.069-.009l-13 4.625-13 4.625-6.5 2.313-6.5 2.313a.067.067 0 00-.036.028.097.097 0 00-.013.046V52.067c0 .026.013.048.032.062s.044.018.069.009l3.267-1.163 3.267-1.163c.015-.005.028-.015.036-.028s.014-.028.014-.044V37.999l.001-6.377c-.001 0 0 0 0 0z"/><path class="st0" d="M23.949 29.976l13-4.625 13-4.625c.015-.005.028-.015.036-.028s.015-.028.015-.044V8.056a.077.077 0 00-.032-.062.076.076 0 00-.069-.009l-13 4.625-13 4.625-6.5 2.313-6.5 2.313a.067.067 0 00-.036.028.074.074 0 00-.014.044V60.045c0 .026.013.048.032.062a.076.076 0 00.069.009l6.475-2.304 6.475-2.304 6.525-2.322 6.525-2.322 6.5-2.313 6.5-2.313c.015-.005.028-.015.036-.028s.014-.025.014-.041V27.193a.077.077 0 00-.032-.062.076.076 0 00-.069-.009l-6.45 2.295L37 31.711a.067.067 0 00-.036.028.074.074 0 00-.014.044v6.272a.077.077 0 01-.05.072l-6.45 2.295L24 42.715a.075.075 0 01-.101-.071V30.046c0-.016.005-.031.014-.044a.08.08 0 01.036-.026z"/></svg>'; |
| 338 | |
| 339 | /** |
| 340 | * Filters the icon shown in the dashboard header tile for the current screen. |
| 341 | * |
| 342 | * Return SVG markup; it is sanitized with |
| 343 | * generateblocks_get_dashboard_header_icon_allowed_html(). |
| 344 | * |
| 345 | * @since 2.4.0 |
| 346 | * |
| 347 | * @param string $default_icon Default SVG markup (the GenerateBlocks logo). |
| 348 | * @param string $screen_id Current admin screen id. |
| 349 | */ |
| 350 | $icon = (string) apply_filters( 'generateblocks_dashboard_header_icon', $default_icon, $screen_id ); |
| 351 | ?> |
| 352 | <div class="gblocks-dashboard-header"> |
| 353 | <div class="gblocks-dashboard-header-title"> |
| 354 | <h1> |
| 355 | <?php echo wp_kses( $icon, generateblocks_get_dashboard_header_icon_allowed_html() ); ?> |
| 356 | <?php echo esc_html( get_admin_page_title() ); ?> |
| 357 | </h1> |
| 358 | </div> |
| 359 | |
| 360 | <?php if ( ! defined( 'GENERATEBLOCKS_PRO_VERSION' ) ) : ?> |
| 361 | <div class="gblocks-dashboard-header-actions"> |
| 362 | <a class="button button-primary gblocks-get-pro" href="https://generatepress.com/blocks/"> |
| 363 | <?php esc_html_e( 'Get Pro', 'generateblocks' ); ?> |
| 364 | </a> |
| 365 | </div> |
| 366 | <?php endif; ?> |
| 367 | </div> |
| 368 | <?php |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Output our Dashboard HTML. |
| 373 | * |
| 374 | * @since 0.1 |
| 375 | */ |
| 376 | function generateblocks_do_dashboard() { |
| 377 | ?> |
| 378 | <div class="wrap gblocks-dashboard-wrap"> |
| 379 | <div id="gblocks-dashboard" /> |
| 380 | </div> |
| 381 | <?php |
| 382 | } |
| 383 | |
| 384 | add_action( 'admin_init', 'generateblocks_do_upgrade_redirect' ); |
| 385 | /** |
| 386 | * Redirect to the sales page when landing on the upgrade page. |
| 387 | */ |
| 388 | function generateblocks_do_upgrade_redirect() { |
| 389 | if ( empty( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | if ( 'generateblocks-upgrade' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 394 | wp_redirect( 'https://generatepress.com/blocks' ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 395 | exit; |
| 396 | } |
| 397 | } |
| 398 |