From 32e8f10d5e8e5e393c3f1c1fce458810a01c1edb Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Sat, 27 Feb 2021 21:52:11 -0300 Subject: [PATCH] Checks if blocksy theme is enabled before loading plugin. --- functions.php | 34 +++++++++++++++++---------- inc/customizer.php | 4 ++-- inc/enqueues.php | 2 +- inc/plugin.php | 4 ++-- tainacan/item-single-page.php | 6 ++--- utils.php | 44 +++++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 utils.php diff --git a/functions.php b/functions.php index ccac0b7..4f047db 100644 --- a/functions.php +++ b/functions.php @@ -18,21 +18,31 @@ if (! defined('WP_DEBUG') ) { const BLOCKSY_TAINACAN_VERSION = '0.1.0'; const BLOCKSY_TAINACAN_IS_PLUGIN = false; -$plugin_root_url = BLOCKSY_TAINACAN_IS_PLUGIN ? plugin_dir_url(__FILE__) : get_stylesheet_directory_uri(); +/* Tools to define our next constants */ +require 'utils.php'; + +$plugin_root_url = blocksy_tainacan_get_plugin_dir_url(); define('BLOCKSY_TAINACAN_PLUGIN_URL_PATH', $plugin_root_url); -$plugin_root_dir = BLOCKSY_TAINACAN_IS_PLUGIN ? plugin_dir_path(__FILE__) : get_stylesheet_directory() ; +$plugin_root_dir = blocksy_tainacan_get_plugin_dir_path(); define('BLOCKSY_TAINACAN_PLUGIN_DIR_PATH', $plugin_root_dir); -/* Basic styles and script enqueues */ -require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/enqueues.php'; +$blocksy_tainacan_is_blocksy_activated = blocksy_tainacan_is_blocksy_activated(); +define('BLOCKSY_TAINACAN_IS_BLOCKSY_ACTIVATED', $blocksy_tainacan_is_blocksy_activated); -/* Template redirection necessary only if in a plugin */ -if ( BLOCKSY_TAINACAN_IS_PLUGIN ) { - require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/plugin.php'; -} +/* This should only be used if in the child theme or if is a plugin and blocksy theme is installed */ +if (!BLOCKSY_TAINACAN_IS_PLUGIN || (BLOCKSY_TAINACAN_IS_BLOCKSY_ACTIVATED && BLOCKSY_TAINACAN_IS_PLUGIN) ) { -/* Requires several settings, functions and helpers */ -require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/integration.php'; -require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/customizer.php'; -require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/navigation.php'; \ No newline at end of file + /* Basic styles and script enqueues */ + require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/enqueues.php'; + + /* Template redirection necessary only if in a plugin */ + if ( BLOCKSY_TAINACAN_IS_PLUGIN ) { + require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/plugin.php'; + } + + /* Requires several settings, functions and helpers */ + require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/integration.php'; + require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/customizer.php'; + require BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/inc/navigation.php'; +} \ No newline at end of file diff --git a/inc/customizer.php b/inc/customizer.php index 77659be..0efba28 100644 --- a/inc/customizer.php +++ b/inc/customizer.php @@ -148,13 +148,13 @@ if ( !function_exists('blocksy_tainacan_the_content_for_items') ) { // Check if we're inside the main loop in a single Post. if (in_array($post_type, $collections_post_types) && is_singular() && in_the_loop() && is_main_query() ) { - return get_template_part( 'tainacan/item-single-page' ); + return blocksy_tainacan_get_template_part( 'tainacan/item-single-page' ); } } return $content; } } -add_filter( 'the_content', 'blocksy_tainacan_the_content_for_items'); +add_filter( 'the_content', 'blocksy_tainacan_the_content_for_items', 11); ?> \ No newline at end of file diff --git a/inc/enqueues.php b/inc/enqueues.php index d63deb4..12bc207 100644 --- a/inc/enqueues.php +++ b/inc/enqueues.php @@ -3,7 +3,7 @@ /** * Enqueues styles and scripts * Some JS files here are only necessary for Tainacan Item pages - */ + */ function blocksy_tainacan_enqueue_scripts() { // First, we enqueue parent theme styles diff --git a/inc/plugin.php b/inc/plugin.php index 123982f..8a60064 100644 --- a/inc/plugin.php +++ b/inc/plugin.php @@ -4,7 +4,7 @@ * Uses Template redirect for setting the proper template to items * archives on Tainacan pages */ -if ( !function_exists('blocksy_tainacan_archive_templates_redirects') ) +if ( !function_exists('blocksy_tainacan_archive_templates_redirects') ) { function blocksy_tainacan_archive_templates_redirects() { global $wp_query; if (is_post_type_archive()) { @@ -43,7 +43,7 @@ add_action('template_redirect', 'blocksy_tainacan_archive_templates_redirects'); * Uses Template redirect for setting the proper template to items * archives on Tainacan pages */ -if ( !function_exists('blocksy_tainacan_update_extensions_paths') ) +if ( !function_exists('blocksy_tainacan_update_extensions_paths') ) { function blocksy_tainacan_update_extensions_paths($paths) { var_dump($paths); return $paths; diff --git a/tainacan/item-single-page.php b/tainacan/item-single-page.php index aa9f602..7af9b65 100644 --- a/tainacan/item-single-page.php +++ b/tainacan/item-single-page.php @@ -33,13 +33,13 @@ if ($page_structure_type == 'type-gm' || $page_structure_type == 'type-mg') {
diff --git a/utils.php b/utils.php new file mode 100644 index 0000000..5a8ec27 --- /dev/null +++ b/utils.php @@ -0,0 +1,44 @@ +get_stylesheet(), 'blocksy' ) !== false; + $is_child_theme = $theme->parent() !== false; + $another_theme_in_preview = false; + if ( (isset( $_REQUEST['theme'] ) && strpos( strtolower( $_REQUEST['theme'] ), 'blocksy' ) === false || isset( $_REQUEST['customize_theme'] ) && strpos( strtolower( $_REQUEST['customize_theme'] ), 'blocksy' ) === false) && strpos( $_SERVER['REQUEST_URI'], 'customize' ) !== false ) { + $another_theme_in_preview = true; + } + return $is_correct_theme && !$is_child_theme && !$another_theme_in_preview; + } +} + +/** + * Checks is the current activate theme is blocksy + */ +if ( !function_exists('blocksy_tainacan_get_plugin_dir_url') ) { + function blocksy_tainacan_get_plugin_dir_url() { + return BLOCKSY_TAINACAN_IS_PLUGIN ? plugin_dir_url(__FILE__) : get_stylesheet_directory_uri(); + } +} + +/** + * Checks is the current activate theme is blocksy + */ +if ( !function_exists('blocksy_tainacan_get_plugin_dir_path') ) { + function blocksy_tainacan_get_plugin_dir_path() { + return BLOCKSY_TAINACAN_IS_PLUGIN ? plugin_dir_path(__FILE__) : get_stylesheet_directory(); + } +} + +/** + * Manages correct template location in plugin + */ +if ( !function_exists('blocksy_tainacan_get_template_part') ) { + function blocksy_tainacan_get_template_part($path) { + return BLOCKSY_TAINACAN_IS_PLUGIN ? include(BLOCKSY_TAINACAN_PLUGIN_DIR_PATH . '/' . $path . '.php') : get_template_part($path); + } +}