| 1 |
<?php |
| 2 |
|
| 3 |
class Kadence_Page_Templater_Pinnacle { |
| 4 |
|
| 5 |
|
| 6 |
protected $plugin_slug; |
| 7 |
private static $instance; |
| 8 |
protected $templates; |
| 9 |
|
| 10 |
public static function get_instance() { |
| 11 |
|
| 12 |
if( null == self::$instance ) { |
| 13 |
self::$instance = new Kadence_Page_Templater_Pinnacle(); |
| 14 |
} |
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
private function __construct() { |
| 18 |
|
| 19 |
$this->templates = array(); |
| 20 |
|
| 21 |
add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); |
| 22 |
|
| 23 |
add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); |
| 24 |
|
| 25 |
add_filter('template_include', array( $this, 'view_project_template') ); |
| 26 |
|
| 27 |
$this->templates = array( |
| 28 |
'template-contact.php' => __('Contact', 'kadencetoolkit'), |
| 29 |
); |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
public function register_project_templates( $atts ) { |
| 34 |
|
| 35 |
// Create the key used for the themes cache |
| 36 |
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 37 |
|
| 38 |
$templates = wp_get_theme()->get_page_templates(); |
| 39 |
|
| 40 |
if ( empty( $templates ) ) { |
| 41 |
$templates = array(); |
| 42 |
} |
| 43 |
// New cache, therefore remove the old one |
| 44 |
wp_cache_delete( $cache_key , 'themes'); |
| 45 |
|
| 46 |
// Now add our template to the list of templates by merging our templates |
| 47 |
// with the existing templates array from the cache. |
| 48 |
$templates = array_merge( $templates, $this->templates ); |
| 49 |
|
| 50 |
// Add the modified cache to allow WordPress to pick it up for listing |
| 51 |
// available templates |
| 52 |
wp_cache_add( $cache_key, $templates, 'themes', 1800 ); |
| 53 |
|
| 54 |
return $atts; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Checks if the template is assigned to the page |
| 60 |
*/ |
| 61 |
public function view_project_template( $template ) { |
| 62 |
|
| 63 |
global $post; |
| 64 |
|
| 65 |
if ( !isset( $post ) ) return $template; |
| 66 |
|
| 67 |
if (!isset($this->templates[get_post_meta( $post->ID, '_wp_page_template', true )] ) ) { |
| 68 |
|
| 69 |
return $template; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
$file = plugin_dir_path(__FILE__). get_post_meta( $post->ID, '_wp_page_template', true ); |
| 74 |
|
| 75 |
// Just to be safe, we check if the file exist first |
| 76 |
if( file_exists( $file ) ) { |
| 77 |
return $file; |
| 78 |
} else { |
| 79 |
echo $file; |
| 80 |
} |
| 81 |
|
| 82 |
return $template; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
$the_theme = wp_get_theme(); |
| 88 |
if( ($the_theme->get( 'Name' ) == 'Pinnacle' && $the_theme->get( 'Version') >= '1.0.6' ) || ($the_theme->get( 'Template') == 'pinnacle') ) { |
| 89 |
add_action( 'plugins_loaded', array( 'Kadence_Page_Templater_Pinnacle', 'get_instance' ) ); |
| 90 |
} |
| 91 |
class Kadence_Page_Templater_Virtue { |
| 92 |
|
| 93 |
|
| 94 |
protected $plugin_slug; |
| 95 |
private static $instance; |
| 96 |
protected $templates; |
| 97 |
|
| 98 |
public static function get_instance() { |
| 99 |
|
| 100 |
if( null == self::$instance ) { |
| 101 |
self::$instance = new Kadence_Page_Templater_Virtue(); |
| 102 |
} |
| 103 |
return self::$instance; |
| 104 |
} |
| 105 |
private function __construct() { |
| 106 |
|
| 107 |
$this->templates = array(); |
| 108 |
|
| 109 |
add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); |
| 110 |
|
| 111 |
add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); |
| 112 |
|
| 113 |
add_filter('template_include', array( $this, 'view_project_template') ); |
| 114 |
|
| 115 |
$this->templates = array( |
| 116 |
'page-contact.php' => __('Contact', 'kadencetoolkit'), |
| 117 |
); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
public function register_project_templates( $atts ) { |
| 122 |
|
| 123 |
// Create the key used for the themes cache |
| 124 |
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 125 |
|
| 126 |
$templates = wp_get_theme()->get_page_templates(); |
| 127 |
|
| 128 |
if ( empty( $templates ) ) { |
| 129 |
$templates = array(); |
| 130 |
} |
| 131 |
// New cache, therefore remove the old one |
| 132 |
wp_cache_delete( $cache_key , 'themes'); |
| 133 |
|
| 134 |
// Now add our template to the list of templates by merging our templates |
| 135 |
// with the existing templates array from the cache. |
| 136 |
$templates = array_merge( $templates, $this->templates ); |
| 137 |
|
| 138 |
// Add the modified cache to allow WordPress to pick it up for listing |
| 139 |
// available templates |
| 140 |
wp_cache_add( $cache_key, $templates, 'themes', 1800 ); |
| 141 |
|
| 142 |
return $atts; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Checks if the template is assigned to the page |
| 148 |
*/ |
| 149 |
public function view_project_template( $template ) { |
| 150 |
|
| 151 |
global $post; |
| 152 |
|
| 153 |
if ( !isset( $post ) ) return $template; |
| 154 |
|
| 155 |
if (!isset($this->templates[get_post_meta( $post->ID, '_wp_page_template', true )] ) ) { |
| 156 |
|
| 157 |
return $template; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
$file = plugin_dir_path(__FILE__). get_post_meta( $post->ID, '_wp_page_template', true ); |
| 162 |
|
| 163 |
// Just to be safe, we check if the file exist first |
| 164 |
if( file_exists( $file ) ) { |
| 165 |
return $file; |
| 166 |
} else { |
| 167 |
echo $file; |
| 168 |
} |
| 169 |
|
| 170 |
return $template; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
$the_theme = wp_get_theme(); |
| 176 |
if( ($the_theme->get( 'Name' ) == 'Virtue' && $the_theme->get( 'Version') >= '2.3.5') || ($the_theme->get( 'Template') == 'virtue') ) { |
| 177 |
add_action( 'plugins_loaded', array( 'Kadence_Page_Templater_Virtue', 'get_instance' ) ); |
| 178 |
} |
| 179 |
|