PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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 trunk, at class/class-mainwp-notification-template.php

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