PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
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 5.2.1 All 155 releases
← All changes | class/class-mainwp-notification-template.php +362 -348 5.0 → 6.2.2 View file →
@@ -6,409 +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 - $template = $filter_template;
154 - }
157 + if ( $filter_template !== $template ) {
158 + $template = $filter_template;
159 + }
155 160
156 - $located = $template;
161 + $located = $template;
157 162
158 - if ( ! file_exists( $located ) ) {
159 - return;
160 - }
163 + if ( ! file_exists( $located ) ) {
164 + return '';
165 + }
161 166
162 - extract( $args ); // @codingStandardsIgnoreLine
167 + extract( $args ); // @codingStandardsIgnoreLine
163 168
164 - ob_start();
169 + ob_start();
165 170
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 );
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 );
178 183
179 - include $located;
184 + include $located; // NOSONAR - WP compatible. // `include` to fix loop load the template content.
180 185
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 );
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 );
193 198
194 - $content = ob_get_clean();
199 + $content = ob_get_clean();
195 200
196 - if ( isset( $current_email_site ) && is_object( $current_email_site ) ) {
201 + if ( isset( $current_email_site ) && is_object( $current_email_site ) ) {
197 202
198 - $content = MainWP_Notification_Settings::replace_tokens_for_content( $content, $current_email_site );
203 + $content = MainWP_Notification_Settings::replace_tokens_for_content( $content, $current_email_site );
199 204
200 - if ( isset( $child_site_tokens ) && ! empty( $child_site_tokens ) ) {
205 + if ( isset( $child_site_tokens ) && ! empty( $child_site_tokens ) ) {
201 206
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 + 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 + }
207 212
208 - if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
213 + if ( preg_match( '/\[[^\]]+\]/is', $content, $matches ) ) {
209 214
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 );
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 );
218 223
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 - }
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 + }
230 235
231 - return $content;
232 - }
236 + return $content;
237 + }
233 238
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 ) {
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 ) {
249 254
250 - $template = '';
255 + $template = '';
251 256
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 + // 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 + }
257 262
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 - }
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 + }
264 269
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 - }
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 + }
277 282
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 = self::get_template_name_by_notification_type( $type );
287 - if ( file_exists( $this->template_custom_path . $templ ) ) {
288 - return true;
289 - }
290 - return false;
291 - }
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 + }
292 297
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 - );
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 + );
308 314
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 - }
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 + }
313 319
314 - return isset( $types[ $type ] ) ? $types[ $type ] : null;
315 - }
320 + return isset( $types[ $type ] ) ? $types[ $type ] : null;
321 + }
316 322
317 323
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() {
328 - $updated_templ = false;
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 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
336 + $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
331 337
332 - /**
333 - * WordPress files system object.
334 - *
335 - * @global object
336 - */
337 - global $wp_filesystem;
338 + /**
339 + * WordPress files system object.
340 + *
341 + * @global object
342 + */
343 + global $wp_filesystem;
338 344
339 - $type = isset( $_GET['edit-email'] ) ? sanitize_text_field( wp_unslash( $_GET['edit-email'] ) ) : '';
340 - $templ_base_name = ! empty( $type ) ? self::get_template_name_by_notification_type( $type ) : '';
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 ) : '';
341 347
342 - if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'delete-email-template' ) ) {
343 - if ( $hasWPFileSystem ) {
344 - $dir = $this->template_custom_path;
345 - $deleted = $wp_filesystem->delete( $dir . $templ_base_name );
346 - if ( $deleted ) {
347 - $updated_templ = 1;
348 - }
349 - }
350 - }
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 + }
351 355
352 - if ( ! empty( $templ_base_name ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'copy-email-template' ) ) {
353 - if ( $hasWPFileSystem ) {
354 - $template_path = $this->template_path;
355 - $source_dir = apply_filters( 'mainwp_default_template_source_dir', $template_path, $templ_base_name );
356 - $dest_dir = $this->template_custom_path;
357 - $copied = $wp_filesystem->copy( $source_dir . $templ_base_name, $dest_dir . $templ_base_name );
358 - if ( $copied ) {
359 - $updated_templ = 2;
360 - }
361 - }
362 - }
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 + }
363 365
364 - if ( ! empty( $templ_base_name ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'save-email-template' ) ) {
365 - $template_code = isset( $_POST[ 'edit_' . $type . '_code' ] ) ? wp_unslash( $_POST[ 'edit_' . $type . '_code' ] ) : ''; //phpcs:ignore -- saving template content.
366 - $updated = $this->save_template( $template_code, $templ_base_name );
367 - if ( $updated ) {
368 - $updated_templ = 3;
369 - }
370 - }
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 + }
371 375
372 - return $updated_templ;
373 - }
376 + return $updated_templ;
377 + }
374 378
375 - /**
376 - * Save the email templates.
377 - *
378 - * Credits.
379 - *
380 - * Plugin-Name: WooCommerce.
381 - * Plugin URI: https://woocommerce.com/.
382 - * Author: Automattic.
383 - * Author URI: https://woocommerce.com.
384 - * License: GPLv3 or later.
385 - *
386 - * @since 4.1
387 - * @param string $template_code Template code.
388 - * @param string $template Template.
389 - */
390 - public function save_template( $template_code, $template ) {
391 - if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template ) ) {
392 - $saved = false;
393 - $file = $this->template_custom_path . $template;
394 - $code = str_replace( PHP_EOL, '', $template_code ); // to fix issue create extra line breaks in custom template file.
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.
395 404
396 - $is_writable = MainWP_System_Utility::is_writable( $file );
397 - // phpcs:disable WordPress.WP.AlternativeFunctions
398 - if ( $is_writable ) {
399 - $f = fopen( $file, 'w+' );
400 - if ( false !== $f ) {
401 - fwrite( $f, $code );
402 - fclose( $f );
403 - $saved = true;
404 - }
405 - }
406 - //phpcs:enable
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
407 420
408 - if ( $saved ) {
409 - return true;
410 - }
411 - }
412 - return false;
413 - }
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 + }
414 428 }