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

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