| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers utility class for plugin-wide common functionality. |
| 4 |
* |
| 5 |
* @package gutenify |
| 6 |
* @subpackage Utilities |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace gutenify; |
| 11 |
|
| 12 |
/** |
| 13 |
* Prevent direct access to the file. |
| 14 |
* |
| 15 |
* Ensures this file is being loaded within the WordPress environment. |
| 16 |
*/ |
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Helpers |
| 21 |
* |
| 22 |
* Provides commonly used helper functions across the plugin. |
| 23 |
*/ |
| 24 |
class Helpers { |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns an array of active block slugs, filtered for customization. |
| 28 |
* |
| 29 |
* @return array List of block slugs. |
| 30 |
*/ |
| 31 |
public static function active_blocks() { |
| 32 |
$plugin_constants = self::plugin_constants(); |
| 33 |
$function_prefix = $plugin_constants['plugin_main_function_prefix']; |
| 34 |
|
| 35 |
$blocks = array( |
| 36 |
'templates-browser', |
| 37 |
'advance-slide-item', |
| 38 |
'advance-slider', |
| 39 |
'buttons', |
| 40 |
'button', |
| 41 |
'container', |
| 42 |
'content-toggle-item', |
| 43 |
'faqs', |
| 44 |
'gallery-carousel', |
| 45 |
'grid', |
| 46 |
'grid-item', |
| 47 |
'icon', |
| 48 |
'info-box', |
| 49 |
'map', |
| 50 |
'post-carousel', |
| 51 |
'post-list', |
| 52 |
'section-title', |
| 53 |
'service', |
| 54 |
'slide-item', |
| 55 |
'slider', |
| 56 |
'star-rating', |
| 57 |
'team-member', |
| 58 |
'team', |
| 59 |
'testimonial', |
| 60 |
'testimonials', |
| 61 |
'wc-product-carousel', |
| 62 |
'wc-product-list', |
| 63 |
'notice-bar', |
| 64 |
'countup-v2', |
| 65 |
'icon-v2', |
| 66 |
'countdown-timer', |
| 67 |
'stacking-cards', |
| 68 |
'stacking-card-item', |
| 69 |
'marquee', |
| 70 |
'marquee-item', |
| 71 |
// 'back-to-top', |
| 72 |
// 'image-marquee', |
| 73 |
// 'image-marquee-item', |
| 74 |
// 'search-toggle', |
| 75 |
// 'search-toggle-container', |
| 76 |
|
| 77 |
// Optional blocks (can be enabled via filters or config). |
| 78 |
/** |
| 79 |
* Depricated. |
| 80 |
*/ |
| 81 |
'countup', |
| 82 |
// 'link-wrapper', |
| 83 |
// 'masonry', |
| 84 |
// 'custom-list', |
| 85 |
// 'social-share', |
| 86 |
// 'slider-v2', |
| 87 |
); |
| 88 |
|
| 89 |
// advanced-group is still in development; only enable it when WP_DEBUG is on. |
| 90 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 91 |
$blocks[] = 'advanced-group'; |
| 92 |
} |
| 93 |
|
| 94 |
// These PRO-origin blocks ship in this lite framework but stay |
| 95 |
// gated (PRO active, or the active theme opts in — see |
| 96 |
// Helpers::is_pro_block_active(), checked by each block's own |
| 97 |
// register_block()). Always included here rather than |
| 98 |
// conditionally: active_blocks() runs at plugin-file-load time, |
| 99 |
// before the active theme's own after_setup_theme callback has |
| 100 |
// registered its add_theme_support() calls, so a theme-support |
| 101 |
// check isn't reliable this early. Including them unconditionally |
| 102 |
// only means each block's class file loads (cheap — just defines |
| 103 |
// the class and an `init`-hooked callback); the real gate is |
| 104 |
// checked later, on `init`, once both PRO's activation state and |
| 105 |
// the theme's declared support are reliably known. |
| 106 |
$blocks[] = 'popup'; |
| 107 |
$blocks[] = 'hover-card'; |
| 108 |
$blocks[] = 'hover-card-normal'; |
| 109 |
$blocks[] = 'hover-card-hover'; |
| 110 |
$blocks[] = 'wc-product-images-slider'; |
| 111 |
|
| 112 |
return apply_filters( "{$function_prefix}_active_blocks", $blocks ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns plugin constants such as slugs, URLs, versions. |
| 117 |
* |
| 118 |
* @return array Associative array of plugin constants. |
| 119 |
*/ |
| 120 |
public static function plugin_constants() { |
| 121 |
$defaults = array(); |
| 122 |
|
| 123 |
// Filters allow other components or themes to define these constants. |
| 124 |
$filter_name = 'gutenify_plugin_constants'; |
| 125 |
return apply_filters( $filter_name, $defaults ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Reads the asset file generated by wp-scripts and returns dependencies/version. |
| 130 |
* |
| 131 |
* @param string $path Path to the asset file. |
| 132 |
* @return array Array containing 'dependencies' and 'version'. |
| 133 |
*/ |
| 134 |
public static function asset_file_values( $path ) { |
| 135 |
$plugin_constants = self::plugin_constants(); |
| 136 |
$version = $plugin_constants['plugin_main_version']; |
| 137 |
|
| 138 |
if ( file_exists( $path ) ) { |
| 139 |
return include $path; |
| 140 |
} |
| 141 |
|
| 142 |
// Fallback if asset file doesn't exist. |
| 143 |
return array( |
| 144 |
'dependencies' => array(), |
| 145 |
'version' => $version, |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns the base URL of the plugin's core directory. |
| 151 |
* |
| 152 |
* @return string Trailing-slashed URL. |
| 153 |
*/ |
| 154 |
public static function core_base_url() { |
| 155 |
$plugin_constants = self::plugin_constants(); |
| 156 |
return trailingslashit( $plugin_constants['core_base_url'] ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns the base filesystem path of the plugin's core directory. |
| 161 |
* |
| 162 |
* @return string Trailing-slashed directory path. |
| 163 |
*/ |
| 164 |
public static function core_base_dir() { |
| 165 |
$plugin_constants = self::plugin_constants(); |
| 166 |
return trailingslashit( $plugin_constants['core_base_dir'] ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Checks whether the PRO version of the plugin is active. |
| 171 |
* |
| 172 |
* @return bool True if PRO version is active. |
| 173 |
*/ |
| 174 |
public static function is_pro_active() { |
| 175 |
$constants = self::plugin_constants(); |
| 176 |
$plugin_main_function_prefix = $constants['plugin_main_function_prefix']; |
| 177 |
return apply_filters( $plugin_main_function_prefix . '_pro_activation_status', false ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Whether a PRO-origin block hosted in this lite framework should |
| 182 |
* register: either the PRO plugin is loaded, or the active theme has |
| 183 |
* opted in via `add_theme_support( '{plugin_main_slug}-pro-blocks' )` — |
| 184 |
* one flag shared by every block that has made this PRO-to-lite move, |
| 185 |
* not a per-block flag. Callers must check this on `init` or later |
| 186 |
* (never from Helpers::active_blocks(), which runs at plugin-file-load |
| 187 |
* time): by `init`, every active plugin has finished loading (so |
| 188 |
* `function_exists()` here is reliable regardless of plugin load |
| 189 |
* order) and the active theme's own `after_setup_theme` callback has |
| 190 |
* already run (so its `add_theme_support()` call, if any, is visible |
| 191 |
* to `current_theme_supports()`). |
| 192 |
* |
| 193 |
* @return bool |
| 194 |
*/ |
| 195 |
public static function is_pro_block_active() { |
| 196 |
$constants = self::plugin_constants(); |
| 197 |
$plugin_main_function_prefix = $constants['plugin_main_function_prefix']; |
| 198 |
$plugin_main_slug = $constants['plugin_main_slug']; |
| 199 |
return function_exists( $plugin_main_function_prefix . '_pro' ) || current_theme_supports( $plugin_main_slug . '-pro-blocks' ); |
| 200 |
} |
| 201 |
} |
| 202 |
|