PluginProbe
Extendify / 0.8.4
Extendify v0.8.4
3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 0.8.0 All 125 releases
extendify / editorplus / EditorPlus.php

EditorPlus.php in Extendify 0.8.4, at editorplus/EditorPlus.php

222 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles editor related changes.
4 * Loaded (or not) in /bootstrap.php
5 */
6
7 if (!class_exists('edpl__EditorPlus')) {
8 // phpcs:ignore Squiz.Classes.ClassFileName.NoMatch,Squiz.Commenting.ClassComment.Missing,PEAR.Commenting.ClassComment.Missing
9 final class ExtendifyEditorPlus
10 {
11
12 /**
13 * A reference to an instance of this class.
14 *
15 * @var $instance
16 */
17 public static $instance;
18
19 /**
20 * The array of templates that this plugin tracks.
21 *
22 * @var array $templates
23 */
24 protected $templates = ['editorplus-template.php' => 'Extendify Template'];
25
26 /**
27 * Returns an instance of this class.
28 *
29 * @return self
30 */
31 public static function getInstance()
32 {
33 if (!current_user_can('install_plugins')) {
34 return;
35 }
36
37 if (is_null(self::$instance)) {
38 self::$instance = new ExtendifyEditorPlus();
39 }
40
41 return self::$instance;
42 }
43
44 /**
45 * Check whether we need to use the Extendify/EP template.
46 */
47 public function __construct()
48 {
49 // Maybe show the styles on the frontend.
50 add_action('wp_head', function () {
51 if ($this->useDeprecatedTemplate()) {
52 $this->showStylesheet();
53 }
54 });
55
56 // Maybe show the styles in admin.
57 add_action('admin_head', function () {
58 if ($this->useDeprecatedTemplate()) {
59 $this->showStylesheet();
60 }
61 });
62
63 // Maybe load the JS to inject the admin styles.
64 add_action(
65 'admin_enqueue_scripts',
66 function () {
67 wp_enqueue_script(
68 'extendify-editorplus-scripts',
69 EXTENDIFY_BASE_URL . 'public/editorplus/editorplus.min.js',
70 [],
71 '1.0',
72 true
73 );
74 }
75 );
76
77 // Maybe add the body class name to the front end.
78 add_filter(
79 'body_class',
80 function ($classes) {
81 if ($this->useDeprecatedTemplate()) {
82 $classes[] = 'eplus_styles';
83 }
84
85 return $classes;
86 }
87 );
88
89 // Maybe add the body class name to the admin.
90 add_filter(
91 'admin_body_class',
92 function ($classes) {
93 if ($this->useDeprecatedTemplate()) {
94 $classes .= ' eplus_styles';
95 }
96
97 return $classes;
98 }
99 );
100
101 // Maybe register the template into WP.
102 add_filter('theme_page_templates', function ($templates) {
103 if (!$this->useDeprecatedTemplate()) {
104 return $templates;
105 }
106
107 return array_merge($templates, $this->templates);
108 });
109
110 // Maybe add template to the dropdown list.
111 add_filter('wp_insert_post_data', [$this, 'registerProjectTemplates']);
112
113 // Maybe add template file path.
114 add_filter('template_include', [$this, 'viewProjectTemplate']);
115 }
116
117 /**
118 * Checks whether the page needs the EP template
119 *
120 * @return boolean
121 */
122 public function useDeprecatedTemplate()
123 {
124 $post = get_post();
125 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
126 if (is_admin() && isset($_GET['post'])) {
127 // This will populate on the admin.
128 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
129 $post = get_post(sanitize_text_field(wp_unslash($_GET['post'])));
130 }
131
132 return isset($post->ID) && get_post_meta($post->ID, '_wp_page_template', true) === 'editorplus-template.php';
133 }
134
135 /**
136 * Used to echo out page template stylesheet if the page template is not active.
137 *
138 * @return void
139 */
140 public function showStylesheet()
141 {
142 $post = get_post();
143 $cssContent = apply_filters(
144 // For historical reasons do not change this key.
145 'extendifysdk_template_css',
146 get_post_meta($post->ID, 'extendify_custom_stylesheet', true),
147 $post
148 );
149
150 // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
151 // See: https://github.com/WordPress/WordPress/blob/ccdb1766aead26d4cef79badb015bb2727fefd59/wp-includes/theme.php#L1824-L1833 for reference.
152 if ($cssContent) {
153 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
154 echo "<style id='extendify-custom-stylesheet' type='text/css'>" . wp_strip_all_tags($cssContent) . '</style>';
155 }
156 }
157
158 /**
159 * Adds our template to the pages cache in order to trick WordPress,
160 * into thinking the template file exists where it doens't really exist.
161 *
162 * @param array $attributes - The attributes.
163 * @return array
164 */
165 public function registerProjectTemplates($attributes)
166 {
167 if (!$this->useDeprecatedTemplate()) {
168 return $attributes;
169 }
170
171 // Create the key used for the themes cache.
172 $cacheKey = 'page_templates-' . wp_hash(get_theme_root() . '/' . get_stylesheet());
173 // Retrieve the cache list.
174 // If it doesn't exist, or it's empty prepare an array.
175 $templates = wp_get_theme()->get_page_templates();
176 if (empty($templates)) {
177 $templates = [];
178 }
179
180 // New cache, therefore remove the old one.
181 wp_cache_delete($cacheKey, 'themes');
182 // Now add our template to the list of templates by merging our templates.
183 // with the existing templates array from the cache.
184 $templates = array_merge($templates, $this->templates);
185 // Add the modified cache to allow WordPress to pick it up for listing available templates.
186 wp_cache_add($cacheKey, $templates, 'themes', 1800);
187 return $attributes;
188 }
189
190 /**
191 * Checks if the template is assigned to the page.
192 *
193 * @param string $template - The template.
194 * @return string
195 */
196 public function viewProjectTemplate($template)
197 {
198 $post = get_post();
199 if (!$post || !$this->useDeprecatedTemplate()) {
200 return $template;
201 }
202
203 $currentTemplate = get_post_meta($post->ID, '_wp_page_template', true);
204
205 // Check that the set template is one we have defined.
206 if (!is_string($currentTemplate) || !array_key_exists($currentTemplate, $this->templates)) {
207 return $template;
208 }
209
210 $file = plugin_dir_path(__FILE__) . $currentTemplate;
211 if (!file_exists($file)) {
212 return $template;
213 }
214
215 return $file;
216 }
217 // phpcs:ignore Squiz.Classes.ClassDeclaration.SpaceBeforeCloseBrace
218 }
219
220 add_action('after_setup_theme', ['ExtendifyEditorPlus', 'getInstance']);
221 }//end if
222