PluginProbe
Extendify / 1.2.2
Extendify v1.2.2
3.2.0 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 All 126 releases
extendify / editorplus / EditorPlus.php

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

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