PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-notification-template.php

class-mainwp-notification-template.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.1, at class/class-mainwp-notification-template.php

411 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP notification template
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Notification_Template
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_Notification_Template { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
16
17 /**
18 * Private static variable to hold the single instance of the class.
19 *
20 * @static
21 *
22 * @var mixed Default null.
23 */
24 private static $instance = null;
25
26
27 /**
28 * Template directory.
29 *
30 * @var string Default empty.
31 */
32 private $template_path = '';
33
34 /**
35 * Custom template directory.
36 *
37 * @var string Default empty.
38 */
39 private $template_custom_path = '';
40
41 /**
42 * Method get_class_name()
43 *
44 * Get Class Name.
45 *
46 * @return string Class name.
47 */
48 public static function get_class_name() {
49 return __CLASS__;
50 }
51
52 /**
53 * Create a new Self Instance.
54 *
55 * @return mixed static::$instance
56 */
57 public static function instance() {
58 if ( null === static::$instance ) {
59 static::$instance = new self();
60 }
61 return static::$instance;
62 }
63
64 /**
65 * MainWP_Notification_Template constructor.
66 *
67 * Run each time the class is called.
68 *
69 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_sub_dir()
70 */
71 public function __construct() {
72 $this->template_path = MAINWP_PLUGIN_DIR . 'templates/';
73 $this->template_custom_path = MainWP_System_Utility::get_mainwp_sub_dir( 'templates' );
74 // create custom template folders if does not existed.
75 MainWP_System_Utility::get_mainwp_sub_dir( 'templates/emails' );
76 }
77
78 /**
79 * Get default templates folder.
80 *
81 * @return string folder.
82 */
83 public function get_default_templates_dir() {
84 return $this->template_path;
85 }
86
87 /**
88 * Get custom templates folder.
89 *
90 * @return string folder.
91 */
92 public function get_custom_templates_dir() {
93 return $this->template_custom_path;
94 }
95
96 /**
97 * Get template HTML.
98 *
99 * Credits.
100 *
101 * Plugin-Name: WooCommerce.
102 * Plugin URI: https://woocommerce.com/.
103 * Author: Automattic.
104 * Author URI: https://woocommerce.com.
105 * License: GPLv3 or later.
106 *
107 * @param string $template_name Template name.
108 * @param array $args Arguments. (default: array).
109 *
110 * @return string
111 */
112 public function get_template_html( $template_name, $args = array() ) {
113 return $this->get_template( $template_name, $args );
114 }
115
116 /**
117 * Get templates.
118 *
119 * Credits.
120 *
121 * Plugin-Name: WooCommerce.
122 * Plugin URI: https://woocommerce.com/.
123 * Author: Automattic.
124 * Author URI: https://woocommerce.com.
125 * License: GPLv3 or later.
126 *
127 * @param string $template_name Template name.
128 * @param array $args Arguments. (default: array).
129 *
130 * @return false|string|void
131 *
132 * @uses \MainWP\Dashboard\MainWP_Notification_Settings::replace_tokens_for_content()
133 * @uses \MainWP\Dashboard\MainWP_Utility::get_timestamp()
134 */
135 public function get_template( $template_name, $args = array() ) {
136
137 // template full path.
138 $template = $this->locate_template( $template_name );
139
140 /**
141 * Filter: mainwp_get_template
142 *
143 * Filters available templates and adds support for 3rd party templates.
144 *
145 * @param string $template_name Template name.
146 * @param array $args Args.
147 *
148 * @since 4.1
149 */
150 $filter_template = apply_filters( 'mainwp_get_template', $template, $template_name, $args );
151
152 if ( $filter_template !== $template ) {
153 $template = $filter_template;
154 }
155
156 $located = $template;
157
158 if ( ! file_exists( $located ) ) {
159 return '';
160 }
161
162 extract( $args ); // @codingStandardsIgnoreLine
163
164 ob_start();
165
166 /**
167 * Action: mainwp_before_template_part
168 *
169 * Fires before the email template is loaded.
170 *
171 * @param string $template_name Template name.
172 * @param resource $located Template file.
173 * @param array $args Args.
174 *
175 * @since 4.1
176 */
177 do_action( 'mainwp_before_template_part', $template_name, $located, $args );
178
179 include_once $located; // NOSONAR - WP compatible.
180
181 /**
182 * Action: mainwp_after_template_part
183 *
184 * Fires after the email template is loaded.
185 *
186 * @param string $template_name Template name.
187 * @param resource $located Template file.
188 * @param array $args Args.
189 *
190 * @since 4.1
191 */
192 do_action( 'mainwp_after_template_part', $template_name, $located, $args );
193
194 $content = ob_get_clean();
195
196 if ( isset( $current_email_site ) && is_object( $current_email_site ) ) {
197
198 $content = MainWP_Notification_Settings::replace_tokens_for_content( $content, $current_email_site );
199
200 if ( isset( $child_site_tokens ) && ! empty( $child_site_tokens ) ) {
201
202 if ( ! isset( $timestamp_from_date ) || empty( $timestamp_from_date ) || ! isset( $timestamp_to_date ) || empty( $timestamp_to_date ) ) {
203 $now_timestamp = MainWP_Utility::get_timestamp();
204 $timestamp_from_date = $now_timestamp - DAY_IN_SECONDS;
205 $timestamp_to_date = $now_timestamp;
206 }
207
208 if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
209
210 /**
211 * Filter: mainwp_pro_reports_generate_content
212 *
213 * Filters the Pro Reports available content.
214 *
215 * @since 4.1
216 */
217 $content = apply_filters( 'mainwp_pro_reports_generate_content', $content, $current_email_site->id, $timestamp_from_date, $timestamp_to_date );
218
219 /**
220 * Filter: mainwp_client_report_generate_content
221 *
222 * Filters the Client Reports available content.
223 *
224 * @since 4.1
225 */
226 $content = apply_filters( 'mainwp_client_report_generate_content', $content, $current_email_site->id, $timestamp_from_date, $timestamp_to_date );
227 }
228 }
229 }
230
231 return $content;
232 }
233
234 /**
235 * Locate a template and return the path for inclusion.
236 *
237 * Credits.
238 *
239 * Plugin-Name: WooCommerce.
240 * Plugin URI: https://woocommerce.com/.
241 * Author: Automattic.
242 * Author URI: https://woocommerce.com.
243 * License: GPLv3 or later.
244 *
245 * @param string $template_name Template name.
246 * @return string
247 */
248 private function locate_template( $template_name ) {
249
250 $template = '';
251
252 // Look within custom path - this is priority.
253 $template_path = $this->template_custom_path;
254 if ( file_exists( $template_path . $template_name ) ) {
255 $template = $template_path . $template_name;
256 }
257
258 // Get default template.
259 if ( ! $template ) {
260 $template_path = $this->template_path;
261 $template_path = apply_filters( 'mainwp_default_template_source_dir', $template_path, $template_name );
262 $template = $template_path . $template_name;
263 }
264
265 /**
266 * Filer: mainwp_locate_template
267 *
268 * Filters the template location.
269 *
270 * @param string $template_name Template name.
271 * @param string $template_path Template path.
272 *
273 * @since 4.1
274 */
275 return apply_filters( 'mainwp_locate_template', $template, $template_name, $template_path );
276 }
277
278 /**
279 * Check if it is overrided template.
280 *
281 * @param string $type Email type.
282 *
283 * @return bool True|False
284 */
285 public function is_overrided_template( $type ) {
286 $templ = static::get_template_name_by_notification_type( $type );
287 if ( file_exists( $this->template_custom_path . $templ ) ) {
288 return true;
289 }
290 return false;
291 }
292
293 /**
294 * Get default template name by email/notification type.
295 *
296 * @param string $type email/notification type.
297 *
298 * @return string|null Template name.
299 */
300 public static function get_template_name_by_notification_type( $type = '' ) {
301 $types = array(
302 'daily_digest' => 'emails/mainwp-daily-digest-email.php',
303 'uptime' => 'emails/mainwp-uptime-monitoring-email.php',
304 'site_health' => 'emails/mainwp-site-health-monitoring-email.php',
305 'http_check' => 'emails/mainwp-after-update-http-check-email.php',
306 'deactivated_license_alert' => 'emails/mainwp-licenses-deactivated-alert-email.php',
307 );
308
309 $addition_template_name = apply_filters( 'mainwp_get_notification_template_name_by_type', '', $type );
310 if ( ! empty( $addition_template_name ) ) {
311 return $addition_template_name;
312 }
313
314 return isset( $types[ $type ] ) ? $types[ $type ] : null;
315 }
316
317
318 /**
319 * Method handle_template_file_action()
320 *
321 * Handle template file action.
322 *
323 * @return bool $done handle result.
324 *
325 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
326 */
327 public function handle_template_file_action() { // phpcs:ignore -- NOSONAR - complex.
328 $updated_templ = false;
329
330 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
331
332 /**
333 * WordPress files system object.
334 *
335 * @global object
336 */
337 global $wp_filesystem;
338
339 $type = isset( $_GET['edit-email'] ) ? sanitize_text_field( wp_unslash( $_GET['edit-email'] ) ) : '';
340 $templ_base_name = ! empty( $type ) ? static::get_template_name_by_notification_type( $type ) : '';
341
342 if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'delete-email-template' ) && $hasWPFileSystem ) {
343 $dir = $this->template_custom_path;
344 $deleted = $wp_filesystem->delete( $dir . $templ_base_name );
345 if ( $deleted ) {
346 $updated_templ = 1;
347 }
348 }
349
350 if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'copy-email-template' ) && $hasWPFileSystem ) {
351 $template_path = $this->template_path;
352 $source_dir = apply_filters( 'mainwp_default_template_source_dir', $template_path, $templ_base_name );
353 $dest_dir = $this->template_custom_path;
354 $copied = $wp_filesystem->copy( $source_dir . $templ_base_name, $dest_dir . $templ_base_name );
355 if ( $copied ) {
356 $updated_templ = 2;
357 }
358 }
359
360 if ( ! empty( $templ_base_name ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'save-email-template' ) ) {
361 $template_code = isset( $_POST[ 'edit_' . $type . '_code' ] ) ? wp_unslash( $_POST[ 'edit_' . $type . '_code' ] ) : ''; //phpcs:ignore -- saving template content.
362 $updated = $this->save_template( $template_code, $templ_base_name );
363 if ( $updated ) {
364 $updated_templ = 3;
365 }
366 }
367
368 return $updated_templ;
369 }
370
371 /**
372 * Save the email templates.
373 *
374 * Credits.
375 *
376 * Plugin-Name: WooCommerce.
377 * Plugin URI: https://woocommerce.com/.
378 * Author: Automattic.
379 * Author URI: https://woocommerce.com.
380 * License: GPLv3 or later.
381 *
382 * @since 4.1
383 * @param string $template_code Template code.
384 * @param string $template Template.
385 */
386 public function save_template( $template_code, $template ) {
387 if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template ) ) {
388 $saved = false;
389 $file = $this->template_custom_path . $template;
390 $code = str_replace( PHP_EOL, '', $template_code ); // to fix issue create extra line breaks in custom template file.
391
392 $is_writable = MainWP_System_Utility::is_writable( $file );
393 // phpcs:disable WordPress.WP.AlternativeFunctions
394 if ( $is_writable ) {
395 $f = fopen( $file, 'w+' );
396 if ( false !== $f ) {
397 fwrite( $f, $code );
398 fclose( $f );
399 $saved = true;
400 }
401 }
402 //phpcs:enable
403
404 if ( $saved ) {
405 return true;
406 }
407 }
408 return false;
409 }
410 }
411