| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Load google fonts. |
| 5 |
*/ |
| 6 |
|
| 7 |
// Exit if accessed directly. |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Progress_Bar_Helper |
| 13 |
{ |
| 14 |
|
| 15 |
private static $instance; |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers the plugin. |
| 19 |
*/ |
| 20 |
public static function register() |
| 21 |
{ |
| 22 |
if (null === self::$instance) { |
| 23 |
self::$instance = new self; |
| 24 |
} |
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* The Constructor. |
| 30 |
*/ |
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
add_action('admin_enqueue_scripts', array($this, 'enqueues')); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Responsive breakpoints shared by the editor and the frontend. |
| 38 |
* |
| 39 |
* Mirrors EbStyleHandlerParseCss::get_responsive_breakpoints() — same option, |
| 40 |
* same defaults — so the media queries the editor renders match the ones the |
| 41 |
* style handler writes into the generated CSS file. |
| 42 |
* |
| 43 |
* @return array{tablet:int,mobile:int} |
| 44 |
*/ |
| 45 |
public static function get_responsive_breakpoints() |
| 46 |
{ |
| 47 |
$defaults = array( |
| 48 |
'tablet' => 1024, |
| 49 |
'mobile' => 767, |
| 50 |
); |
| 51 |
|
| 52 |
$settings = get_option('eb_settings', array()); |
| 53 |
if (!is_array($settings) || !isset($settings['responsiveBreakpoints'])) { |
| 54 |
return $defaults; |
| 55 |
} |
| 56 |
|
| 57 |
$breakpoints = $settings['responsiveBreakpoints']; |
| 58 |
if (is_string($breakpoints) && strlen($breakpoints) > 0) { |
| 59 |
$breakpoints = json_decode(html_entity_decode(stripslashes($breakpoints)), true); |
| 60 |
} |
| 61 |
if (is_object($breakpoints)) { |
| 62 |
$breakpoints = (array) $breakpoints; |
| 63 |
} |
| 64 |
if (!is_array($breakpoints)) { |
| 65 |
return $defaults; |
| 66 |
} |
| 67 |
|
| 68 |
return array( |
| 69 |
'tablet' => !empty($breakpoints['tablet']) ? absint($breakpoints['tablet']) : $defaults['tablet'], |
| 70 |
'mobile' => !empty($breakpoints['mobile']) ? absint($breakpoints['mobile']) : $defaults['mobile'], |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Load fonts. |
| 76 |
* |
| 77 |
* @access public |
| 78 |
*/ |
| 79 |
public function enqueues($hook) |
| 80 |
{ |
| 81 |
global $pagenow; |
| 82 |
|
| 83 |
/** |
| 84 |
* Only for admin add/edit pages/posts |
| 85 |
*/ |
| 86 |
$query_string = isset($_SERVER['QUERY_STRING']) ? sanitize_text_field(wp_unslash($_SERVER['QUERY_STRING'])) : ''; |
| 87 |
|
| 88 |
if ($pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'site-editor.php' || ($pagenow == 'themes.php' && !empty($query_string) && str_contains($query_string, 'gutenberg-edit-site'))) { |
| 89 |
|
| 90 |
/** |
| 91 |
* `include_once` returns bool `true` when the file was already included |
| 92 |
* earlier in the request, which turns the array reads below into |
| 93 |
* offset-on-bool warnings under PHP 7.4+ / 8.x. Use `require` instead. |
| 94 |
*/ |
| 95 |
$controls_asset_path = PROGRESS_BARS_BLOCKS_ADMIN_PATH . '/dist/modules.asset.php'; |
| 96 |
if (!file_exists($controls_asset_path)) { |
| 97 |
return; |
| 98 |
} |
| 99 |
$controls_dependencies = require $controls_asset_path; |
| 100 |
|
| 101 |
$controls_deps = isset($controls_dependencies['dependencies']) ? $controls_dependencies['dependencies'] : array(); |
| 102 |
$controls_version = isset($controls_dependencies['version']) ? $controls_dependencies['version'] : PROGRESS_BARS_BLOCKS_VERSION; |
| 103 |
|
| 104 |
wp_register_script( |
| 105 |
"progress-bars-blocks-controls-util", |
| 106 |
PROGRESS_BARS_BLOCKS_ADMIN_URL . 'dist/modules.js', |
| 107 |
array_merge($controls_deps, ['lodash']), |
| 108 |
$controls_version, |
| 109 |
true |
| 110 |
); |
| 111 |
|
| 112 |
wp_localize_script('progress-bars-blocks-controls-util', 'EssentialBlocksLocalize', array( |
| 113 |
'eb_wp_version' => (float) get_bloginfo('version'), |
| 114 |
'rest_rootURL' => get_rest_url(), |
| 115 |
/** |
| 116 |
* The editor's StyleComponent builds its responsive media queries from |
| 117 |
* EssentialBlocksLocalize.responsiveBreakpoints. Without this key it |
| 118 |
* emitted `@media all and (max-width: undefinedpx)`, which browsers |
| 119 |
* discard — so tablet and mobile styles silently did nothing in the |
| 120 |
* editor while the frontend (which reads the same option in PHP) |
| 121 |
* applied them. Same source, same defaults, so both now agree. |
| 122 |
*/ |
| 123 |
'responsiveBreakpoints' => self::get_responsive_breakpoints(), |
| 124 |
)); |
| 125 |
|
| 126 |
if ($pagenow == 'post-new.php' || $pagenow == 'post.php') { |
| 127 |
wp_localize_script('progress-bars-blocks-controls-util', 'eb_conditional_localize', array( |
| 128 |
'editor_type' => 'edit-post' |
| 129 |
)); |
| 130 |
} else if ($pagenow == 'site-editor.php' || $pagenow == 'themes.php') { |
| 131 |
wp_localize_script('progress-bars-blocks-controls-util', 'eb_conditional_localize', array( |
| 132 |
'editor_type' => 'edit-site' |
| 133 |
)); |
| 134 |
} |
| 135 |
|
| 136 |
wp_enqueue_style( |
| 137 |
'essential-blocks-editor-css', |
| 138 |
PROGRESS_BARS_BLOCKS_ADMIN_URL . '/dist/modules.css', |
| 139 |
array(), |
| 140 |
$controls_version, |
| 141 |
'all' |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
public static function get_block_register_path($blockname, $blockPath) |
| 146 |
{ |
| 147 |
/** |
| 148 |
* Never cast a WP version to float: "6.10" casts to 6.1 and compares as |
| 149 |
* older than 6.8. Use version_compare() on the raw string instead. |
| 150 |
* |
| 151 |
* NOTE: with a declared floor of WP 6.0 this branch is now unreachable. |
| 152 |
* Left in place pending review rather than removed silently. |
| 153 |
*/ |
| 154 |
if (version_compare(get_bloginfo('version'), '5.8', '<')) { |
| 155 |
return $blockname; |
| 156 |
} else { |
| 157 |
return $blockPath; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
Progress_Bar_Helper::register(); |
| 162 |
|