| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The admin-facing functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://themeatelier.net |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package darkify |
| 10 |
* @subpackage darkify/Admin |
| 11 |
* @author ThemeAtelier<themeatelierbd@gmail.com> |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace ThemeAtelier\Darkify\Admin; |
| 15 |
use ThemeAtelier\Darkify\Admin\TADiscountPage\TADiscountPage; |
| 16 |
use ThemeAtelier\Darkify\Admin\ReviewNotice\ReviewNotice; |
| 17 |
use ThemeAtelier\Darkify\Admin\Views\DarkifyOptions; |
| 18 |
use ThemeAtelier\Darkify\Includes\DarkifyExternalSupport; |
| 19 |
use ThemeAtelier\Darkify\Includes\DarkifyUtils; |
| 20 |
use ThemeAtelier\Darkify\Admin\DBUpdates; |
| 21 |
use ThemeAtelier\Darkify\Admin\ReviewNotice\ThemeAtelier_Offer_Banner; |
| 22 |
|
| 23 |
/** |
| 24 |
* The admin class |
| 25 |
*/ |
| 26 |
class Admin |
| 27 |
{ |
| 28 |
|
| 29 |
/** |
| 30 |
* The slug of this plugin. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* @access private |
| 34 |
* @var string $plugin_slug The slug of this plugin. |
| 35 |
*/ |
| 36 |
private $plugin_slug; |
| 37 |
|
| 38 |
/** |
| 39 |
* The min of this plugin. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* @access private |
| 43 |
* @var string $min The slug of this plugin. |
| 44 |
*/ |
| 45 |
private $min; |
| 46 |
|
| 47 |
/** |
| 48 |
* The version of this plugin. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @access private |
| 52 |
* @var string $version The current version of this plugin. |
| 53 |
*/ |
| 54 |
private $version; |
| 55 |
public $utils; |
| 56 |
public $settings; |
| 57 |
public $external_support; |
| 58 |
public $unique_id; |
| 59 |
private $plugin_name; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* The class constructor. |
| 64 |
* |
| 65 |
* @param string $plugin_slug The slug of the plugin. |
| 66 |
* @param string $version Current version of the plugin. |
| 67 |
*/ |
| 68 |
function __construct($plugin_slug, $version) |
| 69 |
{ |
| 70 |
$this->utils = new DarkifyUtils($this); |
| 71 |
$this->external_support = new DarkifyExternalSupport($this); |
| 72 |
if (function_exists('wp_rand')) { |
| 73 |
$this->unique_id = wp_rand(); |
| 74 |
} |
| 75 |
|
| 76 |
$options = get_option('darkify'); |
| 77 |
|
| 78 |
$enable_admin_panel_dark_mode = isset($options['enable_admin_panel_dark_mode']) ? $options['enable_admin_panel_dark_mode'] : false; |
| 79 |
$this->plugin_name = $plugin_slug; |
| 80 |
$this->min = (apply_filters('darkify_dev_mode', false) || WP_DEBUG) ? '' : '.min'; |
| 81 |
|
| 82 |
if ($enable_admin_panel_dark_mode) { |
| 83 |
add_action('admin_bar_menu', array($this, 'darkify_admin_bar_switch'), 9999); |
| 84 |
add_action('admin_print_scripts', array($this, 'darkify_admin_header_script'), 1); |
| 85 |
add_action('admin_footer', array($this, 'darkify_admin_footer_script')); |
| 86 |
} |
| 87 |
|
| 88 |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 10 ); |
| 89 |
add_action('after_setup_theme', array($this, 'init_components')); |
| 90 |
add_filter('autoptimize_filter_js_exclude', array($this, 'darkify_exclude_js_from_cache_plugins')); |
| 91 |
add_action('admin_init', [$this, 'load_classic_editor_scripts']); |
| 92 |
add_action('plugin_loaded', [$this, 'init']); |
| 93 |
new ReviewNotice(); |
| 94 |
new DBUpdates(); |
| 95 |
if ( ! defined( 'THEMEATELIER_OFFER_BANNER_LOADED' ) ) { |
| 96 |
define( 'THEMEATELIER_OFFER_BANNER_LOADED', true ); |
| 97 |
ThemeAtelier_Offer_Banner::instance(); |
| 98 |
} |
| 99 |
|
| 100 |
$active_plugins = get_option('active_plugins'); |
| 101 |
foreach ($active_plugins as $active_plugin) { |
| 102 |
$_temp = strpos($active_plugin, 'darkify.php'); |
| 103 |
if (false != $_temp) { |
| 104 |
add_filter('plugin_action_links_' . DARKIFY_BASENAME, array($this, 'add_plugin_action_links'), 10, 2); |
| 105 |
add_filter( 'plugin_row_meta', array( $this, 'after_darkify_row_meta' ), 10, 4 ); |
| 106 |
} |
| 107 |
} |
| 108 |
add_filter( 'admin_footer_text', array( $this, 'darkify_admin_footer' ), 1, 2 ); |
| 109 |
} |
| 110 |
|
| 111 |
public function init_components() { |
| 112 |
DarkifyOptions::options('darkify'); |
| 113 |
} |
| 114 |
|
| 115 |
public function darkify_exclude_js_from_cache_plugins($excludes) |
| 116 |
{ |
| 117 |
$excludes .= ',client_main.js,client_main.min.js'; |
| 118 |
return $excludes; |
| 119 |
} |
| 120 |
|
| 121 |
public function enqueue_styles($hook) |
| 122 |
{ |
| 123 |
if($hook === 'toplevel_page_darkify') { |
| 124 |
wp_enqueue_style('darkify-help-page', DARKIFY_DIR_URL . 'src/Admin/HelpPage/assets/css/help-page' . $this->min . '.css', array(), DARKIFY_VERSION, 'all'); |
| 125 |
} |
| 126 |
|
| 127 |
$options = get_option('darkify'); |
| 128 |
$enable_admin_panel_dark_mode = isset($options["enable_admin_panel_dark_mode"]) ? $options["enable_admin_panel_dark_mode"] : false; |
| 129 |
if ($enable_admin_panel_dark_mode) { |
| 130 |
if (!wp_style_is('darkify-admin-switch', 'enqueued')) { |
| 131 |
wp_enqueue_style('darkify-admin-switch', DARKIFY_DIR_URL . 'src/assets/css/client_main' . $this->min . '.css', array(), DARKIFY_VERSION, 'all'); |
| 132 |
} |
| 133 |
} |
| 134 |
// Review notice CSS |
| 135 |
wp_enqueue_style('darkify-review-notice', DARKIFY_DIR_URL . 'src/Admin/ReviewNotice/assets/css/review-notice' . $this->min . '.css', array(), DARKIFY_VERSION, 'all'); |
| 136 |
} |
| 137 |
|
| 138 |
public function enqueue_scripts($hook) |
| 139 |
{ |
| 140 |
$options = get_option('darkify'); |
| 141 |
$enable_admin_panel_dark_mode = isset($options["enable_admin_panel_dark_mode"]) ? $options["enable_admin_panel_dark_mode"] : false; |
| 142 |
if ($enable_admin_panel_dark_mode) { |
| 143 |
wp_enqueue_script('darkify-admin-client-main', DARKIFY_DIR_URL . 'src/assets/js/client_main' . $this->min . '.js', array(), DARKIFY_VERSION); |
| 144 |
} |
| 145 |
|
| 146 |
if($hook === 'toplevel_page_darkify') { |
| 147 |
wp_enqueue_script('darkify-help-page', DARKIFY_DIR_URL . 'src/Admin/HelpPage/assets/js/help-page' . $this->min . '.js', array('darkify-plugins'), DARKIFY_VERSION, true); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
Dequeue Darkify scripts and styles on specific admin pages (e.g., MainWP pages) |
| 153 |
*/ |
| 154 |
|
| 155 |
public function admin_dequeue_for_specefic_pages() |
| 156 |
{ |
| 157 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 158 |
$dequeue_pages = array( |
| 159 |
'mainwp_tab', |
| 160 |
'managesites', |
| 161 |
'ManageClients', |
| 162 |
'CostSummary', |
| 163 |
'InsightsOverview', |
| 164 |
'Extensions', |
| 165 |
'RESTAPI', |
| 166 |
'mainwp-setup', |
| 167 |
'ThemesManage', |
| 168 |
'PluginsManage', |
| 169 |
'InsightsManage', |
| 170 |
'ManageGroups', |
| 171 |
'UpdatesManage', |
| 172 |
'PluginsInstall', |
| 173 |
'PluginsAutoUpdate', |
| 174 |
'PluginsIgnore', |
| 175 |
'PluginsAbandoned', |
| 176 |
'PluginsIgnoredAbandoned', |
| 177 |
'ThemesInstall', |
| 178 |
'ThemesAutoUpdate', |
| 179 |
'ThemesIgnore', |
| 180 |
'ThemesAbandoned', |
| 181 |
'ThemesIgnoredAbandoned', |
| 182 |
'UserBulkManage', |
| 183 |
'UserBulkAdd', |
| 184 |
'BulkImportUsers', |
| 185 |
'UpdateAdminPasswords', |
| 186 |
'PostBulkManage', |
| 187 |
'Extensions-Mainwp-Backups', |
| 188 |
'Extensions-Mainwp-Security', |
| 189 |
'Extensions-Mainwp-Analytics', |
| 190 |
'Mainwp-Monitoring', |
| 191 |
'Extensions-Mainwp-Agency', |
| 192 |
'Extensions-Mainwp-Administrative', |
| 193 |
'Extensions-Mainwp-Development', |
| 194 |
'Extensions-Mainwp-Performance', |
| 195 |
'ClientAddNew', |
| 196 |
'ClientImport', |
| 197 |
'ManageCostTracker', |
| 198 |
'CostTrackerAdd', |
| 199 |
'AddApiKeys', |
| 200 |
); |
| 201 |
|
| 202 |
|
| 203 |
if ( in_array( $page, $dequeue_pages, true ) ) { |
| 204 |
wp_deregister_style( 'darkify-admin-switch' ); |
| 205 |
wp_dequeue_script( 'darkify-admin-client-main' ); |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Review Text. |
| 212 |
* |
| 213 |
* @param string $text text. |
| 214 |
* |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
public function darkify_admin_footer( $text ) { |
| 218 |
|
| 219 |
$screen = get_current_screen(); |
| 220 |
|
| 221 |
if ( 'toplevel_page_darkify' === $screen->id ) { |
| 222 |
$text = sprintf( |
| 223 |
/* translators: 1: start strong tag, 2: close strong tag. 3: start link 4: close link */ |
| 224 |
__( '<i>Enjoying %1$sDarkify?%2$s Please rate us %3$sWordPress.org%4$s. Your positive feedback will help us grow more. Thank you! 😊</i>', 'darkify' ), |
| 225 |
'<strong>', |
| 226 |
'</strong>', |
| 227 |
'<span class="greet-footer-text-star">� |
| 228 |
� |
| 229 |
� |
| 230 |
� |
| 231 |
� |
| 232 |
</span> <a href="https://wordpress.org/support/plugin/darkify/reviews/#new-post" target="_blank">', |
| 233 |
'</a>' |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
return $text; |
| 238 |
} |
| 239 |
|
| 240 |
// Plugin settings in plugin list |
| 241 |
public function add_plugin_action_links(array $links) |
| 242 |
{ |
| 243 |
$new_links = array( |
| 244 |
sprintf('<a href="' . esc_url(admin_url('admin.php?page=darkify')) . '">' . esc_html__('Settings', 'darkify') . '</a>'), |
| 245 |
sprintf('<a target="_blank" href="https://wordpress.org/support/plugin/darkify/">' . esc_html__('Support', 'darkify') . '</a>'), |
| 246 |
// sprintf('<a style="font-weight: bold;color:#171717" target="_blank" href="https://darkifywp.com/pricing/?ref=1">' . esc_html__('Go Pro', 'darkify') . '</a>'), |
| 247 |
); |
| 248 |
|
| 249 |
$links[] = sprintf( '<a style="font-weight: bold;color:#35b747" target="_blank" href="https://darkifywp.com/pricing/?utm_source=darkify_plugin&utm_medium=action_link&utm_campaign=regular">%s</a>', esc_html__( 'Go Pro!', 'darkify' )); |
| 250 |
|
| 251 |
return array_merge($new_links, $links); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Add plugin row meta link. |
| 256 |
* |
| 257 |
* @since 2.0 |
| 258 |
* |
| 259 |
* @param array $plugin_meta . |
| 260 |
* @param string $file . |
| 261 |
* |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
public function after_darkify_row_meta( $plugin_meta, $file ) { |
| 265 |
|
| 266 |
if ( DARKIFY_BASENAME === $file ) { |
| 267 |
$plugin_meta[] = '<a href="'.DARKIFY_DEMO_URL.'" target="_blank">' . __( 'Live Demo', 'darkify' ) . '</a>'; |
| 268 |
} |
| 269 |
|
| 270 |
return $plugin_meta; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
public function darkify_admin_bar_switch($wp_admin_bar) |
| 275 |
{ |
| 276 |
$args = array( |
| 277 |
'parent' => 'top-secondary', |
| 278 |
'id' => 'darkify_admin_bar_switch_container', |
| 279 |
'meta' => array( |
| 280 |
'class' => 'darkify_admin_bar_switch_container', |
| 281 |
'onclick' => 'darkify_switch_trigger()', |
| 282 |
) |
| 283 |
); |
| 284 |
$wp_admin_bar->add_node($args); |
| 285 |
} |
| 286 |
|
| 287 |
public function darkify_admin_header_script() |
| 288 |
{ |
| 289 |
include_once DarkifyUtils::darkify_locate_template('header_script.php'); |
| 290 |
} |
| 291 |
|
| 292 |
public function darkify_admin_footer_script() |
| 293 |
{ |
| 294 |
include_once DarkifyUtils::darkify_locate_template('footer_script.php'); |
| 295 |
} |
| 296 |
|
| 297 |
public function admin_menu() |
| 298 |
{ |
| 299 |
// global $menu, $admin_page_hooks; |
| 300 |
$capability = apply_filters( 'darkify_ui_permission', 'manage_options' ); |
| 301 |
$menu_icon = 'data:image/svg+xml;base64,' . base64_encode('<svg width="44" height="44" viewBox="0 0 44 44" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 302 |
<path d="M23.5 1.5C23.5 0.671573 22.8284 0 22 0C21.1716 0 20.5 0.671573 20.5 1.5V4.5C20.5 5.32843 21.1716 6 22 6C22.8284 6 23.5 5.32843 23.5 4.5V1.5Z" fill="white" /> |
| 303 |
<path d="M34 22C34 28.6274 28.6274 34 22 34C15.3726 34 10 28.6274 10 22C10 15.3726 15.3726 10 22 10C28.6274 10 34 15.3726 34 22Z" fill="white" /> |
| 304 |
<path d="M22 38C22.8284 38 23.5 38.6716 23.5 39.5V42.5C23.5 43.3284 22.8284 44 22 44C21.1716 44 20.5 43.3284 20.5 42.5V39.5C20.5 38.6716 21.1716 38 22 38Z" fill="white" /> |
| 305 |
<path d="M10.6863 10.6863C10.1005 11.2721 9.15074 11.2721 8.56495 10.6863L6.44363 8.56498C5.85784 7.97919 5.85784 7.02945 6.44363 6.44366C7.02942 5.85787 7.97916 5.85787 8.56495 6.44366L10.6863 8.56498C11.2721 9.15077 11.2721 10.1005 10.6863 10.6863Z" fill="white" /> |
| 306 |
<path d="M35.435 37.5564C36.0208 38.1421 36.9705 38.1421 37.5563 37.5564C38.1421 36.9706 38.1421 36.0208 37.5563 35.435L35.435 33.3137C34.8492 32.7279 33.8995 32.7279 33.3137 33.3137C32.7279 33.8995 32.7279 34.8493 33.3137 35.435L35.435 37.5564Z" fill="white" /> |
| 307 |
<path d="M10.6863 33.3137C11.2721 33.8995 11.2721 34.8492 10.6863 35.435L8.56495 37.5563C7.97916 38.1421 7.02942 38.1421 6.44363 37.5563C5.85784 36.9706 5.85784 36.0208 6.44363 35.435L8.56495 33.3137C9.15074 32.7279 10.1005 32.7279 10.6863 33.3137Z" fill="white" /> |
| 308 |
<path d="M37.5563 8.56496C38.1421 7.97918 38.1421 7.02943 37.5563 6.44364C36.9705 5.85786 36.0208 5.85786 35.435 6.44364L33.3137 8.56496C32.7279 9.15075 32.7279 10.1005 33.3137 10.6863C33.8995 11.2721 34.8492 11.2721 35.435 10.6863L37.5563 8.56496Z" fill="white" /> |
| 309 |
<path d="M6 22C6 22.8284 5.32843 23.5 4.5 23.5H1.5C0.671573 23.5 0 22.8284 0 22C0 21.1716 0.671573 20.5 1.5 20.5H4.5C5.32843 20.5 6 21.1716 6 22Z" fill="white" /> |
| 310 |
<path d="M42.5 23.5C43.3284 23.5 44 22.8284 44 22C44 21.1716 43.3284 20.5 42.5 20.5H39.5C38.6716 20.5 38 21.1716 38 22C38 22.8284 38.6716 23.5 39.5 23.5H42.5Z" fill="white" /> |
| 311 |
</svg>'); |
| 312 |
// This page will be under "Settings" |
| 313 |
add_menu_page( |
| 314 |
esc_html__('Darkify', 'darkify'), |
| 315 |
esc_html__('Darkify', 'darkify'), |
| 316 |
$capability, |
| 317 |
'darkify', |
| 318 |
[$this, 'darkify_settings'], |
| 319 |
$menu_icon, |
| 320 |
60 |
| 321 |
); |
| 322 |
add_submenu_page( |
| 323 |
'darkify', |
| 324 |
__( |
| 325 |
'Upgrade To Pro! 👑', |
| 326 |
'darkify' |
| 327 |
), |
| 328 |
sprintf('<span style="color: #35b747;font-weight:600;" class="darkify-get-pro-text">%s</span>', __('Upgrade To Pro! 👑', 'darkify')), |
| 329 |
'manage_options', |
| 330 |
'https://darkifywp.com/pricing/?utm_source=darkify_plugin&utm_medium=submenu_page&utm_campaign=regular' |
| 331 |
); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
public function darkify_settings() {} |
| 336 |
|
| 337 |
/** |
| 338 |
* Load classic editor scripts. |
| 339 |
* |
| 340 |
* @since 5.0.0 |
| 341 |
*/ |
| 342 |
public function load_classic_editor_scripts() |
| 343 |
{ |
| 344 |
$options = get_option('darkify'); |
| 345 |
$classic_editor_dark_mode = isset($options['classic_editor_dark_mode']) ? $options['classic_editor_dark_mode'] : ''; |
| 346 |
if (! current_user_can('edit_posts') && ! current_user_can('edit_pages')) { |
| 347 |
return; |
| 348 |
} |
| 349 |
if (get_user_option('rich_editing') !== 'true') { |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
// Bail if admin_classic_editor is not enabled. |
| 354 |
if (! wp_validate_boolean($classic_editor_dark_mode)) { |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
add_filter('mce_external_plugins', function ($plugins) { |
| 359 |
$plugins['darkify_button'] = plugins_url('src/assets/js/admin-classic-editor.js', DARKIFY_FILE); |
| 360 |
return $plugins; |
| 361 |
}); |
| 362 |
|
| 363 |
add_filter('mce_buttons', function ($buttons) { |
| 364 |
$buttons[] = 'darkify_button'; |
| 365 |
return $buttons; |
| 366 |
}); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Register WP Ajax actions. |
| 371 |
*/ |
| 372 |
public static function init() |
| 373 |
{ |
| 374 |
$options = get_option('darkify'); |
| 375 |
$block_editor_dark_mode = isset($options['block_editor_dark_mode']) ? $options['block_editor_dark_mode'] : ''; |
| 376 |
if (is_admin() && function_exists('register_block_type') && $block_editor_dark_mode) { |
| 377 |
if ( |
| 378 |
substr($_SERVER['PHP_SELF'], '-8') == 'post.php' || |
| 379 |
substr($_SERVER['PHP_SELF'], '-12') == 'post-new.php' |
| 380 |
) { |
| 381 |
|
| 382 |
if (self::isGutenbergActive()) { |
| 383 |
/** @var static $class */ |
| 384 |
$class = get_called_class(); |
| 385 |
add_action('enqueue_block_editor_assets', function () use ($class) { |
| 386 |
self::registerBlockType(); |
| 387 |
}); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Check if Block Editor is active. |
| 395 |
* |
| 396 |
* @return bool |
| 397 |
*/ |
| 398 |
public static function isGutenbergActive() |
| 399 |
{ |
| 400 |
// Gutenberg plugin is installed and activated. |
| 401 |
$gutenberg = !(false === has_filter('replace_editor', 'gutenberg_init')); |
| 402 |
|
| 403 |
// Block editor since 5.0. |
| 404 |
$block_editor = version_compare($GLOBALS['wp_version'], '5.0-beta', '>'); |
| 405 |
|
| 406 |
if (!$gutenberg && !$block_editor) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
if (self::isClassicEditorPluginActive()) { |
| 411 |
$editor_option = get_option('classic-editor-replace'); |
| 412 |
$block_editor_active = array('no-replace', 'block'); |
| 413 |
|
| 414 |
return in_array($editor_option, $block_editor_active, true); |
| 415 |
} |
| 416 |
|
| 417 |
// Fix for conflict with Avada - Fusion builder and gutenberg blocks |
| 418 |
// Fix for Gutenberg blocks when Avada's post/page types are disabled |
| 419 |
if (class_exists('FusionBuilder') && !(isset($_GET['gutenberg-editor']))) { |
| 420 |
$postTypes = \FusionBuilder::allowed_post_types(); |
| 421 |
return count(array_intersect(['page', 'post'], $postTypes)) < 2; |
| 422 |
} |
| 423 |
|
| 424 |
// Fix for conflict with WooCommerce product page |
| 425 |
if (class_exists('WooCommerce') && (isset($_GET['post_type'])) && ($_GET['post_type']) == "product") { |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
// Fix for conflict with Disable Gutenberg plugin |
| 430 |
if (class_exists('DisableGutenberg')) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Check if Classic Editor plugin is active |
| 439 |
* |
| 440 |
* @return bool |
| 441 |
*/ |
| 442 |
public static function isClassicEditorPluginActive() |
| 443 |
{ |
| 444 |
|
| 445 |
if (!function_exists('is_plugin_active')) { |
| 446 |
|
| 447 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 448 |
} |
| 449 |
|
| 450 |
if (is_plugin_active('classic-editor/classic-editor.php')) { |
| 451 |
|
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Register block for gutenberg |
| 460 |
*/ |
| 461 |
public static function registerBlockType() |
| 462 |
{ |
| 463 |
wp_enqueue_script( |
| 464 |
'darkify-gutenberg-block', |
| 465 |
plugins_url('src/assets/js/admin-gutenberg-block.js', DARKIFY_FILE), |
| 466 |
array('wp-blocks', 'wp-components', 'wp-element', 'wp-editor') |
| 467 |
); |
| 468 |
|
| 469 |
wp_localize_script( |
| 470 |
'darkify-gutenberg-block', |
| 471 |
'darkify', |
| 472 |
array( |
| 473 |
'title' => 'Darkify', |
| 474 |
'description' => 'Darkify dark mode plugin.', |
| 475 |
'data' => self::getAllTablesAndChartsForPageBuilders('gutenberg', 'charts') |
| 476 |
) |
| 477 |
); |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Helper function for getting all tables and charts from the database for page builders |
| 482 |
* |
| 483 |
* @param $builder |
| 484 |
* @param $type |
| 485 |
* |
| 486 |
* @return array |
| 487 |
*/ |
| 488 |
public static function getAllTablesAndChartsForPageBuilders($builder, $type) |
| 489 |
{ |
| 490 |
$selectedType = substr($type, 0, -1); |
| 491 |
|
| 492 |
global $wpdb; |
| 493 |
$returnData = []; |
| 494 |
|
| 495 |
$table_name = $wpdb->prefix . 'wpdata' . $type; |
| 496 |
|
| 497 |
// Check whether the table exists to avoid DB errors when the related plugin/table is absent. |
| 498 |
$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ); |
| 499 |
|
| 500 |
if ( ! $table_exists ) { |
| 501 |
return $returnData; |
| 502 |
} |
| 503 |
|
| 504 |
$query = "SELECT id, title FROM {$table_name} ORDER BY id"; |
| 505 |
|
| 506 |
$allItems = $wpdb->get_results( $query, ARRAY_A ); |
| 507 |
|
| 508 |
if ($builder === 'avada' || $builder === 'elementor' || $builder === 'divi') { |
| 509 |
$returnData[0] = esc_attr__('Select a ' . $selectedType, 'darkify'); |
| 510 |
} else if ($builder === 'bakery') { |
| 511 |
$returnData[__('Select a ' . $selectedType, 'darkify')] = ''; |
| 512 |
} |
| 513 |
|
| 514 |
if ($allItems != null) { |
| 515 |
foreach ($allItems as $item) { |
| 516 |
switch ($builder) { |
| 517 |
case 'gutenberg': |
| 518 |
$returnData[] = [ |
| 519 |
'name' => $item['title'], |
| 520 |
'id' => $item['id'], |
| 521 |
]; |
| 522 |
break; |
| 523 |
case 'avada': |
| 524 |
case 'elementor': |
| 525 |
case 'divi': |
| 526 |
$returnData[$item['id']] = $item['title'] . ' (id: ' . $item['id'] . ')'; |
| 527 |
break; |
| 528 |
case 'bakery': |
| 529 |
$returnData[$item['title']] = $item['id']; |
| 530 |
break; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
return $returnData; |
| 536 |
} |
| 537 |
} |
| 538 |
|