PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.3 6.2.2 6.2.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 All 156 releases
← All changes | class/class-mainwp-notification-template.php +363 -347 4.6.0.1 → 6.2.1 View file →
@@ -6,407 +6,423 @@
6 6 */
7 7
8 8 namespace MainWP\Dashboard;
9 9
10 +// Exit if accessed directly.
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
14 +
10 15 /**
11 16 * Class MainWP_Notification_Template
12 17 *
13 18 * @package MainWP\Dashboard
14 19 */
15 -class MainWP_Notification_Template {
20 +class MainWP_Notification_Template { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
16 21
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;
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;
25 30
26 31
27 - /**
28 - * Template directory.
29 - *
30 - * @var string Default empty.
31 - */
32 - private $template_path = '';
32 + /**
33 + * Template directory.
34 + *
35 + * @var string Default empty.
36 + */
37 + private $template_path = '';
33 38
34 - /**
35 - * Custom template directory.
36 - *
37 - * @var string Default empty.
38 - */
39 - private $template_custom_path = '';
39 + /**
40 + * Custom template directory.
41 + *
42 + * @var string Default empty.
43 + */
44 + private $template_custom_path = '';
40 45
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 - }
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 + }
51 56
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 - }
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 + }
63 68
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 - }
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 + }
77 82
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 - }
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 + }
86 91
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 - }
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 + }
95 100
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 - }
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 + }
115 120
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() ) {
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() ) {
136 141
137 - // template full path.
138 - $template = $this->locate_template( $template_name );
142 + // template full path.
143 + $template = $this->locate_template( $template_name );
139 144
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 );
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 );
151 156
152 - if ( $filter_template !== $template ) {
153 - if ( ! file_exists( $filter_template ) ) {
154 - return;
155 - }
156 - $template = $filter_template;
157 - }
157 + if ( $filter_template !== $template ) {
158 + $template = $filter_template;
159 + }
158 160
159 - $located = $template;
161 + $located = $template;
160 162
161 - extract( $args ); // @codingStandardsIgnoreLine
163 + if ( ! file_exists( $located ) ) {
164 + return '';
165 + }
162 166
163 - ob_start();
167 + extract( $args ); // @codingStandardsIgnoreLine
164 168
165 - /**
166 - * Action: mainwp_before_template_part
167 - *
168 - * Fires before the email template is loaded.
169 - *
170 - * @param string $template_name Template name.
171 - * @param resource $located Template file.
172 - * @param array $args Args.
173 - *
174 - * @since 4.1
175 - */
176 - do_action( 'mainwp_before_template_part', $template_name, $located, $args );
169 + ob_start();
177 170
178 - include $located;
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 );
179 183
180 - /**
181 - * Action: mainwp_after_template_part
182 - *
183 - * Fires after the email template is loaded.
184 - *
185 - * @param string $template_name Template name.
186 - * @param resource $located Template file.
187 - * @param array $args Args.
188 - *
189 - * @since 4.1
190 - */
191 - do_action( 'mainwp_after_template_part', $template_name, $located, $args );
184 + include $located; // NOSONAR - WP compatible. // `include` to fix loop load the template content.
192 185
193 - $content = ob_get_clean();
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 );
194 198
195 - if ( isset( $current_email_site ) && is_object( $current_email_site ) ) {
199 + $content = ob_get_clean();
196 200
197 - $content = MainWP_Notification_Settings::replace_tokens_for_content( $content, $current_email_site );
201 + if ( isset( $current_email_site ) && is_object( $current_email_site ) ) {
198 202
199 - if ( isset( $child_site_tokens ) && ! empty( $child_site_tokens ) ) {
203 + $content = MainWP_Notification_Settings::replace_tokens_for_content( $content, $current_email_site );
200 204
201 - if ( ! isset( $timestamp_from_date ) || empty( $timestamp_from_date ) || ! isset( $timestamp_to_date ) || empty( $timestamp_to_date ) ) {
202 - $now_timestamp = MainWP_Utility::get_timestamp();
203 - $timestamp_from_date = $now_timestamp - DAY_IN_SECONDS;
204 - $timestamp_to_date = $now_timestamp;
205 - }
205 + if ( isset( $child_site_tokens ) && ! empty( $child_site_tokens ) ) {
206 206
207 - if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
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 + }
208 212
209 - /**
210 - * Filter: mainwp_pro_reports_generate_content
211 - *
212 - * Filters the Pro Reports available content.
213 - *
214 - * @since 4.1
215 - */
216 - $content = apply_filters( 'mainwp_pro_reports_generate_content', $content, $current_email_site->id, $timestamp_from_date, $timestamp_to_date );
213 + if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
217 214
218 - /**
219 - * Filter: mainwp_client_report_generate_content
220 - *
221 - * Filters the Client Reports available content.
222 - *
223 - * @since 4.1
224 - */
225 - $content = apply_filters( 'mainwp_client_report_generate_content', $content, $current_email_site->id, $timestamp_from_date, $timestamp_to_date );
226 - }
227 - }
228 - }
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 );
229 223
230 - return $content;
231 - }
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 + }
232 235
233 - /**
234 - * Locate a template and return the path for inclusion.
235 - *
236 - * Credits.
237 - *
238 - * Plugin-Name: WooCommerce.
239 - * Plugin URI: https://woocommerce.com/.
240 - * Author: Automattic.
241 - * Author URI: https://woocommerce.com.
242 - * License: GPLv3 or later.
243 - *
244 - * @param string $template_name Template name.
245 - * @return string
246 - */
247 - private function locate_template( $template_name ) {
236 + return $content;
237 + }
248 238
249 - $template = '';
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 ) {
250 254
251 - // Look within custom path - this is priority.
252 - $template_path = $this->template_custom_path;
253 - if ( file_exists( $template_path . $template_name ) ) {
254 - $template = $template_path . $template_name;
255 - }
255 + $template = '';
256 256
257 - // Get default template.
258 - if ( ! $template ) {
259 - $template_path = $this->template_path;
260 - $template_path = apply_filters( 'mainwp_default_template_source_dir', $template_path, $template_name );
261 - $template = $template_path . $template_name;
262 - }
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 + }
263 262
264 - /**
265 - * Filer: mainwp_locate_template
266 - *
267 - * Filters the template location.
268 - *
269 - * @param string $template_name Template name.
270 - * @param string $template_path Template path.
271 - *
272 - * @since 4.1
273 - */
274 - return apply_filters( 'mainwp_locate_template', $template, $template_name, $template_path );
275 - }
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 + }
276 269
277 - /**
278 - * Check if it is overrided template.
279 - *
280 - * @param string $type Email type.
281 - *
282 - * @return bool True|False
283 - */
284 - public function is_overrided_template( $type ) {
285 - $templ = self::get_template_name_by_notification_type( $type );
286 - if ( file_exists( $this->template_custom_path . $templ ) ) {
287 - return true;
288 - }
289 - return false;
290 - }
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 + }
291 282
292 - /**
293 - * Get default template name by email/notification type.
294 - *
295 - * @param string $type email/notification type.
296 - *
297 - * @return string|null Template name.
298 - */
299 - public static function get_template_name_by_notification_type( $type = '' ) {
300 - $types = array(
301 - 'daily_digest' => 'emails/mainwp-daily-digest-email.php',
302 - 'uptime' => 'emails/mainwp-uptime-monitoring-email.php',
303 - 'site_health' => 'emails/mainwp-site-health-monitoring-email.php',
304 - 'http_check' => 'emails/mainwp-after-update-http-check-email.php',
305 - );
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 + }
306 297
307 - $addition_template_name = apply_filters( 'mainwp_get_notification_template_name_by_type', '', $type );
308 - if ( ! empty( $addition_template_name ) ) {
309 - return $addition_template_name;
310 - }
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 + );
311 314
312 - return isset( $types[ $type ] ) ? $types[ $type ] : null;
313 - }
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 + }
314 319
320 + return isset( $types[ $type ] ) ? $types[ $type ] : null;
321 + }
315 322
316 - /**
317 - * Method handle_template_file_action()
318 - *
319 - * Handle template file action.
320 - *
321 - * @return bool $done handle result.
322 - *
323 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
324 - */
325 - public function handle_template_file_action() {
326 - $updated_templ = false;
327 323
328 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
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;
329 335
330 - /**
331 - * WordPress files system object.
332 - *
333 - * @global object
334 - */
335 - global $wp_filesystem;
336 + $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
336 337
337 - $type = isset( $_GET['edit-email'] ) ? sanitize_text_field( wp_unslash( $_GET['edit-email'] ) ) : '';
338 - $templ_base_name = ! empty( $type ) ? self::get_template_name_by_notification_type( $type ) : '';
338 + /**
339 + * WordPress files system object.
340 + *
341 + * @global object
342 + */
343 + global $wp_filesystem;
339 344
340 - if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'delete-email-template' ) ) {
341 - if ( $hasWPFileSystem ) {
342 - $dir = $this->template_custom_path;
343 - $deleted = $wp_filesystem->delete( $dir . $templ_base_name );
344 - if ( $deleted ) {
345 - $updated_templ = 1;
346 - }
347 - }
348 - }
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 ) : '';
349 347
350 - if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'copy-email-template' ) ) {
351 - if ( $hasWPFileSystem ) {
352 - $template_path = $this->template_path;
353 - $source_dir = apply_filters( 'mainwp_default_template_source_dir', $template_path, $templ_base_name );
354 - $dest_dir = $this->template_custom_path;
355 - $copied = $wp_filesystem->copy( $source_dir . $templ_base_name, $dest_dir . $templ_base_name );
356 - if ( $copied ) {
357 - $updated_templ = 2;
358 - }
359 - }
360 - }
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 + }
361 355
362 - if ( ! empty( $templ_base_name ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'save-email-template' ) ) {
363 - $template_code = isset( $_POST[ 'edit_' . $type . '_code' ] ) ? wp_unslash( $_POST[ 'edit_' . $type . '_code' ] ) : ''; //phpcs:ignore -- saving template content.
364 - $updated = $this->save_template( $template_code, $templ_base_name );
365 - if ( $updated ) {
366 - $updated_templ = 3;
367 - }
368 - }
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 + }
369 365
370 - return $updated_templ;
371 - }
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 + }
372 375
373 - /**
374 - * Save the email templates.
375 - *
376 - * Credits.
377 - *
378 - * Plugin-Name: WooCommerce.
379 - * Plugin URI: https://woocommerce.com/.
380 - * Author: Automattic.
381 - * Author URI: https://woocommerce.com.
382 - * License: GPLv3 or later.
383 - *
384 - * @since 4.1
385 - * @param string $template_code Template code.
386 - * @param string $template Template.
387 - */
388 - public function save_template( $template_code, $template ) {
389 - if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template ) ) {
390 - $saved = false;
391 - $file = $this->template_custom_path . $template;
392 - $code = str_replace( PHP_EOL, '', $template_code ); // to fix issue create extra line breaks in custom template file.
376 + return $updated_templ;
377 + }
393 378
394 - $is_writable = MainWP_System_Utility::is_writable( $file );
395 - // phpcs:disable WordPress.WP.AlternativeFunctions
396 - if ( $is_writable ) {
397 - $f = fopen( $file, 'w+' );
398 - if ( false !== $f ) {
399 - fwrite( $f, $code );
400 - fclose( $f );
401 - $saved = true;
402 - }
403 - }
404 - //phpcs:enable
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.
405 404
406 - if ( $saved ) {
407 - return true;
408 - }
409 - }
410 - return false;
411 - }
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 + }
412 428 }