| @@ -23,10 +23,13 @@ | ||
| 23 | 23 | add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); |
| 24 | 24 | |
| 25 | 25 | add_filter('template_include', array( $this, 'view_project_template') ); |
| 26 | 26 | |
| 27 | + add_filter('theme_page_templates', array( $this, 'register_project_templates_update') ); | |
| 28 | + | |
| 29 | + // Initialize template keys without translation (translation happens lazily) | |
| 27 | 30 | $this->templates = array( |
| 28 | - 'template-contact.php' => __('Contact', 'kadencetoolkit'), | |
| 31 | + 'template-contact.php' => 'Contact', | |
| 29 | 32 | ); |
| 30 | 33 | |
| 31 | 34 | } |
| 32 | 35 | |
| @@ -34,9 +37,9 @@ | ||
| 34 | 37 | |
| 35 | 38 | // Create the key used for the themes cache |
| 36 | 39 | $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 37 | 40 | |
| 38 | - $templates = wp_get_theme()->get_page_templates(); | |
| 41 | + $templates = wp_get_theme()->get_page_templates(); | |
| 39 | 42 | |
| 40 | 43 | if ( empty( $templates ) ) { |
| 41 | 44 | $templates = array(); |
| 42 | 45 | } |
| @@ -53,9 +56,21 @@ | ||
| 53 | 56 | |
| 54 | 57 | return $atts; |
| 55 | 58 | |
| 56 | 59 | } |
| 60 | + public function register_project_templates_update($templates ) { | |
| 61 | + if ( ! version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) { | |
| 62 | + // Translate template names when they're actually needed (after init) | |
| 63 | + $translated_templates = array(); | |
| 64 | + foreach ( $this->templates as $key => $label ) { | |
| 65 | + $translated_templates[ $key ] = __( $label, 'virtue-toolkit' ); | |
| 66 | + } | |
| 67 | + $templates = array_merge( $templates, $translated_templates ); | |
| 57 | 68 | |
| 69 | + } | |
| 70 | + return $templates; | |
| 71 | + | |
| 72 | + } | |
| 58 | 73 | /** |
| 59 | 74 | * Checks if the template is assigned to the page |
| 60 | 75 | */ |
| 61 | 76 | public function view_project_template( $template ) { |
| @@ -61,8 +76,10 @@ | ||
| 61 | 76 | public function view_project_template( $template ) { |
| 62 | 77 | |
| 63 | 78 | global $post; |
| 64 | 79 | |
| 80 | + if ( !isset( $post ) ) return $template; | |
| 81 | + | |
| 65 | 82 | if (!isset($this->templates[get_post_meta( $post->ID, '_wp_page_template', true )] ) ) { |
| 66 | 83 | |
| 67 | 84 | return $template; |
| 68 | 85 | |
| @@ -103,16 +120,19 @@ | ||
| 103 | 120 | private function __construct() { |
| 104 | 121 | |
| 105 | 122 | $this->templates = array(); |
| 106 | 123 | |
| 107 | - add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); | |
| 124 | + add_filter('wp_dropdown_pages', array( $this, 'register_project_templates' ) ); | |
| 108 | 125 | |
| 109 | 126 | add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); |
| 110 | 127 | |
| 111 | 128 | add_filter('template_include', array( $this, 'view_project_template') ); |
| 112 | 129 | |
| 130 | + add_filter('theme_page_templates', array( $this, 'register_project_templates_update') ); | |
| 131 | + | |
| 132 | + // Initialize template keys without translation (translation happens lazily) | |
| 113 | 133 | $this->templates = array( |
| 114 | - 'page-contact.php' => __('Contact', 'kadencetoolkit'), | |
| 134 | + 'page-contact.php' => 'Contact', | |
| 115 | 135 | ); |
| 116 | 136 | |
| 117 | 137 | } |
| 118 | 138 | |
| @@ -139,9 +159,22 @@ | ||
| 139 | 159 | |
| 140 | 160 | return $atts; |
| 141 | 161 | |
| 142 | 162 | } |
| 163 | + public function register_project_templates_update($templates ) { | |
| 164 | + if ( ! version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) { | |
| 165 | + // Translate template names when they're actually needed (after init) | |
| 166 | + $translated_templates = array(); | |
| 167 | + foreach ( $this->templates as $key => $label ) { | |
| 168 | + $translated_templates[ $key ] = __( $label, 'virtue-toolkit' ); | |
| 169 | + } | |
| 170 | + $templates = array_merge( $templates, $translated_templates ); | |
| 143 | 171 | |
| 172 | + } | |
| 173 | + return $templates; | |
| 174 | + | |
| 175 | + } | |
| 176 | + | |
| 144 | 177 | /** |
| 145 | 178 | * Checks if the template is assigned to the page |
| 146 | 179 | */ |
| 147 | 180 | public function view_project_template( $template ) { |
| @@ -147,8 +180,10 @@ | ||
| 147 | 180 | public function view_project_template( $template ) { |
| 148 | 181 | |
| 149 | 182 | global $post; |
| 150 | 183 | |
| 184 | + if ( !isset( $post ) ) return $template; | |
| 185 | + | |
| 151 | 186 | if (!isset($this->templates[get_post_meta( $post->ID, '_wp_page_template', true )] ) ) { |
| 152 | 187 | |
| 153 | 188 | return $template; |
| 154 | 189 | |
| @@ -170,5 +205,5 @@ | ||
| 170 | 205 | } |
| 171 | 206 | $the_theme = wp_get_theme(); |
| 172 | 207 | if( ($the_theme->get( 'Name' ) == 'Virtue' && $the_theme->get( 'Version') >= '2.3.5') || ($the_theme->get( 'Template') == 'virtue') ) { |
| 173 | 208 | add_action( 'plugins_loaded', array( 'Kadence_Page_Templater_Virtue', 'get_instance' ) ); |
| 174 | -} | |
| 209 | +} | |