| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Templates |
| 4 | 4 | * |
| 5 | + * @deprecated since v3.1.0 | |
| 6 | + * | |
| 5 | 7 | * @package ghostkit |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | /** |
| @@ -8,80 +10,174 @@ | ||
| 8 | 10 | /** |
| 9 | 11 | * GhostKit_Templates |
| 10 | 12 | */ |
| 11 | 13 | class GhostKit_Templates { |
| 12 | - /** | |
| 13 | - * GhostKit_Templates constructor. | |
| 14 | - */ | |
| 15 | - public function __construct() { | |
| 16 | - // Register custom post type. | |
| 17 | - add_action( 'init', array( $this, 'add_custom_post_type' ) ); | |
| 18 | - } | |
| 14 | + /** | |
| 15 | + * Is templates feature allowed to display in UI. | |
| 16 | + * | |
| 17 | + * @var boolean | |
| 18 | + */ | |
| 19 | + public static $is_allowed = null; | |
| 19 | 20 | |
| 20 | - /** | |
| 21 | - * Register custom post type. | |
| 22 | - */ | |
| 23 | - public function add_custom_post_type() { | |
| 24 | - register_post_type( | |
| 25 | - 'ghostkit_template', | |
| 26 | - array( | |
| 27 | - 'labels' => array( | |
| 28 | - 'name' => _x( 'Templates', 'Post Type General Name', 'ghostkit' ), | |
| 29 | - 'singular_name' => _x( 'Template', 'Post Type Singular Name', 'ghostkit' ), | |
| 30 | - 'menu_name' => __( 'Templates', 'ghostkit' ), | |
| 31 | - 'parent_item_colon' => __( 'Parent Template', 'ghostkit' ), | |
| 32 | - 'all_items' => __( 'Templates', 'ghostkit' ), | |
| 33 | - 'view_item' => __( 'View Template', 'ghostkit' ), | |
| 34 | - 'add_new_item' => __( 'Add New Template', 'ghostkit' ), | |
| 35 | - 'add_new' => __( 'Add New', 'ghostkit' ), | |
| 36 | - 'edit_item' => __( 'Edit Template', 'ghostkit' ), | |
| 37 | - 'update_item' => __( 'Update Template', 'ghostkit' ), | |
| 38 | - 'search_items' => __( 'Search Template', 'ghostkit' ), | |
| 39 | - 'not_found' => __( 'Not Found', 'ghostkit' ), | |
| 40 | - 'not_found_in_trash' => __( 'Not found in Trash', 'ghostkit' ), | |
| 41 | - ), | |
| 42 | - 'public' => false, // true? | |
| 43 | - 'publicly_queryable' => false, // true? | |
| 44 | - 'has_archive' => false, | |
| 45 | - 'show_ui' => true, | |
| 46 | - 'exclude_from_search' => true, | |
| 47 | - 'show_in_nav_menus' => false, | |
| 48 | - 'rewrite' => false, | |
| 49 | - // phpcs:ignore | |
| 50 | - 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( ghostkit()->plugin_path . 'assets/images/admin-icon-templates.svg' ) ), | |
| 51 | - 'menu_position' => 57, | |
| 52 | - 'hierarchical' => false, | |
| 53 | - 'show_in_menu' => true, | |
| 54 | - 'show_in_admin_bar' => true, | |
| 55 | - 'show_in_rest' => true, | |
| 56 | - 'taxonomies' => array( | |
| 57 | - 'ghostkit_template_category', | |
| 58 | - ), | |
| 59 | - 'capability_type' => 'post', | |
| 60 | - 'supports' => array( | |
| 61 | - 'title', | |
| 62 | - 'editor', | |
| 63 | - 'thumbnail', | |
| 64 | - ), | |
| 65 | - ) | |
| 66 | - ); | |
| 21 | + /** | |
| 22 | + * GhostKit_Templates constructor. | |
| 23 | + */ | |
| 24 | + public static function init() { | |
| 25 | + add_action( 'init', 'GhostKit_Templates::add_custom_post_type' ); | |
| 26 | + add_filter( 'admin_notices', 'GhostKit_Templates::add_templates_page_notice' ); | |
| 67 | 27 | |
| 68 | - register_taxonomy( | |
| 69 | - 'ghostkit_template_category', | |
| 70 | - 'ghostkit_template', | |
| 71 | - array( | |
| 72 | - 'label' => esc_html__( 'Categories', 'ghostkit' ), | |
| 73 | - 'labels' => array( | |
| 74 | - 'menu_name' => esc_html__( 'Categories', 'ghostkit' ), | |
| 75 | - ), | |
| 76 | - 'rewrite' => false, | |
| 77 | - 'hierarchical' => false, | |
| 78 | - 'publicly_queryable' => false, | |
| 79 | - 'show_in_nav_menus' => false, | |
| 80 | - 'show_in_rest' => true, | |
| 81 | - 'show_admin_column' => true, | |
| 82 | - ) | |
| 83 | - ); | |
| 84 | - } | |
| 28 | + add_action( | |
| 29 | + 'wp_loaded', | |
| 30 | + function () { | |
| 31 | + if ( ! is_admin() ) { | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + | |
| 35 | + $allow_templates = GhostKit_Templates::is_allowed() ? 'true' : 'false'; | |
| 36 | + | |
| 37 | + // Disable customizer settings from GhostKit. | |
| 38 | + wp_add_inline_script( 'ghostkit-helper', 'if (ghostkitVariables) { ghostkitVariables.allowTemplates = ' . $allow_templates . '; }', 'before' ); | |
| 39 | + }, | |
| 40 | + 9 | |
| 41 | + ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Check if displaying Templates allowed. | |
| 46 | + * | |
| 47 | + * @return boolean | |
| 48 | + */ | |
| 49 | + public static function is_allowed() { | |
| 50 | + if ( null !== self::$is_allowed ) { | |
| 51 | + return self::$is_allowed; | |
| 52 | + } | |
| 53 | + | |
| 54 | + self::$is_allowed = false; | |
| 55 | + | |
| 56 | + /* | |
| 57 | + * Check theme templates. | |
| 58 | + */ | |
| 59 | + if ( count( glob( get_template_directory() . '/ghostkit/templates/*/content.php' ) ) ) { | |
| 60 | + self::$is_allowed = true; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /* | |
| 64 | + * Check child theme templates. | |
| 65 | + */ | |
| 66 | + if ( ! self::$is_allowed && get_stylesheet_directory() !== get_template_directory() && count( glob( get_stylesheet_directory() . '/ghostkit/templates/*/content.php' ) ) ) { | |
| 67 | + self::$is_allowed = true; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Check Templates posts. | |
| 72 | + */ | |
| 73 | + if ( ! self::$is_allowed ) { | |
| 74 | + $db_templates = new WP_Query( | |
| 75 | + array( | |
| 76 | + 'post_type' => 'ghostkit_template', | |
| 77 | + 'posts_per_page' => 1, | |
| 78 | + 'showposts' => 1, | |
| 79 | + ) | |
| 80 | + ); | |
| 81 | + | |
| 82 | + self::$is_allowed = $db_templates->have_posts(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + return self::$is_allowed; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Register custom post type. | |
| 90 | + */ | |
| 91 | + public static function add_custom_post_type() { | |
| 92 | + register_post_type( | |
| 93 | + 'ghostkit_template', | |
| 94 | + array( | |
| 95 | + 'labels' => array( | |
| 96 | + 'name' => _x( 'Templates', 'Post Type General Name', 'ghostkit' ), | |
| 97 | + 'singular_name' => _x( 'Template', 'Post Type Singular Name', 'ghostkit' ), | |
| 98 | + 'menu_name' => __( 'Templates', 'ghostkit' ), | |
| 99 | + 'parent_item_colon' => __( 'Parent Template', 'ghostkit' ), | |
| 100 | + 'all_items' => __( 'Templates', 'ghostkit' ), | |
| 101 | + 'view_item' => __( 'View Template', 'ghostkit' ), | |
| 102 | + 'add_new_item' => __( 'Add New Template', 'ghostkit' ), | |
| 103 | + 'add_new' => __( 'Add New', 'ghostkit' ), | |
| 104 | + 'edit_item' => __( 'Edit Template', 'ghostkit' ), | |
| 105 | + 'update_item' => __( 'Update Template', 'ghostkit' ), | |
| 106 | + 'search_items' => __( 'Search Template', 'ghostkit' ), | |
| 107 | + 'not_found' => __( 'Not Found', 'ghostkit' ), | |
| 108 | + 'not_found_in_trash' => __( 'Not found in Trash', 'ghostkit' ), | |
| 109 | + ), | |
| 110 | + 'public' => false, // true? | |
| 111 | + 'publicly_queryable' => false, // true? | |
| 112 | + 'has_archive' => false, | |
| 113 | + 'show_ui' => true, | |
| 114 | + 'exclude_from_search' => true, | |
| 115 | + 'show_in_nav_menus' => false, | |
| 116 | + 'rewrite' => false, | |
| 117 | + 'hierarchical' => false, | |
| 118 | + 'show_in_menu' => false, | |
| 119 | + 'show_in_admin_bar' => true, | |
| 120 | + 'show_in_rest' => true, | |
| 121 | + 'taxonomies' => array( | |
| 122 | + 'ghostkit_template_category', | |
| 123 | + ), | |
| 124 | + 'capability_type' => 'post', | |
| 125 | + 'supports' => array( | |
| 126 | + 'title', | |
| 127 | + 'editor', | |
| 128 | + 'thumbnail', | |
| 129 | + ), | |
| 130 | + ) | |
| 131 | + ); | |
| 132 | + | |
| 133 | + register_taxonomy( | |
| 134 | + 'ghostkit_template_category', | |
| 135 | + 'ghostkit_template', | |
| 136 | + array( | |
| 137 | + 'label' => esc_html__( 'Categories', 'ghostkit' ), | |
| 138 | + 'labels' => array( | |
| 139 | + 'menu_name' => esc_html__( 'Categories', 'ghostkit' ), | |
| 140 | + ), | |
| 141 | + 'rewrite' => false, | |
| 142 | + 'hierarchical' => false, | |
| 143 | + 'publicly_queryable' => false, | |
| 144 | + 'show_in_nav_menus' => false, | |
| 145 | + 'show_in_rest' => true, | |
| 146 | + 'show_admin_column' => true, | |
| 147 | + ) | |
| 148 | + ); | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Show notice in templates list admin page. | |
| 153 | + */ | |
| 154 | + public static function add_templates_page_notice() { | |
| 155 | + $current_screen = get_current_screen(); | |
| 156 | + | |
| 157 | + if ( ! isset( $current_screen->post_type ) || 'ghostkit_template' !== $current_screen->post_type ) { | |
| 158 | + return; | |
| 159 | + } | |
| 160 | + | |
| 161 | + ?> | |
| 162 | + <div class="notice notice-error"> | |
| 163 | + <h3> | |
| 164 | + <?php echo esc_html__( 'Templates Deprecated', 'ghostkit' ); ?> | |
| 165 | + </h3> | |
| 166 | + <p> | |
| 167 | + <?php | |
| 168 | + echo esc_html__( 'Please avoid using the Templates feature. It has been deprecated since Ghost Kit v3.1.0 and will be removed in future updates.', 'ghostkit' ); | |
| 169 | + ?> | |
| 170 | + <br /> | |
| 171 | + <?php | |
| 172 | + echo wp_kses_post( sprintf( __( 'To create a block template, you can use the built-in WordPress feature named Patterns.', 'ghostkit' ), 'https://wordpress.org/documentation/article/site-editor-patterns/' ) ); | |
| 173 | + ?> | |
| 174 | + </p> | |
| 175 | + <p> | |
| 176 | + <a href="https://wordpress.org/documentation/article/site-editor-patterns/" target="_blank" class="button button-primary"><?php echo esc_html__( 'Read About Patterns', 'ghostkit' ); ?></a> | |
| 177 | + </p> | |
| 178 | + </div> | |
| 179 | + <?php | |
| 180 | + } | |
| 85 | 181 | } |
| 86 | 182 | |
| 87 | -new GhostKit_Templates(); | |
| 183 | +GhostKit_Templates::init(); | |