@@ -1001,8 +1001,8 @@ |
||
1001 | 1001 | aui_flip_color_scheme_on_scroll(); |
1002 | 1002 | |
1003 | 1003 | <?php |
1004 | - // FSE tweaks. |
|
1005 | - if(!empty($_REQUEST['postType'])){ ?> |
|
1004 | + // FSE tweaks. |
|
1005 | + if(!empty($_REQUEST['postType'])){ ?> |
|
1006 | 1006 | function aui_fse_set_data_scroll() { |
1007 | 1007 | console.log('init scroll'); |
1008 | 1008 | let Iframe = document.getElementsByClassName("edit-site-visual-editor__editor-canvas"); |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | function aui_init_flatpickr(){ |
253 | 253 | if ( typeof jQuery.fn.flatpickr === "function" && !$aui_doing_init_flatpickr) { |
254 | 254 | $aui_doing_init_flatpickr = true; |
255 | - <?php if ( ! empty( $flatpickr_locale ) ) { ?>try{flatpickr.localize(<?php echo $flatpickr_locale; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>);}catch(err){console.log(err.message);}<?php } ?> |
|
255 | + <?php if (!empty($flatpickr_locale)) { ?>try{flatpickr.localize(<?php echo $flatpickr_locale; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>);}catch(err){console.log(err.message);}<?php } ?> |
|
256 | 256 | jQuery('input[data-aui-init="flatpickr"]:not(.flatpickr-input)').flatpickr(); |
257 | 257 | } |
258 | 258 | $aui_doing_init_flatpickr = false; |
@@ -1013,7 +1013,7 @@ discard block |
||
1013 | 1013 | |
1014 | 1014 | <?php |
1015 | 1015 | // FSE tweaks. |
1016 | - if(!empty($_REQUEST['postType'])){ ?> |
|
1016 | + if (!empty($_REQUEST['postType'])) { ?> |
|
1017 | 1017 | function aui_fse_set_data_scroll() { |
1018 | 1018 | console.log('init scroll'); |
1019 | 1019 | let Iframe = document.getElementsByClassName("edit-site-visual-editor__editor-canvas"); |
@@ -1,264 +1,264 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | if ( ! class_exists( "AyeCode_Connect_Helper" ) ) { |
7 | - /** |
|
8 | - * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
9 | - * |
|
10 | - * Class AyeCode_Connect_Helper |
|
11 | - */ |
|
12 | - class AyeCode_Connect_Helper { |
|
13 | - |
|
14 | - // Hold the version number |
|
15 | - var $version = "1.0.4"; |
|
16 | - |
|
17 | - // Hold the default strings. |
|
18 | - var $strings = array(); |
|
19 | - |
|
20 | - // Hold the default pages. |
|
21 | - var $pages = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * The constructor. |
|
25 | - * |
|
26 | - * AyeCode_Connect_Helper constructor. |
|
27 | - * |
|
28 | - * @param array $strings |
|
29 | - * @param array $pages |
|
30 | - */ |
|
31 | - public function __construct( $strings = array(), $pages = array() ) { |
|
32 | - // Only fire if not localhost and the current user has the right permissions. |
|
33 | - if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
34 | - // set default strings |
|
35 | - $default_strings = array( |
|
36 | - 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | - 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | - 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | - 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | - 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | - 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
43 | - ); |
|
44 | - $this->strings = array_merge( $default_strings, $strings ); |
|
45 | - |
|
46 | - // set default pages |
|
47 | - $default_pages = array(); |
|
48 | - $this->pages = array_merge( $default_pages, $pages ); |
|
49 | - |
|
50 | - // maybe show connect site notice |
|
51 | - add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
52 | - |
|
53 | - // add ajax action if not already added |
|
54 | - if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | - add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - // add ajax action if not already added |
|
60 | - if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | - add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Give a way to check we can connect via a external redirect. |
|
67 | - */ |
|
68 | - public function ayecode_connect_helper_installed(){ |
|
69 | - $active = array( |
|
70 | - 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | - 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | - 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
73 | - ); |
|
74 | - wp_send_json_success( $active ); |
|
75 | - wp_die(); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Get slug from path |
|
80 | - * |
|
81 | - * @param string $key |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - private function format_plugin_slug( $key ) { |
|
86 | - $slug = explode( '/', $key ); |
|
87 | - $slug = explode( '.', end( $slug ) ); |
|
88 | - |
|
89 | - return $slug[0]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Install and activate the AyeCode Connect Plugin |
|
94 | - */ |
|
95 | - public function ayecode_connect_install() { |
|
96 | - // bail if localhost |
|
97 | - if ( $this->is_localhost() ) { |
|
98 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
99 | - } |
|
100 | - |
|
101 | - // Explicitly clear the event. |
|
102 | - wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
103 | - |
|
104 | - $success = true; |
|
105 | - $plugin_slug = "ayecode-connect"; |
|
106 | - if ( ! empty( $plugin_slug ) ) { |
|
107 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | - require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
111 | - |
|
112 | - WP_Filesystem(); |
|
113 | - |
|
114 | - $skin = new Automatic_Upgrader_Skin; |
|
115 | - $upgrader = new WP_Upgrader( $skin ); |
|
116 | - $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
117 | - $plugin_slug = $plugin_slug; |
|
118 | - $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
119 | - $installed = false; |
|
120 | - $activate = false; |
|
121 | - |
|
122 | - // See if the plugin is installed already |
|
123 | - if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
124 | - $installed = true; |
|
125 | - $activate = ! is_plugin_active( $plugin ); |
|
126 | - } |
|
127 | - |
|
128 | - // Install this thing! |
|
129 | - if ( ! $installed ) { |
|
130 | - |
|
131 | - // Suppress feedback |
|
132 | - ob_start(); |
|
133 | - |
|
134 | - try { |
|
135 | - $plugin_information = plugins_api( 'plugin_information', array( |
|
136 | - 'slug' => $plugin_slug, |
|
137 | - 'fields' => array( |
|
138 | - 'short_description' => false, |
|
139 | - 'sections' => false, |
|
140 | - 'requires' => false, |
|
141 | - 'rating' => false, |
|
142 | - 'ratings' => false, |
|
143 | - 'downloaded' => false, |
|
144 | - 'last_updated' => false, |
|
145 | - 'added' => false, |
|
146 | - 'tags' => false, |
|
147 | - 'homepage' => false, |
|
148 | - 'donate_link' => false, |
|
149 | - 'author_profile' => false, |
|
150 | - 'author' => false, |
|
151 | - ), |
|
152 | - ) ); |
|
153 | - |
|
154 | - if ( is_wp_error( $plugin_information ) ) { |
|
155 | - throw new Exception( $plugin_information->get_error_message() ); |
|
156 | - } |
|
157 | - |
|
158 | - $package = $plugin_information->download_link; |
|
159 | - $download = $upgrader->download_package( $package ); |
|
160 | - |
|
161 | - if ( is_wp_error( $download ) ) { |
|
162 | - throw new Exception( $download->get_error_message() ); |
|
163 | - } |
|
164 | - |
|
165 | - $working_dir = $upgrader->unpack_package( $download, true ); |
|
166 | - |
|
167 | - if ( is_wp_error( $working_dir ) ) { |
|
168 | - throw new Exception( $working_dir->get_error_message() ); |
|
169 | - } |
|
170 | - |
|
171 | - $result = $upgrader->install_package( array( |
|
172 | - 'source' => $working_dir, |
|
173 | - 'destination' => WP_PLUGIN_DIR, |
|
174 | - 'clear_destination' => false, |
|
175 | - 'abort_if_destination_exists' => false, |
|
176 | - 'clear_working' => true, |
|
177 | - 'hook_extra' => array( |
|
178 | - 'type' => 'plugin', |
|
179 | - 'action' => 'install', |
|
180 | - ), |
|
181 | - ) ); |
|
182 | - |
|
183 | - if ( is_wp_error( $result ) ) { |
|
184 | - throw new Exception( $result->get_error_message() ); |
|
185 | - } |
|
186 | - |
|
187 | - $activate = true; |
|
188 | - |
|
189 | - } catch ( Exception $e ) { |
|
190 | - $success = false; |
|
191 | - } |
|
192 | - |
|
193 | - // Discard feedback |
|
194 | - ob_end_clean(); |
|
195 | - } |
|
196 | - |
|
197 | - wp_clean_plugins_cache(); |
|
198 | - |
|
199 | - // Activate this thing |
|
200 | - if ( $activate ) { |
|
201 | - try { |
|
202 | - $result = activate_plugin( $plugin ); |
|
203 | - |
|
204 | - if ( is_wp_error( $result ) ) { |
|
205 | - $success = false; |
|
206 | - } else { |
|
207 | - $success = true; |
|
208 | - } |
|
209 | - } catch ( Exception $e ) { |
|
210 | - $success = false; |
|
211 | - } |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | - ayecode_connect();// init |
|
217 | - $args = ayecode_connect_args(); |
|
218 | - $client = new AyeCode_Connect( $args ); |
|
219 | - $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | - $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | - wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
222 | - } else { |
|
223 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
224 | - } |
|
225 | - wp_die(); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Check if maybe localhost. |
|
230 | - * |
|
231 | - * @return bool |
|
232 | - */ |
|
233 | - public function is_localhost() { |
|
234 | - $localhost = false; |
|
235 | - |
|
236 | - $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | - $localhost_domains = array( |
|
238 | - 'localhost', |
|
239 | - 'localhost.localdomain', |
|
240 | - '127.0.0.1', |
|
241 | - '::1' |
|
242 | - ); |
|
243 | - |
|
244 | - if ( in_array( $host, $localhost_domains ) ) { |
|
245 | - $localhost = true; |
|
246 | - } |
|
247 | - |
|
248 | - return $localhost; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Show notice to connect site. |
|
253 | - */ |
|
254 | - public function ayecode_connect_install_notice() { |
|
255 | - if ( $this->maybe_show() ) { |
|
256 | - $connect_title_string = $this->strings['connect_title']; |
|
257 | - $connect_external_string = $this->strings['connect_external']; |
|
258 | - $connect_string = $this->strings['connect']; |
|
259 | - $connect_button_string = $this->strings['connect_button']; |
|
260 | - $connecting_button_string = $this->strings['connecting_button']; |
|
261 | - ?> |
|
7 | + /** |
|
8 | + * Allow the quick setup and connection of our AyeCode Connect plugin. |
|
9 | + * |
|
10 | + * Class AyeCode_Connect_Helper |
|
11 | + */ |
|
12 | + class AyeCode_Connect_Helper { |
|
13 | + |
|
14 | + // Hold the version number |
|
15 | + var $version = "1.0.4"; |
|
16 | + |
|
17 | + // Hold the default strings. |
|
18 | + var $strings = array(); |
|
19 | + |
|
20 | + // Hold the default pages. |
|
21 | + var $pages = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * The constructor. |
|
25 | + * |
|
26 | + * AyeCode_Connect_Helper constructor. |
|
27 | + * |
|
28 | + * @param array $strings |
|
29 | + * @param array $pages |
|
30 | + */ |
|
31 | + public function __construct( $strings = array(), $pages = array() ) { |
|
32 | + // Only fire if not localhost and the current user has the right permissions. |
|
33 | + if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
34 | + // set default strings |
|
35 | + $default_strings = array( |
|
36 | + 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | + 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | + 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | + 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | + 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | + 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | + 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
43 | + ); |
|
44 | + $this->strings = array_merge( $default_strings, $strings ); |
|
45 | + |
|
46 | + // set default pages |
|
47 | + $default_pages = array(); |
|
48 | + $this->pages = array_merge( $default_pages, $pages ); |
|
49 | + |
|
50 | + // maybe show connect site notice |
|
51 | + add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
52 | + |
|
53 | + // add ajax action if not already added |
|
54 | + if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | + add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + // add ajax action if not already added |
|
60 | + if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | + add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Give a way to check we can connect via a external redirect. |
|
67 | + */ |
|
68 | + public function ayecode_connect_helper_installed(){ |
|
69 | + $active = array( |
|
70 | + 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | + 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | + 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
73 | + ); |
|
74 | + wp_send_json_success( $active ); |
|
75 | + wp_die(); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Get slug from path |
|
80 | + * |
|
81 | + * @param string $key |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + private function format_plugin_slug( $key ) { |
|
86 | + $slug = explode( '/', $key ); |
|
87 | + $slug = explode( '.', end( $slug ) ); |
|
88 | + |
|
89 | + return $slug[0]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Install and activate the AyeCode Connect Plugin |
|
94 | + */ |
|
95 | + public function ayecode_connect_install() { |
|
96 | + // bail if localhost |
|
97 | + if ( $this->is_localhost() ) { |
|
98 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
99 | + } |
|
100 | + |
|
101 | + // Explicitly clear the event. |
|
102 | + wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
103 | + |
|
104 | + $success = true; |
|
105 | + $plugin_slug = "ayecode-connect"; |
|
106 | + if ( ! empty( $plugin_slug ) ) { |
|
107 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | + require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
111 | + |
|
112 | + WP_Filesystem(); |
|
113 | + |
|
114 | + $skin = new Automatic_Upgrader_Skin; |
|
115 | + $upgrader = new WP_Upgrader( $skin ); |
|
116 | + $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
117 | + $plugin_slug = $plugin_slug; |
|
118 | + $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
|
119 | + $installed = false; |
|
120 | + $activate = false; |
|
121 | + |
|
122 | + // See if the plugin is installed already |
|
123 | + if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
124 | + $installed = true; |
|
125 | + $activate = ! is_plugin_active( $plugin ); |
|
126 | + } |
|
127 | + |
|
128 | + // Install this thing! |
|
129 | + if ( ! $installed ) { |
|
130 | + |
|
131 | + // Suppress feedback |
|
132 | + ob_start(); |
|
133 | + |
|
134 | + try { |
|
135 | + $plugin_information = plugins_api( 'plugin_information', array( |
|
136 | + 'slug' => $plugin_slug, |
|
137 | + 'fields' => array( |
|
138 | + 'short_description' => false, |
|
139 | + 'sections' => false, |
|
140 | + 'requires' => false, |
|
141 | + 'rating' => false, |
|
142 | + 'ratings' => false, |
|
143 | + 'downloaded' => false, |
|
144 | + 'last_updated' => false, |
|
145 | + 'added' => false, |
|
146 | + 'tags' => false, |
|
147 | + 'homepage' => false, |
|
148 | + 'donate_link' => false, |
|
149 | + 'author_profile' => false, |
|
150 | + 'author' => false, |
|
151 | + ), |
|
152 | + ) ); |
|
153 | + |
|
154 | + if ( is_wp_error( $plugin_information ) ) { |
|
155 | + throw new Exception( $plugin_information->get_error_message() ); |
|
156 | + } |
|
157 | + |
|
158 | + $package = $plugin_information->download_link; |
|
159 | + $download = $upgrader->download_package( $package ); |
|
160 | + |
|
161 | + if ( is_wp_error( $download ) ) { |
|
162 | + throw new Exception( $download->get_error_message() ); |
|
163 | + } |
|
164 | + |
|
165 | + $working_dir = $upgrader->unpack_package( $download, true ); |
|
166 | + |
|
167 | + if ( is_wp_error( $working_dir ) ) { |
|
168 | + throw new Exception( $working_dir->get_error_message() ); |
|
169 | + } |
|
170 | + |
|
171 | + $result = $upgrader->install_package( array( |
|
172 | + 'source' => $working_dir, |
|
173 | + 'destination' => WP_PLUGIN_DIR, |
|
174 | + 'clear_destination' => false, |
|
175 | + 'abort_if_destination_exists' => false, |
|
176 | + 'clear_working' => true, |
|
177 | + 'hook_extra' => array( |
|
178 | + 'type' => 'plugin', |
|
179 | + 'action' => 'install', |
|
180 | + ), |
|
181 | + ) ); |
|
182 | + |
|
183 | + if ( is_wp_error( $result ) ) { |
|
184 | + throw new Exception( $result->get_error_message() ); |
|
185 | + } |
|
186 | + |
|
187 | + $activate = true; |
|
188 | + |
|
189 | + } catch ( Exception $e ) { |
|
190 | + $success = false; |
|
191 | + } |
|
192 | + |
|
193 | + // Discard feedback |
|
194 | + ob_end_clean(); |
|
195 | + } |
|
196 | + |
|
197 | + wp_clean_plugins_cache(); |
|
198 | + |
|
199 | + // Activate this thing |
|
200 | + if ( $activate ) { |
|
201 | + try { |
|
202 | + $result = activate_plugin( $plugin ); |
|
203 | + |
|
204 | + if ( is_wp_error( $result ) ) { |
|
205 | + $success = false; |
|
206 | + } else { |
|
207 | + $success = true; |
|
208 | + } |
|
209 | + } catch ( Exception $e ) { |
|
210 | + $success = false; |
|
211 | + } |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | + ayecode_connect();// init |
|
217 | + $args = ayecode_connect_args(); |
|
218 | + $client = new AyeCode_Connect( $args ); |
|
219 | + $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | + $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | + wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
222 | + } else { |
|
223 | + wp_send_json_error( $this->strings['error_localhost'] ); |
|
224 | + } |
|
225 | + wp_die(); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Check if maybe localhost. |
|
230 | + * |
|
231 | + * @return bool |
|
232 | + */ |
|
233 | + public function is_localhost() { |
|
234 | + $localhost = false; |
|
235 | + |
|
236 | + $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | + $localhost_domains = array( |
|
238 | + 'localhost', |
|
239 | + 'localhost.localdomain', |
|
240 | + '127.0.0.1', |
|
241 | + '::1' |
|
242 | + ); |
|
243 | + |
|
244 | + if ( in_array( $host, $localhost_domains ) ) { |
|
245 | + $localhost = true; |
|
246 | + } |
|
247 | + |
|
248 | + return $localhost; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Show notice to connect site. |
|
253 | + */ |
|
254 | + public function ayecode_connect_install_notice() { |
|
255 | + if ( $this->maybe_show() ) { |
|
256 | + $connect_title_string = $this->strings['connect_title']; |
|
257 | + $connect_external_string = $this->strings['connect_external']; |
|
258 | + $connect_string = $this->strings['connect']; |
|
259 | + $connect_button_string = $this->strings['connect_button']; |
|
260 | + $connecting_button_string = $this->strings['connecting_button']; |
|
261 | + ?> |
|
262 | 262 | <div class="notice notice-info acch-notice"> |
263 | 263 | <span class="acch-float-left"> |
264 | 264 | <svg width="61px" height="61px" viewBox="0 0 61 61" version="1.1" |
@@ -305,9 +305,9 @@ discard block |
||
305 | 305 | </div> |
306 | 306 | |
307 | 307 | <?php |
308 | - // only include the popup HTML if needed. |
|
309 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
310 | - ?> |
|
308 | + // only include the popup HTML if needed. |
|
309 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
310 | + ?> |
|
311 | 311 | <div id="ayecode-connect-helper-external-confirm" style="display:none;"> |
312 | 312 | <div class="noticex notice-info acch-notice" style="border: none;"> |
313 | 313 | <span class="acch-float-left"> |
@@ -353,23 +353,23 @@ discard block |
||
353 | 353 | </div> |
354 | 354 | </div> |
355 | 355 | <?php |
356 | - } |
|
357 | - |
|
358 | - // add required scripts |
|
359 | - $this->script(); |
|
360 | - } |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Get the JS Script. |
|
365 | - */ |
|
366 | - public function script() { |
|
367 | - |
|
368 | - // add thickbox if external request is requested |
|
369 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
370 | - add_thickbox(); |
|
371 | - } |
|
372 | - ?> |
|
356 | + } |
|
357 | + |
|
358 | + // add required scripts |
|
359 | + $this->script(); |
|
360 | + } |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Get the JS Script. |
|
365 | + */ |
|
366 | + public function script() { |
|
367 | + |
|
368 | + // add thickbox if external request is requested |
|
369 | + if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
370 | + add_thickbox(); |
|
371 | + } |
|
372 | + ?> |
|
373 | 373 | <style> |
374 | 374 | .acch-title { |
375 | 375 | margin: 0; |
@@ -435,38 +435,38 @@ discard block |
||
435 | 435 | }); |
436 | 436 | } |
437 | 437 | <?php |
438 | - // add thickbox if external request is requested |
|
439 | - if(! empty( $_REQUEST['external-connect-request'] )) { |
|
440 | - ?> |
|
438 | + // add thickbox if external request is requested |
|
439 | + if(! empty( $_REQUEST['external-connect-request'] )) { |
|
440 | + ?> |
|
441 | 441 | jQuery(function () { |
442 | 442 | setTimeout(function () { |
443 | 443 | tb_show("AyeCode Connect", "?TB_inline?width=300&height=80&inlineId=ayecode-connect-helper-external-confirm"); |
444 | 444 | }, 200); |
445 | 445 | }); |
446 | 446 | <?php |
447 | - } |
|
448 | - ?> |
|
447 | + } |
|
448 | + ?> |
|
449 | 449 | </script> |
450 | 450 | <?php |
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Decide what pages to show on. |
|
455 | - * |
|
456 | - * @return bool |
|
457 | - */ |
|
458 | - public function maybe_show() { |
|
459 | - $show = false; |
|
460 | - |
|
461 | - // check if on a page set to show |
|
462 | - if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
463 | - // check if not active and connected |
|
464 | - if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
465 | - $show = true; |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - return $show; |
|
470 | - } |
|
471 | - } |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Decide what pages to show on. |
|
455 | + * |
|
456 | + * @return bool |
|
457 | + */ |
|
458 | + public function maybe_show() { |
|
459 | + $show = false; |
|
460 | + |
|
461 | + // check if on a page set to show |
|
462 | + if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
463 | + // check if not active and connected |
|
464 | + if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
465 | + $show = true; |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + return $show; |
|
470 | + } |
|
471 | + } |
|
472 | 472 | } |
@@ -1,9 +1,9 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if (!defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
6 | -if ( ! class_exists( "AyeCode_Connect_Helper" ) ) { |
|
6 | +if (!class_exists("AyeCode_Connect_Helper")) { |
|
7 | 7 | /** |
8 | 8 | * Allow the quick setup and connection of our AyeCode Connect plugin. |
9 | 9 | * |
@@ -28,50 +28,50 @@ discard block |
||
28 | 28 | * @param array $strings |
29 | 29 | * @param array $pages |
30 | 30 | */ |
31 | - public function __construct( $strings = array(), $pages = array() ) { |
|
31 | + public function __construct($strings = array(), $pages = array()) { |
|
32 | 32 | // Only fire if not localhost and the current user has the right permissions. |
33 | - if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) { |
|
33 | + if (!$this->is_localhost() && current_user_can('manage_options')) { |
|
34 | 34 | // set default strings |
35 | 35 | $default_strings = array( |
36 | - 'connect_title' => __( "Thanks for choosing an AyeCode Product!", 'ayecode-connect' ), |
|
37 | - 'connect_external' => __( "Please confirm you wish to connect your site?", 'ayecode-connect' ), |
|
38 | - 'connect' => wp_sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ), |
|
39 | - 'connect_button' => __( "Connect Site", 'ayecode-connect' ), |
|
40 | - 'connecting_button' => __( "Connecting...", 'ayecode-connect' ), |
|
41 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost.", 'ayecode-connect' ), |
|
42 | - 'error' => __( "Something went wrong, please refresh and try again.", 'ayecode-connect' ), |
|
36 | + 'connect_title' => __("Thanks for choosing an AyeCode Product!", 'ayecode-connect'), |
|
37 | + 'connect_external' => __("Please confirm you wish to connect your site?", 'ayecode-connect'), |
|
38 | + 'connect' => wp_sprintf(__("<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s", 'ayecode-connect'), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>"), |
|
39 | + 'connect_button' => __("Connect Site", 'ayecode-connect'), |
|
40 | + 'connecting_button' => __("Connecting...", 'ayecode-connect'), |
|
41 | + 'error_localhost' => __("This service will only work with a live domain, not a localhost.", 'ayecode-connect'), |
|
42 | + 'error' => __("Something went wrong, please refresh and try again.", 'ayecode-connect'), |
|
43 | 43 | ); |
44 | - $this->strings = array_merge( $default_strings, $strings ); |
|
44 | + $this->strings = array_merge($default_strings, $strings); |
|
45 | 45 | |
46 | 46 | // set default pages |
47 | 47 | $default_pages = array(); |
48 | - $this->pages = array_merge( $default_pages, $pages ); |
|
48 | + $this->pages = array_merge($default_pages, $pages); |
|
49 | 49 | |
50 | 50 | // maybe show connect site notice |
51 | - add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) ); |
|
51 | + add_action('admin_notices', array($this, 'ayecode_connect_install_notice')); |
|
52 | 52 | |
53 | 53 | // add ajax action if not already added |
54 | - if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) { |
|
55 | - add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) ); |
|
54 | + if (!has_action('wp_ajax_ayecode_connect_helper')) { |
|
55 | + add_action('wp_ajax_ayecode_connect_helper', array($this, 'ayecode_connect_install')); |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 | |
59 | 59 | // add ajax action if not already added |
60 | - if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) { |
|
61 | - add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) ); |
|
60 | + if (!has_action('wp_ajax_nopriv_ayecode_connect_helper_installed')) { |
|
61 | + add_action('wp_ajax_nopriv_ayecode_connect_helper_installed', array($this, 'ayecode_connect_helper_installed')); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * Give a way to check we can connect via a external redirect. |
67 | 67 | */ |
68 | - public function ayecode_connect_helper_installed(){ |
|
68 | + public function ayecode_connect_helper_installed() { |
|
69 | 69 | $active = array( |
70 | - 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0, |
|
71 | - 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0, |
|
72 | - 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0, |
|
70 | + 'gd' => defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION, '2.0.0.79', '>') ? 1 : 0, |
|
71 | + 'uwp' => defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION, '1.2.1.5', '>') ? 1 : 0, |
|
72 | + 'wpi' => defined('WPINV_VERSION') && version_compare(WPINV_VERSION, '1.0.14', '>') ? 1 : 0, |
|
73 | 73 | ); |
74 | - wp_send_json_success( $active ); |
|
74 | + wp_send_json_success($active); |
|
75 | 75 | wp_die(); |
76 | 76 | } |
77 | 77 | |
@@ -82,9 +82,9 @@ discard block |
||
82 | 82 | * |
83 | 83 | * @return string |
84 | 84 | */ |
85 | - private function format_plugin_slug( $key ) { |
|
86 | - $slug = explode( '/', $key ); |
|
87 | - $slug = explode( '.', end( $slug ) ); |
|
85 | + private function format_plugin_slug($key) { |
|
86 | + $slug = explode('/', $key); |
|
87 | + $slug = explode('.', end($slug)); |
|
88 | 88 | |
89 | 89 | return $slug[0]; |
90 | 90 | } |
@@ -94,45 +94,45 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public function ayecode_connect_install() { |
96 | 96 | // bail if localhost |
97 | - if ( $this->is_localhost() ) { |
|
98 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
97 | + if ($this->is_localhost()) { |
|
98 | + wp_send_json_error($this->strings['error_localhost']); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | // Explicitly clear the event. |
102 | - wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() ); |
|
102 | + wp_clear_scheduled_hook('geodir_plugin_background_installer', func_get_args()); |
|
103 | 103 | |
104 | 104 | $success = true; |
105 | 105 | $plugin_slug = "ayecode-connect"; |
106 | - if ( ! empty( $plugin_slug ) ) { |
|
107 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
108 | - require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
109 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
110 | - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
|
106 | + if (!empty($plugin_slug)) { |
|
107 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
108 | + require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); |
|
109 | + require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
|
110 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
111 | 111 | |
112 | 112 | WP_Filesystem(); |
113 | 113 | |
114 | 114 | $skin = new Automatic_Upgrader_Skin; |
115 | - $upgrader = new WP_Upgrader( $skin ); |
|
116 | - $installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) ); |
|
115 | + $upgrader = new WP_Upgrader($skin); |
|
116 | + $installed_plugins = array_map(array($this, 'format_plugin_slug'), array_keys(get_plugins())); |
|
117 | 117 | $plugin_slug = $plugin_slug; |
118 | 118 | $plugin = $plugin_slug . '/' . $plugin_slug . '.php'; |
119 | 119 | $installed = false; |
120 | 120 | $activate = false; |
121 | 121 | |
122 | 122 | // See if the plugin is installed already |
123 | - if ( in_array( $plugin_slug, $installed_plugins ) ) { |
|
123 | + if (in_array($plugin_slug, $installed_plugins)) { |
|
124 | 124 | $installed = true; |
125 | - $activate = ! is_plugin_active( $plugin ); |
|
125 | + $activate = !is_plugin_active($plugin); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // Install this thing! |
129 | - if ( ! $installed ) { |
|
129 | + if (!$installed) { |
|
130 | 130 | |
131 | 131 | // Suppress feedback |
132 | 132 | ob_start(); |
133 | 133 | |
134 | 134 | try { |
135 | - $plugin_information = plugins_api( 'plugin_information', array( |
|
135 | + $plugin_information = plugins_api('plugin_information', array( |
|
136 | 136 | 'slug' => $plugin_slug, |
137 | 137 | 'fields' => array( |
138 | 138 | 'short_description' => false, |
@@ -149,26 +149,26 @@ discard block |
||
149 | 149 | 'author_profile' => false, |
150 | 150 | 'author' => false, |
151 | 151 | ), |
152 | - ) ); |
|
152 | + )); |
|
153 | 153 | |
154 | - if ( is_wp_error( $plugin_information ) ) { |
|
155 | - throw new Exception( $plugin_information->get_error_message() ); |
|
154 | + if (is_wp_error($plugin_information)) { |
|
155 | + throw new Exception($plugin_information->get_error_message()); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | $package = $plugin_information->download_link; |
159 | - $download = $upgrader->download_package( $package ); |
|
159 | + $download = $upgrader->download_package($package); |
|
160 | 160 | |
161 | - if ( is_wp_error( $download ) ) { |
|
162 | - throw new Exception( $download->get_error_message() ); |
|
161 | + if (is_wp_error($download)) { |
|
162 | + throw new Exception($download->get_error_message()); |
|
163 | 163 | } |
164 | 164 | |
165 | - $working_dir = $upgrader->unpack_package( $download, true ); |
|
165 | + $working_dir = $upgrader->unpack_package($download, true); |
|
166 | 166 | |
167 | - if ( is_wp_error( $working_dir ) ) { |
|
168 | - throw new Exception( $working_dir->get_error_message() ); |
|
167 | + if (is_wp_error($working_dir)) { |
|
168 | + throw new Exception($working_dir->get_error_message()); |
|
169 | 169 | } |
170 | 170 | |
171 | - $result = $upgrader->install_package( array( |
|
171 | + $result = $upgrader->install_package(array( |
|
172 | 172 | 'source' => $working_dir, |
173 | 173 | 'destination' => WP_PLUGIN_DIR, |
174 | 174 | 'clear_destination' => false, |
@@ -178,15 +178,15 @@ discard block |
||
178 | 178 | 'type' => 'plugin', |
179 | 179 | 'action' => 'install', |
180 | 180 | ), |
181 | - ) ); |
|
181 | + )); |
|
182 | 182 | |
183 | - if ( is_wp_error( $result ) ) { |
|
184 | - throw new Exception( $result->get_error_message() ); |
|
183 | + if (is_wp_error($result)) { |
|
184 | + throw new Exception($result->get_error_message()); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | $activate = true; |
188 | 188 | |
189 | - } catch ( Exception $e ) { |
|
189 | + } catch (Exception $e) { |
|
190 | 190 | $success = false; |
191 | 191 | } |
192 | 192 | |
@@ -197,30 +197,30 @@ discard block |
||
197 | 197 | wp_clean_plugins_cache(); |
198 | 198 | |
199 | 199 | // Activate this thing |
200 | - if ( $activate ) { |
|
200 | + if ($activate) { |
|
201 | 201 | try { |
202 | - $result = activate_plugin( $plugin ); |
|
202 | + $result = activate_plugin($plugin); |
|
203 | 203 | |
204 | - if ( is_wp_error( $result ) ) { |
|
204 | + if (is_wp_error($result)) { |
|
205 | 205 | $success = false; |
206 | 206 | } else { |
207 | 207 | $success = true; |
208 | 208 | } |
209 | - } catch ( Exception $e ) { |
|
209 | + } catch (Exception $e) { |
|
210 | 210 | $success = false; |
211 | 211 | } |
212 | 212 | } |
213 | 213 | } |
214 | 214 | |
215 | - if ( $success && function_exists( 'ayecode_connect_args' ) ) { |
|
216 | - ayecode_connect();// init |
|
215 | + if ($success && function_exists('ayecode_connect_args')) { |
|
216 | + ayecode_connect(); // init |
|
217 | 217 | $args = ayecode_connect_args(); |
218 | - $client = new AyeCode_Connect( $args ); |
|
219 | - $redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : ''; |
|
220 | - $redirect = $client->build_connect_url( $redirect_to ); |
|
221 | - wp_send_json_success( array( 'connect_url' => $redirect ) ); |
|
218 | + $client = new AyeCode_Connect($args); |
|
219 | + $redirect_to = !empty($_POST['redirect_to']) ? esc_url_raw($_POST['redirect_to']) : ''; |
|
220 | + $redirect = $client->build_connect_url($redirect_to); |
|
221 | + wp_send_json_success(array('connect_url' => $redirect)); |
|
222 | 222 | } else { |
223 | - wp_send_json_error( $this->strings['error_localhost'] ); |
|
223 | + wp_send_json_error($this->strings['error_localhost']); |
|
224 | 224 | } |
225 | 225 | wp_die(); |
226 | 226 | } |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | public function is_localhost() { |
234 | 234 | $localhost = false; |
235 | 235 | |
236 | - $host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : ''; |
|
236 | + $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; |
|
237 | 237 | $localhost_domains = array( |
238 | 238 | 'localhost', |
239 | 239 | 'localhost.localdomain', |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | '::1' |
242 | 242 | ); |
243 | 243 | |
244 | - if ( in_array( $host, $localhost_domains ) ) { |
|
244 | + if (in_array($host, $localhost_domains)) { |
|
245 | 245 | $localhost = true; |
246 | 246 | } |
247 | 247 | |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | * Show notice to connect site. |
253 | 253 | */ |
254 | 254 | public function ayecode_connect_install_notice() { |
255 | - if ( $this->maybe_show() ) { |
|
255 | + if ($this->maybe_show()) { |
|
256 | 256 | $connect_title_string = $this->strings['connect_title']; |
257 | 257 | $connect_external_string = $this->strings['connect_external']; |
258 | 258 | $connect_string = $this->strings['connect']; |
@@ -294,19 +294,19 @@ discard block |
||
294 | 294 | </svg> |
295 | 295 | </span> |
296 | 296 | <span class="acch-float-left acch-text"> |
297 | - <h3 class="acch-title"><?php echo esc_attr( $connect_title_string ); ?></h3> |
|
297 | + <h3 class="acch-title"><?php echo esc_attr($connect_title_string); ?></h3> |
|
298 | 298 | <p><?php echo $connect_string; ?> |
299 | 299 | </p> |
300 | 300 | </span> |
301 | 301 | |
302 | 302 | <span class="acch-float-left acch-button"> |
303 | - <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button> |
|
303 | + <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr($connecting_button_string); ?>"><?php echo esc_attr($connect_button_string) ?></button> |
|
304 | 304 | </span> |
305 | 305 | </div> |
306 | 306 | |
307 | 307 | <?php |
308 | 308 | // only include the popup HTML if needed. |
309 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
309 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
310 | 310 | ?> |
311 | 311 | <div id="ayecode-connect-helper-external-confirm" style="display:none;"> |
312 | 312 | <div class="noticex notice-info acch-notice" style="border: none;"> |
@@ -344,11 +344,11 @@ discard block |
||
344 | 344 | </svg> |
345 | 345 | </span> |
346 | 346 | <span class="acch-float-left acch-text"> |
347 | - <h3 class="acch-title"><?php echo esc_attr( $connect_external_string ); ?></h3> |
|
347 | + <h3 class="acch-title"><?php echo esc_attr($connect_external_string); ?></h3> |
|
348 | 348 | </span> |
349 | 349 | |
350 | 350 | <span class="acch-float-left acch-button"> |
351 | - <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button> |
|
351 | + <button onclick="ayecode_connect_helper(this);" id="gd-connect-site" class="button button-primary" data-connecting="<?php echo esc_attr($connecting_button_string); ?>"><?php echo esc_attr($connect_button_string) ?></button> |
|
352 | 352 | </span> |
353 | 353 | </div> |
354 | 354 | </div> |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | public function script() { |
367 | 367 | |
368 | 368 | // add thickbox if external request is requested |
369 | - if ( ! empty( $_REQUEST['external-connect-request'] ) ) { |
|
369 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
370 | 370 | add_thickbox(); |
371 | 371 | } |
372 | 372 | ?> |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | url: ajaxurl, |
418 | 418 | data: { |
419 | 419 | action: 'ayecode_connect_helper', |
420 | - security: '<?php echo wp_create_nonce( 'ayecode-connect-helper' );?>', |
|
420 | + security: '<?php echo wp_create_nonce('ayecode-connect-helper'); ?>', |
|
421 | 421 | redirect_to: $current_url |
422 | 422 | }, |
423 | 423 | beforeSend: function () { |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | } |
437 | 437 | <?php |
438 | 438 | // add thickbox if external request is requested |
439 | - if(! empty( $_REQUEST['external-connect-request'] )) { |
|
439 | + if (!empty($_REQUEST['external-connect-request'])) { |
|
440 | 440 | ?> |
441 | 441 | jQuery(function () { |
442 | 442 | setTimeout(function () { |
@@ -459,9 +459,9 @@ discard block |
||
459 | 459 | $show = false; |
460 | 460 | |
461 | 461 | // check if on a page set to show |
462 | - if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) { |
|
462 | + if (isset($_REQUEST['page']) && in_array($_REQUEST['page'], $this->pages)) { |
|
463 | 463 | // check if not active and connected |
464 | - if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) { |
|
464 | + if (!defined('AYECODE_CONNECT_VERSION') || !get_option('ayecode_connect_blog_token')) { |
|
465 | 465 | $show = true; |
466 | 466 | } |
467 | 467 | } |
@@ -1,99 +1,99 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; |
|
4 | + exit; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | if ( ! class_exists( 'AyeCode_Deactivation_Survey' ) ) { |
8 | 8 | |
9 | - class AyeCode_Deactivation_Survey { |
|
9 | + class AyeCode_Deactivation_Survey { |
|
10 | 10 | |
11 | - /** |
|
12 | - * AyeCode_Deactivation_Survey instance. |
|
13 | - * |
|
14 | - * @access private |
|
15 | - * @since 1.0.0 |
|
16 | - * @var AyeCode_Deactivation_Survey There can be only one! |
|
17 | - */ |
|
18 | - private static $instance = null; |
|
11 | + /** |
|
12 | + * AyeCode_Deactivation_Survey instance. |
|
13 | + * |
|
14 | + * @access private |
|
15 | + * @since 1.0.0 |
|
16 | + * @var AyeCode_Deactivation_Survey There can be only one! |
|
17 | + */ |
|
18 | + private static $instance = null; |
|
19 | 19 | |
20 | - public static $plugins; |
|
20 | + public static $plugins; |
|
21 | 21 | |
22 | - public $version = "1.0.7"; |
|
22 | + public $version = "1.0.7"; |
|
23 | 23 | |
24 | - public static function instance( $plugin = array() ) { |
|
25 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
26 | - self::$instance = new AyeCode_Deactivation_Survey; |
|
27 | - self::$plugins = array(); |
|
24 | + public static function instance( $plugin = array() ) { |
|
25 | + if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
26 | + self::$instance = new AyeCode_Deactivation_Survey; |
|
27 | + self::$plugins = array(); |
|
28 | 28 | |
29 | - add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
29 | + add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
30 | 30 | |
31 | - do_action( 'ayecode_deactivation_survey_loaded' ); |
|
32 | - } |
|
31 | + do_action( 'ayecode_deactivation_survey_loaded' ); |
|
32 | + } |
|
33 | 33 | |
34 | - if(!empty($plugin)){ |
|
35 | - self::$plugins[] = (object)$plugin; |
|
36 | - } |
|
34 | + if(!empty($plugin)){ |
|
35 | + self::$plugins[] = (object)$plugin; |
|
36 | + } |
|
37 | 37 | |
38 | - return self::$instance; |
|
39 | - } |
|
38 | + return self::$instance; |
|
39 | + } |
|
40 | 40 | |
41 | - public function scripts() { |
|
42 | - global $pagenow; |
|
41 | + public function scripts() { |
|
42 | + global $pagenow; |
|
43 | 43 | |
44 | - // Bail if we are not on the plugins page |
|
45 | - if ( $pagenow != "plugins.php" ) { |
|
46 | - return; |
|
47 | - } |
|
44 | + // Bail if we are not on the plugins page |
|
45 | + if ( $pagenow != "plugins.php" ) { |
|
46 | + return; |
|
47 | + } |
|
48 | 48 | |
49 | - // Enqueue scripts |
|
50 | - add_thickbox(); |
|
51 | - wp_enqueue_script('ayecode-deactivation-survey', plugin_dir_url(__FILE__) . 'ayecode-ds.js'); |
|
49 | + // Enqueue scripts |
|
50 | + add_thickbox(); |
|
51 | + wp_enqueue_script('ayecode-deactivation-survey', plugin_dir_url(__FILE__) . 'ayecode-ds.js'); |
|
52 | 52 | |
53 | - /* |
|
53 | + /* |
|
54 | 54 | * Localized strings. Strings can be localised by plugins using this class. |
55 | 55 | * We deliberately don't add textdomains here so that double textdomain warning is not given in theme review. |
56 | 56 | */ |
57 | - wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
|
58 | - 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | - 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | - 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | - 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | - 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | - 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | - 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | - 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | - 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | - 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | - 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | - 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | - 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
71 | - )); |
|
72 | - |
|
73 | - // Plugins |
|
74 | - $plugins = apply_filters('ayecode_deactivation_survey_plugins', self::$plugins); |
|
75 | - |
|
76 | - // Reasons |
|
77 | - $defaultReasons = array( |
|
78 | - 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | - 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | - 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | - 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | - 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | - 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | - 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | - 'other' => __( 'Other', 'ayecode-connect' ), |
|
86 | - ); |
|
87 | - |
|
88 | - foreach( $plugins as $plugin ) { |
|
89 | - $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
90 | - $plugin->url = home_url(); |
|
91 | - $plugin->activated = 0; |
|
92 | - } |
|
93 | - |
|
94 | - // Send plugin data |
|
95 | - wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_plugins', $plugins); |
|
96 | - } |
|
97 | - } |
|
57 | + wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
|
58 | + 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | + 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | + 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | + 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | + 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | + 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | + 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | + 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | + 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | + 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | + 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | + 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | + 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
71 | + )); |
|
72 | + |
|
73 | + // Plugins |
|
74 | + $plugins = apply_filters('ayecode_deactivation_survey_plugins', self::$plugins); |
|
75 | + |
|
76 | + // Reasons |
|
77 | + $defaultReasons = array( |
|
78 | + 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | + 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | + 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | + 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | + 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | + 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | + 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | + 'other' => __( 'Other', 'ayecode-connect' ), |
|
86 | + ); |
|
87 | + |
|
88 | + foreach( $plugins as $plugin ) { |
|
89 | + $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
90 | + $plugin->url = home_url(); |
|
91 | + $plugin->activated = 0; |
|
92 | + } |
|
93 | + |
|
94 | + // Send plugin data |
|
95 | + wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_plugins', $plugins); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -1,10 +1,10 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( ! defined( 'ABSPATH' ) ) { |
|
3 | +if (!defined('ABSPATH')) { |
|
4 | 4 | exit; |
5 | 5 | } |
6 | 6 | |
7 | -if ( ! class_exists( 'AyeCode_Deactivation_Survey' ) ) { |
|
7 | +if (!class_exists('AyeCode_Deactivation_Survey')) { |
|
8 | 8 | |
9 | 9 | class AyeCode_Deactivation_Survey { |
10 | 10 | |
@@ -21,18 +21,18 @@ discard block |
||
21 | 21 | |
22 | 22 | public $version = "1.0.7"; |
23 | 23 | |
24 | - public static function instance( $plugin = array() ) { |
|
25 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AyeCode_Deactivation_Survey ) ) { |
|
24 | + public static function instance($plugin = array()) { |
|
25 | + if (!isset(self::$instance) && !(self::$instance instanceof AyeCode_Deactivation_Survey)) { |
|
26 | 26 | self::$instance = new AyeCode_Deactivation_Survey; |
27 | 27 | self::$plugins = array(); |
28 | 28 | |
29 | - add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); |
|
29 | + add_action('admin_enqueue_scripts', array(self::$instance, 'scripts')); |
|
30 | 30 | |
31 | - do_action( 'ayecode_deactivation_survey_loaded' ); |
|
31 | + do_action('ayecode_deactivation_survey_loaded'); |
|
32 | 32 | } |
33 | 33 | |
34 | - if(!empty($plugin)){ |
|
35 | - self::$plugins[] = (object)$plugin; |
|
34 | + if (!empty($plugin)) { |
|
35 | + self::$plugins[] = (object) $plugin; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | return self::$instance; |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | global $pagenow; |
43 | 43 | |
44 | 44 | // Bail if we are not on the plugins page |
45 | - if ( $pagenow != "plugins.php" ) { |
|
45 | + if ($pagenow != "plugins.php") { |
|
46 | 46 | return; |
47 | 47 | } |
48 | 48 | |
@@ -55,19 +55,19 @@ discard block |
||
55 | 55 | * We deliberately don't add textdomains here so that double textdomain warning is not given in theme review. |
56 | 56 | */ |
57 | 57 | wp_localize_script('ayecode-deactivation-survey', 'ayecodeds_deactivate_feedback_form_strings', array( |
58 | - 'quick_feedback' => __( 'Quick Feedback', 'ayecode-connect' ), |
|
59 | - 'foreword' => __( 'If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect' ), |
|
60 | - 'better_plugins_name' => __( 'Please tell us which plugin?', 'ayecode-connect' ), |
|
61 | - 'please_tell_us' => __( 'Please tell us the reason so we can improve the plugin', 'ayecode-connect' ), |
|
62 | - 'do_not_attach_email' => __( 'Do not send my e-mail address with this feedback', 'ayecode-connect' ), |
|
63 | - 'brief_description' => __( 'Please give us any feedback that could help us improve', 'ayecode-connect' ), |
|
64 | - 'cancel' => __( 'Cancel', 'ayecode-connect' ), |
|
65 | - 'skip_and_deactivate' => __( 'Skip & Deactivate', 'ayecode-connect' ), |
|
66 | - 'submit_and_deactivate' => __( 'Submit & Deactivate', 'ayecode-connect' ), |
|
67 | - 'please_wait' => __( 'Please wait', 'ayecode-connect' ), |
|
68 | - 'get_support' => __( 'Get Support', 'ayecode-connect' ), |
|
69 | - 'documentation' => __( 'Documentation', 'ayecode-connect' ), |
|
70 | - 'thank_you' => __( 'Thank you!', 'ayecode-connect' ), |
|
58 | + 'quick_feedback' => __('Quick Feedback', 'ayecode-connect'), |
|
59 | + 'foreword' => __('If you would be kind enough, please tell us why you\'re deactivating?', 'ayecode-connect'), |
|
60 | + 'better_plugins_name' => __('Please tell us which plugin?', 'ayecode-connect'), |
|
61 | + 'please_tell_us' => __('Please tell us the reason so we can improve the plugin', 'ayecode-connect'), |
|
62 | + 'do_not_attach_email' => __('Do not send my e-mail address with this feedback', 'ayecode-connect'), |
|
63 | + 'brief_description' => __('Please give us any feedback that could help us improve', 'ayecode-connect'), |
|
64 | + 'cancel' => __('Cancel', 'ayecode-connect'), |
|
65 | + 'skip_and_deactivate' => __('Skip & Deactivate', 'ayecode-connect'), |
|
66 | + 'submit_and_deactivate' => __('Submit & Deactivate', 'ayecode-connect'), |
|
67 | + 'please_wait' => __('Please wait', 'ayecode-connect'), |
|
68 | + 'get_support' => __('Get Support', 'ayecode-connect'), |
|
69 | + 'documentation' => __('Documentation', 'ayecode-connect'), |
|
70 | + 'thank_you' => __('Thank you!', 'ayecode-connect'), |
|
71 | 71 | )); |
72 | 72 | |
73 | 73 | // Plugins |
@@ -75,18 +75,18 @@ discard block |
||
75 | 75 | |
76 | 76 | // Reasons |
77 | 77 | $defaultReasons = array( |
78 | - 'suddenly-stopped-working' => __( 'The plugin suddenly stopped working', 'ayecode-connect' ), |
|
79 | - 'plugin-broke-site' => __( 'The plugin broke my site', 'ayecode-connect' ), |
|
80 | - 'plugin-setup-difficult' => __( 'Too difficult to setup', 'ayecode-connect' ), |
|
81 | - 'plugin-design-difficult' => __( 'Too difficult to get the design i want', 'ayecode-connect' ), |
|
82 | - 'no-longer-needed' => __( 'I don\'t need this plugin any more', 'ayecode-connect' ), |
|
83 | - 'found-better-plugin' => __( 'I found a better plugin', 'ayecode-connect' ), |
|
84 | - 'temporary-deactivation' => __( 'It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect' ), |
|
85 | - 'other' => __( 'Other', 'ayecode-connect' ), |
|
78 | + 'suddenly-stopped-working' => __('The plugin suddenly stopped working', 'ayecode-connect'), |
|
79 | + 'plugin-broke-site' => __('The plugin broke my site', 'ayecode-connect'), |
|
80 | + 'plugin-setup-difficult' => __('Too difficult to setup', 'ayecode-connect'), |
|
81 | + 'plugin-design-difficult' => __('Too difficult to get the design i want', 'ayecode-connect'), |
|
82 | + 'no-longer-needed' => __('I don\'t need this plugin any more', 'ayecode-connect'), |
|
83 | + 'found-better-plugin' => __('I found a better plugin', 'ayecode-connect'), |
|
84 | + 'temporary-deactivation' => __('It\'s a temporary deactivation, I\'m troubleshooting', 'ayecode-connect'), |
|
85 | + 'other' => __('Other', 'ayecode-connect'), |
|
86 | 86 | ); |
87 | 87 | |
88 | - foreach( $plugins as $plugin ) { |
|
89 | - $plugin->reasons = apply_filters( 'ayecode_deactivation_survey_reasons', $defaultReasons, $plugin ); |
|
88 | + foreach ($plugins as $plugin) { |
|
89 | + $plugin->reasons = apply_filters('ayecode_deactivation_survey_reasons', $defaultReasons, $plugin); |
|
90 | 90 | $plugin->url = home_url(); |
91 | 91 | $plugin->activated = 0; |
92 | 92 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * @since 2.0.0 |
8 | 8 | */ |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * GetPaid REST date based controller class. |
@@ -43,26 +43,26 @@ discard block |
||
43 | 43 | * @param WP_REST_Request $request Request object. |
44 | 44 | * @return array The appropriate date range. |
45 | 45 | */ |
46 | - public function get_date_range( $request ) { |
|
46 | + public function get_date_range($request) { |
|
47 | 47 | |
48 | 48 | // Check if the period is x_days. |
49 | - if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | - $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | - } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | - $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
49 | + if (preg_match('/^(\d+)_days$/', $request['period'], $matches)) { |
|
50 | + $date_range = $this->get_x_days_date_range(absint($matches[1])); |
|
51 | + } elseif (is_callable(array($this, 'get_' . $request['period'] . '_date_range'))) { |
|
52 | + $date_range = call_user_func(array($this, 'get_' . $request['period'] . '_date_range'), $request); |
|
53 | 53 | } else { |
54 | 54 | $request['period'] = '7_days'; |
55 | 55 | $date_range = $this->get_x_days_date_range(); |
56 | 56 | } |
57 | 57 | |
58 | 58 | // 3 months max for day view. |
59 | - $before = strtotime( $date_range['before'] ); |
|
60 | - $after = strtotime( $date_range['after'] ); |
|
61 | - if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
59 | + $before = strtotime($date_range['before']); |
|
60 | + $after = strtotime($date_range['after']); |
|
61 | + if (floor(($before - $after) / MONTH_IN_SECONDS) > 2) { |
|
62 | 62 | $this->groupby = 'month'; |
63 | 63 | } |
64 | 64 | |
65 | - $this->prepare_interval( $date_range ); |
|
65 | + $this->prepare_interval($date_range); |
|
66 | 66 | |
67 | 67 | return $date_range; |
68 | 68 | |
@@ -74,25 +74,25 @@ discard block |
||
74 | 74 | * @param array $range Date range. |
75 | 75 | * @return array The appropriate date range. |
76 | 76 | */ |
77 | - public function prepare_interval( $range ) { |
|
77 | + public function prepare_interval($range) { |
|
78 | 78 | |
79 | - $before = strtotime( $range['before'] ); |
|
80 | - $after = strtotime( $range['after'] ); |
|
81 | - if ( 'day' === $this->groupby ) { |
|
82 | - $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | - $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
79 | + $before = strtotime($range['before']); |
|
80 | + $after = strtotime($range['after']); |
|
81 | + if ('day' === $this->groupby) { |
|
82 | + $difference = max(DAY_IN_SECONDS, (DAY_IN_SECONDS + $before - $after)); // Prevent division by 0; |
|
83 | + $this->interval = absint(ceil(max(1, $difference / DAY_IN_SECONDS))); |
|
84 | 84 | return; |
85 | 85 | } |
86 | 86 | |
87 | 87 | $this->interval = 0; |
88 | - $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
88 | + $min_date = strtotime(gmdate('Y-m-01', $after)); |
|
89 | 89 | |
90 | - while ( $min_date <= $before ) { |
|
91 | - $this->interval ++; |
|
92 | - $min_date = strtotime( '+1 MONTH', $min_date ); |
|
90 | + while ($min_date <= $before) { |
|
91 | + $this->interval++; |
|
92 | + $min_date = strtotime('+1 MONTH', $min_date); |
|
93 | 93 | } |
94 | 94 | |
95 | - $this->interval = max( 1, $this->interval ); |
|
95 | + $this->interval = max(1, $this->interval); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
@@ -102,27 +102,27 @@ discard block |
||
102 | 102 | * @param WP_REST_Request $request Request object. |
103 | 103 | * @return array The appropriate date range. |
104 | 104 | */ |
105 | - public function get_custom_date_range( $request ) { |
|
105 | + public function get_custom_date_range($request) { |
|
106 | 106 | |
107 | - $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | - $before = gmdate( 'Y-m-d' ); |
|
107 | + $after = max(strtotime('-20 years'), strtotime(sanitize_text_field($request['after']))); |
|
108 | + $before = gmdate('Y-m-d'); |
|
109 | 109 | |
110 | - if ( ! empty( $request['before'] ) ) { |
|
111 | - $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
110 | + if (!empty($request['before'])) { |
|
111 | + $before = min(strtotime($before), strtotime(sanitize_text_field($request['before']))); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | // Set the previous date range. |
115 | 115 | $difference = $before - $after; |
116 | 116 | $this->previous_range = array( |
117 | 117 | 'period' => 'custom', |
118 | - 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | - 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
118 | + 'before' => gmdate('Y-m-d', $before - $difference - DAY_IN_SECONDS), |
|
119 | + 'after' => gmdate('Y-m-d', $after - $difference - DAY_IN_SECONDS), |
|
120 | 120 | ); |
121 | 121 | |
122 | 122 | // Generate the report. |
123 | 123 | return array( |
124 | - 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | - 'after' => gmdate( 'Y-m-d', $after ), |
|
124 | + 'before' => gmdate('Y-m-d', $before), |
|
125 | + 'after' => gmdate('Y-m-d', $after), |
|
126 | 126 | ); |
127 | 127 | |
128 | 128 | } |
@@ -141,8 +141,8 @@ discard block |
||
141 | 141 | |
142 | 142 | // Generate the report. |
143 | 143 | return array( |
144 | - 'before' => gmdate( 'Y-m-d' ), |
|
145 | - 'after' => gmdate( 'Y-m-d' ), |
|
144 | + 'before' => gmdate('Y-m-d'), |
|
145 | + 'after' => gmdate('Y-m-d'), |
|
146 | 146 | ); |
147 | 147 | |
148 | 148 | } |
@@ -157,14 +157,14 @@ discard block |
||
157 | 157 | // Set the previous date range. |
158 | 158 | $this->previous_range = array( |
159 | 159 | 'period' => 'custom', |
160 | - 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | - 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
160 | + 'before' => gmdate('Y-m-d', strtotime('-2 days')), |
|
161 | + 'after' => gmdate('Y-m-d', strtotime('-2 days')), |
|
162 | 162 | ); |
163 | 163 | |
164 | 164 | // Generate the report. |
165 | 165 | return array( |
166 | - 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | - 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
166 | + 'before' => gmdate('Y-m-d', strtotime('-1 day')), |
|
167 | + 'after' => gmdate('Y-m-d', strtotime('-1 day')), |
|
168 | 168 | ); |
169 | 169 | |
170 | 170 | } |
@@ -182,10 +182,10 @@ discard block |
||
182 | 182 | ); |
183 | 183 | |
184 | 184 | // Generate the report. |
185 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
185 | + $week_starts = absint(get_option('start_of_week')); |
|
186 | 186 | return array( |
187 | - 'before' => gmdate( 'Y-m-d' ), |
|
188 | - 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
187 | + 'before' => gmdate('Y-m-d'), |
|
188 | + 'after' => gmdate('Y-m-d', strtotime('next Sunday -' . (7 - $week_starts) . ' days')), |
|
189 | 189 | ); |
190 | 190 | } |
191 | 191 | |
@@ -196,19 +196,19 @@ discard block |
||
196 | 196 | */ |
197 | 197 | public function get_last_week_date_range() { |
198 | 198 | |
199 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | - $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
199 | + $week_starts = absint(get_option('start_of_week')); |
|
200 | + $week_starts = strtotime('last Sunday -' . (7 - $week_starts) . ' days'); |
|
201 | 201 | $date_range = array( |
202 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
202 | + 'before' => gmdate('Y-m-d', $week_starts + 6 * DAY_IN_SECONDS), |
|
203 | + 'after' => gmdate('Y-m-d', $week_starts), |
|
204 | 204 | ); |
205 | 205 | |
206 | 206 | // Set the previous date range. |
207 | 207 | $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
208 | 208 | $this->previous_range = array( |
209 | 209 | 'period' => 'custom', |
210 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
210 | + 'before' => gmdate('Y-m-d', $week_starts + 6 * DAY_IN_SECONDS), |
|
211 | + 'after' => gmdate('Y-m-d', $week_starts), |
|
212 | 212 | ); |
213 | 213 | |
214 | 214 | // Generate the report. |
@@ -220,13 +220,13 @@ discard block |
||
220 | 220 | * |
221 | 221 | * @return array The appropriate date range. |
222 | 222 | */ |
223 | - public function get_x_days_date_range( $days = 7 ) { |
|
223 | + public function get_x_days_date_range($days = 7) { |
|
224 | 224 | |
225 | 225 | $days--; |
226 | 226 | |
227 | - $date_range = array( |
|
228 | - 'before' => gmdate( 'Y-m-d' ), |
|
229 | - 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
227 | + $date_range = array( |
|
228 | + 'before' => gmdate('Y-m-d'), |
|
229 | + 'after' => gmdate('Y-m-d', strtotime("-$days days")), |
|
230 | 230 | ); |
231 | 231 | |
232 | 232 | $days++; |
@@ -234,8 +234,8 @@ discard block |
||
234 | 234 | // Set the previous date range. |
235 | 235 | $this->previous_range = array( |
236 | 236 | 'period' => 'custom', |
237 | - 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | - 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
237 | + 'before' => gmdate('Y-m-d', strtotime($date_range['before']) - $days * DAY_IN_SECONDS), |
|
238 | + 'after' => gmdate('Y-m-d', strtotime($date_range['after']) - $days * DAY_IN_SECONDS), |
|
239 | 239 | ); |
240 | 240 | |
241 | 241 | // Generate the report. |
@@ -256,8 +256,8 @@ discard block |
||
256 | 256 | |
257 | 257 | // Generate the report. |
258 | 258 | return array( |
259 | - 'after' => gmdate( 'Y-m-01' ), |
|
260 | - 'before' => gmdate( 'Y-m-t' ), |
|
259 | + 'after' => gmdate('Y-m-01'), |
|
260 | + 'before' => gmdate('Y-m-t'), |
|
261 | 261 | ); |
262 | 262 | |
263 | 263 | } |
@@ -272,14 +272,14 @@ discard block |
||
272 | 272 | // Set the previous date range. |
273 | 273 | $this->previous_range = array( |
274 | 274 | 'period' => 'custom', |
275 | - 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | - 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
275 | + 'after' => gmdate('Y-m-01', strtotime('-2 months')), |
|
276 | + 'before' => gmdate('Y-m-t', strtotime('-2 months')), |
|
277 | 277 | ); |
278 | 278 | |
279 | 279 | // Generate the report. |
280 | 280 | return array( |
281 | - 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | - 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
281 | + 'after' => gmdate('Y-m-01', strtotime('last month')), |
|
282 | + 'before' => gmdate('Y-m-t', strtotime('last month')), |
|
283 | 283 | ); |
284 | 284 | |
285 | 285 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | */ |
292 | 292 | public function get_quarters() { |
293 | 293 | |
294 | - $year = (int) gmdate( 'Y' ); |
|
294 | + $year = (int) gmdate('Y'); |
|
295 | 295 | $last_year = (int) $year - 1; |
296 | 296 | return array( |
297 | 297 | |
@@ -340,9 +340,9 @@ discard block |
||
340 | 340 | */ |
341 | 341 | public function get_quarter() { |
342 | 342 | |
343 | - $month = (int) gmdate( 'n' ); |
|
344 | - $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | - return $quarters[ $month - 1 ]; |
|
343 | + $month = (int) gmdate('n'); |
|
344 | + $quarters = array(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4); |
|
345 | + return $quarters[$month - 1]; |
|
346 | 346 | |
347 | 347 | } |
348 | 348 | |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | // Generate the report. |
362 | 362 | $quarter = $this->get_quarter(); |
363 | 363 | $quarters = $this->get_quarters(); |
364 | - return $quarters[ $quarter + 1 ]; |
|
364 | + return $quarters[$quarter + 1]; |
|
365 | 365 | |
366 | 366 | } |
367 | 367 | |
@@ -377,12 +377,12 @@ discard block |
||
377 | 377 | |
378 | 378 | // Set the previous date range. |
379 | 379 | $this->previous_range = array_merge( |
380 | - $quarters[ $quarter - 1 ], |
|
381 | - array( 'period' => 'custom' ) |
|
380 | + $quarters[$quarter - 1], |
|
381 | + array('period' => 'custom') |
|
382 | 382 | ); |
383 | 383 | |
384 | 384 | // Generate the report. |
385 | - return $quarters[ $quarter ]; |
|
385 | + return $quarters[$quarter]; |
|
386 | 386 | |
387 | 387 | } |
388 | 388 | |
@@ -400,8 +400,8 @@ discard block |
||
400 | 400 | |
401 | 401 | // Generate the report. |
402 | 402 | return array( |
403 | - 'after' => gmdate( 'Y-01-01' ), |
|
404 | - 'before' => gmdate( 'Y-12-31' ), |
|
403 | + 'after' => gmdate('Y-01-01'), |
|
404 | + 'before' => gmdate('Y-12-31'), |
|
405 | 405 | ); |
406 | 406 | |
407 | 407 | } |
@@ -416,14 +416,14 @@ discard block |
||
416 | 416 | // Set the previous date range. |
417 | 417 | $this->previous_range = array( |
418 | 418 | 'period' => 'custom', |
419 | - 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | - 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
419 | + 'after' => gmdate('Y-01-01', strtotime('-2 years')), |
|
420 | + 'before' => gmdate('Y-12-31', strtotime('-2 years')), |
|
421 | 421 | ); |
422 | 422 | |
423 | 423 | // Generate the report. |
424 | 424 | return array( |
425 | - 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | - 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
425 | + 'after' => gmdate('Y-01-01', strtotime('last year')), |
|
426 | + 'before' => gmdate('Y-12-31', strtotime('last year')), |
|
427 | 427 | ); |
428 | 428 | |
429 | 429 | } |
@@ -435,20 +435,20 @@ discard block |
||
435 | 435 | * @param string $date_field The date field. |
436 | 436 | * @return string The appropriate SQL. |
437 | 437 | */ |
438 | - public function get_date_range_sql( $request, $date_field ) { |
|
438 | + public function get_date_range_sql($request, $date_field) { |
|
439 | 439 | global $wpdb; |
440 | 440 | |
441 | 441 | $sql = '1=1'; |
442 | - $range = $this->get_date_range( $request ); |
|
442 | + $range = $this->get_date_range($request); |
|
443 | 443 | |
444 | - if ( ! empty( $range['after'] ) ) { |
|
444 | + if (!empty($range['after'])) { |
|
445 | 445 | $sql .= ' AND ' . $wpdb->prepare( |
446 | 446 | "$date_field >= %s", |
447 | 447 | $range['after'] |
448 | 448 | ); |
449 | 449 | } |
450 | 450 | |
451 | - if ( ! empty( $range['before'] ) ) { |
|
451 | + if (!empty($range['before'])) { |
|
452 | 452 | $sql .= ' AND ' . $wpdb->prepare( |
453 | 453 | "$date_field <= %s", |
454 | 454 | $range['before'] |
@@ -465,9 +465,9 @@ discard block |
||
465 | 465 | * @param string $date_field The date field. |
466 | 466 | * @return string The appropriate SQL. |
467 | 467 | */ |
468 | - public function get_group_by_sql( $date_field ) { |
|
468 | + public function get_group_by_sql($date_field) { |
|
469 | 469 | |
470 | - if ( 'day' === $this->groupby ) { |
|
470 | + if ('day' === $this->groupby) { |
|
471 | 471 | return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
472 | 472 | } |
473 | 473 | |
@@ -481,30 +481,30 @@ discard block |
||
481 | 481 | */ |
482 | 482 | public function get_collection_params() { |
483 | 483 | return array( |
484 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
484 | + 'context' => $this->get_context_param(array('default' => 'view')), |
|
485 | 485 | 'period' => array( |
486 | - 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
486 | + 'description' => __('Limit to results of a specific period.', 'invoicing'), |
|
487 | 487 | 'type' => 'string', |
488 | - 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
488 | + 'enum' => array('custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter'), |
|
489 | 489 | 'validate_callback' => 'rest_validate_request_arg', |
490 | 490 | 'sanitize_callback' => 'sanitize_text_field', |
491 | 491 | 'default' => '7_days', |
492 | 492 | ), |
493 | 493 | 'after' => array( |
494 | 494 | /* translators: %s: date format */ |
495 | - 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
495 | + 'description' => sprintf(__('Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing'), 'YYYY-MM-DD'), |
|
496 | 496 | 'type' => 'string', |
497 | 497 | 'validate_callback' => 'rest_validate_request_arg', |
498 | 498 | 'sanitize_callback' => 'sanitize_text_field', |
499 | - 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
499 | + 'default' => gmdate('Y-m-d', strtotime('-7 days')), |
|
500 | 500 | ), |
501 | 501 | 'before' => array( |
502 | 502 | /* translators: %s: date format */ |
503 | - 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
503 | + 'description' => sprintf(__('Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing'), 'YYYY-MM-DD'), |
|
504 | 504 | 'type' => 'string', |
505 | 505 | 'validate_callback' => 'rest_validate_request_arg', |
506 | 506 | 'sanitize_callback' => 'sanitize_text_field', |
507 | - 'default' => gmdate( 'Y-m-d' ), |
|
507 | + 'default' => gmdate('Y-m-d'), |
|
508 | 508 | ), |
509 | 509 | ); |
510 | 510 | } |
@@ -16,496 +16,496 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_REST_Date_Based_Controller extends GetPaid_REST_Controller { |
18 | 18 | |
19 | - /** |
|
20 | - * Group response items by day or month. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $groupby = 'day'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns an array with arguments to request the previous report. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - public $previous_range = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * The period interval. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public $interval; |
|
39 | - |
|
40 | - /** |
|
41 | - * Retrieves the before and after dates. |
|
42 | - * |
|
43 | - * @param WP_REST_Request $request Request object. |
|
44 | - * @return array The appropriate date range. |
|
45 | - */ |
|
46 | - public function get_date_range( $request ) { |
|
47 | - |
|
48 | - // Check if the period is x_days. |
|
49 | - if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | - $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | - } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | - $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | - } else { |
|
54 | - $request['period'] = '7_days'; |
|
55 | - $date_range = $this->get_x_days_date_range(); |
|
56 | - } |
|
57 | - |
|
58 | - // 3 months max for day view. |
|
59 | - $before = strtotime( $date_range['before'] ); |
|
60 | - $after = strtotime( $date_range['after'] ); |
|
61 | - if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | - $this->groupby = 'month'; |
|
63 | - } |
|
64 | - |
|
65 | - $this->prepare_interval( $date_range ); |
|
66 | - |
|
67 | - return $date_range; |
|
68 | - |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Groups by month or days. |
|
73 | - * |
|
74 | - * @param array $range Date range. |
|
75 | - * @return array The appropriate date range. |
|
76 | - */ |
|
77 | - public function prepare_interval( $range ) { |
|
78 | - |
|
79 | - $before = strtotime( $range['before'] ); |
|
80 | - $after = strtotime( $range['after'] ); |
|
81 | - if ( 'day' === $this->groupby ) { |
|
82 | - $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | - $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | - return; |
|
85 | - } |
|
86 | - |
|
87 | - $this->interval = 0; |
|
88 | - $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | - |
|
90 | - while ( $min_date <= $before ) { |
|
91 | - $this->interval ++; |
|
92 | - $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | - } |
|
94 | - |
|
95 | - $this->interval = max( 1, $this->interval ); |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Retrieves a custom date range. |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Request object. |
|
103 | - * @return array The appropriate date range. |
|
104 | - */ |
|
105 | - public function get_custom_date_range( $request ) { |
|
106 | - |
|
107 | - $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | - $before = gmdate( 'Y-m-d' ); |
|
19 | + /** |
|
20 | + * Group response items by day or month. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $groupby = 'day'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns an array with arguments to request the previous report. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + public $previous_range = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * The period interval. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public $interval; |
|
39 | + |
|
40 | + /** |
|
41 | + * Retrieves the before and after dates. |
|
42 | + * |
|
43 | + * @param WP_REST_Request $request Request object. |
|
44 | + * @return array The appropriate date range. |
|
45 | + */ |
|
46 | + public function get_date_range( $request ) { |
|
47 | + |
|
48 | + // Check if the period is x_days. |
|
49 | + if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | + $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | + } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | + $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | + } else { |
|
54 | + $request['period'] = '7_days'; |
|
55 | + $date_range = $this->get_x_days_date_range(); |
|
56 | + } |
|
57 | + |
|
58 | + // 3 months max for day view. |
|
59 | + $before = strtotime( $date_range['before'] ); |
|
60 | + $after = strtotime( $date_range['after'] ); |
|
61 | + if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | + $this->groupby = 'month'; |
|
63 | + } |
|
64 | + |
|
65 | + $this->prepare_interval( $date_range ); |
|
66 | + |
|
67 | + return $date_range; |
|
68 | + |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Groups by month or days. |
|
73 | + * |
|
74 | + * @param array $range Date range. |
|
75 | + * @return array The appropriate date range. |
|
76 | + */ |
|
77 | + public function prepare_interval( $range ) { |
|
78 | + |
|
79 | + $before = strtotime( $range['before'] ); |
|
80 | + $after = strtotime( $range['after'] ); |
|
81 | + if ( 'day' === $this->groupby ) { |
|
82 | + $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | + $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | + return; |
|
85 | + } |
|
86 | + |
|
87 | + $this->interval = 0; |
|
88 | + $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | + |
|
90 | + while ( $min_date <= $before ) { |
|
91 | + $this->interval ++; |
|
92 | + $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | + } |
|
94 | + |
|
95 | + $this->interval = max( 1, $this->interval ); |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Retrieves a custom date range. |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Request object. |
|
103 | + * @return array The appropriate date range. |
|
104 | + */ |
|
105 | + public function get_custom_date_range( $request ) { |
|
106 | + |
|
107 | + $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | + $before = gmdate( 'Y-m-d' ); |
|
109 | 109 | |
110 | - if ( ! empty( $request['before'] ) ) { |
|
111 | - $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | - } |
|
113 | - |
|
114 | - // Set the previous date range. |
|
115 | - $difference = $before - $after; |
|
116 | - $this->previous_range = array( |
|
117 | - 'period' => 'custom', |
|
118 | - 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | - 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | - ); |
|
121 | - |
|
122 | - // Generate the report. |
|
123 | - return array( |
|
124 | - 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | - 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | - ); |
|
127 | - |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Retrieves todays date range. |
|
132 | - * |
|
133 | - * @return array The appropriate date range. |
|
134 | - */ |
|
135 | - public function get_today_date_range() { |
|
136 | - |
|
137 | - // Set the previous date range. |
|
138 | - $this->previous_range = array( |
|
139 | - 'period' => 'yesterday', |
|
140 | - ); |
|
141 | - |
|
142 | - // Generate the report. |
|
143 | - return array( |
|
144 | - 'before' => gmdate( 'Y-m-d' ), |
|
145 | - 'after' => gmdate( 'Y-m-d' ), |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Retrieves yesterdays date range. |
|
152 | - * |
|
153 | - * @return array The appropriate date range. |
|
154 | - */ |
|
155 | - public function get_yesterday_date_range() { |
|
156 | - |
|
157 | - // Set the previous date range. |
|
158 | - $this->previous_range = array( |
|
159 | - 'period' => 'custom', |
|
160 | - 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | - 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | - ); |
|
163 | - |
|
164 | - // Generate the report. |
|
165 | - return array( |
|
166 | - 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | - 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | - ); |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Retrieves this week's date range. |
|
174 | - * |
|
175 | - * @return array The appropriate date range. |
|
176 | - */ |
|
177 | - public function get_week_date_range() { |
|
178 | - |
|
179 | - // Set the previous date range. |
|
180 | - $this->previous_range = array( |
|
181 | - 'period' => 'last_week', |
|
182 | - ); |
|
183 | - |
|
184 | - // Generate the report. |
|
185 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | - return array( |
|
187 | - 'before' => gmdate( 'Y-m-d' ), |
|
188 | - 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | - ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Retrieves last week's date range. |
|
194 | - * |
|
195 | - * @return array The appropriate date range. |
|
196 | - */ |
|
197 | - public function get_last_week_date_range() { |
|
198 | - |
|
199 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | - $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | - $date_range = array( |
|
202 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | - ); |
|
205 | - |
|
206 | - // Set the previous date range. |
|
207 | - $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | - $this->previous_range = array( |
|
209 | - 'period' => 'custom', |
|
210 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | - ); |
|
213 | - |
|
214 | - // Generate the report. |
|
215 | - return $date_range; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Retrieves last x days date range. |
|
220 | - * |
|
221 | - * @return array The appropriate date range. |
|
222 | - */ |
|
223 | - public function get_x_days_date_range( $days = 7 ) { |
|
224 | - |
|
225 | - $days--; |
|
226 | - |
|
227 | - $date_range = array( |
|
228 | - 'before' => gmdate( 'Y-m-d' ), |
|
229 | - 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | - ); |
|
231 | - |
|
232 | - $days++; |
|
233 | - |
|
234 | - // Set the previous date range. |
|
235 | - $this->previous_range = array( |
|
236 | - 'period' => 'custom', |
|
237 | - 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | - 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | - ); |
|
240 | - |
|
241 | - // Generate the report. |
|
242 | - return $date_range; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Retrieves this month date range. |
|
247 | - * |
|
248 | - * @return array The appropriate date range. |
|
249 | - */ |
|
250 | - public function get_month_date_range() { |
|
251 | - |
|
252 | - // Set the previous date range. |
|
253 | - $this->previous_range = array( |
|
254 | - 'period' => 'last_month', |
|
255 | - ); |
|
256 | - |
|
257 | - // Generate the report. |
|
258 | - return array( |
|
259 | - 'after' => gmdate( 'Y-m-01' ), |
|
260 | - 'before' => gmdate( 'Y-m-t' ), |
|
261 | - ); |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Retrieves last month's date range. |
|
267 | - * |
|
268 | - * @return array The appropriate date range. |
|
269 | - */ |
|
270 | - public function get_last_month_date_range() { |
|
271 | - |
|
272 | - // Set the previous date range. |
|
273 | - $this->previous_range = array( |
|
274 | - 'period' => 'custom', |
|
275 | - 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | - 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | - ); |
|
278 | - |
|
279 | - // Generate the report. |
|
280 | - return array( |
|
281 | - 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | - 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | - ); |
|
284 | - |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Retrieves this quarter date range. |
|
289 | - * |
|
290 | - * @return array The available quarters. |
|
291 | - */ |
|
292 | - public function get_quarters() { |
|
293 | - |
|
294 | - $year = (int) gmdate( 'Y' ); |
|
295 | - $last_year = (int) $year - 1; |
|
296 | - return array( |
|
297 | - |
|
298 | - // Third quarter of previous year: July 1st to September 30th |
|
299 | - array( |
|
300 | - 'before' => "{$last_year}-09-30", |
|
301 | - 'after' => "{$last_year}-07-01", |
|
302 | - ), |
|
303 | - |
|
304 | - // Last quarter of previous year: October 1st to December 31st |
|
305 | - array( |
|
306 | - 'before' => "{$last_year}-12-31", |
|
307 | - 'after' => "{$last_year}-10-01", |
|
308 | - ), |
|
309 | - |
|
310 | - // First quarter: January 1st to March 31st |
|
311 | - array( |
|
312 | - 'before' => "{$year}-03-31", |
|
313 | - 'after' => "{$year}-01-01", |
|
314 | - ), |
|
315 | - |
|
316 | - // Second quarter: April 1st to June 30th |
|
317 | - array( |
|
318 | - 'before' => "{$year}-06-30", |
|
319 | - 'after' => "{$year}-04-01", |
|
320 | - ), |
|
321 | - |
|
322 | - // Third quarter: July 1st to September 30th |
|
323 | - array( |
|
324 | - 'before' => "{$year}-09-30", |
|
325 | - 'after' => "{$year}-07-01", |
|
326 | - ), |
|
327 | - |
|
328 | - // Fourth quarter: October 1st to December 31st |
|
329 | - array( |
|
330 | - 'before' => "{$year}-12-31", |
|
331 | - 'after' => "{$year}-10-01", |
|
332 | - ), |
|
333 | - ); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Retrieves the current quater. |
|
338 | - * |
|
339 | - * @return int The current quarter. |
|
340 | - */ |
|
341 | - public function get_quarter() { |
|
342 | - |
|
343 | - $month = (int) gmdate( 'n' ); |
|
344 | - $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | - return $quarters[ $month - 1 ]; |
|
346 | - |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Retrieves this quarter date range. |
|
351 | - * |
|
352 | - * @return array The appropriate date range. |
|
353 | - */ |
|
354 | - public function get_quarter_date_range() { |
|
355 | - |
|
356 | - // Set the previous date range. |
|
357 | - $this->previous_range = array( |
|
358 | - 'period' => 'last_quarter', |
|
359 | - ); |
|
360 | - |
|
361 | - // Generate the report. |
|
362 | - $quarter = $this->get_quarter(); |
|
363 | - $quarters = $this->get_quarters(); |
|
364 | - return $quarters[ $quarter + 1 ]; |
|
365 | - |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Retrieves last quarter's date range. |
|
370 | - * |
|
371 | - * @return array The appropriate date range. |
|
372 | - */ |
|
373 | - public function get_last_quarter_date_range() { |
|
374 | - |
|
375 | - $quarters = $this->get_quarters(); |
|
376 | - $quarter = $this->get_quarter(); |
|
377 | - |
|
378 | - // Set the previous date range. |
|
379 | - $this->previous_range = array_merge( |
|
380 | - $quarters[ $quarter - 1 ], |
|
381 | - array( 'period' => 'custom' ) |
|
382 | - ); |
|
383 | - |
|
384 | - // Generate the report. |
|
385 | - return $quarters[ $quarter ]; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Retrieves this year date range. |
|
391 | - * |
|
392 | - * @return array The appropriate date range. |
|
393 | - */ |
|
394 | - public function get_year_date_range() { |
|
395 | - |
|
396 | - // Set the previous date range. |
|
397 | - $this->previous_range = array( |
|
398 | - 'period' => 'last_year', |
|
399 | - ); |
|
400 | - |
|
401 | - // Generate the report. |
|
402 | - return array( |
|
403 | - 'after' => gmdate( 'Y-01-01' ), |
|
404 | - 'before' => gmdate( 'Y-12-31' ), |
|
405 | - ); |
|
406 | - |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Retrieves last year date range. |
|
411 | - * |
|
412 | - * @return array The appropriate date range. |
|
413 | - */ |
|
414 | - public function get_last_year_date_range() { |
|
415 | - |
|
416 | - // Set the previous date range. |
|
417 | - $this->previous_range = array( |
|
418 | - 'period' => 'custom', |
|
419 | - 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | - 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | - ); |
|
422 | - |
|
423 | - // Generate the report. |
|
424 | - return array( |
|
425 | - 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | - 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | - ); |
|
428 | - |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Prepare a the request date for SQL usage. |
|
433 | - * |
|
434 | - * @param WP_REST_Request $request Request object. |
|
435 | - * @param string $date_field The date field. |
|
436 | - * @return string The appropriate SQL. |
|
437 | - */ |
|
438 | - public function get_date_range_sql( $request, $date_field ) { |
|
439 | - global $wpdb; |
|
440 | - |
|
441 | - $sql = '1=1'; |
|
442 | - $range = $this->get_date_range( $request ); |
|
443 | - |
|
444 | - if ( ! empty( $range['after'] ) ) { |
|
445 | - $sql .= ' AND ' . $wpdb->prepare( |
|
446 | - "$date_field >= %s", |
|
447 | - $range['after'] |
|
448 | - ); |
|
449 | - } |
|
450 | - |
|
451 | - if ( ! empty( $range['before'] ) ) { |
|
452 | - $sql .= ' AND ' . $wpdb->prepare( |
|
453 | - "$date_field <= %s", |
|
454 | - $range['before'] |
|
455 | - ); |
|
456 | - } |
|
457 | - |
|
458 | - return $sql; |
|
459 | - |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Prepares a group by query. |
|
464 | - * |
|
465 | - * @param string $date_field The date field. |
|
466 | - * @return string The appropriate SQL. |
|
467 | - */ |
|
468 | - public function get_group_by_sql( $date_field ) { |
|
469 | - |
|
470 | - if ( 'day' === $this->groupby ) { |
|
471 | - return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | - } |
|
473 | - |
|
474 | - return "YEAR($date_field), MONTH($date_field)"; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * Get the query params for collections. |
|
479 | - * |
|
480 | - * @return array |
|
481 | - */ |
|
482 | - public function get_collection_params() { |
|
483 | - return array( |
|
484 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | - 'period' => array( |
|
486 | - 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | - 'type' => 'string', |
|
488 | - 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | - 'validate_callback' => 'rest_validate_request_arg', |
|
490 | - 'sanitize_callback' => 'sanitize_text_field', |
|
491 | - 'default' => '7_days', |
|
492 | - ), |
|
493 | - 'after' => array( |
|
494 | - /* translators: %s: date format */ |
|
495 | - 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | - 'type' => 'string', |
|
497 | - 'validate_callback' => 'rest_validate_request_arg', |
|
498 | - 'sanitize_callback' => 'sanitize_text_field', |
|
499 | - 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | - ), |
|
501 | - 'before' => array( |
|
502 | - /* translators: %s: date format */ |
|
503 | - 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | - 'type' => 'string', |
|
505 | - 'validate_callback' => 'rest_validate_request_arg', |
|
506 | - 'sanitize_callback' => 'sanitize_text_field', |
|
507 | - 'default' => gmdate( 'Y-m-d' ), |
|
508 | - ), |
|
509 | - ); |
|
510 | - } |
|
110 | + if ( ! empty( $request['before'] ) ) { |
|
111 | + $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | + } |
|
113 | + |
|
114 | + // Set the previous date range. |
|
115 | + $difference = $before - $after; |
|
116 | + $this->previous_range = array( |
|
117 | + 'period' => 'custom', |
|
118 | + 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | + 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | + ); |
|
121 | + |
|
122 | + // Generate the report. |
|
123 | + return array( |
|
124 | + 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | + 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | + ); |
|
127 | + |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Retrieves todays date range. |
|
132 | + * |
|
133 | + * @return array The appropriate date range. |
|
134 | + */ |
|
135 | + public function get_today_date_range() { |
|
136 | + |
|
137 | + // Set the previous date range. |
|
138 | + $this->previous_range = array( |
|
139 | + 'period' => 'yesterday', |
|
140 | + ); |
|
141 | + |
|
142 | + // Generate the report. |
|
143 | + return array( |
|
144 | + 'before' => gmdate( 'Y-m-d' ), |
|
145 | + 'after' => gmdate( 'Y-m-d' ), |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Retrieves yesterdays date range. |
|
152 | + * |
|
153 | + * @return array The appropriate date range. |
|
154 | + */ |
|
155 | + public function get_yesterday_date_range() { |
|
156 | + |
|
157 | + // Set the previous date range. |
|
158 | + $this->previous_range = array( |
|
159 | + 'period' => 'custom', |
|
160 | + 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | + 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | + ); |
|
163 | + |
|
164 | + // Generate the report. |
|
165 | + return array( |
|
166 | + 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | + 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | + ); |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Retrieves this week's date range. |
|
174 | + * |
|
175 | + * @return array The appropriate date range. |
|
176 | + */ |
|
177 | + public function get_week_date_range() { |
|
178 | + |
|
179 | + // Set the previous date range. |
|
180 | + $this->previous_range = array( |
|
181 | + 'period' => 'last_week', |
|
182 | + ); |
|
183 | + |
|
184 | + // Generate the report. |
|
185 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | + return array( |
|
187 | + 'before' => gmdate( 'Y-m-d' ), |
|
188 | + 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | + ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Retrieves last week's date range. |
|
194 | + * |
|
195 | + * @return array The appropriate date range. |
|
196 | + */ |
|
197 | + public function get_last_week_date_range() { |
|
198 | + |
|
199 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | + $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | + $date_range = array( |
|
202 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | + ); |
|
205 | + |
|
206 | + // Set the previous date range. |
|
207 | + $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | + $this->previous_range = array( |
|
209 | + 'period' => 'custom', |
|
210 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | + ); |
|
213 | + |
|
214 | + // Generate the report. |
|
215 | + return $date_range; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Retrieves last x days date range. |
|
220 | + * |
|
221 | + * @return array The appropriate date range. |
|
222 | + */ |
|
223 | + public function get_x_days_date_range( $days = 7 ) { |
|
224 | + |
|
225 | + $days--; |
|
226 | + |
|
227 | + $date_range = array( |
|
228 | + 'before' => gmdate( 'Y-m-d' ), |
|
229 | + 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | + ); |
|
231 | + |
|
232 | + $days++; |
|
233 | + |
|
234 | + // Set the previous date range. |
|
235 | + $this->previous_range = array( |
|
236 | + 'period' => 'custom', |
|
237 | + 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | + 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | + ); |
|
240 | + |
|
241 | + // Generate the report. |
|
242 | + return $date_range; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Retrieves this month date range. |
|
247 | + * |
|
248 | + * @return array The appropriate date range. |
|
249 | + */ |
|
250 | + public function get_month_date_range() { |
|
251 | + |
|
252 | + // Set the previous date range. |
|
253 | + $this->previous_range = array( |
|
254 | + 'period' => 'last_month', |
|
255 | + ); |
|
256 | + |
|
257 | + // Generate the report. |
|
258 | + return array( |
|
259 | + 'after' => gmdate( 'Y-m-01' ), |
|
260 | + 'before' => gmdate( 'Y-m-t' ), |
|
261 | + ); |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Retrieves last month's date range. |
|
267 | + * |
|
268 | + * @return array The appropriate date range. |
|
269 | + */ |
|
270 | + public function get_last_month_date_range() { |
|
271 | + |
|
272 | + // Set the previous date range. |
|
273 | + $this->previous_range = array( |
|
274 | + 'period' => 'custom', |
|
275 | + 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | + 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | + ); |
|
278 | + |
|
279 | + // Generate the report. |
|
280 | + return array( |
|
281 | + 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | + 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | + ); |
|
284 | + |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Retrieves this quarter date range. |
|
289 | + * |
|
290 | + * @return array The available quarters. |
|
291 | + */ |
|
292 | + public function get_quarters() { |
|
293 | + |
|
294 | + $year = (int) gmdate( 'Y' ); |
|
295 | + $last_year = (int) $year - 1; |
|
296 | + return array( |
|
297 | + |
|
298 | + // Third quarter of previous year: July 1st to September 30th |
|
299 | + array( |
|
300 | + 'before' => "{$last_year}-09-30", |
|
301 | + 'after' => "{$last_year}-07-01", |
|
302 | + ), |
|
303 | + |
|
304 | + // Last quarter of previous year: October 1st to December 31st |
|
305 | + array( |
|
306 | + 'before' => "{$last_year}-12-31", |
|
307 | + 'after' => "{$last_year}-10-01", |
|
308 | + ), |
|
309 | + |
|
310 | + // First quarter: January 1st to March 31st |
|
311 | + array( |
|
312 | + 'before' => "{$year}-03-31", |
|
313 | + 'after' => "{$year}-01-01", |
|
314 | + ), |
|
315 | + |
|
316 | + // Second quarter: April 1st to June 30th |
|
317 | + array( |
|
318 | + 'before' => "{$year}-06-30", |
|
319 | + 'after' => "{$year}-04-01", |
|
320 | + ), |
|
321 | + |
|
322 | + // Third quarter: July 1st to September 30th |
|
323 | + array( |
|
324 | + 'before' => "{$year}-09-30", |
|
325 | + 'after' => "{$year}-07-01", |
|
326 | + ), |
|
327 | + |
|
328 | + // Fourth quarter: October 1st to December 31st |
|
329 | + array( |
|
330 | + 'before' => "{$year}-12-31", |
|
331 | + 'after' => "{$year}-10-01", |
|
332 | + ), |
|
333 | + ); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Retrieves the current quater. |
|
338 | + * |
|
339 | + * @return int The current quarter. |
|
340 | + */ |
|
341 | + public function get_quarter() { |
|
342 | + |
|
343 | + $month = (int) gmdate( 'n' ); |
|
344 | + $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | + return $quarters[ $month - 1 ]; |
|
346 | + |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Retrieves this quarter date range. |
|
351 | + * |
|
352 | + * @return array The appropriate date range. |
|
353 | + */ |
|
354 | + public function get_quarter_date_range() { |
|
355 | + |
|
356 | + // Set the previous date range. |
|
357 | + $this->previous_range = array( |
|
358 | + 'period' => 'last_quarter', |
|
359 | + ); |
|
360 | + |
|
361 | + // Generate the report. |
|
362 | + $quarter = $this->get_quarter(); |
|
363 | + $quarters = $this->get_quarters(); |
|
364 | + return $quarters[ $quarter + 1 ]; |
|
365 | + |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Retrieves last quarter's date range. |
|
370 | + * |
|
371 | + * @return array The appropriate date range. |
|
372 | + */ |
|
373 | + public function get_last_quarter_date_range() { |
|
374 | + |
|
375 | + $quarters = $this->get_quarters(); |
|
376 | + $quarter = $this->get_quarter(); |
|
377 | + |
|
378 | + // Set the previous date range. |
|
379 | + $this->previous_range = array_merge( |
|
380 | + $quarters[ $quarter - 1 ], |
|
381 | + array( 'period' => 'custom' ) |
|
382 | + ); |
|
383 | + |
|
384 | + // Generate the report. |
|
385 | + return $quarters[ $quarter ]; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Retrieves this year date range. |
|
391 | + * |
|
392 | + * @return array The appropriate date range. |
|
393 | + */ |
|
394 | + public function get_year_date_range() { |
|
395 | + |
|
396 | + // Set the previous date range. |
|
397 | + $this->previous_range = array( |
|
398 | + 'period' => 'last_year', |
|
399 | + ); |
|
400 | + |
|
401 | + // Generate the report. |
|
402 | + return array( |
|
403 | + 'after' => gmdate( 'Y-01-01' ), |
|
404 | + 'before' => gmdate( 'Y-12-31' ), |
|
405 | + ); |
|
406 | + |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Retrieves last year date range. |
|
411 | + * |
|
412 | + * @return array The appropriate date range. |
|
413 | + */ |
|
414 | + public function get_last_year_date_range() { |
|
415 | + |
|
416 | + // Set the previous date range. |
|
417 | + $this->previous_range = array( |
|
418 | + 'period' => 'custom', |
|
419 | + 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | + 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | + ); |
|
422 | + |
|
423 | + // Generate the report. |
|
424 | + return array( |
|
425 | + 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | + 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | + ); |
|
428 | + |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Prepare a the request date for SQL usage. |
|
433 | + * |
|
434 | + * @param WP_REST_Request $request Request object. |
|
435 | + * @param string $date_field The date field. |
|
436 | + * @return string The appropriate SQL. |
|
437 | + */ |
|
438 | + public function get_date_range_sql( $request, $date_field ) { |
|
439 | + global $wpdb; |
|
440 | + |
|
441 | + $sql = '1=1'; |
|
442 | + $range = $this->get_date_range( $request ); |
|
443 | + |
|
444 | + if ( ! empty( $range['after'] ) ) { |
|
445 | + $sql .= ' AND ' . $wpdb->prepare( |
|
446 | + "$date_field >= %s", |
|
447 | + $range['after'] |
|
448 | + ); |
|
449 | + } |
|
450 | + |
|
451 | + if ( ! empty( $range['before'] ) ) { |
|
452 | + $sql .= ' AND ' . $wpdb->prepare( |
|
453 | + "$date_field <= %s", |
|
454 | + $range['before'] |
|
455 | + ); |
|
456 | + } |
|
457 | + |
|
458 | + return $sql; |
|
459 | + |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Prepares a group by query. |
|
464 | + * |
|
465 | + * @param string $date_field The date field. |
|
466 | + * @return string The appropriate SQL. |
|
467 | + */ |
|
468 | + public function get_group_by_sql( $date_field ) { |
|
469 | + |
|
470 | + if ( 'day' === $this->groupby ) { |
|
471 | + return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | + } |
|
473 | + |
|
474 | + return "YEAR($date_field), MONTH($date_field)"; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * Get the query params for collections. |
|
479 | + * |
|
480 | + * @return array |
|
481 | + */ |
|
482 | + public function get_collection_params() { |
|
483 | + return array( |
|
484 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | + 'period' => array( |
|
486 | + 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | + 'type' => 'string', |
|
488 | + 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | + 'validate_callback' => 'rest_validate_request_arg', |
|
490 | + 'sanitize_callback' => 'sanitize_text_field', |
|
491 | + 'default' => '7_days', |
|
492 | + ), |
|
493 | + 'after' => array( |
|
494 | + /* translators: %s: date format */ |
|
495 | + 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | + 'type' => 'string', |
|
497 | + 'validate_callback' => 'rest_validate_request_arg', |
|
498 | + 'sanitize_callback' => 'sanitize_text_field', |
|
499 | + 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | + ), |
|
501 | + 'before' => array( |
|
502 | + /* translators: %s: date format */ |
|
503 | + 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | + 'type' => 'string', |
|
505 | + 'validate_callback' => 'rest_validate_request_arg', |
|
506 | + 'sanitize_callback' => 'sanitize_text_field', |
|
507 | + 'default' => gmdate( 'Y-m-d' ), |
|
508 | + ), |
|
509 | + ); |
|
510 | + } |
|
511 | 511 | } |
@@ -12,185 +12,185 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Checkout { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Payment_Form_Submission |
|
17 | - */ |
|
18 | - protected $payment_form_submission; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
24 | - */ |
|
25 | - public function __construct( $submission ) { |
|
26 | - $this->payment_form_submission = $submission; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Processes the checkout. |
|
31 | - * |
|
32 | - */ |
|
33 | - public function process_checkout() { |
|
34 | - |
|
35 | - // Validate the submission. |
|
36 | - $this->validate_submission(); |
|
37 | - |
|
38 | - // Prepare the invoice. |
|
39 | - $items = $this->get_submission_items(); |
|
40 | - $invoice = $this->get_submission_invoice(); |
|
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | - |
|
44 | - $this->prepare_billing_info( $invoice ); |
|
45 | - |
|
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | - |
|
48 | - // Save the invoice. |
|
49 | - $invoice->set_is_viewed( true ); |
|
50 | - $invoice->recalculate_total(); |
|
15 | + /** |
|
16 | + * @var GetPaid_Payment_Form_Submission |
|
17 | + */ |
|
18 | + protected $payment_form_submission; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
24 | + */ |
|
25 | + public function __construct( $submission ) { |
|
26 | + $this->payment_form_submission = $submission; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Processes the checkout. |
|
31 | + * |
|
32 | + */ |
|
33 | + public function process_checkout() { |
|
34 | + |
|
35 | + // Validate the submission. |
|
36 | + $this->validate_submission(); |
|
37 | + |
|
38 | + // Prepare the invoice. |
|
39 | + $items = $this->get_submission_items(); |
|
40 | + $invoice = $this->get_submission_invoice(); |
|
41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | + |
|
44 | + $this->prepare_billing_info( $invoice ); |
|
45 | + |
|
46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | + |
|
48 | + // Save the invoice. |
|
49 | + $invoice->set_is_viewed( true ); |
|
50 | + $invoice->recalculate_total(); |
|
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
54 | 54 | |
55 | - // Send to the gateway. |
|
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | - } |
|
55 | + // Send to the gateway. |
|
56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Validates the submission. |
|
61 | - * |
|
62 | - */ |
|
63 | - protected function validate_submission() { |
|
59 | + /** |
|
60 | + * Validates the submission. |
|
61 | + * |
|
62 | + */ |
|
63 | + protected function validate_submission() { |
|
64 | 64 | |
65 | - $submission = $this->payment_form_submission; |
|
66 | - $data = $submission->get_data(); |
|
65 | + $submission = $this->payment_form_submission; |
|
66 | + $data = $submission->get_data(); |
|
67 | 67 | |
68 | - // Do we have an error? |
|
68 | + // Do we have an error? |
|
69 | 69 | if ( ! empty( $submission->last_error ) ) { |
70 | - wp_send_json_error( $submission->last_error ); |
|
70 | + wp_send_json_error( $submission->last_error ); |
|
71 | 71 | } |
72 | 72 | |
73 | - // We need a billing email. |
|
73 | + // We need a billing email. |
|
74 | 74 | if ( ! $submission->has_billing_email() ) { |
75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | - } |
|
78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | + } |
|
82 | 82 | |
83 | - // Ensure the gateway is active. |
|
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | - } |
|
83 | + // Ensure the gateway is active. |
|
84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | + wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | + } |
|
87 | 87 | |
88 | - // Clear any existing errors. |
|
89 | - wpinv_clear_errors(); |
|
88 | + // Clear any existing errors. |
|
89 | + wpinv_clear_errors(); |
|
90 | 90 | |
91 | - // Allow themes and plugins to hook to errors |
|
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
91 | + // Allow themes and plugins to hook to errors |
|
92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
93 | 93 | |
94 | - // Do we have any errors? |
|
94 | + // Do we have any errors? |
|
95 | 95 | if ( wpinv_get_errors() ) { |
96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
97 | - } |
|
97 | + } |
|
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Retrieves submission items. |
|
103 | - * |
|
104 | - * @return GetPaid_Form_Item[] |
|
105 | - */ |
|
106 | - protected function get_submission_items() { |
|
101 | + /** |
|
102 | + * Retrieves submission items. |
|
103 | + * |
|
104 | + * @return GetPaid_Form_Item[] |
|
105 | + */ |
|
106 | + protected function get_submission_items() { |
|
107 | 107 | |
108 | - $items = $this->payment_form_submission->get_items(); |
|
108 | + $items = $this->payment_form_submission->get_items(); |
|
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
113 | - } |
|
114 | - |
|
115 | - return $items; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Retrieves submission invoice. |
|
120 | - * |
|
121 | - * @return WPInv_Invoice |
|
122 | - */ |
|
123 | - protected function get_submission_invoice() { |
|
124 | - $submission = $this->payment_form_submission; |
|
125 | - |
|
126 | - if ( ! $submission->has_invoice() ) { |
|
127 | - $invoice = new WPInv_Invoice(); |
|
128 | - $invoice->set_created_via( 'payment_form' ); |
|
129 | - return $invoice; |
|
130 | 113 | } |
131 | 114 | |
132 | - $invoice = $submission->get_invoice(); |
|
115 | + return $items; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Retrieves submission invoice. |
|
120 | + * |
|
121 | + * @return WPInv_Invoice |
|
122 | + */ |
|
123 | + protected function get_submission_invoice() { |
|
124 | + $submission = $this->payment_form_submission; |
|
133 | 125 | |
134 | - // Make sure that it is neither paid or refunded. |
|
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | - } |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->set_created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
138 | 131 | |
139 | - return $invoice; |
|
140 | - } |
|
132 | + $invoice = $submission->get_invoice(); |
|
141 | 133 | |
142 | - /** |
|
143 | - * Processes the submission invoice. |
|
144 | - * |
|
145 | - * @param WPInv_Invoice $invoice |
|
146 | - * @param GetPaid_Form_Item[] $items |
|
147 | - * @return WPInv_Invoice |
|
148 | - */ |
|
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
134 | + // Make sure that it is neither paid or refunded. |
|
135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | + } |
|
150 | 138 | |
151 | - $submission = $this->payment_form_submission; |
|
139 | + return $invoice; |
|
140 | + } |
|
152 | 141 | |
153 | - // Set-up the invoice details. |
|
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_submission_id( $submission->id ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
142 | + /** |
|
143 | + * Processes the submission invoice. |
|
144 | + * |
|
145 | + * @param WPInv_Invoice $invoice |
|
146 | + * @param GetPaid_Form_Item[] $items |
|
147 | + * @return WPInv_Invoice |
|
148 | + */ |
|
149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
150 | + |
|
151 | + $submission = $this->payment_form_submission; |
|
152 | + |
|
153 | + // Set-up the invoice details. |
|
154 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | + $invoice->set_submission_id( $submission->id ); |
|
157 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | 158 | $invoice->set_items( $items ); |
159 | 159 | $invoice->set_fees( $submission->get_fees() ); |
160 | 160 | $invoice->set_taxes( $submission->get_taxes() ); |
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | - $invoice->set_currency( $submission->get_currency() ); |
|
161 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | + $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | + $invoice->set_currency( $submission->get_currency() ); |
|
164 | 164 | |
165 | - if ( $submission->has_shipping() ) { |
|
166 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | - } |
|
165 | + if ( $submission->has_shipping() ) { |
|
166 | + $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | + } |
|
168 | 168 | |
169 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
169 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
171 | 171 | |
172 | - if ( $submission->has_discount_code() ) { |
|
172 | + if ( $submission->has_discount_code() ) { |
|
173 | 173 | $invoice->set_discount_code( $submission->get_discount_code() ); |
174 | - } |
|
175 | - |
|
176 | - getpaid_maybe_add_default_address( $invoice ); |
|
177 | - return $invoice; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Retrieves the submission's customer. |
|
182 | - * |
|
183 | - * @return int The customer id. |
|
184 | - */ |
|
185 | - protected function get_submission_customer() { |
|
186 | - $submission = $this->payment_form_submission; |
|
187 | - |
|
188 | - // If this is an existing invoice... |
|
189 | - if ( $submission->has_invoice() ) { |
|
190 | - return $submission->get_invoice()->get_user_id(); |
|
191 | - } |
|
192 | - |
|
193 | - // (Maybe) create the user. |
|
174 | + } |
|
175 | + |
|
176 | + getpaid_maybe_add_default_address( $invoice ); |
|
177 | + return $invoice; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Retrieves the submission's customer. |
|
182 | + * |
|
183 | + * @return int The customer id. |
|
184 | + */ |
|
185 | + protected function get_submission_customer() { |
|
186 | + $submission = $this->payment_form_submission; |
|
187 | + |
|
188 | + // If this is an existing invoice... |
|
189 | + if ( $submission->has_invoice() ) { |
|
190 | + return $submission->get_invoice()->get_user_id(); |
|
191 | + } |
|
192 | + |
|
193 | + // (Maybe) create the user. |
|
194 | 194 | $user = get_current_user_id(); |
195 | 195 | |
196 | 196 | if ( empty( $user ) ) { |
@@ -198,16 +198,16 @@ discard block |
||
198 | 198 | } |
199 | 199 | |
200 | 200 | if ( empty( $user ) ) { |
201 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | - $name = implode( '', array_filter( $name ) ); |
|
201 | + $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | + $name = implode( '', array_filter( $name ) ); |
|
203 | 203 | $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
204 | 204 | |
205 | - // (Maybe) send new user notification. |
|
206 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | - wp_send_new_user_notifications( $user, 'user' ); |
|
209 | - } |
|
210 | - } |
|
205 | + // (Maybe) send new user notification. |
|
206 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | + wp_send_new_user_notifications( $user, 'user' ); |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | 212 | if ( is_wp_error( $user ) ) { |
213 | 213 | wp_send_json_error( $user->get_error_message() ); |
@@ -215,49 +215,49 @@ discard block |
||
215 | 215 | |
216 | 216 | if ( is_numeric( $user ) ) { |
217 | 217 | return $user; |
218 | - } |
|
218 | + } |
|
219 | 219 | |
220 | - return $user->ID; |
|
220 | + return $user->ID; |
|
221 | 221 | |
222 | - } |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
224 | + /** |
|
225 | 225 | * Prepares submission data for saving to the database. |
226 | 226 | * |
227 | - * @return array |
|
227 | + * @return array |
|
228 | 228 | */ |
229 | 229 | public function prepare_submission_data_for_saving() { |
230 | 230 | |
231 | - $submission = $this->payment_form_submission; |
|
231 | + $submission = $this->payment_form_submission; |
|
232 | 232 | |
233 | - // Prepared submission details. |
|
233 | + // Prepared submission details. |
|
234 | 234 | $prepared = array( |
235 | - 'all' => array(), |
|
236 | - 'meta' => array(), |
|
237 | - ); |
|
235 | + 'all' => array(), |
|
236 | + 'meta' => array(), |
|
237 | + ); |
|
238 | 238 | |
239 | 239 | // Raw submission details. |
240 | - $data = $submission->get_data(); |
|
240 | + $data = $submission->get_data(); |
|
241 | 241 | |
242 | - // Loop through the submitted details. |
|
242 | + // Loop through the submitted details. |
|
243 | 243 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
244 | 244 | |
245 | - // Skip premade fields. |
|
245 | + // Skip premade fields. |
|
246 | 246 | if ( ! empty( $field['premade'] ) ) { |
247 | 247 | continue; |
248 | 248 | } |
249 | 249 | |
250 | - // Ensure address is provided. |
|
251 | - if ( 'address' === $field['type'] ) { |
|
250 | + // Ensure address is provided. |
|
251 | + if ( 'address' === $field['type'] ) { |
|
252 | 252 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
253 | 253 | |
254 | - foreach ( $field['fields'] as $address_field ) { |
|
254 | + foreach ( $field['fields'] as $address_field ) { |
|
255 | 255 | |
256 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | - } |
|
259 | - } |
|
260 | - } |
|
256 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | + } |
|
259 | + } |
|
260 | + } |
|
261 | 261 | |
262 | 262 | // If it is required and not set, abort. |
263 | 263 | if ( ! $submission->is_required_field_set( $field ) ) { |
@@ -267,31 +267,31 @@ discard block |
||
267 | 267 | // Handle misc fields. |
268 | 268 | if ( isset( $data[ $field['id'] ] ) ) { |
269 | 269 | |
270 | - // Uploads. |
|
271 | - if ( 'file_upload' === $field['type'] ) { |
|
272 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
270 | + // Uploads. |
|
271 | + if ( 'file_upload' === $field['type'] ) { |
|
272 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
273 | 273 | |
274 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | - } |
|
274 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | + } |
|
277 | 277 | |
278 | - $value = array(); |
|
278 | + $value = array(); |
|
279 | 279 | |
280 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | - $value[] = sprintf( |
|
282 | - '<a href="%s" target="_blank">%s</a>', |
|
283 | - esc_url_raw( $url ), |
|
284 | - esc_html( $name ) |
|
285 | - ); |
|
286 | - } |
|
280 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | + $value[] = sprintf( |
|
282 | + '<a href="%s" target="_blank">%s</a>', |
|
283 | + esc_url_raw( $url ), |
|
284 | + esc_html( $name ) |
|
285 | + ); |
|
286 | + } |
|
287 | 287 | |
288 | - $value = implode( ' | ', $value ); |
|
288 | + $value = implode( ' | ', $value ); |
|
289 | 289 | |
290 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | - } else { |
|
293 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | - } |
|
290 | + } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | + $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | + } else { |
|
293 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | + } |
|
295 | 295 | |
296 | 296 | $label = $field['id']; |
297 | 297 | |
@@ -299,192 +299,192 @@ discard block |
||
299 | 299 | $label = $field['label']; |
300 | 300 | } |
301 | 301 | |
302 | - if ( ! empty( $field['add_meta'] ) ) { |
|
303 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | - } |
|
305 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | + if ( ! empty( $field['add_meta'] ) ) { |
|
303 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | + } |
|
305 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
306 | 306 | |
307 | 307 | } |
308 | - } |
|
308 | + } |
|
309 | 309 | |
310 | - return $prepared; |
|
310 | + return $prepared; |
|
311 | 311 | |
312 | - } |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
314 | + /** |
|
315 | 315 | * Retrieves address details. |
316 | 316 | * |
317 | - * @return array |
|
318 | - * @param WPInv_Invoice $invoice |
|
319 | - * @param string $type |
|
317 | + * @return array |
|
318 | + * @param WPInv_Invoice $invoice |
|
319 | + * @param string $type |
|
320 | 320 | */ |
321 | 321 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
322 | 322 | |
323 | - $data = $this->payment_form_submission->get_data(); |
|
324 | - $type = sanitize_key( $type ); |
|
325 | - $address = array(); |
|
326 | - $prepared = array(); |
|
323 | + $data = $this->payment_form_submission->get_data(); |
|
324 | + $type = sanitize_key( $type ); |
|
325 | + $address = array(); |
|
326 | + $prepared = array(); |
|
327 | 327 | |
328 | - if ( ! empty( $data[ $type ] ) ) { |
|
329 | - $address = $data[ $type ]; |
|
330 | - } |
|
328 | + if ( ! empty( $data[ $type ] ) ) { |
|
329 | + $address = $data[ $type ]; |
|
330 | + } |
|
331 | 331 | |
332 | - // Clean address details. |
|
333 | - foreach ( $address as $key => $value ) { |
|
334 | - $key = sanitize_key( $key ); |
|
335 | - $key = str_replace( 'wpinv_', '', $key ); |
|
336 | - $value = wpinv_clean( $value ); |
|
337 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | - } |
|
332 | + // Clean address details. |
|
333 | + foreach ( $address as $key => $value ) { |
|
334 | + $key = sanitize_key( $key ); |
|
335 | + $key = str_replace( 'wpinv_', '', $key ); |
|
336 | + $value = wpinv_clean( $value ); |
|
337 | + $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | + } |
|
339 | 339 | |
340 | - // Filter address details. |
|
341 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
340 | + // Filter address details. |
|
341 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
342 | 342 | |
343 | - // Remove non-whitelisted values. |
|
344 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
343 | + // Remove non-whitelisted values. |
|
344 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
345 | 345 | |
346 | - } |
|
346 | + } |
|
347 | 347 | |
348 | - /** |
|
348 | + /** |
|
349 | 349 | * Prepares the billing details. |
350 | 350 | * |
351 | - * @return array |
|
352 | - * @param WPInv_Invoice $invoice |
|
351 | + * @return array |
|
352 | + * @param WPInv_Invoice $invoice |
|
353 | 353 | */ |
354 | 354 | protected function prepare_billing_info( &$invoice ) { |
355 | 355 | |
356 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
357 | 357 | |
358 | - // Update the invoice with the billing details. |
|
359 | - $invoice->set_props( $billing_address ); |
|
358 | + // Update the invoice with the billing details. |
|
359 | + $invoice->set_props( $billing_address ); |
|
360 | 360 | |
361 | - } |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
363 | + /** |
|
364 | 364 | * Prepares the shipping details. |
365 | 365 | * |
366 | - * @return array |
|
367 | - * @param WPInv_Invoice $invoice |
|
366 | + * @return array |
|
367 | + * @param WPInv_Invoice $invoice |
|
368 | 368 | */ |
369 | 369 | protected function prepare_shipping_info( $invoice ) { |
370 | 370 | |
371 | - $data = $this->payment_form_submission->get_data(); |
|
371 | + $data = $this->payment_form_submission->get_data(); |
|
372 | 372 | |
373 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | - } |
|
373 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | + } |
|
376 | 376 | |
377 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
378 | 378 | |
379 | - } |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Confirms the submission is valid and send users to the gateway. |
|
383 | - * |
|
384 | - * @param WPInv_Invoice $invoice |
|
385 | - * @param array $prepared_payment_form_data |
|
386 | - * @param array $shipping |
|
387 | - */ |
|
388 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
381 | + /** |
|
382 | + * Confirms the submission is valid and send users to the gateway. |
|
383 | + * |
|
384 | + * @param WPInv_Invoice $invoice |
|
385 | + * @param array $prepared_payment_form_data |
|
386 | + * @param array $shipping |
|
387 | + */ |
|
388 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
389 | 389 | |
390 | - // Ensure the invoice exists. |
|
390 | + // Ensure the invoice exists. |
|
391 | 391 | if ( ! $invoice->exists() ) { |
392 | 392 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
393 | 393 | } |
394 | 394 | |
395 | - // Save payment form data. |
|
396 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
395 | + // Save payment form data. |
|
396 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
397 | 397 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
398 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
398 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
400 | 400 | |
401 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | - } |
|
401 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | + } |
|
404 | 404 | |
405 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | - } |
|
408 | - } |
|
405 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | + } |
|
408 | + } |
|
409 | 409 | |
410 | - // Save payment form data. |
|
411 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
410 | + // Save payment form data. |
|
411 | + $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
412 | 412 | if ( ! empty( $shipping ) ) { |
413 | 413 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
414 | - } |
|
414 | + } |
|
415 | 415 | |
416 | - // Backwards compatibility. |
|
416 | + // Backwards compatibility. |
|
417 | 417 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
418 | 418 | |
419 | - try { |
|
420 | - $this->process_payment( $invoice ); |
|
421 | - } catch ( Exception $e ) { |
|
422 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | - } |
|
419 | + try { |
|
420 | + $this->process_payment( $invoice ); |
|
421 | + } catch ( Exception $e ) { |
|
422 | + wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | + } |
|
424 | 424 | |
425 | 425 | // If we are here, there was an error. |
426 | - wpinv_send_back_to_checkout( $invoice ); |
|
426 | + wpinv_send_back_to_checkout( $invoice ); |
|
427 | 427 | |
428 | - } |
|
428 | + } |
|
429 | 429 | |
430 | - /** |
|
431 | - * Processes the actual payment. |
|
432 | - * |
|
433 | - * @param WPInv_Invoice $invoice |
|
434 | - */ |
|
435 | - protected function process_payment( $invoice ) { |
|
430 | + /** |
|
431 | + * Processes the actual payment. |
|
432 | + * |
|
433 | + * @param WPInv_Invoice $invoice |
|
434 | + */ |
|
435 | + protected function process_payment( $invoice ) { |
|
436 | 436 | |
437 | - // Clear any checkout errors. |
|
438 | - wpinv_clear_errors(); |
|
437 | + // Clear any checkout errors. |
|
438 | + wpinv_clear_errors(); |
|
439 | 439 | |
440 | - // No need to send free invoices to the gateway. |
|
441 | - if ( $invoice->is_free() ) { |
|
442 | - $this->process_free_payment( $invoice ); |
|
443 | - } |
|
440 | + // No need to send free invoices to the gateway. |
|
441 | + if ( $invoice->is_free() ) { |
|
442 | + $this->process_free_payment( $invoice ); |
|
443 | + } |
|
444 | 444 | |
445 | - $submission = $this->payment_form_submission; |
|
445 | + $submission = $this->payment_form_submission; |
|
446 | 446 | |
447 | - // Fires before sending to the gateway. |
|
448 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
447 | + // Fires before sending to the gateway. |
|
448 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
449 | 449 | |
450 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | - $submission_data = $submission->get_data(); |
|
452 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
450 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | + $submission_data = $submission->get_data(); |
|
452 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
454 | 454 | |
455 | - // Validate the currency. |
|
456 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | - wpinv_set_error( 'invalid_currency' ); |
|
458 | - } |
|
455 | + // Validate the currency. |
|
456 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | + wpinv_set_error( 'invalid_currency' ); |
|
458 | + } |
|
459 | 459 | |
460 | - // Check to see if we have any errors. |
|
461 | - if ( wpinv_get_errors() ) { |
|
462 | - wpinv_send_back_to_checkout( $invoice ); |
|
463 | - } |
|
460 | + // Check to see if we have any errors. |
|
461 | + if ( wpinv_get_errors() ) { |
|
462 | + wpinv_send_back_to_checkout( $invoice ); |
|
463 | + } |
|
464 | 464 | |
465 | - // Send info to the gateway for payment processing |
|
466 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
465 | + // Send info to the gateway for payment processing |
|
466 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
467 | 467 | |
468 | - // Backwards compatibility. |
|
469 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
468 | + // Backwards compatibility. |
|
469 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
470 | 470 | |
471 | - } |
|
471 | + } |
|
472 | 472 | |
473 | - /** |
|
474 | - * Marks the invoice as paid in case the checkout is free. |
|
475 | - * |
|
476 | - * @param WPInv_Invoice $invoice |
|
477 | - */ |
|
478 | - protected function process_free_payment( $invoice ) { |
|
473 | + /** |
|
474 | + * Marks the invoice as paid in case the checkout is free. |
|
475 | + * |
|
476 | + * @param WPInv_Invoice $invoice |
|
477 | + */ |
|
478 | + protected function process_free_payment( $invoice ) { |
|
479 | 479 | |
480 | - $invoice->set_gateway( 'none' ); |
|
481 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | - $invoice->mark_paid(); |
|
483 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
480 | + $invoice->set_gateway( 'none' ); |
|
481 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | + $invoice->mark_paid(); |
|
483 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
484 | 484 | |
485 | - } |
|
485 | + } |
|
486 | 486 | |
487 | - /** |
|
487 | + /** |
|
488 | 488 | * Sends a redrect response to payment details. |
489 | 489 | * |
490 | 490 | */ |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Main Checkout Class. |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param GetPaid_Payment_Form_Submission $submission |
24 | 24 | */ |
25 | - public function __construct( $submission ) { |
|
25 | + public function __construct($submission) { |
|
26 | 26 | $this->payment_form_submission = $submission; |
27 | 27 | } |
28 | 28 | |
@@ -38,22 +38,22 @@ discard block |
||
38 | 38 | // Prepare the invoice. |
39 | 39 | $items = $this->get_submission_items(); |
40 | 40 | $invoice = $this->get_submission_invoice(); |
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
41 | + $invoice = $this->process_submission_invoice($invoice, $items); |
|
42 | 42 | $prepared = $this->prepare_submission_data_for_saving(); |
43 | 43 | |
44 | - $this->prepare_billing_info( $invoice ); |
|
44 | + $this->prepare_billing_info($invoice); |
|
45 | 45 | |
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
46 | + $shipping = $this->prepare_shipping_info($invoice); |
|
47 | 47 | |
48 | 48 | // Save the invoice. |
49 | - $invoice->set_is_viewed( true ); |
|
49 | + $invoice->set_is_viewed(true); |
|
50 | 50 | $invoice->recalculate_total(); |
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action('getpaid_checkout_invoice_updated', $invoice); |
|
54 | 54 | |
55 | 55 | // Send to the gateway. |
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
56 | + $this->post_process_submission($invoice, $prepared, $shipping); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
@@ -66,34 +66,34 @@ discard block |
||
66 | 66 | $data = $submission->get_data(); |
67 | 67 | |
68 | 68 | // Do we have an error? |
69 | - if ( ! empty( $submission->last_error ) ) { |
|
70 | - wp_send_json_error( $submission->last_error ); |
|
69 | + if (!empty($submission->last_error)) { |
|
70 | + wp_send_json_error($submission->last_error); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | // We need a billing email. |
74 | - if ( ! $submission->has_billing_email() ) { |
|
75 | - wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
|
74 | + if (!$submission->has_billing_email()) { |
|
75 | + wp_send_json_error(__('Provide a valid billing email.', 'invoicing')); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // Non-recurring gateways should not be allowed to process recurring invoices. |
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
79 | + if ($submission->should_collect_payment_details() && $submission->has_recurring && !wpinv_gateway_support_subscription($data['wpi-gateway'])) { |
|
80 | + wp_send_json_error(__('The selected payment gateway does not support subscription payments.', 'invoicing')); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | // Ensure the gateway is active. |
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
84 | + if ($submission->should_collect_payment_details() && !wpinv_is_gateway_active($data['wpi-gateway'])) { |
|
85 | + wp_send_json_error(__('The selected payment gateway is not active', 'invoicing')); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | // Clear any existing errors. |
89 | 89 | wpinv_clear_errors(); |
90 | 90 | |
91 | 91 | // Allow themes and plugins to hook to errors |
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
92 | + do_action('getpaid_checkout_error_checks', $submission); |
|
93 | 93 | |
94 | 94 | // Do we have any errors? |
95 | - if ( wpinv_get_errors() ) { |
|
96 | - wp_send_json_error( getpaid_get_errors_html() ); |
|
95 | + if (wpinv_get_errors()) { |
|
96 | + wp_send_json_error(getpaid_get_errors_html()); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | } |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | $items = $this->payment_form_submission->get_items(); |
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | - if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
|
112 | - wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
|
111 | + if (empty($items) && !$this->payment_form_submission->has_fees()) { |
|
112 | + wp_send_json_error(__('Please provide at least one item or amount.', 'invoicing')); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | return $items; |
@@ -123,17 +123,17 @@ discard block |
||
123 | 123 | protected function get_submission_invoice() { |
124 | 124 | $submission = $this->payment_form_submission; |
125 | 125 | |
126 | - if ( ! $submission->has_invoice() ) { |
|
126 | + if (!$submission->has_invoice()) { |
|
127 | 127 | $invoice = new WPInv_Invoice(); |
128 | - $invoice->set_created_via( 'payment_form' ); |
|
128 | + $invoice->set_created_via('payment_form'); |
|
129 | 129 | return $invoice; |
130 | 130 | } |
131 | 131 | |
132 | 132 | $invoice = $submission->get_invoice(); |
133 | 133 | |
134 | 134 | // Make sure that it is neither paid or refunded. |
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
135 | + if ($invoice->is_paid() || $invoice->is_refunded()) { |
|
136 | + wp_send_json_error(__('This invoice has already been paid for.', 'invoicing')); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | return $invoice; |
@@ -146,34 +146,34 @@ discard block |
||
146 | 146 | * @param GetPaid_Form_Item[] $items |
147 | 147 | * @return WPInv_Invoice |
148 | 148 | */ |
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
149 | + protected function process_submission_invoice($invoice, $items) { |
|
150 | 150 | |
151 | 151 | $submission = $this->payment_form_submission; |
152 | 152 | |
153 | 153 | // Set-up the invoice details. |
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_submission_id( $submission->id ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | - $invoice->set_items( $items ); |
|
159 | - $invoice->set_fees( $submission->get_fees() ); |
|
160 | - $invoice->set_taxes( $submission->get_taxes() ); |
|
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | - $invoice->set_currency( $submission->get_currency() ); |
|
164 | - |
|
165 | - if ( $submission->has_shipping() ) { |
|
166 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
154 | + $invoice->set_email(sanitize_email($submission->get_billing_email())); |
|
155 | + $invoice->set_user_id($this->get_submission_customer()); |
|
156 | + $invoice->set_submission_id($submission->id); |
|
157 | + $invoice->set_payment_form(absint($submission->get_payment_form()->get_id())); |
|
158 | + $invoice->set_items($items); |
|
159 | + $invoice->set_fees($submission->get_fees()); |
|
160 | + $invoice->set_taxes($submission->get_taxes()); |
|
161 | + $invoice->set_discounts($submission->get_discounts()); |
|
162 | + $invoice->set_gateway($submission->get_field('wpi-gateway')); |
|
163 | + $invoice->set_currency($submission->get_currency()); |
|
164 | + |
|
165 | + if ($submission->has_shipping()) { |
|
166 | + $invoice->set_shipping($submission->get_shipping()); |
|
167 | 167 | } |
168 | 168 | |
169 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
169 | + $address_confirmed = $submission->get_field('confirm-address'); |
|
170 | + $invoice->set_address_confirmed(!empty($address_confirmed)); |
|
171 | 171 | |
172 | - if ( $submission->has_discount_code() ) { |
|
173 | - $invoice->set_discount_code( $submission->get_discount_code() ); |
|
172 | + if ($submission->has_discount_code()) { |
|
173 | + $invoice->set_discount_code($submission->get_discount_code()); |
|
174 | 174 | } |
175 | 175 | |
176 | - getpaid_maybe_add_default_address( $invoice ); |
|
176 | + getpaid_maybe_add_default_address($invoice); |
|
177 | 177 | return $invoice; |
178 | 178 | } |
179 | 179 | |
@@ -186,34 +186,34 @@ discard block |
||
186 | 186 | $submission = $this->payment_form_submission; |
187 | 187 | |
188 | 188 | // If this is an existing invoice... |
189 | - if ( $submission->has_invoice() ) { |
|
189 | + if ($submission->has_invoice()) { |
|
190 | 190 | return $submission->get_invoice()->get_user_id(); |
191 | 191 | } |
192 | 192 | |
193 | 193 | // (Maybe) create the user. |
194 | 194 | $user = get_current_user_id(); |
195 | 195 | |
196 | - if ( empty( $user ) ) { |
|
197 | - $user = get_user_by( 'email', $submission->get_billing_email() ); |
|
196 | + if (empty($user)) { |
|
197 | + $user = get_user_by('email', $submission->get_billing_email()); |
|
198 | 198 | } |
199 | 199 | |
200 | - if ( empty( $user ) ) { |
|
201 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | - $name = implode( '', array_filter( $name ) ); |
|
203 | - $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
|
200 | + if (empty($user)) { |
|
201 | + $name = array($submission->get_field('wpinv_first_name', 'billing'), $submission->get_field('wpinv_last_name', 'billing')); |
|
202 | + $name = implode('', array_filter($name)); |
|
203 | + $user = wpinv_create_user($submission->get_billing_email(), $name); |
|
204 | 204 | |
205 | 205 | // (Maybe) send new user notification. |
206 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | - wp_send_new_user_notifications( $user, 'user' ); |
|
206 | + $should_send_notification = wpinv_get_option('disable_new_user_emails'); |
|
207 | + if (!empty($user) && is_numeric($user) && apply_filters('getpaid_send_new_user_notification', empty($should_send_notification), $user)) { |
|
208 | + wp_send_new_user_notifications($user, 'user'); |
|
209 | 209 | } |
210 | 210 | } |
211 | 211 | |
212 | - if ( is_wp_error( $user ) ) { |
|
213 | - wp_send_json_error( $user->get_error_message() ); |
|
212 | + if (is_wp_error($user)) { |
|
213 | + wp_send_json_error($user->get_error_message()); |
|
214 | 214 | } |
215 | 215 | |
216 | - if ( is_numeric( $user ) ) { |
|
216 | + if (is_numeric($user)) { |
|
217 | 217 | return $user; |
218 | 218 | } |
219 | 219 | |
@@ -237,72 +237,72 @@ discard block |
||
237 | 237 | ); |
238 | 238 | |
239 | 239 | // Raw submission details. |
240 | - $data = $submission->get_data(); |
|
240 | + $data = $submission->get_data(); |
|
241 | 241 | |
242 | 242 | // Loop through the submitted details. |
243 | - foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
|
243 | + foreach ($submission->get_payment_form()->get_elements() as $field) { |
|
244 | 244 | |
245 | 245 | // Skip premade fields. |
246 | - if ( ! empty( $field['premade'] ) ) { |
|
246 | + if (!empty($field['premade'])) { |
|
247 | 247 | continue; |
248 | 248 | } |
249 | 249 | |
250 | 250 | // Ensure address is provided. |
251 | - if ( 'address' === $field['type'] ) { |
|
252 | - $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
|
251 | + if ('address' === $field['type']) { |
|
252 | + $address_type = isset($field['address_type']) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
|
253 | 253 | |
254 | - foreach ( $field['fields'] as $address_field ) { |
|
254 | + foreach ($field['fields'] as $address_field) { |
|
255 | 255 | |
256 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
256 | + if (!empty($address_field['visible']) && !empty($address_field['required']) && '' === trim($_POST[$address_type][$address_field['name']])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | + wp_send_json_error(__('Please fill all required fields.', 'invoicing')); |
|
258 | 258 | } |
259 | 259 | } |
260 | 260 | } |
261 | 261 | |
262 | 262 | // If it is required and not set, abort. |
263 | - if ( ! $submission->is_required_field_set( $field ) ) { |
|
264 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
263 | + if (!$submission->is_required_field_set($field)) { |
|
264 | + wp_send_json_error(__('Please fill all required fields.', 'invoicing')); |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | // Handle misc fields. |
268 | - if ( isset( $data[ $field['id'] ] ) ) { |
|
268 | + if (isset($data[$field['id']])) { |
|
269 | 269 | |
270 | 270 | // Uploads. |
271 | - if ( 'file_upload' === $field['type'] ) { |
|
272 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
271 | + if ('file_upload' === $field['type']) { |
|
272 | + $max_file_num = empty($field['max_file_num']) ? 1 : absint($field['max_file_num']); |
|
273 | 273 | |
274 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
274 | + if (count($data[$field['id']]) > $max_file_num) { |
|
275 | + wp_send_json_error(__('Maximum number of allowed files exceeded.', 'invoicing')); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | $value = array(); |
279 | 279 | |
280 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
280 | + foreach ($data[$field['id']] as $url => $name) { |
|
281 | 281 | $value[] = sprintf( |
282 | 282 | '<a href="%s" target="_blank">%s</a>', |
283 | - esc_url_raw( $url ), |
|
284 | - esc_html( $name ) |
|
283 | + esc_url_raw($url), |
|
284 | + esc_html($name) |
|
285 | 285 | ); |
286 | 286 | } |
287 | 287 | |
288 | - $value = implode( ' | ', $value ); |
|
288 | + $value = implode(' | ', $value); |
|
289 | 289 | |
290 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
290 | + } elseif ('checkbox' === $field['type']) { |
|
291 | + $value = !empty($data[$field['id']]) ? __('Yes', 'invoicing') : __('No', 'invoicing'); |
|
292 | 292 | } else { |
293 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
293 | + $value = wp_kses_post($data[$field['id']]); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | $label = $field['id']; |
297 | 297 | |
298 | - if ( isset( $field['label'] ) ) { |
|
298 | + if (isset($field['label'])) { |
|
299 | 299 | $label = $field['label']; |
300 | 300 | } |
301 | 301 | |
302 | - if ( ! empty( $field['add_meta'] ) ) { |
|
303 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | + if (!empty($field['add_meta'])) { |
|
303 | + $prepared['meta'][wpinv_clean($label)] = wp_kses_post_deep($value); |
|
304 | 304 | } |
305 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
305 | + $prepared['all'][wpinv_clean($label)] = wp_kses_post_deep($value); |
|
306 | 306 | |
307 | 307 | } |
308 | 308 | } |
@@ -318,30 +318,30 @@ discard block |
||
318 | 318 | * @param WPInv_Invoice $invoice |
319 | 319 | * @param string $type |
320 | 320 | */ |
321 | - public function prepare_address_details( $invoice, $type = 'billing' ) { |
|
321 | + public function prepare_address_details($invoice, $type = 'billing') { |
|
322 | 322 | |
323 | 323 | $data = $this->payment_form_submission->get_data(); |
324 | - $type = sanitize_key( $type ); |
|
324 | + $type = sanitize_key($type); |
|
325 | 325 | $address = array(); |
326 | 326 | $prepared = array(); |
327 | 327 | |
328 | - if ( ! empty( $data[ $type ] ) ) { |
|
329 | - $address = $data[ $type ]; |
|
328 | + if (!empty($data[$type])) { |
|
329 | + $address = $data[$type]; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | // Clean address details. |
333 | - foreach ( $address as $key => $value ) { |
|
334 | - $key = sanitize_key( $key ); |
|
335 | - $key = str_replace( 'wpinv_', '', $key ); |
|
336 | - $value = wpinv_clean( $value ); |
|
337 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
333 | + foreach ($address as $key => $value) { |
|
334 | + $key = sanitize_key($key); |
|
335 | + $key = str_replace('wpinv_', '', $key); |
|
336 | + $value = wpinv_clean($value); |
|
337 | + $prepared[$key] = apply_filters("getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice); |
|
338 | 338 | } |
339 | 339 | |
340 | 340 | // Filter address details. |
341 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
341 | + $prepared = apply_filters("getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice); |
|
342 | 342 | |
343 | 343 | // Remove non-whitelisted values. |
344 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
344 | + return array_filter($prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY); |
|
345 | 345 | |
346 | 346 | } |
347 | 347 | |
@@ -351,12 +351,12 @@ discard block |
||
351 | 351 | * @return array |
352 | 352 | * @param WPInv_Invoice $invoice |
353 | 353 | */ |
354 | - protected function prepare_billing_info( &$invoice ) { |
|
354 | + protected function prepare_billing_info(&$invoice) { |
|
355 | 355 | |
356 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | + $billing_address = $this->prepare_address_details($invoice, 'billing'); |
|
357 | 357 | |
358 | 358 | // Update the invoice with the billing details. |
359 | - $invoice->set_props( $billing_address ); |
|
359 | + $invoice->set_props($billing_address); |
|
360 | 360 | |
361 | 361 | } |
362 | 362 | |
@@ -366,15 +366,15 @@ discard block |
||
366 | 366 | * @return array |
367 | 367 | * @param WPInv_Invoice $invoice |
368 | 368 | */ |
369 | - protected function prepare_shipping_info( $invoice ) { |
|
369 | + protected function prepare_shipping_info($invoice) { |
|
370 | 370 | |
371 | 371 | $data = $this->payment_form_submission->get_data(); |
372 | 372 | |
373 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
373 | + if (empty($data['same-shipping-address'])) { |
|
374 | + return $this->prepare_address_details($invoice, 'shipping'); |
|
375 | 375 | } |
376 | 376 | |
377 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | + return $this->prepare_address_details($invoice, 'billing'); |
|
378 | 378 | |
379 | 379 | } |
380 | 380 | |
@@ -385,45 +385,45 @@ discard block |
||
385 | 385 | * @param array $prepared_payment_form_data |
386 | 386 | * @param array $shipping |
387 | 387 | */ |
388 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
388 | + protected function post_process_submission($invoice, $prepared_payment_form_data, $shipping) { |
|
389 | 389 | |
390 | 390 | // Ensure the invoice exists. |
391 | - if ( ! $invoice->exists() ) { |
|
392 | - wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
|
391 | + if (!$invoice->exists()) { |
|
392 | + wp_send_json_error(__('An error occured while saving your invoice. Please try again.', 'invoicing')); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | // Save payment form data. |
396 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
397 | - delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
|
398 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
396 | + $prepared_payment_form_data = apply_filters('getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice); |
|
397 | + delete_post_meta($invoice->get_id(), 'payment_form_data'); |
|
398 | + delete_post_meta($invoice->get_id(), 'additional_meta_data'); |
|
399 | + if (!empty($prepared_payment_form_data)) { |
|
400 | 400 | |
401 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
401 | + if (!empty($prepared_payment_form_data['all'])) { |
|
402 | + update_post_meta($invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all']); |
|
403 | 403 | } |
404 | 404 | |
405 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
405 | + if (!empty($prepared_payment_form_data['meta'])) { |
|
406 | + update_post_meta($invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta']); |
|
407 | 407 | } |
408 | 408 | } |
409 | 409 | |
410 | 410 | // Save payment form data. |
411 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
412 | - if ( ! empty( $shipping ) ) { |
|
413 | - update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
|
411 | + $shipping = apply_filters('getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission); |
|
412 | + if (!empty($shipping)) { |
|
413 | + update_post_meta($invoice->get_id(), 'shipping_address', $shipping); |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | // Backwards compatibility. |
417 | - add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
|
417 | + add_filter('wp_redirect', array($this, 'send_redirect_response')); |
|
418 | 418 | |
419 | 419 | try { |
420 | - $this->process_payment( $invoice ); |
|
421 | - } catch ( Exception $e ) { |
|
422 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
420 | + $this->process_payment($invoice); |
|
421 | + } catch (Exception $e) { |
|
422 | + wpinv_set_error('payment_error', $e->getMessage()); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | // If we are here, there was an error. |
426 | - wpinv_send_back_to_checkout( $invoice ); |
|
426 | + wpinv_send_back_to_checkout($invoice); |
|
427 | 427 | |
428 | 428 | } |
429 | 429 | |
@@ -432,41 +432,41 @@ discard block |
||
432 | 432 | * |
433 | 433 | * @param WPInv_Invoice $invoice |
434 | 434 | */ |
435 | - protected function process_payment( $invoice ) { |
|
435 | + protected function process_payment($invoice) { |
|
436 | 436 | |
437 | 437 | // Clear any checkout errors. |
438 | 438 | wpinv_clear_errors(); |
439 | 439 | |
440 | 440 | // No need to send free invoices to the gateway. |
441 | - if ( $invoice->is_free() ) { |
|
442 | - $this->process_free_payment( $invoice ); |
|
441 | + if ($invoice->is_free()) { |
|
442 | + $this->process_free_payment($invoice); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | $submission = $this->payment_form_submission; |
446 | 446 | |
447 | 447 | // Fires before sending to the gateway. |
448 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
448 | + do_action('getpaid_checkout_before_gateway', $invoice, $submission); |
|
449 | 449 | |
450 | 450 | // Allow the sumission data to be modified before it is sent to the gateway. |
451 | 451 | $submission_data = $submission->get_data(); |
452 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
452 | + $submission_gateway = apply_filters('getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice); |
|
453 | + $submission_data = apply_filters('getpaid_gateway_submission_data', $submission_data, $submission, $invoice); |
|
454 | 454 | |
455 | 455 | // Validate the currency. |
456 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | - wpinv_set_error( 'invalid_currency' ); |
|
456 | + if (!apply_filters("getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency())) { |
|
457 | + wpinv_set_error('invalid_currency'); |
|
458 | 458 | } |
459 | 459 | |
460 | 460 | // Check to see if we have any errors. |
461 | - if ( wpinv_get_errors() ) { |
|
462 | - wpinv_send_back_to_checkout( $invoice ); |
|
461 | + if (wpinv_get_errors()) { |
|
462 | + wpinv_send_back_to_checkout($invoice); |
|
463 | 463 | } |
464 | 464 | |
465 | 465 | // Send info to the gateway for payment processing |
466 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
466 | + do_action("getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission); |
|
467 | 467 | |
468 | 468 | // Backwards compatibility. |
469 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
469 | + wpinv_send_to_gateway($submission_gateway, $invoice); |
|
470 | 470 | |
471 | 471 | } |
472 | 472 | |
@@ -475,12 +475,12 @@ discard block |
||
475 | 475 | * |
476 | 476 | * @param WPInv_Invoice $invoice |
477 | 477 | */ |
478 | - protected function process_free_payment( $invoice ) { |
|
478 | + protected function process_free_payment($invoice) { |
|
479 | 479 | |
480 | - $invoice->set_gateway( 'none' ); |
|
481 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
480 | + $invoice->set_gateway('none'); |
|
481 | + $invoice->add_note(__("This is a free invoice and won't be sent to the payment gateway", 'invoicing'), false, false, true); |
|
482 | 482 | $invoice->mark_paid(); |
483 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
483 | + wpinv_send_to_success_page(array('invoice_key' => $invoice->get_key())); |
|
484 | 484 | |
485 | 485 | } |
486 | 486 | |
@@ -488,9 +488,9 @@ discard block |
||
488 | 488 | * Sends a redrect response to payment details. |
489 | 489 | * |
490 | 490 | */ |
491 | - public function send_redirect_response( $url ) { |
|
492 | - $url = rawurlencode( $url ); |
|
493 | - wp_send_json_success( $url ); |
|
491 | + public function send_redirect_response($url) { |
|
492 | + $url = rawurlencode($url); |
|
493 | + wp_send_json_success($url); |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | } |
@@ -18,688 +18,688 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Report_Sales_Controller extends GetPaid_REST_Date_Based_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * Route base. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $rest_base = 'reports/sales'; |
|
27 | - |
|
28 | - /** |
|
29 | - * The report data. |
|
30 | - * |
|
31 | - * @var stdClass |
|
32 | - */ |
|
33 | - public $report_data; |
|
34 | - |
|
35 | - /** |
|
36 | - * The report range. |
|
37 | - * |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - public $report_range; |
|
41 | - |
|
42 | - /** |
|
43 | - * Registers the routes for the objects of the controller. |
|
44 | - * |
|
45 | - * @since 2.0.0 |
|
46 | - * |
|
47 | - * @see register_rest_route() |
|
48 | - */ |
|
49 | - public function register_namespace_routes( $namespace ) { |
|
50 | - |
|
51 | - // Get sales report. |
|
52 | - register_rest_route( |
|
53 | - $namespace, |
|
54 | - $this->rest_base, |
|
55 | - array( |
|
56 | - array( |
|
57 | - 'methods' => WP_REST_Server::READABLE, |
|
58 | - 'callback' => array( $this, 'get_items' ), |
|
59 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
60 | - 'args' => $this->get_collection_params(), |
|
61 | - ), |
|
62 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
63 | - ) |
|
64 | - ); |
|
65 | - |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Makes sure the current user has access to READ the report APIs. |
|
70 | - * |
|
71 | - * @since 2.0.0 |
|
72 | - * @param WP_REST_Request $request Full data about the request. |
|
73 | - * @return WP_Error|boolean |
|
74 | - */ |
|
75 | - public function get_items_permissions_check( $request ) { |
|
76 | - |
|
77 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
78 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
79 | - } |
|
80 | - |
|
81 | - return true; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Get sales reports. |
|
86 | - * |
|
87 | - * @param WP_REST_Request $request |
|
88 | - * @return array|WP_Error |
|
89 | - */ |
|
90 | - public function get_items( $request ) { |
|
91 | - $data = array(); |
|
92 | - $item = $this->prepare_item_for_response( null, $request ); |
|
93 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
94 | - |
|
95 | - return rest_ensure_response( $data ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Prepare a report sales object for serialization. |
|
100 | - * |
|
101 | - * @param null $_ |
|
102 | - * @param WP_REST_Request $request Request object. |
|
103 | - * @return WP_REST_Response $response Response data. |
|
104 | - */ |
|
105 | - public function prepare_item_for_response( $_, $request ) { |
|
106 | - |
|
107 | - // Set report range. |
|
108 | - $this->report_range = $this->get_date_range( $request ); |
|
109 | - |
|
110 | - $report_data = $this->get_report_data(); |
|
111 | - $period_totals = array(); |
|
112 | - |
|
113 | - // Setup period totals by ensuring each period in the interval has data. |
|
114 | - $start_date = strtotime( $this->report_range['after'] ); |
|
115 | - |
|
116 | - if ( 'month' === $this->groupby ) { |
|
117 | - $start_date = strtotime( gmdate( 'Y-m-01', $start_date ) ); |
|
118 | - } |
|
119 | - |
|
120 | - for ( $i = 0; $i < $this->interval; $i++ ) { |
|
121 | - |
|
122 | - switch ( $this->groupby ) { |
|
123 | - case 'day': |
|
124 | - $time = gmdate( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
125 | - break; |
|
126 | - default: |
|
127 | - $time = gmdate( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
128 | - break; |
|
129 | - } |
|
130 | - |
|
131 | - // Set the defaults for each period. |
|
132 | - $period_totals[ $time ] = array( |
|
133 | - 'invoices' => 0, |
|
134 | - 'items' => 0, |
|
135 | - 'refunded_items' => 0, |
|
136 | - 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
137 | - 'subtotal' => wpinv_round_amount( 0.00 ), |
|
138 | - 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
139 | - 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
140 | - 'discount' => wpinv_round_amount( 0.00 ), |
|
141 | - ); |
|
142 | - |
|
143 | - foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
144 | - if ( ! isset( $period_totals[ $time ][ $key ] ) ) { |
|
145 | - $period_totals[ $time ][ $key ] = wpinv_round_amount( 0.00 ); |
|
146 | - } |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - // add total sales, total invoice count, total tax for each period |
|
151 | - $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
152 | - foreach ( $report_data->invoices as $invoice ) { |
|
153 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
154 | - |
|
155 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
156 | - continue; |
|
157 | - } |
|
158 | - |
|
159 | - $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
160 | - $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
161 | - $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
162 | - $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
163 | - |
|
164 | - } |
|
165 | - |
|
166 | - foreach ( $report_data->refunds as $invoice ) { |
|
167 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
168 | - |
|
169 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
170 | - continue; |
|
171 | - } |
|
172 | - |
|
173 | - $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
174 | - $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
175 | - $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
176 | - $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
177 | - |
|
178 | - } |
|
179 | - |
|
180 | - foreach ( $report_data->invoice_counts as $invoice ) { |
|
181 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
182 | - |
|
183 | - if ( isset( $period_totals[ $time ] ) ) { |
|
184 | - $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - // Add total invoice items for each period. |
|
189 | - foreach ( $report_data->invoice_items as $invoice_item ) { |
|
190 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : gmdate( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
191 | - |
|
192 | - if ( isset( $period_totals[ $time ] ) ) { |
|
193 | - $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - // Add total discount for each period. |
|
198 | - foreach ( $report_data->coupons as $discount ) { |
|
199 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $discount->post_date ) ) : gmdate( 'Y-m', strtotime( $discount->post_date ) ); |
|
200 | - |
|
201 | - if ( isset( $period_totals[ $time ] ) ) { |
|
202 | - $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - // Extra fields. |
|
207 | - foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
208 | - |
|
209 | - // Abort unprepared. |
|
210 | - if ( ! isset( $report_data->$key ) ) { |
|
211 | - continue; |
|
212 | - } |
|
213 | - |
|
214 | - // Abort defaults. |
|
215 | - if ( in_array( $key, array( 'sales', 'refunds', 'tax', 'fees', 'discount', 'invoices', 'items' ) ) ) { |
|
216 | - continue; |
|
217 | - } |
|
218 | - |
|
219 | - // Set values. |
|
220 | - foreach ( $report_data->$key as $item ) { |
|
221 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $item->date ) ) : gmdate( 'Y-m', strtotime( $item->date ) ); |
|
222 | - |
|
223 | - if ( isset( $period_totals[ $time ] ) ) { |
|
224 | - $period_totals[ $time ][ $key ] = wpinv_round_amount( $item->val ); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - unset( $report_data->$key ); |
|
229 | - } |
|
230 | - |
|
231 | - $report_data->totals = $period_totals; |
|
232 | - $report_data->grouped_by = $this->groupby; |
|
233 | - $report_data->interval = max( $this->interval, 1 ); |
|
234 | - $report_data->currency = wpinv_get_currency(); |
|
235 | - $report_data->currency_symbol = wpinv_currency_symbol(); |
|
236 | - $report_data->currency_position = wpinv_currency_position(); |
|
237 | - $report_data->decimal_places = wpinv_decimals(); |
|
238 | - $report_data->thousands_sep = wpinv_thousands_separator(); |
|
239 | - $report_data->decimals_sep = wpinv_decimal_separator(); |
|
240 | - $report_data->start_date = gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ); |
|
241 | - $report_data->end_date = gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ); |
|
242 | - $report_data->start_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ) ); |
|
243 | - $report_data->end_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ) ); |
|
244 | - $report_data->decimals_sep = wpinv_decimal_separator(); |
|
245 | - |
|
246 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
247 | - $data = $report_data; |
|
248 | - unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
249 | - $data = $this->add_additional_fields_to_object( (array) $data, $request ); |
|
250 | - $data = $this->filter_response_by_context( $data, $context ); |
|
251 | - |
|
252 | - // Wrap the data in a response object. |
|
253 | - $response = rest_ensure_response( $data ); |
|
254 | - $response->add_links( |
|
21 | + /** |
|
22 | + * Route base. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $rest_base = 'reports/sales'; |
|
27 | + |
|
28 | + /** |
|
29 | + * The report data. |
|
30 | + * |
|
31 | + * @var stdClass |
|
32 | + */ |
|
33 | + public $report_data; |
|
34 | + |
|
35 | + /** |
|
36 | + * The report range. |
|
37 | + * |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + public $report_range; |
|
41 | + |
|
42 | + /** |
|
43 | + * Registers the routes for the objects of the controller. |
|
44 | + * |
|
45 | + * @since 2.0.0 |
|
46 | + * |
|
47 | + * @see register_rest_route() |
|
48 | + */ |
|
49 | + public function register_namespace_routes( $namespace ) { |
|
50 | + |
|
51 | + // Get sales report. |
|
52 | + register_rest_route( |
|
53 | + $namespace, |
|
54 | + $this->rest_base, |
|
255 | 55 | array( |
256 | - 'about' => array( |
|
257 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
258 | - ), |
|
56 | + array( |
|
57 | + 'methods' => WP_REST_Server::READABLE, |
|
58 | + 'callback' => array( $this, 'get_items' ), |
|
59 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
60 | + 'args' => $this->get_collection_params(), |
|
61 | + ), |
|
62 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
259 | 63 | ) |
260 | 64 | ); |
261 | 65 | |
262 | - return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Get report data. |
|
267 | - * |
|
268 | - * @return stdClass |
|
269 | - */ |
|
270 | - public function get_report_data() { |
|
271 | - if ( empty( $this->report_data ) ) { |
|
272 | - $this->query_report_data(); |
|
273 | - } |
|
274 | - return $this->report_data; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Get all data needed for this report and store in the class. |
|
279 | - */ |
|
280 | - protected function query_report_data() { |
|
281 | - |
|
282 | - // Prepare reports. |
|
283 | - $this->report_data = (object) array( |
|
284 | - 'invoice_counts' => $this->query_invoice_counts(), //count, post_date |
|
285 | - 'coupons' => $this->query_coupon_counts(), // discount_amount, post_date |
|
286 | - 'invoice_items' => $this->query_item_counts(), // invoice_item_count, post_date |
|
287 | - 'refunded_items' => $this->count_refunded_items(), // invoice_item_count, post_date |
|
288 | - 'invoices' => $this->query_invoice_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
289 | - 'refunds' => $this->query_refunded_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
290 | - 'previous_range' => $this->previous_range, |
|
291 | - ); |
|
292 | - |
|
293 | - // Calculated totals. |
|
294 | - $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
295 | - $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
296 | - $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
297 | - $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
298 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
299 | - $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
300 | - $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
301 | - $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
302 | - $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
303 | - $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
304 | - $this->report_data->refunded_subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
305 | - $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
306 | - |
|
307 | - // Calculate average based on net. |
|
308 | - $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
309 | - $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
310 | - |
|
311 | - // Total invoices in this period, even if refunded. |
|
312 | - $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
313 | - |
|
314 | - // Items invoiced in this period, even if refunded. |
|
315 | - $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
316 | - |
|
317 | - // 3rd party filtering of report data |
|
318 | - $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data, $this ); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Prepares invoice counts. |
|
323 | - * |
|
324 | - * @return array. |
|
325 | - */ |
|
326 | - protected function query_invoice_counts() { |
|
327 | - |
|
328 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
329 | - array( |
|
330 | - 'data' => array( |
|
331 | - 'ID' => array( |
|
332 | - 'type' => 'post_data', |
|
333 | - 'function' => 'COUNT', |
|
334 | - 'name' => 'count', |
|
335 | - 'distinct' => true, |
|
336 | - ), |
|
337 | - 'post_date' => array( |
|
338 | - 'type' => 'post_data', |
|
339 | - 'function' => 'MIN', |
|
340 | - 'name' => 'post_date', |
|
341 | - ), |
|
342 | - ), |
|
343 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
344 | - 'order_by' => 'post_date ASC', |
|
345 | - 'query_type' => 'get_results', |
|
346 | - 'filter_range' => $this->report_range, |
|
347 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
348 | - ) |
|
349 | - ); |
|
350 | - |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Prepares coupon counts. |
|
355 | - * |
|
356 | - * @return array. |
|
357 | - */ |
|
358 | - protected function query_coupon_counts() { |
|
359 | - |
|
360 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
361 | - array( |
|
362 | - 'data' => array( |
|
363 | - 'discount' => array( |
|
364 | - 'type' => 'invoice_data', |
|
365 | - 'function' => 'SUM', |
|
366 | - 'name' => 'discount_amount', |
|
367 | - ), |
|
368 | - 'post_date' => array( |
|
369 | - 'type' => 'post_data', |
|
370 | - 'function' => 'MIN', |
|
371 | - 'name' => 'post_date', |
|
372 | - ), |
|
373 | - ), |
|
374 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
375 | - 'order_by' => 'post_date ASC', |
|
376 | - 'query_type' => 'get_results', |
|
377 | - 'filter_range' => $this->report_range, |
|
378 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
379 | - ) |
|
380 | - ); |
|
381 | - |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Prepares item counts. |
|
386 | - * |
|
387 | - * @return array. |
|
388 | - */ |
|
389 | - protected function query_item_counts() { |
|
390 | - |
|
391 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
392 | - array( |
|
393 | - 'data' => array( |
|
394 | - 'quantity' => array( |
|
395 | - 'type' => 'invoice_item', |
|
396 | - 'function' => 'SUM', |
|
397 | - 'name' => 'invoice_item_count', |
|
398 | - ), |
|
399 | - 'post_date' => array( |
|
400 | - 'type' => 'post_data', |
|
401 | - 'function' => 'MIN', |
|
402 | - 'name' => 'post_date', |
|
403 | - ), |
|
404 | - ), |
|
405 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
406 | - 'order_by' => 'post_date ASC', |
|
407 | - 'query_type' => 'get_results', |
|
408 | - 'filter_range' => $this->report_range, |
|
409 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
410 | - ) |
|
411 | - ); |
|
412 | - |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Prepares refunded item counts. |
|
417 | - * |
|
418 | - * @return array. |
|
419 | - */ |
|
420 | - protected function count_refunded_items() { |
|
421 | - |
|
422 | - return (int) GetPaid_Reports_Helper::get_invoice_report_data( |
|
423 | - array( |
|
424 | - 'data' => array( |
|
425 | - 'quantity' => array( |
|
426 | - 'type' => 'invoice_item', |
|
427 | - 'function' => 'SUM', |
|
428 | - 'name' => 'invoice_item_count', |
|
429 | - ), |
|
430 | - ), |
|
431 | - 'query_type' => 'get_var', |
|
432 | - 'filter_range' => $this->report_range, |
|
433 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
434 | - ) |
|
435 | - ); |
|
436 | - |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Prepares daily invoice totals. |
|
441 | - * |
|
442 | - * @return array. |
|
443 | - */ |
|
444 | - protected function query_invoice_totals() { |
|
445 | - |
|
446 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
447 | - array( |
|
448 | - 'data' => array( |
|
449 | - 'total' => array( |
|
450 | - 'type' => 'invoice_data', |
|
451 | - 'function' => 'SUM', |
|
452 | - 'name' => 'total_sales', |
|
453 | - ), |
|
454 | - 'tax' => array( |
|
455 | - 'type' => 'invoice_data', |
|
456 | - 'function' => 'SUM', |
|
457 | - 'name' => 'total_tax', |
|
458 | - ), |
|
459 | - 'discount' => array( |
|
460 | - 'type' => 'invoice_data', |
|
461 | - 'function' => 'SUM', |
|
462 | - 'name' => 'total_discount', |
|
463 | - ), |
|
464 | - 'fees_total' => array( |
|
465 | - 'type' => 'invoice_data', |
|
466 | - 'function' => 'SUM', |
|
467 | - 'name' => 'total_fees', |
|
468 | - ), |
|
469 | - 'subtotal' => array( |
|
470 | - 'type' => 'invoice_data', |
|
471 | - 'function' => 'SUM', |
|
472 | - 'name' => 'subtotal', |
|
473 | - ), |
|
474 | - 'post_date' => array( |
|
475 | - 'type' => 'post_data', |
|
476 | - 'function' => '', |
|
477 | - 'name' => 'post_date', |
|
478 | - ), |
|
479 | - ), |
|
480 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
481 | - 'order_by' => 'post_date ASC', |
|
482 | - 'query_type' => 'get_results', |
|
483 | - 'filter_range' => $this->report_range, |
|
484 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-renewal' ), |
|
485 | - ) |
|
486 | - ); |
|
487 | - |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Prepares daily invoice totals. |
|
492 | - * |
|
493 | - * @return array. |
|
494 | - */ |
|
495 | - protected function query_refunded_totals() { |
|
496 | - |
|
497 | - return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
498 | - array( |
|
499 | - 'data' => array( |
|
500 | - 'total' => array( |
|
501 | - 'type' => 'invoice_data', |
|
502 | - 'function' => 'SUM', |
|
503 | - 'name' => 'total_sales', |
|
504 | - ), |
|
505 | - 'tax' => array( |
|
506 | - 'type' => 'invoice_data', |
|
507 | - 'function' => 'SUM', |
|
508 | - 'name' => 'total_tax', |
|
509 | - ), |
|
510 | - 'discount' => array( |
|
511 | - 'type' => 'invoice_data', |
|
512 | - 'function' => 'SUM', |
|
513 | - 'name' => 'total_discount', |
|
514 | - ), |
|
515 | - 'fees_total' => array( |
|
516 | - 'type' => 'invoice_data', |
|
517 | - 'function' => 'SUM', |
|
518 | - 'name' => 'total_fees', |
|
519 | - ), |
|
520 | - 'subtotal' => array( |
|
521 | - 'type' => 'invoice_data', |
|
522 | - 'function' => 'SUM', |
|
523 | - 'name' => 'subtotal', |
|
524 | - ), |
|
525 | - 'post_date' => array( |
|
526 | - 'type' => 'post_data', |
|
527 | - 'function' => '', |
|
528 | - 'name' => 'post_date', |
|
529 | - ), |
|
530 | - ), |
|
531 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
532 | - 'order_by' => 'post_date ASC', |
|
533 | - 'query_type' => 'get_results', |
|
534 | - 'filter_range' => $this->report_range, |
|
535 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
536 | - ) |
|
537 | - ); |
|
538 | - |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Get the Report's schema, conforming to JSON Schema. |
|
543 | - * |
|
544 | - * @return array |
|
545 | - */ |
|
546 | - public function get_item_schema() { |
|
547 | - |
|
548 | - $schema = array( |
|
549 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
550 | - 'title' => 'sales_report', |
|
551 | - 'type' => 'object', |
|
552 | - 'properties' => array( |
|
553 | - 'total_sales' => array( |
|
554 | - 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
555 | - 'type' => 'string', |
|
556 | - 'context' => array( 'view' ), |
|
557 | - 'readonly' => true, |
|
558 | - ), |
|
559 | - 'net_sales' => array( |
|
560 | - 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
561 | - 'type' => 'string', |
|
562 | - 'context' => array( 'view' ), |
|
563 | - 'readonly' => true, |
|
564 | - ), |
|
565 | - 'average_sales' => array( |
|
566 | - 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
567 | - 'type' => 'string', |
|
568 | - 'context' => array( 'view' ), |
|
569 | - 'readonly' => true, |
|
570 | - ), |
|
571 | - 'average_total_sales' => array( |
|
572 | - 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
573 | - 'type' => 'string', |
|
574 | - 'context' => array( 'view' ), |
|
575 | - 'readonly' => true, |
|
576 | - ), |
|
577 | - 'total_invoices' => array( |
|
578 | - 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
579 | - 'type' => 'integer', |
|
580 | - 'context' => array( 'view' ), |
|
581 | - 'readonly' => true, |
|
582 | - ), |
|
583 | - 'total_items' => array( |
|
584 | - 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
585 | - 'type' => 'integer', |
|
586 | - 'context' => array( 'view' ), |
|
587 | - 'readonly' => true, |
|
588 | - ), |
|
589 | - 'refunded_items' => array( |
|
590 | - 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
591 | - 'type' => 'integer', |
|
592 | - 'context' => array( 'view' ), |
|
593 | - 'readonly' => true, |
|
594 | - ), |
|
595 | - 'total_tax' => array( |
|
596 | - 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
597 | - 'type' => 'string', |
|
598 | - 'context' => array( 'view' ), |
|
599 | - 'readonly' => true, |
|
600 | - ), |
|
601 | - 'total_refunded_tax' => array( |
|
602 | - 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
603 | - 'type' => 'string', |
|
604 | - 'context' => array( 'view' ), |
|
605 | - 'readonly' => true, |
|
606 | - ), |
|
607 | - 'total_fees' => array( |
|
608 | - 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
609 | - 'type' => 'string', |
|
610 | - 'context' => array( 'view' ), |
|
611 | - 'readonly' => true, |
|
612 | - ), |
|
613 | - 'total_refunds' => array( |
|
614 | - 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
615 | - 'type' => 'integer', |
|
616 | - 'context' => array( 'view' ), |
|
617 | - 'readonly' => true, |
|
618 | - ), |
|
619 | - 'net_refunds' => array( |
|
620 | - 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
621 | - 'type' => 'integer', |
|
622 | - 'context' => array( 'view' ), |
|
623 | - 'readonly' => true, |
|
624 | - ), |
|
625 | - 'total_discount' => array( |
|
626 | - 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
627 | - 'type' => 'integer', |
|
628 | - 'context' => array( 'view' ), |
|
629 | - 'readonly' => true, |
|
630 | - ), |
|
631 | - 'totals' => array( |
|
632 | - 'description' => __( 'Totals.', 'invoicing' ), |
|
633 | - 'type' => 'array', |
|
634 | - 'items' => array( |
|
635 | - 'type' => 'array', |
|
636 | - ), |
|
637 | - 'context' => array( 'view' ), |
|
638 | - 'readonly' => true, |
|
639 | - ), |
|
640 | - 'interval' => array( |
|
641 | - 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
642 | - 'type' => 'integer', |
|
643 | - 'context' => array( 'view' ), |
|
644 | - 'readonly' => true, |
|
645 | - ), |
|
646 | - 'previous_range' => array( |
|
647 | - 'description' => __( 'The previous report period.', 'invoicing' ), |
|
648 | - 'type' => 'array', |
|
649 | - 'items' => array( |
|
650 | - 'type' => 'string', |
|
651 | - ), |
|
652 | - 'context' => array( 'view' ), |
|
653 | - 'readonly' => true, |
|
654 | - ), |
|
655 | - 'grouped_by' => array( |
|
656 | - 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
657 | - 'type' => 'string', |
|
658 | - 'context' => array( 'view' ), |
|
659 | - 'enum' => array( 'day', 'month' ), |
|
660 | - 'readonly' => true, |
|
661 | - ), |
|
662 | - 'currency' => array( |
|
663 | - 'description' => __( 'The default store currency.', 'invoicing' ), |
|
664 | - 'type' => 'string', |
|
665 | - 'context' => array( 'view' ), |
|
666 | - 'readonly' => true, |
|
667 | - ), |
|
668 | - 'currency_symbol' => array( |
|
669 | - 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
670 | - 'type' => 'string', |
|
671 | - 'context' => array( 'view' ), |
|
672 | - 'readonly' => true, |
|
673 | - ), |
|
674 | - 'currency_position' => array( |
|
675 | - 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
676 | - 'type' => 'string', |
|
677 | - 'context' => array( 'view' ), |
|
678 | - 'readonly' => true, |
|
679 | - ), |
|
680 | - 'decimal_places' => array( |
|
681 | - 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
682 | - 'type' => 'string', |
|
683 | - 'context' => array( 'view' ), |
|
684 | - 'readonly' => true, |
|
685 | - ), |
|
686 | - 'thousands_sep' => array( |
|
687 | - 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
688 | - 'type' => 'string', |
|
689 | - 'context' => array( 'view' ), |
|
690 | - 'readonly' => true, |
|
691 | - ), |
|
692 | - 'decimals_sep' => array( |
|
693 | - 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
694 | - 'type' => 'string', |
|
695 | - 'context' => array( 'view' ), |
|
696 | - 'readonly' => true, |
|
697 | - ), |
|
698 | - ), |
|
699 | - ); |
|
700 | - |
|
701 | - return $this->add_additional_fields_schema( $schema ); |
|
702 | - |
|
703 | - } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Makes sure the current user has access to READ the report APIs. |
|
70 | + * |
|
71 | + * @since 2.0.0 |
|
72 | + * @param WP_REST_Request $request Full data about the request. |
|
73 | + * @return WP_Error|boolean |
|
74 | + */ |
|
75 | + public function get_items_permissions_check( $request ) { |
|
76 | + |
|
77 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
78 | + return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
79 | + } |
|
80 | + |
|
81 | + return true; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Get sales reports. |
|
86 | + * |
|
87 | + * @param WP_REST_Request $request |
|
88 | + * @return array|WP_Error |
|
89 | + */ |
|
90 | + public function get_items( $request ) { |
|
91 | + $data = array(); |
|
92 | + $item = $this->prepare_item_for_response( null, $request ); |
|
93 | + $data[] = $this->prepare_response_for_collection( $item ); |
|
94 | + |
|
95 | + return rest_ensure_response( $data ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Prepare a report sales object for serialization. |
|
100 | + * |
|
101 | + * @param null $_ |
|
102 | + * @param WP_REST_Request $request Request object. |
|
103 | + * @return WP_REST_Response $response Response data. |
|
104 | + */ |
|
105 | + public function prepare_item_for_response( $_, $request ) { |
|
106 | + |
|
107 | + // Set report range. |
|
108 | + $this->report_range = $this->get_date_range( $request ); |
|
109 | + |
|
110 | + $report_data = $this->get_report_data(); |
|
111 | + $period_totals = array(); |
|
112 | + |
|
113 | + // Setup period totals by ensuring each period in the interval has data. |
|
114 | + $start_date = strtotime( $this->report_range['after'] ); |
|
115 | + |
|
116 | + if ( 'month' === $this->groupby ) { |
|
117 | + $start_date = strtotime( gmdate( 'Y-m-01', $start_date ) ); |
|
118 | + } |
|
119 | + |
|
120 | + for ( $i = 0; $i < $this->interval; $i++ ) { |
|
121 | + |
|
122 | + switch ( $this->groupby ) { |
|
123 | + case 'day': |
|
124 | + $time = gmdate( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
125 | + break; |
|
126 | + default: |
|
127 | + $time = gmdate( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
128 | + break; |
|
129 | + } |
|
130 | + |
|
131 | + // Set the defaults for each period. |
|
132 | + $period_totals[ $time ] = array( |
|
133 | + 'invoices' => 0, |
|
134 | + 'items' => 0, |
|
135 | + 'refunded_items' => 0, |
|
136 | + 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
137 | + 'subtotal' => wpinv_round_amount( 0.00 ), |
|
138 | + 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
139 | + 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
140 | + 'discount' => wpinv_round_amount( 0.00 ), |
|
141 | + ); |
|
142 | + |
|
143 | + foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
144 | + if ( ! isset( $period_totals[ $time ][ $key ] ) ) { |
|
145 | + $period_totals[ $time ][ $key ] = wpinv_round_amount( 0.00 ); |
|
146 | + } |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + // add total sales, total invoice count, total tax for each period |
|
151 | + $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
152 | + foreach ( $report_data->invoices as $invoice ) { |
|
153 | + $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
154 | + |
|
155 | + if ( ! isset( $period_totals[ $time ] ) ) { |
|
156 | + continue; |
|
157 | + } |
|
158 | + |
|
159 | + $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
160 | + $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
161 | + $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
162 | + $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
163 | + |
|
164 | + } |
|
165 | + |
|
166 | + foreach ( $report_data->refunds as $invoice ) { |
|
167 | + $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
168 | + |
|
169 | + if ( ! isset( $period_totals[ $time ] ) ) { |
|
170 | + continue; |
|
171 | + } |
|
172 | + |
|
173 | + $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
174 | + $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
175 | + $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
176 | + $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
177 | + |
|
178 | + } |
|
179 | + |
|
180 | + foreach ( $report_data->invoice_counts as $invoice ) { |
|
181 | + $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
182 | + |
|
183 | + if ( isset( $period_totals[ $time ] ) ) { |
|
184 | + $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + // Add total invoice items for each period. |
|
189 | + foreach ( $report_data->invoice_items as $invoice_item ) { |
|
190 | + $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : gmdate( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
191 | + |
|
192 | + if ( isset( $period_totals[ $time ] ) ) { |
|
193 | + $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + // Add total discount for each period. |
|
198 | + foreach ( $report_data->coupons as $discount ) { |
|
199 | + $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $discount->post_date ) ) : gmdate( 'Y-m', strtotime( $discount->post_date ) ); |
|
200 | + |
|
201 | + if ( isset( $period_totals[ $time ] ) ) { |
|
202 | + $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + // Extra fields. |
|
207 | + foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
208 | + |
|
209 | + // Abort unprepared. |
|
210 | + if ( ! isset( $report_data->$key ) ) { |
|
211 | + continue; |
|
212 | + } |
|
213 | + |
|
214 | + // Abort defaults. |
|
215 | + if ( in_array( $key, array( 'sales', 'refunds', 'tax', 'fees', 'discount', 'invoices', 'items' ) ) ) { |
|
216 | + continue; |
|
217 | + } |
|
218 | + |
|
219 | + // Set values. |
|
220 | + foreach ( $report_data->$key as $item ) { |
|
221 | + $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $item->date ) ) : gmdate( 'Y-m', strtotime( $item->date ) ); |
|
222 | + |
|
223 | + if ( isset( $period_totals[ $time ] ) ) { |
|
224 | + $period_totals[ $time ][ $key ] = wpinv_round_amount( $item->val ); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + unset( $report_data->$key ); |
|
229 | + } |
|
230 | + |
|
231 | + $report_data->totals = $period_totals; |
|
232 | + $report_data->grouped_by = $this->groupby; |
|
233 | + $report_data->interval = max( $this->interval, 1 ); |
|
234 | + $report_data->currency = wpinv_get_currency(); |
|
235 | + $report_data->currency_symbol = wpinv_currency_symbol(); |
|
236 | + $report_data->currency_position = wpinv_currency_position(); |
|
237 | + $report_data->decimal_places = wpinv_decimals(); |
|
238 | + $report_data->thousands_sep = wpinv_thousands_separator(); |
|
239 | + $report_data->decimals_sep = wpinv_decimal_separator(); |
|
240 | + $report_data->start_date = gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ); |
|
241 | + $report_data->end_date = gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ); |
|
242 | + $report_data->start_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ) ); |
|
243 | + $report_data->end_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ) ); |
|
244 | + $report_data->decimals_sep = wpinv_decimal_separator(); |
|
245 | + |
|
246 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
247 | + $data = $report_data; |
|
248 | + unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
249 | + $data = $this->add_additional_fields_to_object( (array) $data, $request ); |
|
250 | + $data = $this->filter_response_by_context( $data, $context ); |
|
251 | + |
|
252 | + // Wrap the data in a response object. |
|
253 | + $response = rest_ensure_response( $data ); |
|
254 | + $response->add_links( |
|
255 | + array( |
|
256 | + 'about' => array( |
|
257 | + 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
258 | + ), |
|
259 | + ) |
|
260 | + ); |
|
261 | + |
|
262 | + return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Get report data. |
|
267 | + * |
|
268 | + * @return stdClass |
|
269 | + */ |
|
270 | + public function get_report_data() { |
|
271 | + if ( empty( $this->report_data ) ) { |
|
272 | + $this->query_report_data(); |
|
273 | + } |
|
274 | + return $this->report_data; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Get all data needed for this report and store in the class. |
|
279 | + */ |
|
280 | + protected function query_report_data() { |
|
281 | + |
|
282 | + // Prepare reports. |
|
283 | + $this->report_data = (object) array( |
|
284 | + 'invoice_counts' => $this->query_invoice_counts(), //count, post_date |
|
285 | + 'coupons' => $this->query_coupon_counts(), // discount_amount, post_date |
|
286 | + 'invoice_items' => $this->query_item_counts(), // invoice_item_count, post_date |
|
287 | + 'refunded_items' => $this->count_refunded_items(), // invoice_item_count, post_date |
|
288 | + 'invoices' => $this->query_invoice_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
289 | + 'refunds' => $this->query_refunded_totals(), // total_sales, total_tax, total_discount, total_fees, subtotal, post_date |
|
290 | + 'previous_range' => $this->previous_range, |
|
291 | + ); |
|
292 | + |
|
293 | + // Calculated totals. |
|
294 | + $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
295 | + $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
296 | + $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
297 | + $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
298 | + $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
299 | + $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
300 | + $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
301 | + $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
302 | + $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
303 | + $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
304 | + $this->report_data->refunded_subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
305 | + $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
306 | + |
|
307 | + // Calculate average based on net. |
|
308 | + $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
309 | + $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
310 | + |
|
311 | + // Total invoices in this period, even if refunded. |
|
312 | + $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
313 | + |
|
314 | + // Items invoiced in this period, even if refunded. |
|
315 | + $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
316 | + |
|
317 | + // 3rd party filtering of report data |
|
318 | + $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data, $this ); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Prepares invoice counts. |
|
323 | + * |
|
324 | + * @return array. |
|
325 | + */ |
|
326 | + protected function query_invoice_counts() { |
|
327 | + |
|
328 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
329 | + array( |
|
330 | + 'data' => array( |
|
331 | + 'ID' => array( |
|
332 | + 'type' => 'post_data', |
|
333 | + 'function' => 'COUNT', |
|
334 | + 'name' => 'count', |
|
335 | + 'distinct' => true, |
|
336 | + ), |
|
337 | + 'post_date' => array( |
|
338 | + 'type' => 'post_data', |
|
339 | + 'function' => 'MIN', |
|
340 | + 'name' => 'post_date', |
|
341 | + ), |
|
342 | + ), |
|
343 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
344 | + 'order_by' => 'post_date ASC', |
|
345 | + 'query_type' => 'get_results', |
|
346 | + 'filter_range' => $this->report_range, |
|
347 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
348 | + ) |
|
349 | + ); |
|
350 | + |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Prepares coupon counts. |
|
355 | + * |
|
356 | + * @return array. |
|
357 | + */ |
|
358 | + protected function query_coupon_counts() { |
|
359 | + |
|
360 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
361 | + array( |
|
362 | + 'data' => array( |
|
363 | + 'discount' => array( |
|
364 | + 'type' => 'invoice_data', |
|
365 | + 'function' => 'SUM', |
|
366 | + 'name' => 'discount_amount', |
|
367 | + ), |
|
368 | + 'post_date' => array( |
|
369 | + 'type' => 'post_data', |
|
370 | + 'function' => 'MIN', |
|
371 | + 'name' => 'post_date', |
|
372 | + ), |
|
373 | + ), |
|
374 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
375 | + 'order_by' => 'post_date ASC', |
|
376 | + 'query_type' => 'get_results', |
|
377 | + 'filter_range' => $this->report_range, |
|
378 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
379 | + ) |
|
380 | + ); |
|
381 | + |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Prepares item counts. |
|
386 | + * |
|
387 | + * @return array. |
|
388 | + */ |
|
389 | + protected function query_item_counts() { |
|
390 | + |
|
391 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
392 | + array( |
|
393 | + 'data' => array( |
|
394 | + 'quantity' => array( |
|
395 | + 'type' => 'invoice_item', |
|
396 | + 'function' => 'SUM', |
|
397 | + 'name' => 'invoice_item_count', |
|
398 | + ), |
|
399 | + 'post_date' => array( |
|
400 | + 'type' => 'post_data', |
|
401 | + 'function' => 'MIN', |
|
402 | + 'name' => 'post_date', |
|
403 | + ), |
|
404 | + ), |
|
405 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
406 | + 'order_by' => 'post_date ASC', |
|
407 | + 'query_type' => 'get_results', |
|
408 | + 'filter_range' => $this->report_range, |
|
409 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
410 | + ) |
|
411 | + ); |
|
412 | + |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Prepares refunded item counts. |
|
417 | + * |
|
418 | + * @return array. |
|
419 | + */ |
|
420 | + protected function count_refunded_items() { |
|
421 | + |
|
422 | + return (int) GetPaid_Reports_Helper::get_invoice_report_data( |
|
423 | + array( |
|
424 | + 'data' => array( |
|
425 | + 'quantity' => array( |
|
426 | + 'type' => 'invoice_item', |
|
427 | + 'function' => 'SUM', |
|
428 | + 'name' => 'invoice_item_count', |
|
429 | + ), |
|
430 | + ), |
|
431 | + 'query_type' => 'get_var', |
|
432 | + 'filter_range' => $this->report_range, |
|
433 | + 'invoice_status' => array( 'wpi-refunded' ), |
|
434 | + ) |
|
435 | + ); |
|
436 | + |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Prepares daily invoice totals. |
|
441 | + * |
|
442 | + * @return array. |
|
443 | + */ |
|
444 | + protected function query_invoice_totals() { |
|
445 | + |
|
446 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
447 | + array( |
|
448 | + 'data' => array( |
|
449 | + 'total' => array( |
|
450 | + 'type' => 'invoice_data', |
|
451 | + 'function' => 'SUM', |
|
452 | + 'name' => 'total_sales', |
|
453 | + ), |
|
454 | + 'tax' => array( |
|
455 | + 'type' => 'invoice_data', |
|
456 | + 'function' => 'SUM', |
|
457 | + 'name' => 'total_tax', |
|
458 | + ), |
|
459 | + 'discount' => array( |
|
460 | + 'type' => 'invoice_data', |
|
461 | + 'function' => 'SUM', |
|
462 | + 'name' => 'total_discount', |
|
463 | + ), |
|
464 | + 'fees_total' => array( |
|
465 | + 'type' => 'invoice_data', |
|
466 | + 'function' => 'SUM', |
|
467 | + 'name' => 'total_fees', |
|
468 | + ), |
|
469 | + 'subtotal' => array( |
|
470 | + 'type' => 'invoice_data', |
|
471 | + 'function' => 'SUM', |
|
472 | + 'name' => 'subtotal', |
|
473 | + ), |
|
474 | + 'post_date' => array( |
|
475 | + 'type' => 'post_data', |
|
476 | + 'function' => '', |
|
477 | + 'name' => 'post_date', |
|
478 | + ), |
|
479 | + ), |
|
480 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
481 | + 'order_by' => 'post_date ASC', |
|
482 | + 'query_type' => 'get_results', |
|
483 | + 'filter_range' => $this->report_range, |
|
484 | + 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-renewal' ), |
|
485 | + ) |
|
486 | + ); |
|
487 | + |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Prepares daily invoice totals. |
|
492 | + * |
|
493 | + * @return array. |
|
494 | + */ |
|
495 | + protected function query_refunded_totals() { |
|
496 | + |
|
497 | + return (array) GetPaid_Reports_Helper::get_invoice_report_data( |
|
498 | + array( |
|
499 | + 'data' => array( |
|
500 | + 'total' => array( |
|
501 | + 'type' => 'invoice_data', |
|
502 | + 'function' => 'SUM', |
|
503 | + 'name' => 'total_sales', |
|
504 | + ), |
|
505 | + 'tax' => array( |
|
506 | + 'type' => 'invoice_data', |
|
507 | + 'function' => 'SUM', |
|
508 | + 'name' => 'total_tax', |
|
509 | + ), |
|
510 | + 'discount' => array( |
|
511 | + 'type' => 'invoice_data', |
|
512 | + 'function' => 'SUM', |
|
513 | + 'name' => 'total_discount', |
|
514 | + ), |
|
515 | + 'fees_total' => array( |
|
516 | + 'type' => 'invoice_data', |
|
517 | + 'function' => 'SUM', |
|
518 | + 'name' => 'total_fees', |
|
519 | + ), |
|
520 | + 'subtotal' => array( |
|
521 | + 'type' => 'invoice_data', |
|
522 | + 'function' => 'SUM', |
|
523 | + 'name' => 'subtotal', |
|
524 | + ), |
|
525 | + 'post_date' => array( |
|
526 | + 'type' => 'post_data', |
|
527 | + 'function' => '', |
|
528 | + 'name' => 'post_date', |
|
529 | + ), |
|
530 | + ), |
|
531 | + 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
532 | + 'order_by' => 'post_date ASC', |
|
533 | + 'query_type' => 'get_results', |
|
534 | + 'filter_range' => $this->report_range, |
|
535 | + 'invoice_status' => array( 'wpi-refunded' ), |
|
536 | + ) |
|
537 | + ); |
|
538 | + |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Get the Report's schema, conforming to JSON Schema. |
|
543 | + * |
|
544 | + * @return array |
|
545 | + */ |
|
546 | + public function get_item_schema() { |
|
547 | + |
|
548 | + $schema = array( |
|
549 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
550 | + 'title' => 'sales_report', |
|
551 | + 'type' => 'object', |
|
552 | + 'properties' => array( |
|
553 | + 'total_sales' => array( |
|
554 | + 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
555 | + 'type' => 'string', |
|
556 | + 'context' => array( 'view' ), |
|
557 | + 'readonly' => true, |
|
558 | + ), |
|
559 | + 'net_sales' => array( |
|
560 | + 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
561 | + 'type' => 'string', |
|
562 | + 'context' => array( 'view' ), |
|
563 | + 'readonly' => true, |
|
564 | + ), |
|
565 | + 'average_sales' => array( |
|
566 | + 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
567 | + 'type' => 'string', |
|
568 | + 'context' => array( 'view' ), |
|
569 | + 'readonly' => true, |
|
570 | + ), |
|
571 | + 'average_total_sales' => array( |
|
572 | + 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
573 | + 'type' => 'string', |
|
574 | + 'context' => array( 'view' ), |
|
575 | + 'readonly' => true, |
|
576 | + ), |
|
577 | + 'total_invoices' => array( |
|
578 | + 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
579 | + 'type' => 'integer', |
|
580 | + 'context' => array( 'view' ), |
|
581 | + 'readonly' => true, |
|
582 | + ), |
|
583 | + 'total_items' => array( |
|
584 | + 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
585 | + 'type' => 'integer', |
|
586 | + 'context' => array( 'view' ), |
|
587 | + 'readonly' => true, |
|
588 | + ), |
|
589 | + 'refunded_items' => array( |
|
590 | + 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
591 | + 'type' => 'integer', |
|
592 | + 'context' => array( 'view' ), |
|
593 | + 'readonly' => true, |
|
594 | + ), |
|
595 | + 'total_tax' => array( |
|
596 | + 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
597 | + 'type' => 'string', |
|
598 | + 'context' => array( 'view' ), |
|
599 | + 'readonly' => true, |
|
600 | + ), |
|
601 | + 'total_refunded_tax' => array( |
|
602 | + 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
603 | + 'type' => 'string', |
|
604 | + 'context' => array( 'view' ), |
|
605 | + 'readonly' => true, |
|
606 | + ), |
|
607 | + 'total_fees' => array( |
|
608 | + 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
609 | + 'type' => 'string', |
|
610 | + 'context' => array( 'view' ), |
|
611 | + 'readonly' => true, |
|
612 | + ), |
|
613 | + 'total_refunds' => array( |
|
614 | + 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
615 | + 'type' => 'integer', |
|
616 | + 'context' => array( 'view' ), |
|
617 | + 'readonly' => true, |
|
618 | + ), |
|
619 | + 'net_refunds' => array( |
|
620 | + 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
621 | + 'type' => 'integer', |
|
622 | + 'context' => array( 'view' ), |
|
623 | + 'readonly' => true, |
|
624 | + ), |
|
625 | + 'total_discount' => array( |
|
626 | + 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
627 | + 'type' => 'integer', |
|
628 | + 'context' => array( 'view' ), |
|
629 | + 'readonly' => true, |
|
630 | + ), |
|
631 | + 'totals' => array( |
|
632 | + 'description' => __( 'Totals.', 'invoicing' ), |
|
633 | + 'type' => 'array', |
|
634 | + 'items' => array( |
|
635 | + 'type' => 'array', |
|
636 | + ), |
|
637 | + 'context' => array( 'view' ), |
|
638 | + 'readonly' => true, |
|
639 | + ), |
|
640 | + 'interval' => array( |
|
641 | + 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
642 | + 'type' => 'integer', |
|
643 | + 'context' => array( 'view' ), |
|
644 | + 'readonly' => true, |
|
645 | + ), |
|
646 | + 'previous_range' => array( |
|
647 | + 'description' => __( 'The previous report period.', 'invoicing' ), |
|
648 | + 'type' => 'array', |
|
649 | + 'items' => array( |
|
650 | + 'type' => 'string', |
|
651 | + ), |
|
652 | + 'context' => array( 'view' ), |
|
653 | + 'readonly' => true, |
|
654 | + ), |
|
655 | + 'grouped_by' => array( |
|
656 | + 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
657 | + 'type' => 'string', |
|
658 | + 'context' => array( 'view' ), |
|
659 | + 'enum' => array( 'day', 'month' ), |
|
660 | + 'readonly' => true, |
|
661 | + ), |
|
662 | + 'currency' => array( |
|
663 | + 'description' => __( 'The default store currency.', 'invoicing' ), |
|
664 | + 'type' => 'string', |
|
665 | + 'context' => array( 'view' ), |
|
666 | + 'readonly' => true, |
|
667 | + ), |
|
668 | + 'currency_symbol' => array( |
|
669 | + 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
670 | + 'type' => 'string', |
|
671 | + 'context' => array( 'view' ), |
|
672 | + 'readonly' => true, |
|
673 | + ), |
|
674 | + 'currency_position' => array( |
|
675 | + 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
676 | + 'type' => 'string', |
|
677 | + 'context' => array( 'view' ), |
|
678 | + 'readonly' => true, |
|
679 | + ), |
|
680 | + 'decimal_places' => array( |
|
681 | + 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
682 | + 'type' => 'string', |
|
683 | + 'context' => array( 'view' ), |
|
684 | + 'readonly' => true, |
|
685 | + ), |
|
686 | + 'thousands_sep' => array( |
|
687 | + 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
688 | + 'type' => 'string', |
|
689 | + 'context' => array( 'view' ), |
|
690 | + 'readonly' => true, |
|
691 | + ), |
|
692 | + 'decimals_sep' => array( |
|
693 | + 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
694 | + 'type' => 'string', |
|
695 | + 'context' => array( 'view' ), |
|
696 | + 'readonly' => true, |
|
697 | + ), |
|
698 | + ), |
|
699 | + ); |
|
700 | + |
|
701 | + return $this->add_additional_fields_schema( $schema ); |
|
702 | + |
|
703 | + } |
|
704 | 704 | |
705 | 705 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | * @since 2.0.0 |
10 | 10 | */ |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * GetPaid REST reports controller class. |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @see register_rest_route() |
48 | 48 | */ |
49 | - public function register_namespace_routes( $namespace ) { |
|
49 | + public function register_namespace_routes($namespace) { |
|
50 | 50 | |
51 | 51 | // Get sales report. |
52 | 52 | register_rest_route( |
@@ -55,11 +55,11 @@ discard block |
||
55 | 55 | array( |
56 | 56 | array( |
57 | 57 | 'methods' => WP_REST_Server::READABLE, |
58 | - 'callback' => array( $this, 'get_items' ), |
|
59 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
58 | + 'callback' => array($this, 'get_items'), |
|
59 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
60 | 60 | 'args' => $this->get_collection_params(), |
61 | 61 | ), |
62 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
62 | + 'schema' => array($this, 'get_public_item_schema'), |
|
63 | 63 | ) |
64 | 64 | ); |
65 | 65 | |
@@ -72,10 +72,10 @@ discard block |
||
72 | 72 | * @param WP_REST_Request $request Full data about the request. |
73 | 73 | * @return WP_Error|boolean |
74 | 74 | */ |
75 | - public function get_items_permissions_check( $request ) { |
|
75 | + public function get_items_permissions_check($request) { |
|
76 | 76 | |
77 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
78 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
77 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
78 | + return new WP_Error('rest_cannot_view', __('Sorry, you cannot list resources.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | return true; |
@@ -87,12 +87,12 @@ discard block |
||
87 | 87 | * @param WP_REST_Request $request |
88 | 88 | * @return array|WP_Error |
89 | 89 | */ |
90 | - public function get_items( $request ) { |
|
90 | + public function get_items($request) { |
|
91 | 91 | $data = array(); |
92 | - $item = $this->prepare_item_for_response( null, $request ); |
|
93 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
92 | + $item = $this->prepare_item_for_response(null, $request); |
|
93 | + $data[] = $this->prepare_response_for_collection($item); |
|
94 | 94 | |
95 | - return rest_ensure_response( $data ); |
|
95 | + return rest_ensure_response($data); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -102,164 +102,164 @@ discard block |
||
102 | 102 | * @param WP_REST_Request $request Request object. |
103 | 103 | * @return WP_REST_Response $response Response data. |
104 | 104 | */ |
105 | - public function prepare_item_for_response( $_, $request ) { |
|
105 | + public function prepare_item_for_response($_, $request) { |
|
106 | 106 | |
107 | 107 | // Set report range. |
108 | - $this->report_range = $this->get_date_range( $request ); |
|
108 | + $this->report_range = $this->get_date_range($request); |
|
109 | 109 | |
110 | 110 | $report_data = $this->get_report_data(); |
111 | 111 | $period_totals = array(); |
112 | 112 | |
113 | 113 | // Setup period totals by ensuring each period in the interval has data. |
114 | - $start_date = strtotime( $this->report_range['after'] ); |
|
114 | + $start_date = strtotime($this->report_range['after']); |
|
115 | 115 | |
116 | - if ( 'month' === $this->groupby ) { |
|
117 | - $start_date = strtotime( gmdate( 'Y-m-01', $start_date ) ); |
|
116 | + if ('month' === $this->groupby) { |
|
117 | + $start_date = strtotime(gmdate('Y-m-01', $start_date)); |
|
118 | 118 | } |
119 | 119 | |
120 | - for ( $i = 0; $i < $this->interval; $i++ ) { |
|
120 | + for ($i = 0; $i < $this->interval; $i++) { |
|
121 | 121 | |
122 | - switch ( $this->groupby ) { |
|
122 | + switch ($this->groupby) { |
|
123 | 123 | case 'day': |
124 | - $time = gmdate( 'Y-m-d', strtotime( "+{$i} DAY", $start_date ) ); |
|
124 | + $time = gmdate('Y-m-d', strtotime("+{$i} DAY", $start_date)); |
|
125 | 125 | break; |
126 | 126 | default: |
127 | - $time = gmdate( 'Y-m', strtotime( "+{$i} MONTH", $start_date ) ); |
|
127 | + $time = gmdate('Y-m', strtotime("+{$i} MONTH", $start_date)); |
|
128 | 128 | break; |
129 | 129 | } |
130 | 130 | |
131 | 131 | // Set the defaults for each period. |
132 | - $period_totals[ $time ] = array( |
|
132 | + $period_totals[$time] = array( |
|
133 | 133 | 'invoices' => 0, |
134 | 134 | 'items' => 0, |
135 | 135 | 'refunded_items' => 0, |
136 | - 'refunded_tax' => wpinv_round_amount( 0.00 ), |
|
137 | - 'subtotal' => wpinv_round_amount( 0.00 ), |
|
138 | - 'refunded_subtotal' => wpinv_round_amount( 0.00 ), |
|
139 | - 'refunded_fees' => wpinv_round_amount( 0.00 ), |
|
140 | - 'discount' => wpinv_round_amount( 0.00 ), |
|
136 | + 'refunded_tax' => wpinv_round_amount(0.00), |
|
137 | + 'subtotal' => wpinv_round_amount(0.00), |
|
138 | + 'refunded_subtotal' => wpinv_round_amount(0.00), |
|
139 | + 'refunded_fees' => wpinv_round_amount(0.00), |
|
140 | + 'discount' => wpinv_round_amount(0.00), |
|
141 | 141 | ); |
142 | 142 | |
143 | - foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
144 | - if ( ! isset( $period_totals[ $time ][ $key ] ) ) { |
|
145 | - $period_totals[ $time ][ $key ] = wpinv_round_amount( 0.00 ); |
|
143 | + foreach (array_keys(wpinv_get_report_graphs()) as $key) { |
|
144 | + if (!isset($period_totals[$time][$key])) { |
|
145 | + $period_totals[$time][$key] = wpinv_round_amount(0.00); |
|
146 | 146 | } |
147 | 147 | } |
148 | 148 | } |
149 | 149 | |
150 | 150 | // add total sales, total invoice count, total tax for each period |
151 | - $date_format = ( 'day' === $this->groupby ) ? 'Y-m-d' : 'Y-m'; |
|
152 | - foreach ( $report_data->invoices as $invoice ) { |
|
153 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
151 | + $date_format = ('day' === $this->groupby) ? 'Y-m-d' : 'Y-m'; |
|
152 | + foreach ($report_data->invoices as $invoice) { |
|
153 | + $time = gmdate($date_format, strtotime($invoice->post_date)); |
|
154 | 154 | |
155 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
155 | + if (!isset($period_totals[$time])) { |
|
156 | 156 | continue; |
157 | 157 | } |
158 | 158 | |
159 | - $period_totals[ $time ]['sales'] = wpinv_round_amount( $invoice->total_sales ); |
|
160 | - $period_totals[ $time ]['tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
161 | - $period_totals[ $time ]['subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
162 | - $period_totals[ $time ]['fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
159 | + $period_totals[$time]['sales'] = wpinv_round_amount($invoice->total_sales); |
|
160 | + $period_totals[$time]['tax'] = wpinv_round_amount($invoice->total_tax); |
|
161 | + $period_totals[$time]['subtotal'] = wpinv_round_amount($invoice->subtotal); |
|
162 | + $period_totals[$time]['fees'] = wpinv_round_amount($invoice->total_fees); |
|
163 | 163 | |
164 | 164 | } |
165 | 165 | |
166 | - foreach ( $report_data->refunds as $invoice ) { |
|
167 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
166 | + foreach ($report_data->refunds as $invoice) { |
|
167 | + $time = gmdate($date_format, strtotime($invoice->post_date)); |
|
168 | 168 | |
169 | - if ( ! isset( $period_totals[ $time ] ) ) { |
|
169 | + if (!isset($period_totals[$time])) { |
|
170 | 170 | continue; |
171 | 171 | } |
172 | 172 | |
173 | - $period_totals[ $time ]['refunds'] = wpinv_round_amount( $invoice->total_sales ); |
|
174 | - $period_totals[ $time ]['refunded_tax'] = wpinv_round_amount( $invoice->total_tax ); |
|
175 | - $period_totals[ $time ]['refunded_subtotal'] = wpinv_round_amount( $invoice->subtotal ); |
|
176 | - $period_totals[ $time ]['refunded_fees'] = wpinv_round_amount( $invoice->total_fees ); |
|
173 | + $period_totals[$time]['refunds'] = wpinv_round_amount($invoice->total_sales); |
|
174 | + $period_totals[$time]['refunded_tax'] = wpinv_round_amount($invoice->total_tax); |
|
175 | + $period_totals[$time]['refunded_subtotal'] = wpinv_round_amount($invoice->subtotal); |
|
176 | + $period_totals[$time]['refunded_fees'] = wpinv_round_amount($invoice->total_fees); |
|
177 | 177 | |
178 | 178 | } |
179 | 179 | |
180 | - foreach ( $report_data->invoice_counts as $invoice ) { |
|
181 | - $time = gmdate( $date_format, strtotime( $invoice->post_date ) ); |
|
180 | + foreach ($report_data->invoice_counts as $invoice) { |
|
181 | + $time = gmdate($date_format, strtotime($invoice->post_date)); |
|
182 | 182 | |
183 | - if ( isset( $period_totals[ $time ] ) ) { |
|
184 | - $period_totals[ $time ]['invoices'] = (int) $invoice->count; |
|
183 | + if (isset($period_totals[$time])) { |
|
184 | + $period_totals[$time]['invoices'] = (int) $invoice->count; |
|
185 | 185 | } |
186 | 186 | } |
187 | 187 | |
188 | 188 | // Add total invoice items for each period. |
189 | - foreach ( $report_data->invoice_items as $invoice_item ) { |
|
190 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $invoice_item->post_date ) ) : gmdate( 'Y-m', strtotime( $invoice_item->post_date ) ); |
|
189 | + foreach ($report_data->invoice_items as $invoice_item) { |
|
190 | + $time = ('day' === $this->groupby) ? gmdate('Y-m-d', strtotime($invoice_item->post_date)) : gmdate('Y-m', strtotime($invoice_item->post_date)); |
|
191 | 191 | |
192 | - if ( isset( $period_totals[ $time ] ) ) { |
|
193 | - $period_totals[ $time ]['items'] = (int) $invoice_item->invoice_item_count; |
|
192 | + if (isset($period_totals[$time])) { |
|
193 | + $period_totals[$time]['items'] = (int) $invoice_item->invoice_item_count; |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | 197 | // Add total discount for each period. |
198 | - foreach ( $report_data->coupons as $discount ) { |
|
199 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $discount->post_date ) ) : gmdate( 'Y-m', strtotime( $discount->post_date ) ); |
|
198 | + foreach ($report_data->coupons as $discount) { |
|
199 | + $time = ('day' === $this->groupby) ? gmdate('Y-m-d', strtotime($discount->post_date)) : gmdate('Y-m', strtotime($discount->post_date)); |
|
200 | 200 | |
201 | - if ( isset( $period_totals[ $time ] ) ) { |
|
202 | - $period_totals[ $time ]['discount'] = wpinv_round_amount( $discount->discount_amount ); |
|
201 | + if (isset($period_totals[$time])) { |
|
202 | + $period_totals[$time]['discount'] = wpinv_round_amount($discount->discount_amount); |
|
203 | 203 | } |
204 | 204 | } |
205 | 205 | |
206 | 206 | // Extra fields. |
207 | - foreach ( array_keys( wpinv_get_report_graphs() ) as $key ) { |
|
207 | + foreach (array_keys(wpinv_get_report_graphs()) as $key) { |
|
208 | 208 | |
209 | 209 | // Abort unprepared. |
210 | - if ( ! isset( $report_data->$key ) ) { |
|
210 | + if (!isset($report_data->$key)) { |
|
211 | 211 | continue; |
212 | 212 | } |
213 | 213 | |
214 | 214 | // Abort defaults. |
215 | - if ( in_array( $key, array( 'sales', 'refunds', 'tax', 'fees', 'discount', 'invoices', 'items' ) ) ) { |
|
215 | + if (in_array($key, array('sales', 'refunds', 'tax', 'fees', 'discount', 'invoices', 'items'))) { |
|
216 | 216 | continue; |
217 | 217 | } |
218 | 218 | |
219 | 219 | // Set values. |
220 | - foreach ( $report_data->$key as $item ) { |
|
221 | - $time = ( 'day' === $this->groupby ) ? gmdate( 'Y-m-d', strtotime( $item->date ) ) : gmdate( 'Y-m', strtotime( $item->date ) ); |
|
220 | + foreach ($report_data->$key as $item) { |
|
221 | + $time = ('day' === $this->groupby) ? gmdate('Y-m-d', strtotime($item->date)) : gmdate('Y-m', strtotime($item->date)); |
|
222 | 222 | |
223 | - if ( isset( $period_totals[ $time ] ) ) { |
|
224 | - $period_totals[ $time ][ $key ] = wpinv_round_amount( $item->val ); |
|
223 | + if (isset($period_totals[$time])) { |
|
224 | + $period_totals[$time][$key] = wpinv_round_amount($item->val); |
|
225 | 225 | } |
226 | 226 | } |
227 | 227 | |
228 | - unset( $report_data->$key ); |
|
228 | + unset($report_data->$key); |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | $report_data->totals = $period_totals; |
232 | 232 | $report_data->grouped_by = $this->groupby; |
233 | - $report_data->interval = max( $this->interval, 1 ); |
|
233 | + $report_data->interval = max($this->interval, 1); |
|
234 | 234 | $report_data->currency = wpinv_get_currency(); |
235 | 235 | $report_data->currency_symbol = wpinv_currency_symbol(); |
236 | 236 | $report_data->currency_position = wpinv_currency_position(); |
237 | 237 | $report_data->decimal_places = wpinv_decimals(); |
238 | 238 | $report_data->thousands_sep = wpinv_thousands_separator(); |
239 | 239 | $report_data->decimals_sep = wpinv_decimal_separator(); |
240 | - $report_data->start_date = gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ); |
|
241 | - $report_data->end_date = gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ); |
|
242 | - $report_data->start_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['after'] ) ) ); |
|
243 | - $report_data->end_date_locale = getpaid_format_date( gmdate( 'Y-m-d', strtotime( $this->report_range['before'] ) ) ); |
|
240 | + $report_data->start_date = gmdate('Y-m-d', strtotime($this->report_range['after'])); |
|
241 | + $report_data->end_date = gmdate('Y-m-d', strtotime($this->report_range['before'])); |
|
242 | + $report_data->start_date_locale = getpaid_format_date(gmdate('Y-m-d', strtotime($this->report_range['after']))); |
|
243 | + $report_data->end_date_locale = getpaid_format_date(gmdate('Y-m-d', strtotime($this->report_range['before']))); |
|
244 | 244 | $report_data->decimals_sep = wpinv_decimal_separator(); |
245 | 245 | |
246 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
246 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
247 | 247 | $data = $report_data; |
248 | - unset( $data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items ); |
|
249 | - $data = $this->add_additional_fields_to_object( (array) $data, $request ); |
|
250 | - $data = $this->filter_response_by_context( $data, $context ); |
|
248 | + unset($data->invoice_counts, $data->invoices, $data->coupons, $data->refunds, $data->invoice_items); |
|
249 | + $data = $this->add_additional_fields_to_object((array) $data, $request); |
|
250 | + $data = $this->filter_response_by_context($data, $context); |
|
251 | 251 | |
252 | 252 | // Wrap the data in a response object. |
253 | - $response = rest_ensure_response( $data ); |
|
253 | + $response = rest_ensure_response($data); |
|
254 | 254 | $response->add_links( |
255 | 255 | array( |
256 | 256 | 'about' => array( |
257 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
257 | + 'href' => rest_url(sprintf('%s/reports', $this->namespace)), |
|
258 | 258 | ), |
259 | 259 | ) |
260 | 260 | ); |
261 | 261 | |
262 | - return apply_filters( 'getpaid_rest_prepare_report_sales', $response, $report_data, $request ); |
|
262 | + return apply_filters('getpaid_rest_prepare_report_sales', $response, $report_data, $request); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | /** |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | * @return stdClass |
269 | 269 | */ |
270 | 270 | public function get_report_data() { |
271 | - if ( empty( $this->report_data ) ) { |
|
271 | + if (empty($this->report_data)) { |
|
272 | 272 | $this->query_report_data(); |
273 | 273 | } |
274 | 274 | return $this->report_data; |
@@ -291,31 +291,31 @@ discard block |
||
291 | 291 | ); |
292 | 292 | |
293 | 293 | // Calculated totals. |
294 | - $this->report_data->total_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_tax' ) ) ); |
|
295 | - $this->report_data->total_sales = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_sales' ) ) ); |
|
296 | - $this->report_data->total_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_discount' ) ) ); |
|
297 | - $this->report_data->total_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'total_fees' ) ) ); |
|
298 | - $this->report_data->subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->invoices, 'subtotal' ) ) ); |
|
299 | - $this->report_data->net_sales = wpinv_round_amount( $this->report_data->total_sales - max( 0, $this->report_data->total_tax ) ); |
|
300 | - $this->report_data->total_refunded_tax = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_tax' ) ) ); |
|
301 | - $this->report_data->total_refunds = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_sales' ) ) ); |
|
302 | - $this->report_data->refunded_discount = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_discount' ) ) ); |
|
303 | - $this->report_data->refunded_fees = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'total_fees' ) ) ); |
|
304 | - $this->report_data->refunded_subtotal = wpinv_round_amount( array_sum( wp_list_pluck( $this->report_data->refunds, 'subtotal' ) ) ); |
|
305 | - $this->report_data->net_refunds = wpinv_round_amount( $this->report_data->total_refunds + max( 0, $this->report_data->total_refunded_tax ) ); |
|
294 | + $this->report_data->total_tax = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_tax'))); |
|
295 | + $this->report_data->total_sales = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_sales'))); |
|
296 | + $this->report_data->total_discount = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_discount'))); |
|
297 | + $this->report_data->total_fees = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'total_fees'))); |
|
298 | + $this->report_data->subtotal = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->invoices, 'subtotal'))); |
|
299 | + $this->report_data->net_sales = wpinv_round_amount($this->report_data->total_sales - max(0, $this->report_data->total_tax)); |
|
300 | + $this->report_data->total_refunded_tax = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_tax'))); |
|
301 | + $this->report_data->total_refunds = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_sales'))); |
|
302 | + $this->report_data->refunded_discount = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_discount'))); |
|
303 | + $this->report_data->refunded_fees = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'total_fees'))); |
|
304 | + $this->report_data->refunded_subtotal = wpinv_round_amount(array_sum(wp_list_pluck($this->report_data->refunds, 'subtotal'))); |
|
305 | + $this->report_data->net_refunds = wpinv_round_amount($this->report_data->total_refunds + max(0, $this->report_data->total_refunded_tax)); |
|
306 | 306 | |
307 | 307 | // Calculate average based on net. |
308 | - $this->report_data->average_sales = wpinv_round_amount( $this->report_data->net_sales / max( $this->interval, 1 ), 2 ); |
|
309 | - $this->report_data->average_total_sales = wpinv_round_amount( $this->report_data->total_sales / max( $this->interval, 1 ), 2 ); |
|
308 | + $this->report_data->average_sales = wpinv_round_amount($this->report_data->net_sales / max($this->interval, 1), 2); |
|
309 | + $this->report_data->average_total_sales = wpinv_round_amount($this->report_data->total_sales / max($this->interval, 1), 2); |
|
310 | 310 | |
311 | 311 | // Total invoices in this period, even if refunded. |
312 | - $this->report_data->total_invoices = absint( array_sum( wp_list_pluck( $this->report_data->invoice_counts, 'count' ) ) ); |
|
312 | + $this->report_data->total_invoices = absint(array_sum(wp_list_pluck($this->report_data->invoice_counts, 'count'))); |
|
313 | 313 | |
314 | 314 | // Items invoiced in this period, even if refunded. |
315 | - $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->invoice_items, 'invoice_item_count' ) ) ); |
|
315 | + $this->report_data->total_items = absint(array_sum(wp_list_pluck($this->report_data->invoice_items, 'invoice_item_count'))); |
|
316 | 316 | |
317 | 317 | // 3rd party filtering of report data |
318 | - $this->report_data = apply_filters( 'getpaid_rest_api_filter_report_data', $this->report_data, $this ); |
|
318 | + $this->report_data = apply_filters('getpaid_rest_api_filter_report_data', $this->report_data, $this); |
|
319 | 319 | } |
320 | 320 | |
321 | 321 | /** |
@@ -340,11 +340,11 @@ discard block |
||
340 | 340 | 'name' => 'post_date', |
341 | 341 | ), |
342 | 342 | ), |
343 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
343 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
344 | 344 | 'order_by' => 'post_date ASC', |
345 | 345 | 'query_type' => 'get_results', |
346 | 346 | 'filter_range' => $this->report_range, |
347 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
347 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal'), |
|
348 | 348 | ) |
349 | 349 | ); |
350 | 350 | |
@@ -371,11 +371,11 @@ discard block |
||
371 | 371 | 'name' => 'post_date', |
372 | 372 | ), |
373 | 373 | ), |
374 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
374 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
375 | 375 | 'order_by' => 'post_date ASC', |
376 | 376 | 'query_type' => 'get_results', |
377 | 377 | 'filter_range' => $this->report_range, |
378 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
378 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal'), |
|
379 | 379 | ) |
380 | 380 | ); |
381 | 381 | |
@@ -402,11 +402,11 @@ discard block |
||
402 | 402 | 'name' => 'post_date', |
403 | 403 | ), |
404 | 404 | ), |
405 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
405 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
406 | 406 | 'order_by' => 'post_date ASC', |
407 | 407 | 'query_type' => 'get_results', |
408 | 408 | 'filter_range' => $this->report_range, |
409 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal' ), |
|
409 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-renewal'), |
|
410 | 410 | ) |
411 | 411 | ); |
412 | 412 | |
@@ -430,7 +430,7 @@ discard block |
||
430 | 430 | ), |
431 | 431 | 'query_type' => 'get_var', |
432 | 432 | 'filter_range' => $this->report_range, |
433 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
433 | + 'invoice_status' => array('wpi-refunded'), |
|
434 | 434 | ) |
435 | 435 | ); |
436 | 436 | |
@@ -477,11 +477,11 @@ discard block |
||
477 | 477 | 'name' => 'post_date', |
478 | 478 | ), |
479 | 479 | ), |
480 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
480 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
481 | 481 | 'order_by' => 'post_date ASC', |
482 | 482 | 'query_type' => 'get_results', |
483 | 483 | 'filter_range' => $this->report_range, |
484 | - 'invoice_status' => array( 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-renewal' ), |
|
484 | + 'invoice_status' => array('publish', 'wpi-processing', 'wpi-onhold', 'wpi-renewal'), |
|
485 | 485 | ) |
486 | 486 | ); |
487 | 487 | |
@@ -528,11 +528,11 @@ discard block |
||
528 | 528 | 'name' => 'post_date', |
529 | 529 | ), |
530 | 530 | ), |
531 | - 'group_by' => $this->get_group_by_sql( 'posts.post_date' ), |
|
531 | + 'group_by' => $this->get_group_by_sql('posts.post_date'), |
|
532 | 532 | 'order_by' => 'post_date ASC', |
533 | 533 | 'query_type' => 'get_results', |
534 | 534 | 'filter_range' => $this->report_range, |
535 | - 'invoice_status' => array( 'wpi-refunded' ), |
|
535 | + 'invoice_status' => array('wpi-refunded'), |
|
536 | 536 | ) |
537 | 537 | ); |
538 | 538 | |
@@ -551,154 +551,154 @@ discard block |
||
551 | 551 | 'type' => 'object', |
552 | 552 | 'properties' => array( |
553 | 553 | 'total_sales' => array( |
554 | - 'description' => __( 'Gross sales in the period.', 'invoicing' ), |
|
554 | + 'description' => __('Gross sales in the period.', 'invoicing'), |
|
555 | 555 | 'type' => 'string', |
556 | - 'context' => array( 'view' ), |
|
556 | + 'context' => array('view'), |
|
557 | 557 | 'readonly' => true, |
558 | 558 | ), |
559 | 559 | 'net_sales' => array( |
560 | - 'description' => __( 'Net sales in the period.', 'invoicing' ), |
|
560 | + 'description' => __('Net sales in the period.', 'invoicing'), |
|
561 | 561 | 'type' => 'string', |
562 | - 'context' => array( 'view' ), |
|
562 | + 'context' => array('view'), |
|
563 | 563 | 'readonly' => true, |
564 | 564 | ), |
565 | 565 | 'average_sales' => array( |
566 | - 'description' => __( 'Average net daily sales.', 'invoicing' ), |
|
566 | + 'description' => __('Average net daily sales.', 'invoicing'), |
|
567 | 567 | 'type' => 'string', |
568 | - 'context' => array( 'view' ), |
|
568 | + 'context' => array('view'), |
|
569 | 569 | 'readonly' => true, |
570 | 570 | ), |
571 | 571 | 'average_total_sales' => array( |
572 | - 'description' => __( 'Average gross daily sales.', 'invoicing' ), |
|
572 | + 'description' => __('Average gross daily sales.', 'invoicing'), |
|
573 | 573 | 'type' => 'string', |
574 | - 'context' => array( 'view' ), |
|
574 | + 'context' => array('view'), |
|
575 | 575 | 'readonly' => true, |
576 | 576 | ), |
577 | 577 | 'total_invoices' => array( |
578 | - 'description' => __( 'Number of paid invoices.', 'invoicing' ), |
|
578 | + 'description' => __('Number of paid invoices.', 'invoicing'), |
|
579 | 579 | 'type' => 'integer', |
580 | - 'context' => array( 'view' ), |
|
580 | + 'context' => array('view'), |
|
581 | 581 | 'readonly' => true, |
582 | 582 | ), |
583 | 583 | 'total_items' => array( |
584 | - 'description' => __( 'Number of items purchased.', 'invoicing' ), |
|
584 | + 'description' => __('Number of items purchased.', 'invoicing'), |
|
585 | 585 | 'type' => 'integer', |
586 | - 'context' => array( 'view' ), |
|
586 | + 'context' => array('view'), |
|
587 | 587 | 'readonly' => true, |
588 | 588 | ), |
589 | 589 | 'refunded_items' => array( |
590 | - 'description' => __( 'Number of items refunded.', 'invoicing' ), |
|
590 | + 'description' => __('Number of items refunded.', 'invoicing'), |
|
591 | 591 | 'type' => 'integer', |
592 | - 'context' => array( 'view' ), |
|
592 | + 'context' => array('view'), |
|
593 | 593 | 'readonly' => true, |
594 | 594 | ), |
595 | 595 | 'total_tax' => array( |
596 | - 'description' => __( 'Total charged for taxes.', 'invoicing' ), |
|
596 | + 'description' => __('Total charged for taxes.', 'invoicing'), |
|
597 | 597 | 'type' => 'string', |
598 | - 'context' => array( 'view' ), |
|
598 | + 'context' => array('view'), |
|
599 | 599 | 'readonly' => true, |
600 | 600 | ), |
601 | 601 | 'total_refunded_tax' => array( |
602 | - 'description' => __( 'Total refunded for taxes.', 'invoicing' ), |
|
602 | + 'description' => __('Total refunded for taxes.', 'invoicing'), |
|
603 | 603 | 'type' => 'string', |
604 | - 'context' => array( 'view' ), |
|
604 | + 'context' => array('view'), |
|
605 | 605 | 'readonly' => true, |
606 | 606 | ), |
607 | 607 | 'total_fees' => array( |
608 | - 'description' => __( 'Total fees charged.', 'invoicing' ), |
|
608 | + 'description' => __('Total fees charged.', 'invoicing'), |
|
609 | 609 | 'type' => 'string', |
610 | - 'context' => array( 'view' ), |
|
610 | + 'context' => array('view'), |
|
611 | 611 | 'readonly' => true, |
612 | 612 | ), |
613 | 613 | 'total_refunds' => array( |
614 | - 'description' => __( 'Total of refunded invoices.', 'invoicing' ), |
|
614 | + 'description' => __('Total of refunded invoices.', 'invoicing'), |
|
615 | 615 | 'type' => 'integer', |
616 | - 'context' => array( 'view' ), |
|
616 | + 'context' => array('view'), |
|
617 | 617 | 'readonly' => true, |
618 | 618 | ), |
619 | 619 | 'net_refunds' => array( |
620 | - 'description' => __( 'Net of refunded invoices.', 'invoicing' ), |
|
620 | + 'description' => __('Net of refunded invoices.', 'invoicing'), |
|
621 | 621 | 'type' => 'integer', |
622 | - 'context' => array( 'view' ), |
|
622 | + 'context' => array('view'), |
|
623 | 623 | 'readonly' => true, |
624 | 624 | ), |
625 | 625 | 'total_discount' => array( |
626 | - 'description' => __( 'Total of discounts used.', 'invoicing' ), |
|
626 | + 'description' => __('Total of discounts used.', 'invoicing'), |
|
627 | 627 | 'type' => 'integer', |
628 | - 'context' => array( 'view' ), |
|
628 | + 'context' => array('view'), |
|
629 | 629 | 'readonly' => true, |
630 | 630 | ), |
631 | 631 | 'totals' => array( |
632 | - 'description' => __( 'Totals.', 'invoicing' ), |
|
632 | + 'description' => __('Totals.', 'invoicing'), |
|
633 | 633 | 'type' => 'array', |
634 | 634 | 'items' => array( |
635 | 635 | 'type' => 'array', |
636 | 636 | ), |
637 | - 'context' => array( 'view' ), |
|
637 | + 'context' => array('view'), |
|
638 | 638 | 'readonly' => true, |
639 | 639 | ), |
640 | 640 | 'interval' => array( |
641 | - 'description' => __( 'Number of months/days in the report period.', 'invoicing' ), |
|
641 | + 'description' => __('Number of months/days in the report period.', 'invoicing'), |
|
642 | 642 | 'type' => 'integer', |
643 | - 'context' => array( 'view' ), |
|
643 | + 'context' => array('view'), |
|
644 | 644 | 'readonly' => true, |
645 | 645 | ), |
646 | 646 | 'previous_range' => array( |
647 | - 'description' => __( 'The previous report period.', 'invoicing' ), |
|
647 | + 'description' => __('The previous report period.', 'invoicing'), |
|
648 | 648 | 'type' => 'array', |
649 | 649 | 'items' => array( |
650 | 650 | 'type' => 'string', |
651 | 651 | ), |
652 | - 'context' => array( 'view' ), |
|
652 | + 'context' => array('view'), |
|
653 | 653 | 'readonly' => true, |
654 | 654 | ), |
655 | 655 | 'grouped_by' => array( |
656 | - 'description' => __( 'The period used to group the totals.', 'invoicing' ), |
|
656 | + 'description' => __('The period used to group the totals.', 'invoicing'), |
|
657 | 657 | 'type' => 'string', |
658 | - 'context' => array( 'view' ), |
|
659 | - 'enum' => array( 'day', 'month' ), |
|
658 | + 'context' => array('view'), |
|
659 | + 'enum' => array('day', 'month'), |
|
660 | 660 | 'readonly' => true, |
661 | 661 | ), |
662 | 662 | 'currency' => array( |
663 | - 'description' => __( 'The default store currency.', 'invoicing' ), |
|
663 | + 'description' => __('The default store currency.', 'invoicing'), |
|
664 | 664 | 'type' => 'string', |
665 | - 'context' => array( 'view' ), |
|
665 | + 'context' => array('view'), |
|
666 | 666 | 'readonly' => true, |
667 | 667 | ), |
668 | 668 | 'currency_symbol' => array( |
669 | - 'description' => __( 'The default store currency symbol.', 'invoicing' ), |
|
669 | + 'description' => __('The default store currency symbol.', 'invoicing'), |
|
670 | 670 | 'type' => 'string', |
671 | - 'context' => array( 'view' ), |
|
671 | + 'context' => array('view'), |
|
672 | 672 | 'readonly' => true, |
673 | 673 | ), |
674 | 674 | 'currency_position' => array( |
675 | - 'description' => __( 'The default store currency position.', 'invoicing' ), |
|
675 | + 'description' => __('The default store currency position.', 'invoicing'), |
|
676 | 676 | 'type' => 'string', |
677 | - 'context' => array( 'view' ), |
|
677 | + 'context' => array('view'), |
|
678 | 678 | 'readonly' => true, |
679 | 679 | ), |
680 | 680 | 'decimal_places' => array( |
681 | - 'description' => __( 'The default store decimal places.', 'invoicing' ), |
|
681 | + 'description' => __('The default store decimal places.', 'invoicing'), |
|
682 | 682 | 'type' => 'string', |
683 | - 'context' => array( 'view' ), |
|
683 | + 'context' => array('view'), |
|
684 | 684 | 'readonly' => true, |
685 | 685 | ), |
686 | 686 | 'thousands_sep' => array( |
687 | - 'description' => __( 'The default store thousands separator.', 'invoicing' ), |
|
687 | + 'description' => __('The default store thousands separator.', 'invoicing'), |
|
688 | 688 | 'type' => 'string', |
689 | - 'context' => array( 'view' ), |
|
689 | + 'context' => array('view'), |
|
690 | 690 | 'readonly' => true, |
691 | 691 | ), |
692 | 692 | 'decimals_sep' => array( |
693 | - 'description' => __( 'The default store decimals separator.', 'invoicing' ), |
|
693 | + 'description' => __('The default store decimals separator.', 'invoicing'), |
|
694 | 694 | 'type' => 'string', |
695 | - 'context' => array( 'view' ), |
|
695 | + 'context' => array('view'), |
|
696 | 696 | 'readonly' => true, |
697 | 697 | ), |
698 | 698 | ), |
699 | 699 | ); |
700 | 700 | |
701 | - return $this->add_additional_fields_schema( $schema ); |
|
701 | + return $this->add_additional_fields_schema($schema); |
|
702 | 702 | |
703 | 703 | } |
704 | 704 |
@@ -18,756 +18,756 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Settings_Controller extends GetPaid_REST_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * An array of available settings. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $settings; |
|
27 | - |
|
28 | - /** |
|
29 | - * Route base. |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $rest_base = 'settings'; |
|
34 | - |
|
35 | - /** |
|
36 | - * Registers the routes for the objects of the controller. |
|
37 | - * |
|
38 | - * @since 2.0.0 |
|
39 | - * |
|
40 | - * @see register_rest_route() |
|
41 | - */ |
|
42 | - public function register_namespace_routes( $namespace ) { |
|
43 | - |
|
44 | - // List all registered tabs. |
|
45 | - register_rest_route( |
|
46 | - $namespace, |
|
47 | - $this->rest_base, |
|
48 | - array( |
|
49 | - array( |
|
50 | - 'methods' => WP_REST_Server::READABLE, |
|
51 | - 'callback' => array( $this, 'get_tabs' ), |
|
52 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
53 | - ), |
|
54 | - 'schema' => '__return_empty_array', |
|
55 | - ) |
|
56 | - ); |
|
57 | - |
|
58 | - // View/Update a single setting. |
|
59 | - register_rest_route( |
|
60 | - $namespace, |
|
61 | - $this->rest_base . '/setting/(?P<id>[\w-]+)', |
|
62 | - array( |
|
63 | - 'args' => array( |
|
64 | - 'id' => array( |
|
65 | - 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
66 | - 'type' => 'string', |
|
67 | - 'required' => true, |
|
68 | - ), |
|
69 | - ), |
|
70 | - array( |
|
71 | - 'methods' => WP_REST_Server::READABLE, |
|
72 | - 'callback' => array( $this, 'get_item' ), |
|
73 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
74 | - ), |
|
75 | - array( |
|
76 | - 'methods' => WP_REST_Server::EDITABLE, |
|
77 | - 'callback' => array( $this, 'update_item' ), |
|
78 | - 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
79 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
80 | - ), |
|
81 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
82 | - ) |
|
83 | - ); |
|
84 | - |
|
85 | - // List registered sections for a given tab. |
|
86 | - register_rest_route( |
|
87 | - $namespace, |
|
88 | - $this->rest_base . '/(?P<tab>[\w-]+)', |
|
89 | - array( |
|
90 | - 'args' => array( |
|
91 | - 'tab' => array( |
|
92 | - 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
93 | - 'type' => 'string', |
|
94 | - 'required' => true, |
|
95 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
96 | - ), |
|
97 | - ), |
|
98 | - array( |
|
99 | - 'methods' => WP_REST_Server::READABLE, |
|
100 | - 'callback' => array( $this, 'get_sections' ), |
|
101 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
102 | - ), |
|
103 | - 'schema' => '__return_empty_array', |
|
104 | - ) |
|
105 | - ); |
|
106 | - |
|
107 | - // List all registered settings for a given tab. |
|
108 | - register_rest_route( |
|
109 | - $namespace, |
|
110 | - $this->rest_base . '/(?P<tab>[\w-]+)/(?P<section>[\w-]+)', |
|
111 | - array( |
|
112 | - 'args' => array( |
|
113 | - 'tab' => array( |
|
114 | - 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
115 | - 'type' => 'string', |
|
116 | - 'required' => true, |
|
117 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
118 | - ), |
|
119 | - 'section' => array( |
|
120 | - 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
121 | - 'type' => 'string', |
|
122 | - 'required' => true, |
|
123 | - ), |
|
124 | - ), |
|
125 | - array( |
|
126 | - 'methods' => WP_REST_Server::READABLE, |
|
127 | - 'callback' => array( $this, 'get_items' ), |
|
128 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
129 | - ), |
|
130 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
131 | - ) |
|
132 | - ); |
|
133 | - |
|
134 | - register_rest_route( |
|
135 | - $namespace, |
|
136 | - '/' . $this->rest_base . '/batch', |
|
137 | - array( |
|
138 | - 'args' => array( |
|
139 | - 'id' => array( |
|
140 | - 'description' => __( 'Setting ID.', 'invoicing' ), |
|
141 | - 'type' => 'string', |
|
142 | - ), |
|
143 | - ), |
|
144 | - array( |
|
145 | - 'methods' => WP_REST_Server::EDITABLE, |
|
146 | - 'callback' => array( $this, 'batch_items' ), |
|
147 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
148 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
149 | - ), |
|
150 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
151 | - ) |
|
152 | - ); |
|
153 | - |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Return all settings. |
|
158 | - * |
|
159 | - * @since 2.0.0 |
|
160 | - * @param WP_REST_Request $request Request data. |
|
161 | - * @return WP_Error|WP_REST_Response |
|
162 | - */ |
|
163 | - public function get_items( $request ) { |
|
164 | - |
|
165 | - $settings = $this->get_settings(); |
|
166 | - |
|
167 | - if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
168 | - return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
169 | - } |
|
170 | - |
|
171 | - if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
172 | - return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
173 | - } |
|
174 | - |
|
175 | - $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
176 | - $prepared = array(); |
|
177 | - |
|
178 | - foreach ( $settings as $setting ) { |
|
179 | - |
|
180 | - $setting = $this->sanitize_setting( $setting ); |
|
181 | - $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
182 | - $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
183 | - |
|
184 | - if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
185 | - $prepared[] = $setting_data; |
|
186 | - } |
|
21 | + /** |
|
22 | + * An array of available settings. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $settings; |
|
27 | + |
|
28 | + /** |
|
29 | + * Route base. |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $rest_base = 'settings'; |
|
34 | + |
|
35 | + /** |
|
36 | + * Registers the routes for the objects of the controller. |
|
37 | + * |
|
38 | + * @since 2.0.0 |
|
39 | + * |
|
40 | + * @see register_rest_route() |
|
41 | + */ |
|
42 | + public function register_namespace_routes( $namespace ) { |
|
43 | + |
|
44 | + // List all registered tabs. |
|
45 | + register_rest_route( |
|
46 | + $namespace, |
|
47 | + $this->rest_base, |
|
48 | + array( |
|
49 | + array( |
|
50 | + 'methods' => WP_REST_Server::READABLE, |
|
51 | + 'callback' => array( $this, 'get_tabs' ), |
|
52 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
53 | + ), |
|
54 | + 'schema' => '__return_empty_array', |
|
55 | + ) |
|
56 | + ); |
|
57 | + |
|
58 | + // View/Update a single setting. |
|
59 | + register_rest_route( |
|
60 | + $namespace, |
|
61 | + $this->rest_base . '/setting/(?P<id>[\w-]+)', |
|
62 | + array( |
|
63 | + 'args' => array( |
|
64 | + 'id' => array( |
|
65 | + 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
66 | + 'type' => 'string', |
|
67 | + 'required' => true, |
|
68 | + ), |
|
69 | + ), |
|
70 | + array( |
|
71 | + 'methods' => WP_REST_Server::READABLE, |
|
72 | + 'callback' => array( $this, 'get_item' ), |
|
73 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
74 | + ), |
|
75 | + array( |
|
76 | + 'methods' => WP_REST_Server::EDITABLE, |
|
77 | + 'callback' => array( $this, 'update_item' ), |
|
78 | + 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
79 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
80 | + ), |
|
81 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
82 | + ) |
|
83 | + ); |
|
84 | + |
|
85 | + // List registered sections for a given tab. |
|
86 | + register_rest_route( |
|
87 | + $namespace, |
|
88 | + $this->rest_base . '/(?P<tab>[\w-]+)', |
|
89 | + array( |
|
90 | + 'args' => array( |
|
91 | + 'tab' => array( |
|
92 | + 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
93 | + 'type' => 'string', |
|
94 | + 'required' => true, |
|
95 | + 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
96 | + ), |
|
97 | + ), |
|
98 | + array( |
|
99 | + 'methods' => WP_REST_Server::READABLE, |
|
100 | + 'callback' => array( $this, 'get_sections' ), |
|
101 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
102 | + ), |
|
103 | + 'schema' => '__return_empty_array', |
|
104 | + ) |
|
105 | + ); |
|
106 | + |
|
107 | + // List all registered settings for a given tab. |
|
108 | + register_rest_route( |
|
109 | + $namespace, |
|
110 | + $this->rest_base . '/(?P<tab>[\w-]+)/(?P<section>[\w-]+)', |
|
111 | + array( |
|
112 | + 'args' => array( |
|
113 | + 'tab' => array( |
|
114 | + 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
115 | + 'type' => 'string', |
|
116 | + 'required' => true, |
|
117 | + 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
118 | + ), |
|
119 | + 'section' => array( |
|
120 | + 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
121 | + 'type' => 'string', |
|
122 | + 'required' => true, |
|
123 | + ), |
|
124 | + ), |
|
125 | + array( |
|
126 | + 'methods' => WP_REST_Server::READABLE, |
|
127 | + 'callback' => array( $this, 'get_items' ), |
|
128 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
129 | + ), |
|
130 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
131 | + ) |
|
132 | + ); |
|
133 | + |
|
134 | + register_rest_route( |
|
135 | + $namespace, |
|
136 | + '/' . $this->rest_base . '/batch', |
|
137 | + array( |
|
138 | + 'args' => array( |
|
139 | + 'id' => array( |
|
140 | + 'description' => __( 'Setting ID.', 'invoicing' ), |
|
141 | + 'type' => 'string', |
|
142 | + ), |
|
143 | + ), |
|
144 | + array( |
|
145 | + 'methods' => WP_REST_Server::EDITABLE, |
|
146 | + 'callback' => array( $this, 'batch_items' ), |
|
147 | + 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
148 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
149 | + ), |
|
150 | + 'schema' => array( $this, 'get_public_batch_schema' ), |
|
151 | + ) |
|
152 | + ); |
|
153 | + |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Return all settings. |
|
158 | + * |
|
159 | + * @since 2.0.0 |
|
160 | + * @param WP_REST_Request $request Request data. |
|
161 | + * @return WP_Error|WP_REST_Response |
|
162 | + */ |
|
163 | + public function get_items( $request ) { |
|
164 | + |
|
165 | + $settings = $this->get_settings(); |
|
166 | + |
|
167 | + if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
168 | + return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
169 | + } |
|
170 | + |
|
171 | + if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
172 | + return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
173 | + } |
|
174 | + |
|
175 | + $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
176 | + $prepared = array(); |
|
177 | + |
|
178 | + foreach ( $settings as $setting ) { |
|
179 | + |
|
180 | + $setting = $this->sanitize_setting( $setting ); |
|
181 | + $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
182 | + $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
183 | + |
|
184 | + if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
185 | + $prepared[] = $setting_data; |
|
186 | + } |
|
187 | 187 | } |
188 | 188 | |
189 | - return rest_ensure_response( $prepared ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Return a single setting. |
|
194 | - * |
|
195 | - * @since 2.0.0 |
|
196 | - * @param WP_REST_Request $request Request data. |
|
197 | - * @return WP_Error|WP_REST_Response |
|
198 | - */ |
|
199 | - public function get_item( $request ) { |
|
200 | - $setting = $this->get_setting( $request['id'] ); |
|
201 | - |
|
202 | - if ( is_wp_error( $setting ) ) { |
|
203 | - return $setting; |
|
204 | - } |
|
205 | - |
|
206 | - $setting = $this->sanitize_setting( $setting ); |
|
207 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
208 | - return rest_ensure_response( $response ); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Update a single setting. |
|
213 | - * |
|
214 | - * @since 2.0.0 |
|
215 | - * @param WP_REST_Request $request Request data. |
|
216 | - * @return WP_Error|WP_REST_Response |
|
217 | - */ |
|
218 | - public function update_item( $request ) { |
|
219 | - $setting = $this->get_setting( $request['id'] ); |
|
220 | - |
|
221 | - if ( is_wp_error( $setting ) ) { |
|
222 | - return $setting; |
|
223 | - } |
|
224 | - |
|
225 | - if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
226 | - $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
227 | - } else { |
|
228 | - $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
229 | - } |
|
230 | - |
|
231 | - if ( is_wp_error( $value ) ) { |
|
232 | - return $value; |
|
233 | - } |
|
234 | - |
|
235 | - wpinv_update_option( $request['id'], $value ); |
|
236 | - $setting['value'] = $value; |
|
237 | - $setting = $this->sanitize_setting( $setting ); |
|
238 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
239 | - |
|
240 | - return rest_ensure_response( $response ); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Makes sure the current user has access to READ the settings APIs. |
|
245 | - * |
|
246 | - * @since 2.0.0 |
|
247 | - * @param WP_REST_Request $request Full data about the request. |
|
248 | - * @return WP_Error|boolean |
|
249 | - */ |
|
250 | - public function get_items_permissions_check( $request ) { |
|
251 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
252 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
253 | - } |
|
254 | - |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Makes sure the current user has access to WRITE the settings APIs. |
|
260 | - * |
|
261 | - * @since 2.0.0 |
|
262 | - * @param WP_REST_Request $request Full data about the request. |
|
263 | - * @return WP_Error|boolean |
|
264 | - */ |
|
265 | - public function update_items_permissions_check( $request ) { |
|
266 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
267 | - return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
268 | - } |
|
269 | - |
|
270 | - return true; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Check if a given request has access batch create, update and delete items. |
|
275 | - * |
|
276 | - * @param WP_REST_Request $request Full details about the request. |
|
277 | - * |
|
278 | - * @return boolean|WP_Error |
|
279 | - */ |
|
280 | - public function batch_items_permissions_check( $request ) { |
|
281 | - return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Prepare links for the request. |
|
286 | - * |
|
287 | - * @param string $setting_id Setting ID. |
|
288 | - * @return array Links for the given setting. |
|
289 | - */ |
|
290 | - protected function prepare_links( $setting_id ) { |
|
291 | - |
|
292 | - $links = array( |
|
293 | - 'self' => array( |
|
294 | - 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
295 | - ), |
|
296 | - 'collection' => array( |
|
297 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
298 | - ), |
|
299 | - ); |
|
300 | - |
|
301 | - return $links; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Prepare a settings object for serialization. |
|
306 | - * |
|
307 | - * @since 2.0.0 |
|
308 | - * @param array $item Setting object. |
|
309 | - * @param WP_REST_Request $request Request object. |
|
310 | - * @return WP_REST_Response $response Response data. |
|
311 | - */ |
|
312 | - public function prepare_item_for_response( $item, $request ) { |
|
313 | - $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
314 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
315 | - $data = $this->filter_response_by_context( $data, $context ); |
|
316 | - |
|
317 | - $response = rest_ensure_response( $data ); |
|
318 | - |
|
319 | - $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
320 | - |
|
321 | - return $response; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Filters out bad values from the settings array/filter so we |
|
326 | - * only return known values via the API. |
|
327 | - * |
|
328 | - * @since 2.0.0 |
|
329 | - * @param array $setting Setting. |
|
330 | - * @return array |
|
331 | - */ |
|
332 | - public function filter_setting( $setting ) { |
|
333 | - return array_intersect_key( |
|
334 | - $setting, |
|
335 | - array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
336 | - ); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Callback for allowed keys for each setting response. |
|
341 | - * |
|
342 | - * @param string $key Key to check. |
|
343 | - * @return boolean |
|
344 | - */ |
|
345 | - public function allowed_setting_keys( $key ) { |
|
346 | - return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Returns default options for a setting. null means the field is required. |
|
351 | - * |
|
352 | - * @since 2.0.0 |
|
353 | - * @return array |
|
354 | - */ |
|
355 | - protected function setting_defaults() { |
|
356 | - return array( |
|
357 | - 'id' => null, |
|
358 | - 'name' => null, |
|
359 | - 'desc' => '', |
|
360 | - 'options' => array(), |
|
361 | - 'std' => false, |
|
362 | - 'value' => false, |
|
363 | - 'placeholder' => '', |
|
364 | - 'readonly' => false, |
|
365 | - 'faux' => false, |
|
366 | - 'section' => 'main', |
|
367 | - 'tab' => 'general', |
|
368 | - 'type' => 'text', |
|
369 | - ); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Sanitizes a setting's field. |
|
374 | - * |
|
375 | - * @param array $setting The setting to sanitize. |
|
376 | - * @return array |
|
377 | - */ |
|
378 | - public function sanitize_setting( $setting ) { |
|
379 | - |
|
380 | - $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
381 | - $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
382 | - return $this->filter_setting( $setting ); |
|
383 | - |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Get setting data. |
|
388 | - * |
|
389 | - * @since 2.0.0 |
|
390 | - * @param string $setting_id Setting ID. |
|
391 | - * @return array|WP_Error |
|
392 | - */ |
|
393 | - public function get_setting( $setting_id ) { |
|
394 | - |
|
395 | - if ( empty( $setting_id ) ) { |
|
396 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
397 | - } |
|
398 | - |
|
399 | - $settings = $this->get_settings(); |
|
400 | - |
|
401 | - foreach ( $settings as $tabs ) { |
|
402 | - |
|
403 | - foreach ( $tabs as $sections ) { |
|
404 | - |
|
405 | - if ( isset( $sections[ $setting_id ] ) ) { |
|
406 | - if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
407 | - return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
408 | - } |
|
409 | - |
|
410 | - return $sections[ $setting_id ]; |
|
411 | - } |
|
189 | + return rest_ensure_response( $prepared ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Return a single setting. |
|
194 | + * |
|
195 | + * @since 2.0.0 |
|
196 | + * @param WP_REST_Request $request Request data. |
|
197 | + * @return WP_Error|WP_REST_Response |
|
198 | + */ |
|
199 | + public function get_item( $request ) { |
|
200 | + $setting = $this->get_setting( $request['id'] ); |
|
201 | + |
|
202 | + if ( is_wp_error( $setting ) ) { |
|
203 | + return $setting; |
|
204 | + } |
|
205 | + |
|
206 | + $setting = $this->sanitize_setting( $setting ); |
|
207 | + $response = $this->prepare_item_for_response( $setting, $request ); |
|
208 | + return rest_ensure_response( $response ); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Update a single setting. |
|
213 | + * |
|
214 | + * @since 2.0.0 |
|
215 | + * @param WP_REST_Request $request Request data. |
|
216 | + * @return WP_Error|WP_REST_Response |
|
217 | + */ |
|
218 | + public function update_item( $request ) { |
|
219 | + $setting = $this->get_setting( $request['id'] ); |
|
220 | + |
|
221 | + if ( is_wp_error( $setting ) ) { |
|
222 | + return $setting; |
|
223 | + } |
|
224 | + |
|
225 | + if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
226 | + $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
227 | + } else { |
|
228 | + $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
229 | + } |
|
230 | + |
|
231 | + if ( is_wp_error( $value ) ) { |
|
232 | + return $value; |
|
233 | + } |
|
234 | + |
|
235 | + wpinv_update_option( $request['id'], $value ); |
|
236 | + $setting['value'] = $value; |
|
237 | + $setting = $this->sanitize_setting( $setting ); |
|
238 | + $response = $this->prepare_item_for_response( $setting, $request ); |
|
239 | + |
|
240 | + return rest_ensure_response( $response ); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Makes sure the current user has access to READ the settings APIs. |
|
245 | + * |
|
246 | + * @since 2.0.0 |
|
247 | + * @param WP_REST_Request $request Full data about the request. |
|
248 | + * @return WP_Error|boolean |
|
249 | + */ |
|
250 | + public function get_items_permissions_check( $request ) { |
|
251 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
252 | + return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
253 | + } |
|
254 | + |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Makes sure the current user has access to WRITE the settings APIs. |
|
260 | + * |
|
261 | + * @since 2.0.0 |
|
262 | + * @param WP_REST_Request $request Full data about the request. |
|
263 | + * @return WP_Error|boolean |
|
264 | + */ |
|
265 | + public function update_items_permissions_check( $request ) { |
|
266 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
267 | + return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
268 | + } |
|
269 | + |
|
270 | + return true; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Check if a given request has access batch create, update and delete items. |
|
275 | + * |
|
276 | + * @param WP_REST_Request $request Full details about the request. |
|
277 | + * |
|
278 | + * @return boolean|WP_Error |
|
279 | + */ |
|
280 | + public function batch_items_permissions_check( $request ) { |
|
281 | + return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Prepare links for the request. |
|
286 | + * |
|
287 | + * @param string $setting_id Setting ID. |
|
288 | + * @return array Links for the given setting. |
|
289 | + */ |
|
290 | + protected function prepare_links( $setting_id ) { |
|
291 | + |
|
292 | + $links = array( |
|
293 | + 'self' => array( |
|
294 | + 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
295 | + ), |
|
296 | + 'collection' => array( |
|
297 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
298 | + ), |
|
299 | + ); |
|
300 | + |
|
301 | + return $links; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Prepare a settings object for serialization. |
|
306 | + * |
|
307 | + * @since 2.0.0 |
|
308 | + * @param array $item Setting object. |
|
309 | + * @param WP_REST_Request $request Request object. |
|
310 | + * @return WP_REST_Response $response Response data. |
|
311 | + */ |
|
312 | + public function prepare_item_for_response( $item, $request ) { |
|
313 | + $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
314 | + $data = $this->add_additional_fields_to_object( $item, $request ); |
|
315 | + $data = $this->filter_response_by_context( $data, $context ); |
|
316 | + |
|
317 | + $response = rest_ensure_response( $data ); |
|
318 | + |
|
319 | + $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
320 | + |
|
321 | + return $response; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Filters out bad values from the settings array/filter so we |
|
326 | + * only return known values via the API. |
|
327 | + * |
|
328 | + * @since 2.0.0 |
|
329 | + * @param array $setting Setting. |
|
330 | + * @return array |
|
331 | + */ |
|
332 | + public function filter_setting( $setting ) { |
|
333 | + return array_intersect_key( |
|
334 | + $setting, |
|
335 | + array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
336 | + ); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Callback for allowed keys for each setting response. |
|
341 | + * |
|
342 | + * @param string $key Key to check. |
|
343 | + * @return boolean |
|
344 | + */ |
|
345 | + public function allowed_setting_keys( $key ) { |
|
346 | + return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Returns default options for a setting. null means the field is required. |
|
351 | + * |
|
352 | + * @since 2.0.0 |
|
353 | + * @return array |
|
354 | + */ |
|
355 | + protected function setting_defaults() { |
|
356 | + return array( |
|
357 | + 'id' => null, |
|
358 | + 'name' => null, |
|
359 | + 'desc' => '', |
|
360 | + 'options' => array(), |
|
361 | + 'std' => false, |
|
362 | + 'value' => false, |
|
363 | + 'placeholder' => '', |
|
364 | + 'readonly' => false, |
|
365 | + 'faux' => false, |
|
366 | + 'section' => 'main', |
|
367 | + 'tab' => 'general', |
|
368 | + 'type' => 'text', |
|
369 | + ); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Sanitizes a setting's field. |
|
374 | + * |
|
375 | + * @param array $setting The setting to sanitize. |
|
376 | + * @return array |
|
377 | + */ |
|
378 | + public function sanitize_setting( $setting ) { |
|
379 | + |
|
380 | + $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
381 | + $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
382 | + return $this->filter_setting( $setting ); |
|
383 | + |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Get setting data. |
|
388 | + * |
|
389 | + * @since 2.0.0 |
|
390 | + * @param string $setting_id Setting ID. |
|
391 | + * @return array|WP_Error |
|
392 | + */ |
|
393 | + public function get_setting( $setting_id ) { |
|
394 | + |
|
395 | + if ( empty( $setting_id ) ) { |
|
396 | + return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
397 | + } |
|
398 | + |
|
399 | + $settings = $this->get_settings(); |
|
400 | + |
|
401 | + foreach ( $settings as $tabs ) { |
|
402 | + |
|
403 | + foreach ( $tabs as $sections ) { |
|
404 | + |
|
405 | + if ( isset( $sections[ $setting_id ] ) ) { |
|
406 | + if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
407 | + return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
408 | + } |
|
409 | + |
|
410 | + return $sections[ $setting_id ]; |
|
411 | + } |
|
412 | 412 | } |
413 | 413 | } |
414 | 414 | |
415 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Get all tabs. |
|
420 | - * |
|
421 | - * @param WP_REST_Request $request Request data. |
|
422 | - * @return array |
|
423 | - */ |
|
424 | - public function get_tabs( $request ) { |
|
425 | - $tabs = wpinv_get_settings_tabs(); |
|
426 | - $prepared = array(); |
|
427 | - |
|
428 | - foreach ( $tabs as $id => $tab ) { |
|
429 | - |
|
430 | - $_request = $request; |
|
431 | - $_request['tab'] = sanitize_title( $id ); |
|
432 | - $data = array( |
|
433 | - 'id' => sanitize_title( $id ), |
|
434 | - 'label' => sanitize_text_field( $tab ), |
|
435 | - 'sections' => $this->get_sections( $_request ), |
|
436 | - ); |
|
437 | - |
|
438 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
439 | - $response = rest_ensure_response( $data ); |
|
440 | - |
|
441 | - if ( ! is_wp_error( $response ) ) { |
|
442 | - $links = array( |
|
443 | - 'sections' => array( |
|
444 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
445 | - ), |
|
446 | - 'collection' => array( |
|
447 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
448 | - ), |
|
449 | - ); |
|
450 | - $response->add_links( $links ); |
|
451 | - $response = $this->prepare_response_for_collection( $response ); |
|
452 | - } |
|
453 | - |
|
454 | - $prepared[] = $response; |
|
455 | - |
|
456 | - } |
|
457 | - |
|
458 | - return rest_ensure_response( $prepared ); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Get all sections. |
|
463 | - * |
|
464 | - * @param WP_REST_Request $request Request data. |
|
465 | - * @return array |
|
466 | - */ |
|
467 | - public function get_sections( $request ) { |
|
468 | - |
|
469 | - $tab = sanitize_title( $request['tab'] ); |
|
470 | - $sections = wpinv_get_settings_tab_sections( $tab ); |
|
471 | - $prepared = array(); |
|
472 | - |
|
473 | - foreach ( $sections as $id => $section ) { |
|
474 | - |
|
475 | - $data = array( |
|
476 | - 'id' => sanitize_title( $id ), |
|
477 | - 'label' => sanitize_text_field( $section ), |
|
478 | - ); |
|
479 | - |
|
480 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
481 | - $response = rest_ensure_response( $data ); |
|
482 | - |
|
483 | - if ( ! is_wp_error( $response ) ) { |
|
484 | - $links = array( |
|
485 | - 'settings' => array( |
|
486 | - 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
487 | - ), |
|
488 | - 'collection' => array( |
|
489 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
490 | - ), |
|
491 | - 'tabs' => array( |
|
492 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
493 | - ), |
|
494 | - ); |
|
495 | - $response->add_links( $links ); |
|
496 | - $response = $this->prepare_response_for_collection( $response ); |
|
497 | - } |
|
498 | - |
|
499 | - $prepared[] = $response; |
|
500 | - |
|
501 | - } |
|
502 | - |
|
503 | - return rest_ensure_response( $prepared ); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Get all settings. |
|
508 | - * |
|
509 | - * @return array |
|
510 | - */ |
|
511 | - public function get_settings() { |
|
512 | - |
|
513 | - if ( empty( $this->settings ) ) { |
|
514 | - $this->settings = wpinv_get_registered_settings(); |
|
515 | - } |
|
516 | - |
|
517 | - return $this->settings; |
|
518 | - |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Boolean for if a setting type is a valid supported setting type. |
|
523 | - * |
|
524 | - * @since 2.0.0 |
|
525 | - * @param string $type Type. |
|
526 | - * @return bool |
|
527 | - */ |
|
528 | - public function is_setting_type_valid( $type ) { |
|
529 | - |
|
530 | - return in_array( |
|
531 | - $type, |
|
415 | + return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Get all tabs. |
|
420 | + * |
|
421 | + * @param WP_REST_Request $request Request data. |
|
422 | + * @return array |
|
423 | + */ |
|
424 | + public function get_tabs( $request ) { |
|
425 | + $tabs = wpinv_get_settings_tabs(); |
|
426 | + $prepared = array(); |
|
427 | + |
|
428 | + foreach ( $tabs as $id => $tab ) { |
|
429 | + |
|
430 | + $_request = $request; |
|
431 | + $_request['tab'] = sanitize_title( $id ); |
|
432 | + $data = array( |
|
433 | + 'id' => sanitize_title( $id ), |
|
434 | + 'label' => sanitize_text_field( $tab ), |
|
435 | + 'sections' => $this->get_sections( $_request ), |
|
436 | + ); |
|
437 | + |
|
438 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
439 | + $response = rest_ensure_response( $data ); |
|
440 | + |
|
441 | + if ( ! is_wp_error( $response ) ) { |
|
442 | + $links = array( |
|
443 | + 'sections' => array( |
|
444 | + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
445 | + ), |
|
446 | + 'collection' => array( |
|
447 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
448 | + ), |
|
449 | + ); |
|
450 | + $response->add_links( $links ); |
|
451 | + $response = $this->prepare_response_for_collection( $response ); |
|
452 | + } |
|
453 | + |
|
454 | + $prepared[] = $response; |
|
455 | + |
|
456 | + } |
|
457 | + |
|
458 | + return rest_ensure_response( $prepared ); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Get all sections. |
|
463 | + * |
|
464 | + * @param WP_REST_Request $request Request data. |
|
465 | + * @return array |
|
466 | + */ |
|
467 | + public function get_sections( $request ) { |
|
468 | + |
|
469 | + $tab = sanitize_title( $request['tab'] ); |
|
470 | + $sections = wpinv_get_settings_tab_sections( $tab ); |
|
471 | + $prepared = array(); |
|
472 | + |
|
473 | + foreach ( $sections as $id => $section ) { |
|
474 | + |
|
475 | + $data = array( |
|
476 | + 'id' => sanitize_title( $id ), |
|
477 | + 'label' => sanitize_text_field( $section ), |
|
478 | + ); |
|
479 | + |
|
480 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
481 | + $response = rest_ensure_response( $data ); |
|
482 | + |
|
483 | + if ( ! is_wp_error( $response ) ) { |
|
484 | + $links = array( |
|
485 | + 'settings' => array( |
|
486 | + 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
487 | + ), |
|
488 | + 'collection' => array( |
|
489 | + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
490 | + ), |
|
491 | + 'tabs' => array( |
|
492 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
493 | + ), |
|
494 | + ); |
|
495 | + $response->add_links( $links ); |
|
496 | + $response = $this->prepare_response_for_collection( $response ); |
|
497 | + } |
|
498 | + |
|
499 | + $prepared[] = $response; |
|
500 | + |
|
501 | + } |
|
502 | + |
|
503 | + return rest_ensure_response( $prepared ); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Get all settings. |
|
508 | + * |
|
509 | + * @return array |
|
510 | + */ |
|
511 | + public function get_settings() { |
|
512 | + |
|
513 | + if ( empty( $this->settings ) ) { |
|
514 | + $this->settings = wpinv_get_registered_settings(); |
|
515 | + } |
|
516 | + |
|
517 | + return $this->settings; |
|
518 | + |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Boolean for if a setting type is a valid supported setting type. |
|
523 | + * |
|
524 | + * @since 2.0.0 |
|
525 | + * @param string $type Type. |
|
526 | + * @return bool |
|
527 | + */ |
|
528 | + public function is_setting_type_valid( $type ) { |
|
529 | + |
|
530 | + return in_array( |
|
531 | + $type, |
|
532 | 532 | array( |
533 | - 'text', // Validates with validate_setting_text_field. |
|
534 | - 'email', // Validates with validate_setting_text_field. |
|
535 | - 'number', // Validates with validate_setting_text_field. |
|
536 | - 'color', // Validates with validate_setting_text_field. |
|
537 | - 'password', // Validates with validate_setting_text_field. |
|
538 | - 'textarea', // Validates with validate_setting_textarea_field. |
|
539 | - 'select', // Validates with validate_setting_select_field. |
|
540 | - 'multiselect', // Validates with validate_setting_multiselect_field. |
|
541 | - 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
542 | - 'checkbox', // Validates with validate_setting_checkbox_field. |
|
543 | - 'header', // Validates with validate_setting_text_field. |
|
544 | - ) |
|
545 | - ); |
|
546 | - |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Get the settings schema, conforming to JSON Schema. |
|
551 | - * |
|
552 | - * @return array |
|
553 | - */ |
|
554 | - public function get_item_schema() { |
|
555 | - |
|
556 | - // Maybe retrieve the schema from cache. |
|
557 | - if ( ! empty( $this->schema ) ) { |
|
558 | - return $this->add_additional_fields_schema( $this->schema ); |
|
559 | - } |
|
560 | - |
|
561 | - $schema = array( |
|
562 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
563 | - 'title' => 'setting', |
|
564 | - 'type' => 'object', |
|
565 | - 'properties' => array( |
|
566 | - 'id' => array( |
|
567 | - 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
568 | - 'type' => 'string', |
|
569 | - 'arg_options' => array( |
|
570 | - 'sanitize_callback' => 'sanitize_title', |
|
571 | - ), |
|
572 | - 'context' => array( 'view', 'edit' ), |
|
573 | - 'readonly' => true, |
|
574 | - ), |
|
575 | - 'tab' => array( |
|
576 | - 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
577 | - 'type' => 'string', |
|
578 | - 'arg_options' => array( |
|
579 | - 'sanitize_callback' => 'sanitize_title', |
|
580 | - ), |
|
581 | - 'context' => array( 'view', 'edit' ), |
|
582 | - 'readonly' => true, |
|
583 | - ), |
|
584 | - 'section' => array( |
|
585 | - 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
586 | - 'type' => 'string', |
|
587 | - 'arg_options' => array( |
|
588 | - 'sanitize_callback' => 'sanitize_title', |
|
589 | - ), |
|
590 | - 'context' => array( 'view', 'edit' ), |
|
591 | - 'readonly' => true, |
|
592 | - ), |
|
593 | - 'name' => array( |
|
594 | - 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
595 | - 'type' => 'string', |
|
596 | - 'arg_options' => array( |
|
597 | - 'sanitize_callback' => 'sanitize_text_field', |
|
598 | - ), |
|
599 | - 'context' => array( 'view', 'edit' ), |
|
600 | - 'readonly' => true, |
|
601 | - ), |
|
602 | - 'desc' => array( |
|
603 | - 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
604 | - 'type' => 'string', |
|
605 | - 'context' => array( 'view', 'edit' ), |
|
606 | - 'readonly' => true, |
|
607 | - ), |
|
608 | - 'value' => array( |
|
609 | - 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
610 | - 'type' => 'mixed', |
|
611 | - 'context' => array( 'view', 'edit' ), |
|
612 | - ), |
|
613 | - 'default' => array( |
|
614 | - 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
615 | - 'type' => 'mixed', |
|
616 | - 'context' => array( 'view', 'edit' ), |
|
617 | - 'readonly' => true, |
|
618 | - ), |
|
619 | - 'placeholder' => array( |
|
620 | - 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
621 | - 'type' => 'string', |
|
622 | - 'arg_options' => array( |
|
623 | - 'sanitize_callback' => 'sanitize_text_field', |
|
624 | - ), |
|
625 | - 'context' => array( 'view', 'edit' ), |
|
626 | - 'readonly' => true, |
|
627 | - ), |
|
628 | - 'type' => array( |
|
629 | - 'description' => __( 'Type of setting.', 'invoicing' ), |
|
630 | - 'type' => 'string', |
|
631 | - 'arg_options' => array( |
|
632 | - 'sanitize_callback' => 'sanitize_text_field', |
|
633 | - ), |
|
634 | - 'context' => array( 'view', 'edit' ), |
|
635 | - 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
636 | - 'readonly' => true, |
|
637 | - ), |
|
638 | - 'options' => array( |
|
639 | - 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
640 | - 'type' => 'object', |
|
641 | - 'context' => array( 'view', 'edit' ), |
|
642 | - 'readonly' => true, |
|
643 | - ), |
|
644 | - 'readonly' => array( |
|
645 | - 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
646 | - 'type' => 'string', |
|
647 | - 'context' => array( 'view' ), |
|
648 | - 'readonly' => true, |
|
649 | - ), |
|
650 | - 'faux' => array( |
|
651 | - 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
652 | - 'type' => 'string', |
|
653 | - 'context' => array( 'view' ), |
|
654 | - 'readonly' => true, |
|
655 | - ), |
|
656 | - ), |
|
657 | - ); |
|
658 | - |
|
659 | - // Filters the settings schema for the REST API. |
|
533 | + 'text', // Validates with validate_setting_text_field. |
|
534 | + 'email', // Validates with validate_setting_text_field. |
|
535 | + 'number', // Validates with validate_setting_text_field. |
|
536 | + 'color', // Validates with validate_setting_text_field. |
|
537 | + 'password', // Validates with validate_setting_text_field. |
|
538 | + 'textarea', // Validates with validate_setting_textarea_field. |
|
539 | + 'select', // Validates with validate_setting_select_field. |
|
540 | + 'multiselect', // Validates with validate_setting_multiselect_field. |
|
541 | + 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
542 | + 'checkbox', // Validates with validate_setting_checkbox_field. |
|
543 | + 'header', // Validates with validate_setting_text_field. |
|
544 | + ) |
|
545 | + ); |
|
546 | + |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Get the settings schema, conforming to JSON Schema. |
|
551 | + * |
|
552 | + * @return array |
|
553 | + */ |
|
554 | + public function get_item_schema() { |
|
555 | + |
|
556 | + // Maybe retrieve the schema from cache. |
|
557 | + if ( ! empty( $this->schema ) ) { |
|
558 | + return $this->add_additional_fields_schema( $this->schema ); |
|
559 | + } |
|
560 | + |
|
561 | + $schema = array( |
|
562 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
563 | + 'title' => 'setting', |
|
564 | + 'type' => 'object', |
|
565 | + 'properties' => array( |
|
566 | + 'id' => array( |
|
567 | + 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
568 | + 'type' => 'string', |
|
569 | + 'arg_options' => array( |
|
570 | + 'sanitize_callback' => 'sanitize_title', |
|
571 | + ), |
|
572 | + 'context' => array( 'view', 'edit' ), |
|
573 | + 'readonly' => true, |
|
574 | + ), |
|
575 | + 'tab' => array( |
|
576 | + 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
577 | + 'type' => 'string', |
|
578 | + 'arg_options' => array( |
|
579 | + 'sanitize_callback' => 'sanitize_title', |
|
580 | + ), |
|
581 | + 'context' => array( 'view', 'edit' ), |
|
582 | + 'readonly' => true, |
|
583 | + ), |
|
584 | + 'section' => array( |
|
585 | + 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
586 | + 'type' => 'string', |
|
587 | + 'arg_options' => array( |
|
588 | + 'sanitize_callback' => 'sanitize_title', |
|
589 | + ), |
|
590 | + 'context' => array( 'view', 'edit' ), |
|
591 | + 'readonly' => true, |
|
592 | + ), |
|
593 | + 'name' => array( |
|
594 | + 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
595 | + 'type' => 'string', |
|
596 | + 'arg_options' => array( |
|
597 | + 'sanitize_callback' => 'sanitize_text_field', |
|
598 | + ), |
|
599 | + 'context' => array( 'view', 'edit' ), |
|
600 | + 'readonly' => true, |
|
601 | + ), |
|
602 | + 'desc' => array( |
|
603 | + 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
604 | + 'type' => 'string', |
|
605 | + 'context' => array( 'view', 'edit' ), |
|
606 | + 'readonly' => true, |
|
607 | + ), |
|
608 | + 'value' => array( |
|
609 | + 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
610 | + 'type' => 'mixed', |
|
611 | + 'context' => array( 'view', 'edit' ), |
|
612 | + ), |
|
613 | + 'default' => array( |
|
614 | + 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
615 | + 'type' => 'mixed', |
|
616 | + 'context' => array( 'view', 'edit' ), |
|
617 | + 'readonly' => true, |
|
618 | + ), |
|
619 | + 'placeholder' => array( |
|
620 | + 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
621 | + 'type' => 'string', |
|
622 | + 'arg_options' => array( |
|
623 | + 'sanitize_callback' => 'sanitize_text_field', |
|
624 | + ), |
|
625 | + 'context' => array( 'view', 'edit' ), |
|
626 | + 'readonly' => true, |
|
627 | + ), |
|
628 | + 'type' => array( |
|
629 | + 'description' => __( 'Type of setting.', 'invoicing' ), |
|
630 | + 'type' => 'string', |
|
631 | + 'arg_options' => array( |
|
632 | + 'sanitize_callback' => 'sanitize_text_field', |
|
633 | + ), |
|
634 | + 'context' => array( 'view', 'edit' ), |
|
635 | + 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
636 | + 'readonly' => true, |
|
637 | + ), |
|
638 | + 'options' => array( |
|
639 | + 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
640 | + 'type' => 'object', |
|
641 | + 'context' => array( 'view', 'edit' ), |
|
642 | + 'readonly' => true, |
|
643 | + ), |
|
644 | + 'readonly' => array( |
|
645 | + 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
646 | + 'type' => 'string', |
|
647 | + 'context' => array( 'view' ), |
|
648 | + 'readonly' => true, |
|
649 | + ), |
|
650 | + 'faux' => array( |
|
651 | + 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
652 | + 'type' => 'string', |
|
653 | + 'context' => array( 'view' ), |
|
654 | + 'readonly' => true, |
|
655 | + ), |
|
656 | + ), |
|
657 | + ); |
|
658 | + |
|
659 | + // Filters the settings schema for the REST API. |
|
660 | 660 | $schema = apply_filters( 'getpaid_rest_settings_schema', $schema ); |
661 | 661 | |
662 | - // Cache the settings schema. |
|
663 | - $this->schema = $schema; |
|
664 | - |
|
665 | - return $this->add_additional_fields_schema( $this->schema ); |
|
666 | - |
|
667 | - } |
|
668 | - |
|
669 | - /** |
|
670 | - * Validate a text value for a text based setting. |
|
671 | - * |
|
672 | - * @since 2.0.0 |
|
673 | - * @param string $value Value. |
|
674 | - * @param array $setting Setting. |
|
675 | - * @return string |
|
676 | - */ |
|
677 | - public function validate_setting_text_field( $value ) { |
|
678 | - $value = is_null( $value ) ? '' : $value; |
|
679 | - return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
680 | - } |
|
681 | - |
|
682 | - /** |
|
683 | - * Validate select based settings. |
|
684 | - * |
|
685 | - * @since 2.0.0 |
|
686 | - * @param string $value Value. |
|
687 | - * @param array $setting Setting. |
|
688 | - * @return string|WP_Error |
|
689 | - */ |
|
690 | - public function validate_setting_select_field( $value, $setting ) { |
|
691 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
692 | - return $value; |
|
693 | - } else { |
|
694 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
695 | - } |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * Validate multiselect based settings. |
|
700 | - * |
|
701 | - * @since 2.0.0 |
|
702 | - * @param array $values Values. |
|
703 | - * @param array $setting Setting. |
|
704 | - * @return array|WP_Error |
|
705 | - */ |
|
706 | - public function validate_setting_multiselect_field( $values, $setting ) { |
|
707 | - if ( empty( $values ) ) { |
|
708 | - return array(); |
|
709 | - } |
|
710 | - |
|
711 | - if ( ! is_array( $values ) ) { |
|
712 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
713 | - } |
|
714 | - |
|
715 | - $final_values = array(); |
|
716 | - foreach ( $values as $value ) { |
|
717 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
718 | - $final_values[] = $value; |
|
719 | - } |
|
720 | - } |
|
721 | - |
|
722 | - return $final_values; |
|
723 | - } |
|
724 | - |
|
725 | - /** |
|
726 | - * Validate radio based settings. |
|
727 | - * |
|
728 | - * @since 2.0.0 |
|
729 | - * @param string $value Value. |
|
730 | - * @param array $setting Setting. |
|
731 | - * @return string|WP_Error |
|
732 | - */ |
|
733 | - public function validate_setting_radio_field( $value, $setting ) { |
|
734 | - return $this->validate_setting_select_field( $value, $setting ); |
|
735 | - } |
|
736 | - |
|
737 | - /** |
|
738 | - * Validate checkbox based settings. |
|
739 | - * |
|
740 | - * @since 2.0.0 |
|
741 | - * @param string $value Value. |
|
742 | - * @return int |
|
743 | - */ |
|
744 | - public function validate_setting_checkbox_field( $value ) { |
|
745 | - return (int) ! empty( $value ); |
|
746 | - } |
|
747 | - |
|
748 | - /** |
|
749 | - * Validate textarea based settings. |
|
750 | - * |
|
751 | - * @since 2.0.0 |
|
752 | - * @param string $value Value. |
|
753 | - * @return string |
|
754 | - */ |
|
755 | - public function validate_setting_textarea_field( $value ) { |
|
756 | - $value = is_null( $value ) ? '' : $value; |
|
757 | - return wp_kses( |
|
758 | - trim( stripslashes( $value ) ), |
|
759 | - array_merge( |
|
760 | - array( |
|
761 | - 'iframe' => array( |
|
762 | - 'src' => true, |
|
763 | - 'style' => true, |
|
764 | - 'id' => true, |
|
765 | - 'class' => true, |
|
766 | - ), |
|
767 | - ), |
|
768 | - wp_kses_allowed_html( 'post' ) |
|
769 | - ) |
|
770 | - ); |
|
771 | - } |
|
662 | + // Cache the settings schema. |
|
663 | + $this->schema = $schema; |
|
664 | + |
|
665 | + return $this->add_additional_fields_schema( $this->schema ); |
|
666 | + |
|
667 | + } |
|
668 | + |
|
669 | + /** |
|
670 | + * Validate a text value for a text based setting. |
|
671 | + * |
|
672 | + * @since 2.0.0 |
|
673 | + * @param string $value Value. |
|
674 | + * @param array $setting Setting. |
|
675 | + * @return string |
|
676 | + */ |
|
677 | + public function validate_setting_text_field( $value ) { |
|
678 | + $value = is_null( $value ) ? '' : $value; |
|
679 | + return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
680 | + } |
|
681 | + |
|
682 | + /** |
|
683 | + * Validate select based settings. |
|
684 | + * |
|
685 | + * @since 2.0.0 |
|
686 | + * @param string $value Value. |
|
687 | + * @param array $setting Setting. |
|
688 | + * @return string|WP_Error |
|
689 | + */ |
|
690 | + public function validate_setting_select_field( $value, $setting ) { |
|
691 | + if ( array_key_exists( $value, $setting['options'] ) ) { |
|
692 | + return $value; |
|
693 | + } else { |
|
694 | + return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
695 | + } |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * Validate multiselect based settings. |
|
700 | + * |
|
701 | + * @since 2.0.0 |
|
702 | + * @param array $values Values. |
|
703 | + * @param array $setting Setting. |
|
704 | + * @return array|WP_Error |
|
705 | + */ |
|
706 | + public function validate_setting_multiselect_field( $values, $setting ) { |
|
707 | + if ( empty( $values ) ) { |
|
708 | + return array(); |
|
709 | + } |
|
710 | + |
|
711 | + if ( ! is_array( $values ) ) { |
|
712 | + return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
713 | + } |
|
714 | + |
|
715 | + $final_values = array(); |
|
716 | + foreach ( $values as $value ) { |
|
717 | + if ( array_key_exists( $value, $setting['options'] ) ) { |
|
718 | + $final_values[] = $value; |
|
719 | + } |
|
720 | + } |
|
721 | + |
|
722 | + return $final_values; |
|
723 | + } |
|
724 | + |
|
725 | + /** |
|
726 | + * Validate radio based settings. |
|
727 | + * |
|
728 | + * @since 2.0.0 |
|
729 | + * @param string $value Value. |
|
730 | + * @param array $setting Setting. |
|
731 | + * @return string|WP_Error |
|
732 | + */ |
|
733 | + public function validate_setting_radio_field( $value, $setting ) { |
|
734 | + return $this->validate_setting_select_field( $value, $setting ); |
|
735 | + } |
|
736 | + |
|
737 | + /** |
|
738 | + * Validate checkbox based settings. |
|
739 | + * |
|
740 | + * @since 2.0.0 |
|
741 | + * @param string $value Value. |
|
742 | + * @return int |
|
743 | + */ |
|
744 | + public function validate_setting_checkbox_field( $value ) { |
|
745 | + return (int) ! empty( $value ); |
|
746 | + } |
|
747 | + |
|
748 | + /** |
|
749 | + * Validate textarea based settings. |
|
750 | + * |
|
751 | + * @since 2.0.0 |
|
752 | + * @param string $value Value. |
|
753 | + * @return string |
|
754 | + */ |
|
755 | + public function validate_setting_textarea_field( $value ) { |
|
756 | + $value = is_null( $value ) ? '' : $value; |
|
757 | + return wp_kses( |
|
758 | + trim( stripslashes( $value ) ), |
|
759 | + array_merge( |
|
760 | + array( |
|
761 | + 'iframe' => array( |
|
762 | + 'src' => true, |
|
763 | + 'style' => true, |
|
764 | + 'id' => true, |
|
765 | + 'class' => true, |
|
766 | + ), |
|
767 | + ), |
|
768 | + wp_kses_allowed_html( 'post' ) |
|
769 | + ) |
|
770 | + ); |
|
771 | + } |
|
772 | 772 | |
773 | 773 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | * @since 2.0.0 |
10 | 10 | */ |
11 | 11 | |
12 | -defined( 'ABSPATH' ) || exit; |
|
12 | +defined('ABSPATH') || exit; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * GetPaid REST Setting controller class. |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @see register_rest_route() |
41 | 41 | */ |
42 | - public function register_namespace_routes( $namespace ) { |
|
42 | + public function register_namespace_routes($namespace) { |
|
43 | 43 | |
44 | 44 | // List all registered tabs. |
45 | 45 | register_rest_route( |
@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | array( |
49 | 49 | array( |
50 | 50 | 'methods' => WP_REST_Server::READABLE, |
51 | - 'callback' => array( $this, 'get_tabs' ), |
|
52 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
51 | + 'callback' => array($this, 'get_tabs'), |
|
52 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
53 | 53 | ), |
54 | 54 | 'schema' => '__return_empty_array', |
55 | 55 | ) |
@@ -62,23 +62,23 @@ discard block |
||
62 | 62 | array( |
63 | 63 | 'args' => array( |
64 | 64 | 'id' => array( |
65 | - 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
65 | + 'description' => __('Unique identifier for the setting.', 'invoicing'), |
|
66 | 66 | 'type' => 'string', |
67 | 67 | 'required' => true, |
68 | 68 | ), |
69 | 69 | ), |
70 | 70 | array( |
71 | 71 | 'methods' => WP_REST_Server::READABLE, |
72 | - 'callback' => array( $this, 'get_item' ), |
|
73 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
72 | + 'callback' => array($this, 'get_item'), |
|
73 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
74 | 74 | ), |
75 | 75 | array( |
76 | 76 | 'methods' => WP_REST_Server::EDITABLE, |
77 | - 'callback' => array( $this, 'update_item' ), |
|
78 | - 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
79 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
77 | + 'callback' => array($this, 'update_item'), |
|
78 | + 'permission_callback' => array($this, 'update_items_permissions_check'), |
|
79 | + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), |
|
80 | 80 | ), |
81 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
81 | + 'schema' => array($this, 'get_public_item_schema'), |
|
82 | 82 | ) |
83 | 83 | ); |
84 | 84 | |
@@ -89,16 +89,16 @@ discard block |
||
89 | 89 | array( |
90 | 90 | 'args' => array( |
91 | 91 | 'tab' => array( |
92 | - 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
92 | + 'description' => __('Unique identifier for the tab whose sections should be retrieved.', 'invoicing'), |
|
93 | 93 | 'type' => 'string', |
94 | 94 | 'required' => true, |
95 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
95 | + 'enum' => array_keys(wpinv_get_settings_tabs()), |
|
96 | 96 | ), |
97 | 97 | ), |
98 | 98 | array( |
99 | 99 | 'methods' => WP_REST_Server::READABLE, |
100 | - 'callback' => array( $this, 'get_sections' ), |
|
101 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
100 | + 'callback' => array($this, 'get_sections'), |
|
101 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
102 | 102 | ), |
103 | 103 | 'schema' => '__return_empty_array', |
104 | 104 | ) |
@@ -111,23 +111,23 @@ discard block |
||
111 | 111 | array( |
112 | 112 | 'args' => array( |
113 | 113 | 'tab' => array( |
114 | - 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
114 | + 'description' => __('Unique identifier for the tab whose settings should be retrieved.', 'invoicing'), |
|
115 | 115 | 'type' => 'string', |
116 | 116 | 'required' => true, |
117 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
117 | + 'enum' => array_keys(wpinv_get_settings_tabs()), |
|
118 | 118 | ), |
119 | 119 | 'section' => array( |
120 | - 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
120 | + 'description' => __('The section in the tab whose settings should be retrieved.', 'invoicing'), |
|
121 | 121 | 'type' => 'string', |
122 | 122 | 'required' => true, |
123 | 123 | ), |
124 | 124 | ), |
125 | 125 | array( |
126 | 126 | 'methods' => WP_REST_Server::READABLE, |
127 | - 'callback' => array( $this, 'get_items' ), |
|
128 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
127 | + 'callback' => array($this, 'get_items'), |
|
128 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
129 | 129 | ), |
130 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
130 | + 'schema' => array($this, 'get_public_item_schema'), |
|
131 | 131 | ) |
132 | 132 | ); |
133 | 133 | |
@@ -137,17 +137,17 @@ discard block |
||
137 | 137 | array( |
138 | 138 | 'args' => array( |
139 | 139 | 'id' => array( |
140 | - 'description' => __( 'Setting ID.', 'invoicing' ), |
|
140 | + 'description' => __('Setting ID.', 'invoicing'), |
|
141 | 141 | 'type' => 'string', |
142 | 142 | ), |
143 | 143 | ), |
144 | 144 | array( |
145 | 145 | 'methods' => WP_REST_Server::EDITABLE, |
146 | - 'callback' => array( $this, 'batch_items' ), |
|
147 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
148 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
146 | + 'callback' => array($this, 'batch_items'), |
|
147 | + 'permission_callback' => array($this, 'batch_items_permissions_check'), |
|
148 | + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), |
|
149 | 149 | ), |
150 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
150 | + 'schema' => array($this, 'get_public_batch_schema'), |
|
151 | 151 | ) |
152 | 152 | ); |
153 | 153 | |
@@ -160,33 +160,33 @@ discard block |
||
160 | 160 | * @param WP_REST_Request $request Request data. |
161 | 161 | * @return WP_Error|WP_REST_Response |
162 | 162 | */ |
163 | - public function get_items( $request ) { |
|
163 | + public function get_items($request) { |
|
164 | 164 | |
165 | 165 | $settings = $this->get_settings(); |
166 | 166 | |
167 | - if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
168 | - return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
167 | + if (!isset($settings[$request['tab']])) { |
|
168 | + return new WP_Error('rest_invalid_tab', __('Invalid tab.', 'invoicing'), array('status' => 400)); |
|
169 | 169 | } |
170 | 170 | |
171 | - if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
172 | - return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
171 | + if (!isset($settings[$request['tab']][$request['section']])) { |
|
172 | + return new WP_Error('rest_invalid_section', __('Invalid section.', 'invoicing'), array('status' => 400)); |
|
173 | 173 | } |
174 | 174 | |
175 | - $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
175 | + $settings = $settings[$request['tab']][$request['section']]; |
|
176 | 176 | $prepared = array(); |
177 | 177 | |
178 | - foreach ( $settings as $setting ) { |
|
178 | + foreach ($settings as $setting) { |
|
179 | 179 | |
180 | - $setting = $this->sanitize_setting( $setting ); |
|
181 | - $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
182 | - $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
180 | + $setting = $this->sanitize_setting($setting); |
|
181 | + $setting_data = $this->prepare_item_for_response($setting, $request); |
|
182 | + $setting_data = $this->prepare_response_for_collection($setting_data); |
|
183 | 183 | |
184 | - if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
185 | - $prepared[] = $setting_data; |
|
184 | + if ($this->is_setting_type_valid($setting['type'])) { |
|
185 | + $prepared[] = $setting_data; |
|
186 | 186 | } |
187 | 187 | } |
188 | 188 | |
189 | - return rest_ensure_response( $prepared ); |
|
189 | + return rest_ensure_response($prepared); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -196,16 +196,16 @@ discard block |
||
196 | 196 | * @param WP_REST_Request $request Request data. |
197 | 197 | * @return WP_Error|WP_REST_Response |
198 | 198 | */ |
199 | - public function get_item( $request ) { |
|
200 | - $setting = $this->get_setting( $request['id'] ); |
|
199 | + public function get_item($request) { |
|
200 | + $setting = $this->get_setting($request['id']); |
|
201 | 201 | |
202 | - if ( is_wp_error( $setting ) ) { |
|
202 | + if (is_wp_error($setting)) { |
|
203 | 203 | return $setting; |
204 | 204 | } |
205 | 205 | |
206 | - $setting = $this->sanitize_setting( $setting ); |
|
207 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
208 | - return rest_ensure_response( $response ); |
|
206 | + $setting = $this->sanitize_setting($setting); |
|
207 | + $response = $this->prepare_item_for_response($setting, $request); |
|
208 | + return rest_ensure_response($response); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
@@ -215,29 +215,29 @@ discard block |
||
215 | 215 | * @param WP_REST_Request $request Request data. |
216 | 216 | * @return WP_Error|WP_REST_Response |
217 | 217 | */ |
218 | - public function update_item( $request ) { |
|
219 | - $setting = $this->get_setting( $request['id'] ); |
|
218 | + public function update_item($request) { |
|
219 | + $setting = $this->get_setting($request['id']); |
|
220 | 220 | |
221 | - if ( is_wp_error( $setting ) ) { |
|
221 | + if (is_wp_error($setting)) { |
|
222 | 222 | return $setting; |
223 | 223 | } |
224 | 224 | |
225 | - if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
226 | - $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
225 | + if (is_callable(array($this, 'validate_setting_' . $setting['type'] . '_field'))) { |
|
226 | + $value = $this->{'validate_setting_' . $setting['type'] . '_field'}($request['value'], $setting); |
|
227 | 227 | } else { |
228 | - $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
228 | + $value = $this->validate_setting_text_field($request['value'], $setting); |
|
229 | 229 | } |
230 | 230 | |
231 | - if ( is_wp_error( $value ) ) { |
|
231 | + if (is_wp_error($value)) { |
|
232 | 232 | return $value; |
233 | 233 | } |
234 | 234 | |
235 | - wpinv_update_option( $request['id'], $value ); |
|
235 | + wpinv_update_option($request['id'], $value); |
|
236 | 236 | $setting['value'] = $value; |
237 | - $setting = $this->sanitize_setting( $setting ); |
|
238 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
237 | + $setting = $this->sanitize_setting($setting); |
|
238 | + $response = $this->prepare_item_for_response($setting, $request); |
|
239 | 239 | |
240 | - return rest_ensure_response( $response ); |
|
240 | + return rest_ensure_response($response); |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | /** |
@@ -247,9 +247,9 @@ discard block |
||
247 | 247 | * @param WP_REST_Request $request Full data about the request. |
248 | 248 | * @return WP_Error|boolean |
249 | 249 | */ |
250 | - public function get_items_permissions_check( $request ) { |
|
251 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
252 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
250 | + public function get_items_permissions_check($request) { |
|
251 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
252 | + return new WP_Error('rest_cannot_view', __('Sorry, you cannot list resources.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | return true; |
@@ -262,9 +262,9 @@ discard block |
||
262 | 262 | * @param WP_REST_Request $request Full data about the request. |
263 | 263 | * @return WP_Error|boolean |
264 | 264 | */ |
265 | - public function update_items_permissions_check( $request ) { |
|
266 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
267 | - return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
265 | + public function update_items_permissions_check($request) { |
|
266 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
267 | + return new WP_Error('rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | return true; |
@@ -277,8 +277,8 @@ discard block |
||
277 | 277 | * |
278 | 278 | * @return boolean|WP_Error |
279 | 279 | */ |
280 | - public function batch_items_permissions_check( $request ) { |
|
281 | - return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
280 | + public function batch_items_permissions_check($request) { |
|
281 | + return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error('rest_cannot_batch', __('Sorry, you are not allowed to batch manipulate this resource.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
282 | 282 | } |
283 | 283 | |
284 | 284 | /** |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | * @param string $setting_id Setting ID. |
288 | 288 | * @return array Links for the given setting. |
289 | 289 | */ |
290 | - protected function prepare_links( $setting_id ) { |
|
290 | + protected function prepare_links($setting_id) { |
|
291 | 291 | |
292 | 292 | $links = array( |
293 | 293 | 'self' => array( |
294 | - 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
294 | + 'href' => rest_url(sprintf('/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id)), |
|
295 | 295 | ), |
296 | 296 | 'collection' => array( |
297 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
297 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
298 | 298 | ), |
299 | 299 | ); |
300 | 300 | |
@@ -309,14 +309,14 @@ discard block |
||
309 | 309 | * @param WP_REST_Request $request Request object. |
310 | 310 | * @return WP_REST_Response $response Response data. |
311 | 311 | */ |
312 | - public function prepare_item_for_response( $item, $request ) { |
|
313 | - $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
314 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
315 | - $data = $this->filter_response_by_context( $data, $context ); |
|
312 | + public function prepare_item_for_response($item, $request) { |
|
313 | + $context = empty($request['context']) ? 'view' : $request['context']; |
|
314 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
315 | + $data = $this->filter_response_by_context($data, $context); |
|
316 | 316 | |
317 | - $response = rest_ensure_response( $data ); |
|
317 | + $response = rest_ensure_response($data); |
|
318 | 318 | |
319 | - $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
319 | + $response->add_links($this->prepare_links($item['id'])); |
|
320 | 320 | |
321 | 321 | return $response; |
322 | 322 | } |
@@ -329,10 +329,10 @@ discard block |
||
329 | 329 | * @param array $setting Setting. |
330 | 330 | * @return array |
331 | 331 | */ |
332 | - public function filter_setting( $setting ) { |
|
332 | + public function filter_setting($setting) { |
|
333 | 333 | return array_intersect_key( |
334 | 334 | $setting, |
335 | - array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
335 | + array_flip(array_filter(array_keys($setting), array($this, 'allowed_setting_keys'))) |
|
336 | 336 | ); |
337 | 337 | } |
338 | 338 | |
@@ -342,8 +342,8 @@ discard block |
||
342 | 342 | * @param string $key Key to check. |
343 | 343 | * @return boolean |
344 | 344 | */ |
345 | - public function allowed_setting_keys( $key ) { |
|
346 | - return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
345 | + public function allowed_setting_keys($key) { |
|
346 | + return in_array($key, array_keys($this->setting_defaults()), true); |
|
347 | 347 | } |
348 | 348 | |
349 | 349 | /** |
@@ -375,11 +375,11 @@ discard block |
||
375 | 375 | * @param array $setting The setting to sanitize. |
376 | 376 | * @return array |
377 | 377 | */ |
378 | - public function sanitize_setting( $setting ) { |
|
378 | + public function sanitize_setting($setting) { |
|
379 | 379 | |
380 | - $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
381 | - $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
382 | - return $this->filter_setting( $setting ); |
|
380 | + $setting = wp_parse_args($setting, $this->setting_defaults()); |
|
381 | + $setting['value'] = wpinv_get_option($setting['id'], $setting['std']); |
|
382 | + return $this->filter_setting($setting); |
|
383 | 383 | |
384 | 384 | } |
385 | 385 | |
@@ -390,29 +390,29 @@ discard block |
||
390 | 390 | * @param string $setting_id Setting ID. |
391 | 391 | * @return array|WP_Error |
392 | 392 | */ |
393 | - public function get_setting( $setting_id ) { |
|
393 | + public function get_setting($setting_id) { |
|
394 | 394 | |
395 | - if ( empty( $setting_id ) ) { |
|
396 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
395 | + if (empty($setting_id)) { |
|
396 | + return new WP_Error('rest_setting_setting_invalid', __('Invalid setting.', 'invoicing'), array('status' => 404)); |
|
397 | 397 | } |
398 | 398 | |
399 | - $settings = $this->get_settings(); |
|
399 | + $settings = $this->get_settings(); |
|
400 | 400 | |
401 | - foreach ( $settings as $tabs ) { |
|
401 | + foreach ($settings as $tabs) { |
|
402 | 402 | |
403 | - foreach ( $tabs as $sections ) { |
|
403 | + foreach ($tabs as $sections) { |
|
404 | 404 | |
405 | - if ( isset( $sections[ $setting_id ] ) ) { |
|
406 | - if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
407 | - return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
405 | + if (isset($sections[$setting_id])) { |
|
406 | + if (!$this->is_setting_type_valid($sections[$setting_id]['type'])) { |
|
407 | + return new WP_Error('rest_setting_setting_type_invalid', __('Invalid setting type.', 'invoicing'), array('status' => 404)); |
|
408 | 408 | } |
409 | 409 | |
410 | - return $sections[ $setting_id ]; |
|
410 | + return $sections[$setting_id]; |
|
411 | 411 | } |
412 | 412 | } |
413 | 413 | } |
414 | 414 | |
415 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
415 | + return new WP_Error('rest_setting_setting_invalid', __('Invalid setting.', 'invoicing'), array('status' => 404)); |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | /** |
@@ -421,41 +421,41 @@ discard block |
||
421 | 421 | * @param WP_REST_Request $request Request data. |
422 | 422 | * @return array |
423 | 423 | */ |
424 | - public function get_tabs( $request ) { |
|
424 | + public function get_tabs($request) { |
|
425 | 425 | $tabs = wpinv_get_settings_tabs(); |
426 | 426 | $prepared = array(); |
427 | 427 | |
428 | - foreach ( $tabs as $id => $tab ) { |
|
428 | + foreach ($tabs as $id => $tab) { |
|
429 | 429 | |
430 | 430 | $_request = $request; |
431 | - $_request['tab'] = sanitize_title( $id ); |
|
431 | + $_request['tab'] = sanitize_title($id); |
|
432 | 432 | $data = array( |
433 | - 'id' => sanitize_title( $id ), |
|
434 | - 'label' => sanitize_text_field( $tab ), |
|
435 | - 'sections' => $this->get_sections( $_request ), |
|
433 | + 'id' => sanitize_title($id), |
|
434 | + 'label' => sanitize_text_field($tab), |
|
435 | + 'sections' => $this->get_sections($_request), |
|
436 | 436 | ); |
437 | 437 | |
438 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
439 | - $response = rest_ensure_response( $data ); |
|
438 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
439 | + $response = rest_ensure_response($data); |
|
440 | 440 | |
441 | - if ( ! is_wp_error( $response ) ) { |
|
441 | + if (!is_wp_error($response)) { |
|
442 | 442 | $links = array( |
443 | 443 | 'sections' => array( |
444 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
444 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $id)), |
|
445 | 445 | ), |
446 | 446 | 'collection' => array( |
447 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
447 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
448 | 448 | ), |
449 | 449 | ); |
450 | - $response->add_links( $links ); |
|
451 | - $response = $this->prepare_response_for_collection( $response ); |
|
450 | + $response->add_links($links); |
|
451 | + $response = $this->prepare_response_for_collection($response); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | $prepared[] = $response; |
455 | 455 | |
456 | 456 | } |
457 | 457 | |
458 | - return rest_ensure_response( $prepared ); |
|
458 | + return rest_ensure_response($prepared); |
|
459 | 459 | } |
460 | 460 | |
461 | 461 | /** |
@@ -464,43 +464,43 @@ discard block |
||
464 | 464 | * @param WP_REST_Request $request Request data. |
465 | 465 | * @return array |
466 | 466 | */ |
467 | - public function get_sections( $request ) { |
|
467 | + public function get_sections($request) { |
|
468 | 468 | |
469 | - $tab = sanitize_title( $request['tab'] ); |
|
470 | - $sections = wpinv_get_settings_tab_sections( $tab ); |
|
469 | + $tab = sanitize_title($request['tab']); |
|
470 | + $sections = wpinv_get_settings_tab_sections($tab); |
|
471 | 471 | $prepared = array(); |
472 | 472 | |
473 | - foreach ( $sections as $id => $section ) { |
|
473 | + foreach ($sections as $id => $section) { |
|
474 | 474 | |
475 | - $data = array( |
|
476 | - 'id' => sanitize_title( $id ), |
|
477 | - 'label' => sanitize_text_field( $section ), |
|
475 | + $data = array( |
|
476 | + 'id' => sanitize_title($id), |
|
477 | + 'label' => sanitize_text_field($section), |
|
478 | 478 | ); |
479 | 479 | |
480 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
481 | - $response = rest_ensure_response( $data ); |
|
480 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
481 | + $response = rest_ensure_response($data); |
|
482 | 482 | |
483 | - if ( ! is_wp_error( $response ) ) { |
|
483 | + if (!is_wp_error($response)) { |
|
484 | 484 | $links = array( |
485 | 485 | 'settings' => array( |
486 | - 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
486 | + 'href' => rest_url(sprintf('/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id)), |
|
487 | 487 | ), |
488 | 488 | 'collection' => array( |
489 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
489 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $tab)), |
|
490 | 490 | ), |
491 | 491 | 'tabs' => array( |
492 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
492 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
493 | 493 | ), |
494 | 494 | ); |
495 | - $response->add_links( $links ); |
|
496 | - $response = $this->prepare_response_for_collection( $response ); |
|
495 | + $response->add_links($links); |
|
496 | + $response = $this->prepare_response_for_collection($response); |
|
497 | 497 | } |
498 | 498 | |
499 | 499 | $prepared[] = $response; |
500 | 500 | |
501 | 501 | } |
502 | 502 | |
503 | - return rest_ensure_response( $prepared ); |
|
503 | + return rest_ensure_response($prepared); |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | /** |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | */ |
511 | 511 | public function get_settings() { |
512 | 512 | |
513 | - if ( empty( $this->settings ) ) { |
|
513 | + if (empty($this->settings)) { |
|
514 | 514 | $this->settings = wpinv_get_registered_settings(); |
515 | 515 | } |
516 | 516 | |
@@ -525,22 +525,22 @@ discard block |
||
525 | 525 | * @param string $type Type. |
526 | 526 | * @return bool |
527 | 527 | */ |
528 | - public function is_setting_type_valid( $type ) { |
|
528 | + public function is_setting_type_valid($type) { |
|
529 | 529 | |
530 | 530 | return in_array( |
531 | 531 | $type, |
532 | 532 | array( |
533 | - 'text', // Validates with validate_setting_text_field. |
|
534 | - 'email', // Validates with validate_setting_text_field. |
|
535 | - 'number', // Validates with validate_setting_text_field. |
|
536 | - 'color', // Validates with validate_setting_text_field. |
|
537 | - 'password', // Validates with validate_setting_text_field. |
|
538 | - 'textarea', // Validates with validate_setting_textarea_field. |
|
539 | - 'select', // Validates with validate_setting_select_field. |
|
540 | - 'multiselect', // Validates with validate_setting_multiselect_field. |
|
541 | - 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
542 | - 'checkbox', // Validates with validate_setting_checkbox_field. |
|
543 | - 'header', // Validates with validate_setting_text_field. |
|
533 | + 'text', // Validates with validate_setting_text_field. |
|
534 | + 'email', // Validates with validate_setting_text_field. |
|
535 | + 'number', // Validates with validate_setting_text_field. |
|
536 | + 'color', // Validates with validate_setting_text_field. |
|
537 | + 'password', // Validates with validate_setting_text_field. |
|
538 | + 'textarea', // Validates with validate_setting_textarea_field. |
|
539 | + 'select', // Validates with validate_setting_select_field. |
|
540 | + 'multiselect', // Validates with validate_setting_multiselect_field. |
|
541 | + 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
542 | + 'checkbox', // Validates with validate_setting_checkbox_field. |
|
543 | + 'header', // Validates with validate_setting_text_field. |
|
544 | 544 | ) |
545 | 545 | ); |
546 | 546 | |
@@ -554,8 +554,8 @@ discard block |
||
554 | 554 | public function get_item_schema() { |
555 | 555 | |
556 | 556 | // Maybe retrieve the schema from cache. |
557 | - if ( ! empty( $this->schema ) ) { |
|
558 | - return $this->add_additional_fields_schema( $this->schema ); |
|
557 | + if (!empty($this->schema)) { |
|
558 | + return $this->add_additional_fields_schema($this->schema); |
|
559 | 559 | } |
560 | 560 | |
561 | 561 | $schema = array( |
@@ -564,105 +564,105 @@ discard block |
||
564 | 564 | 'type' => 'object', |
565 | 565 | 'properties' => array( |
566 | 566 | 'id' => array( |
567 | - 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
567 | + 'description' => __('A unique identifier for the setting.', 'invoicing'), |
|
568 | 568 | 'type' => 'string', |
569 | 569 | 'arg_options' => array( |
570 | 570 | 'sanitize_callback' => 'sanitize_title', |
571 | 571 | ), |
572 | - 'context' => array( 'view', 'edit' ), |
|
572 | + 'context' => array('view', 'edit'), |
|
573 | 573 | 'readonly' => true, |
574 | 574 | ), |
575 | 575 | 'tab' => array( |
576 | - 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
576 | + 'description' => __('An identifier for the tab this setting belongs to.', 'invoicing'), |
|
577 | 577 | 'type' => 'string', |
578 | 578 | 'arg_options' => array( |
579 | 579 | 'sanitize_callback' => 'sanitize_title', |
580 | 580 | ), |
581 | - 'context' => array( 'view', 'edit' ), |
|
581 | + 'context' => array('view', 'edit'), |
|
582 | 582 | 'readonly' => true, |
583 | 583 | ), |
584 | 584 | 'section' => array( |
585 | - 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
585 | + 'description' => __('An identifier for the section this setting belongs to.', 'invoicing'), |
|
586 | 586 | 'type' => 'string', |
587 | 587 | 'arg_options' => array( |
588 | 588 | 'sanitize_callback' => 'sanitize_title', |
589 | 589 | ), |
590 | - 'context' => array( 'view', 'edit' ), |
|
590 | + 'context' => array('view', 'edit'), |
|
591 | 591 | 'readonly' => true, |
592 | 592 | ), |
593 | 593 | 'name' => array( |
594 | - 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
594 | + 'description' => __('A human readable label for the setting used in interfaces.', 'invoicing'), |
|
595 | 595 | 'type' => 'string', |
596 | 596 | 'arg_options' => array( |
597 | 597 | 'sanitize_callback' => 'sanitize_text_field', |
598 | 598 | ), |
599 | - 'context' => array( 'view', 'edit' ), |
|
599 | + 'context' => array('view', 'edit'), |
|
600 | 600 | 'readonly' => true, |
601 | 601 | ), |
602 | 602 | 'desc' => array( |
603 | - 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
603 | + 'description' => __('A human readable description for the setting used in interfaces.', 'invoicing'), |
|
604 | 604 | 'type' => 'string', |
605 | - 'context' => array( 'view', 'edit' ), |
|
605 | + 'context' => array('view', 'edit'), |
|
606 | 606 | 'readonly' => true, |
607 | 607 | ), |
608 | 608 | 'value' => array( |
609 | - 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
609 | + 'description' => __('The current value of this setting.', 'invoicing'), |
|
610 | 610 | 'type' => 'mixed', |
611 | - 'context' => array( 'view', 'edit' ), |
|
611 | + 'context' => array('view', 'edit'), |
|
612 | 612 | ), |
613 | 613 | 'default' => array( |
614 | - 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
614 | + 'description' => __('Default value for the setting.', 'invoicing'), |
|
615 | 615 | 'type' => 'mixed', |
616 | - 'context' => array( 'view', 'edit' ), |
|
616 | + 'context' => array('view', 'edit'), |
|
617 | 617 | 'readonly' => true, |
618 | 618 | ), |
619 | 619 | 'placeholder' => array( |
620 | - 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
620 | + 'description' => __('Placeholder text to be displayed in text inputs.', 'invoicing'), |
|
621 | 621 | 'type' => 'string', |
622 | 622 | 'arg_options' => array( |
623 | 623 | 'sanitize_callback' => 'sanitize_text_field', |
624 | 624 | ), |
625 | - 'context' => array( 'view', 'edit' ), |
|
625 | + 'context' => array('view', 'edit'), |
|
626 | 626 | 'readonly' => true, |
627 | 627 | ), |
628 | 628 | 'type' => array( |
629 | - 'description' => __( 'Type of setting.', 'invoicing' ), |
|
629 | + 'description' => __('Type of setting.', 'invoicing'), |
|
630 | 630 | 'type' => 'string', |
631 | 631 | 'arg_options' => array( |
632 | 632 | 'sanitize_callback' => 'sanitize_text_field', |
633 | 633 | ), |
634 | - 'context' => array( 'view', 'edit' ), |
|
635 | - 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
634 | + 'context' => array('view', 'edit'), |
|
635 | + 'enum' => array('text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html'), |
|
636 | 636 | 'readonly' => true, |
637 | 637 | ), |
638 | 638 | 'options' => array( |
639 | - 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
639 | + 'description' => __('Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing'), |
|
640 | 640 | 'type' => 'object', |
641 | - 'context' => array( 'view', 'edit' ), |
|
641 | + 'context' => array('view', 'edit'), |
|
642 | 642 | 'readonly' => true, |
643 | 643 | ), |
644 | 644 | 'readonly' => array( |
645 | - 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
645 | + 'description' => __('Whether or not this setting is readonly', 'invoicing'), |
|
646 | 646 | 'type' => 'string', |
647 | - 'context' => array( 'view' ), |
|
647 | + 'context' => array('view'), |
|
648 | 648 | 'readonly' => true, |
649 | 649 | ), |
650 | 650 | 'faux' => array( |
651 | - 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
651 | + 'description' => __('Whether or not this setting is readonly/faux', 'invoicing'), |
|
652 | 652 | 'type' => 'string', |
653 | - 'context' => array( 'view' ), |
|
653 | + 'context' => array('view'), |
|
654 | 654 | 'readonly' => true, |
655 | 655 | ), |
656 | 656 | ), |
657 | 657 | ); |
658 | 658 | |
659 | 659 | // Filters the settings schema for the REST API. |
660 | - $schema = apply_filters( 'getpaid_rest_settings_schema', $schema ); |
|
660 | + $schema = apply_filters('getpaid_rest_settings_schema', $schema); |
|
661 | 661 | |
662 | 662 | // Cache the settings schema. |
663 | 663 | $this->schema = $schema; |
664 | 664 | |
665 | - return $this->add_additional_fields_schema( $this->schema ); |
|
665 | + return $this->add_additional_fields_schema($this->schema); |
|
666 | 666 | |
667 | 667 | } |
668 | 668 | |
@@ -674,9 +674,9 @@ discard block |
||
674 | 674 | * @param array $setting Setting. |
675 | 675 | * @return string |
676 | 676 | */ |
677 | - public function validate_setting_text_field( $value ) { |
|
678 | - $value = is_null( $value ) ? '' : $value; |
|
679 | - return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
677 | + public function validate_setting_text_field($value) { |
|
678 | + $value = is_null($value) ? '' : $value; |
|
679 | + return wp_kses_post(trim(stripslashes($value))); |
|
680 | 680 | } |
681 | 681 | |
682 | 682 | /** |
@@ -687,11 +687,11 @@ discard block |
||
687 | 687 | * @param array $setting Setting. |
688 | 688 | * @return string|WP_Error |
689 | 689 | */ |
690 | - public function validate_setting_select_field( $value, $setting ) { |
|
691 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
690 | + public function validate_setting_select_field($value, $setting) { |
|
691 | + if (array_key_exists($value, $setting['options'])) { |
|
692 | 692 | return $value; |
693 | 693 | } else { |
694 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
694 | + return new WP_Error('rest_setting_value_invalid', __('An invalid setting value was passed.', 'invoicing'), array('status' => 400)); |
|
695 | 695 | } |
696 | 696 | } |
697 | 697 | |
@@ -703,18 +703,18 @@ discard block |
||
703 | 703 | * @param array $setting Setting. |
704 | 704 | * @return array|WP_Error |
705 | 705 | */ |
706 | - public function validate_setting_multiselect_field( $values, $setting ) { |
|
707 | - if ( empty( $values ) ) { |
|
706 | + public function validate_setting_multiselect_field($values, $setting) { |
|
707 | + if (empty($values)) { |
|
708 | 708 | return array(); |
709 | 709 | } |
710 | 710 | |
711 | - if ( ! is_array( $values ) ) { |
|
712 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
711 | + if (!is_array($values)) { |
|
712 | + return new WP_Error('rest_setting_value_invalid', __('An invalid setting value was passed.', 'invoicing'), array('status' => 400)); |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | $final_values = array(); |
716 | - foreach ( $values as $value ) { |
|
717 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
716 | + foreach ($values as $value) { |
|
717 | + if (array_key_exists($value, $setting['options'])) { |
|
718 | 718 | $final_values[] = $value; |
719 | 719 | } |
720 | 720 | } |
@@ -730,8 +730,8 @@ discard block |
||
730 | 730 | * @param array $setting Setting. |
731 | 731 | * @return string|WP_Error |
732 | 732 | */ |
733 | - public function validate_setting_radio_field( $value, $setting ) { |
|
734 | - return $this->validate_setting_select_field( $value, $setting ); |
|
733 | + public function validate_setting_radio_field($value, $setting) { |
|
734 | + return $this->validate_setting_select_field($value, $setting); |
|
735 | 735 | } |
736 | 736 | |
737 | 737 | /** |
@@ -741,8 +741,8 @@ discard block |
||
741 | 741 | * @param string $value Value. |
742 | 742 | * @return int |
743 | 743 | */ |
744 | - public function validate_setting_checkbox_field( $value ) { |
|
745 | - return (int) ! empty( $value ); |
|
744 | + public function validate_setting_checkbox_field($value) { |
|
745 | + return (int) !empty($value); |
|
746 | 746 | } |
747 | 747 | |
748 | 748 | /** |
@@ -752,10 +752,10 @@ discard block |
||
752 | 752 | * @param string $value Value. |
753 | 753 | * @return string |
754 | 754 | */ |
755 | - public function validate_setting_textarea_field( $value ) { |
|
756 | - $value = is_null( $value ) ? '' : $value; |
|
755 | + public function validate_setting_textarea_field($value) { |
|
756 | + $value = is_null($value) ? '' : $value; |
|
757 | 757 | return wp_kses( |
758 | - trim( stripslashes( $value ) ), |
|
758 | + trim(stripslashes($value)), |
|
759 | 759 | array_merge( |
760 | 760 | array( |
761 | 761 | 'iframe' => array( |
@@ -765,7 +765,7 @@ discard block |
||
765 | 765 | 'class' => true, |
766 | 766 | ), |
767 | 767 | ), |
768 | - wp_kses_allowed_html( 'post' ) |
|
768 | + wp_kses_allowed_html('post') |
|
769 | 769 | ) |
770 | 770 | ); |
771 | 771 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * @version 1.0.19 |
8 | 8 | */ |
9 | 9 | |
10 | -defined( 'ABSPATH' ) || exit; |
|
10 | +defined('ABSPATH') || exit; |
|
11 | 11 | |
12 | 12 | global $aui_bs5; |
13 | 13 | ?> |
@@ -17,8 +17,8 @@ discard block |
||
17 | 17 | <div class="modal-dialog modal-dialog-centered modal-lg" role="checkout" style="max-width: 650px;"> |
18 | 18 | <div class="modal-content"> |
19 | 19 | <div class="modal-body"> |
20 | - <button type="button" class=" btn-close p-2 getpaid-payment-modal-close d-sm-none" data-<?php echo $aui_bs5 ? 'bs-' : ''; ?>dismiss="modal" aria-label="<?php esc_attr__( 'Close', 'invoicing' ); ?>"> |
|
21 | - <?php if ( empty( $aui_bs5 ) ) : ?> |
|
20 | + <button type="button" class=" btn-close p-2 getpaid-payment-modal-close d-sm-none" data-<?php echo $aui_bs5 ? 'bs-' : ''; ?>dismiss="modal" aria-label="<?php esc_attr__('Close', 'invoicing'); ?>"> |
|
21 | + <?php if (empty($aui_bs5)) : ?> |
|
22 | 22 | <span aria-hidden="true">×</span> |
23 | 23 | <?php endif; ?> |
24 | 24 | </button> |
@@ -9,346 +9,346 @@ discard block |
||
9 | 9 | * @author Roman Ožana <[email protected]> |
10 | 10 | */ |
11 | 11 | class Emogrifier { |
12 | - /** |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - const ENCODING = 'UTF-8'; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var integer |
|
19 | - */ |
|
20 | - const CACHE_KEY_CSS = 0; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var integer |
|
24 | - */ |
|
25 | - const CACHE_KEY_SELECTOR = 1; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var integer |
|
29 | - */ |
|
30 | - const CACHE_KEY_XPATH = 2; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var integer |
|
34 | - */ |
|
35 | - const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | - |
|
37 | - /** |
|
38 | - * for calculating nth-of-type and nth-child selectors. |
|
39 | - * |
|
40 | - * @var integer |
|
41 | - */ |
|
42 | - const INDEX = 0; |
|
43 | - |
|
44 | - /** |
|
45 | - * for calculating nth-of-type and nth-child selectors. |
|
46 | - * |
|
47 | - * @var integer |
|
48 | - */ |
|
49 | - const MULTIPLIER = 1; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string |
|
63 | - */ |
|
64 | - private $html = ''; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - private $css = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var array<string> |
|
73 | - */ |
|
74 | - private $unprocessableHtmlTags = array('wbr'); |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array<array> |
|
78 | - */ |
|
79 | - private $caches = array( |
|
80 | - self::CACHE_KEY_CSS => array(), |
|
81 | - self::CACHE_KEY_SELECTOR => array(), |
|
82 | - self::CACHE_KEY_XPATH => array(), |
|
83 | - self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | - ); |
|
85 | - |
|
86 | - /** |
|
87 | - * the visited nodes with the XPath paths as array keys. |
|
88 | - * |
|
89 | - * @var array<\DOMNode> |
|
90 | - */ |
|
91 | - private $visitedNodes = array(); |
|
92 | - |
|
93 | - /** |
|
94 | - * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | - * as key/value pairs for the inner array. |
|
96 | - * |
|
97 | - * @var array<array><string> |
|
98 | - */ |
|
99 | - private $styleAttributesForNodes = array(); |
|
100 | - |
|
101 | - /** |
|
102 | - * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | - * |
|
104 | - * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | - * |
|
106 | - * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | - * |
|
108 | - * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | - * |
|
110 | - * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | - * |
|
112 | - * @var boolean |
|
113 | - */ |
|
114 | - public $preserveEncoding = false; |
|
115 | - |
|
116 | - public static $_media = ''; |
|
117 | - |
|
118 | - /** |
|
119 | - * The constructor. |
|
120 | - * |
|
121 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | - */ |
|
124 | - public function __construct($html = '', $css = '') { |
|
125 | - $this->setHtml($html); |
|
126 | - $this->setCss($css); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * The destructor. |
|
131 | - */ |
|
132 | - public function __destruct() { |
|
133 | - $this->purgeVisitedNodes(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the HTML to emogrify. |
|
138 | - * |
|
139 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | - */ |
|
141 | - public function setHtml($html = '') { |
|
142 | - $this->html = $html; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Sets the CSS to merge with the HTML. |
|
147 | - * |
|
148 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | - */ |
|
150 | - public function setCss($css = '') { |
|
151 | - $this->css = $css; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Clears all caches. |
|
156 | - */ |
|
157 | - private function clearAllCaches() { |
|
158 | - $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | - $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | - $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | - $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Clears a single cache by key. |
|
166 | - * |
|
167 | - * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | - * |
|
169 | - * @throws InvalidArgumentException |
|
170 | - */ |
|
171 | - private function clearCache($key) { |
|
172 | - $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | - if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | - throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | - } |
|
176 | - |
|
177 | - $this->caches[$key] = array(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Purges the visited nodes. |
|
182 | - */ |
|
183 | - private function purgeVisitedNodes() { |
|
184 | - $this->visitedNodes = array(); |
|
185 | - $this->styleAttributesForNodes = array(); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Marks a tag for removal. |
|
190 | - * |
|
191 | - * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | - * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | - * |
|
194 | - * Note: The tags will not be removed if they have any content. |
|
195 | - * |
|
196 | - * @param string $tagName the tag name, e.g., "p" |
|
197 | - */ |
|
198 | - public function addUnprocessableHtmlTag($tagName) { |
|
199 | - $this->unprocessableHtmlTags[] = $tagName; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Drops a tag from the removal list. |
|
204 | - * |
|
205 | - * @param string $tagName the tag name, e.g., "p" |
|
206 | - */ |
|
207 | - public function removeUnprocessableHtmlTag($tagName) { |
|
208 | - $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | - if ($key !== false) { |
|
210 | - unset($this->unprocessableHtmlTags[$key]); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Applies the CSS you submit to the HTML you submit. |
|
216 | - * |
|
217 | - * This method places the CSS inline. |
|
218 | - * |
|
219 | - * @return string |
|
220 | - * |
|
221 | - * @throws BadMethodCallException |
|
222 | - */ |
|
223 | - public function emogrify() { |
|
224 | - if ($this->html === '') { |
|
225 | - throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | - } |
|
227 | - |
|
228 | - $xmlDocument = $this->createXmlDocument(); |
|
229 | - $xpath = new DOMXPath($xmlDocument); |
|
230 | - $this->clearAllCaches(); |
|
231 | - |
|
232 | - // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | - // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | - // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | - $this->purgeVisitedNodes(); |
|
236 | - |
|
237 | - $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | - if ($nodesWithStyleAttributes !== false) { |
|
239 | - /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | - foreach ($nodesWithStyleAttributes as $node) { |
|
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | - |
|
243 | - // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | - $nodePath = $node->getNodePath(); |
|
245 | - if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | - $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | - $this->visitedNodes[$nodePath] = $node; |
|
248 | - } |
|
249 | - |
|
250 | - $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | - // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | - $allCss = $this->css; |
|
257 | - |
|
258 | - $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | - |
|
260 | - $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | - self::$_media = ''; // reset |
|
262 | - |
|
263 | - $cssKey = md5($cssParts['css']); |
|
264 | - if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | - // process the CSS file for selectors and definitions |
|
266 | - preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | - |
|
268 | - $allSelectors = array(); |
|
269 | - foreach ($matches as $key => $selectorString) { |
|
270 | - // if there is a blank definition, skip |
|
271 | - if (!strlen(trim($selectorString[2]))) { |
|
272 | - continue; |
|
273 | - } |
|
274 | - |
|
275 | - // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | - $selectors = explode(',', $selectorString[1]); |
|
277 | - foreach ($selectors as $selector) { |
|
278 | - // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | - if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | - continue; |
|
281 | - } |
|
282 | - |
|
283 | - $allSelectors[] = array('selector' => trim($selector), |
|
284 | - 'attributes' => trim($selectorString[2]), |
|
285 | - // keep track of where it appears in the file, since order is important |
|
286 | - 'line' => $key, |
|
287 | - ); |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - // now sort the selectors by precedence |
|
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | - |
|
294 | - $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | - } |
|
296 | - |
|
297 | - foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | - // query the body for the xpath selector |
|
299 | - $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | - |
|
301 | - /** @var $node \DOMNode */ |
|
302 | - foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | - // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | - if ($node->hasAttribute('style')) { |
|
305 | - // break it up into an associative array |
|
306 | - $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | - } else { |
|
308 | - $oldStyleDeclarations = array(); |
|
309 | - } |
|
310 | - $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | - foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | - $node = $this->visitedNodes[$nodePath]; |
|
318 | - $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | - } |
|
321 | - |
|
322 | - // This removes styles from your email that contain display:none. |
|
323 | - // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | - // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | - // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | - $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | - // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | - // we don't try to call removeChild on a nonexistent child node |
|
329 | - if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | - /** @var $node \DOMNode */ |
|
331 | - foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | - $node->parentNode->removeChild($node); |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | - |
|
340 | - if ($this->preserveEncoding) { |
|
341 | - // Deprecated since PHP 8.2 |
|
342 | - if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
343 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
344 | - } else { |
|
345 | - return mb_encode_numericentity( $xmlDocument->saveHTML(), [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
346 | - //return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
347 | - } |
|
348 | - } else { |
|
349 | - return $xmlDocument->saveHTML(); |
|
350 | - } |
|
351 | - } |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + const ENCODING = 'UTF-8'; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var integer |
|
19 | + */ |
|
20 | + const CACHE_KEY_CSS = 0; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var integer |
|
24 | + */ |
|
25 | + const CACHE_KEY_SELECTOR = 1; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var integer |
|
29 | + */ |
|
30 | + const CACHE_KEY_XPATH = 2; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var integer |
|
34 | + */ |
|
35 | + const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | + |
|
37 | + /** |
|
38 | + * for calculating nth-of-type and nth-child selectors. |
|
39 | + * |
|
40 | + * @var integer |
|
41 | + */ |
|
42 | + const INDEX = 0; |
|
43 | + |
|
44 | + /** |
|
45 | + * for calculating nth-of-type and nth-child selectors. |
|
46 | + * |
|
47 | + * @var integer |
|
48 | + */ |
|
49 | + const MULTIPLIER = 1; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string |
|
63 | + */ |
|
64 | + private $html = ''; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + private $css = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var array<string> |
|
73 | + */ |
|
74 | + private $unprocessableHtmlTags = array('wbr'); |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array<array> |
|
78 | + */ |
|
79 | + private $caches = array( |
|
80 | + self::CACHE_KEY_CSS => array(), |
|
81 | + self::CACHE_KEY_SELECTOR => array(), |
|
82 | + self::CACHE_KEY_XPATH => array(), |
|
83 | + self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | + ); |
|
85 | + |
|
86 | + /** |
|
87 | + * the visited nodes with the XPath paths as array keys. |
|
88 | + * |
|
89 | + * @var array<\DOMNode> |
|
90 | + */ |
|
91 | + private $visitedNodes = array(); |
|
92 | + |
|
93 | + /** |
|
94 | + * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | + * as key/value pairs for the inner array. |
|
96 | + * |
|
97 | + * @var array<array><string> |
|
98 | + */ |
|
99 | + private $styleAttributesForNodes = array(); |
|
100 | + |
|
101 | + /** |
|
102 | + * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | + * |
|
104 | + * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | + * |
|
106 | + * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | + * |
|
108 | + * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | + * |
|
110 | + * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | + * |
|
112 | + * @var boolean |
|
113 | + */ |
|
114 | + public $preserveEncoding = false; |
|
115 | + |
|
116 | + public static $_media = ''; |
|
117 | + |
|
118 | + /** |
|
119 | + * The constructor. |
|
120 | + * |
|
121 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | + */ |
|
124 | + public function __construct($html = '', $css = '') { |
|
125 | + $this->setHtml($html); |
|
126 | + $this->setCss($css); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * The destructor. |
|
131 | + */ |
|
132 | + public function __destruct() { |
|
133 | + $this->purgeVisitedNodes(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the HTML to emogrify. |
|
138 | + * |
|
139 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | + */ |
|
141 | + public function setHtml($html = '') { |
|
142 | + $this->html = $html; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Sets the CSS to merge with the HTML. |
|
147 | + * |
|
148 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | + */ |
|
150 | + public function setCss($css = '') { |
|
151 | + $this->css = $css; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Clears all caches. |
|
156 | + */ |
|
157 | + private function clearAllCaches() { |
|
158 | + $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | + $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | + $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | + $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Clears a single cache by key. |
|
166 | + * |
|
167 | + * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | + * |
|
169 | + * @throws InvalidArgumentException |
|
170 | + */ |
|
171 | + private function clearCache($key) { |
|
172 | + $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | + if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | + throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | + } |
|
176 | + |
|
177 | + $this->caches[$key] = array(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Purges the visited nodes. |
|
182 | + */ |
|
183 | + private function purgeVisitedNodes() { |
|
184 | + $this->visitedNodes = array(); |
|
185 | + $this->styleAttributesForNodes = array(); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Marks a tag for removal. |
|
190 | + * |
|
191 | + * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | + * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | + * |
|
194 | + * Note: The tags will not be removed if they have any content. |
|
195 | + * |
|
196 | + * @param string $tagName the tag name, e.g., "p" |
|
197 | + */ |
|
198 | + public function addUnprocessableHtmlTag($tagName) { |
|
199 | + $this->unprocessableHtmlTags[] = $tagName; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Drops a tag from the removal list. |
|
204 | + * |
|
205 | + * @param string $tagName the tag name, e.g., "p" |
|
206 | + */ |
|
207 | + public function removeUnprocessableHtmlTag($tagName) { |
|
208 | + $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | + if ($key !== false) { |
|
210 | + unset($this->unprocessableHtmlTags[$key]); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Applies the CSS you submit to the HTML you submit. |
|
216 | + * |
|
217 | + * This method places the CSS inline. |
|
218 | + * |
|
219 | + * @return string |
|
220 | + * |
|
221 | + * @throws BadMethodCallException |
|
222 | + */ |
|
223 | + public function emogrify() { |
|
224 | + if ($this->html === '') { |
|
225 | + throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | + } |
|
227 | + |
|
228 | + $xmlDocument = $this->createXmlDocument(); |
|
229 | + $xpath = new DOMXPath($xmlDocument); |
|
230 | + $this->clearAllCaches(); |
|
231 | + |
|
232 | + // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | + // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | + // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | + $this->purgeVisitedNodes(); |
|
236 | + |
|
237 | + $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | + if ($nodesWithStyleAttributes !== false) { |
|
239 | + /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | + foreach ($nodesWithStyleAttributes as $node) { |
|
241 | + $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | + |
|
243 | + // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | + $nodePath = $node->getNodePath(); |
|
245 | + if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | + $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | + $this->visitedNodes[$nodePath] = $node; |
|
248 | + } |
|
249 | + |
|
250 | + $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | + // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | + $allCss = $this->css; |
|
257 | + |
|
258 | + $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | + |
|
260 | + $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | + self::$_media = ''; // reset |
|
262 | + |
|
263 | + $cssKey = md5($cssParts['css']); |
|
264 | + if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | + // process the CSS file for selectors and definitions |
|
266 | + preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | + |
|
268 | + $allSelectors = array(); |
|
269 | + foreach ($matches as $key => $selectorString) { |
|
270 | + // if there is a blank definition, skip |
|
271 | + if (!strlen(trim($selectorString[2]))) { |
|
272 | + continue; |
|
273 | + } |
|
274 | + |
|
275 | + // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | + $selectors = explode(',', $selectorString[1]); |
|
277 | + foreach ($selectors as $selector) { |
|
278 | + // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | + if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | + continue; |
|
281 | + } |
|
282 | + |
|
283 | + $allSelectors[] = array('selector' => trim($selector), |
|
284 | + 'attributes' => trim($selectorString[2]), |
|
285 | + // keep track of where it appears in the file, since order is important |
|
286 | + 'line' => $key, |
|
287 | + ); |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + // now sort the selectors by precedence |
|
292 | + usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | + |
|
294 | + $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | + } |
|
296 | + |
|
297 | + foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | + // query the body for the xpath selector |
|
299 | + $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | + |
|
301 | + /** @var $node \DOMNode */ |
|
302 | + foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | + // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | + if ($node->hasAttribute('style')) { |
|
305 | + // break it up into an associative array |
|
306 | + $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | + } else { |
|
308 | + $oldStyleDeclarations = array(); |
|
309 | + } |
|
310 | + $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | + foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | + $node = $this->visitedNodes[$nodePath]; |
|
318 | + $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | + } |
|
321 | + |
|
322 | + // This removes styles from your email that contain display:none. |
|
323 | + // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | + // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | + // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | + $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | + // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | + // we don't try to call removeChild on a nonexistent child node |
|
329 | + if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | + /** @var $node \DOMNode */ |
|
331 | + foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | + $node->parentNode->removeChild($node); |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | + |
|
340 | + if ($this->preserveEncoding) { |
|
341 | + // Deprecated since PHP 8.2 |
|
342 | + if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
343 | + return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
344 | + } else { |
|
345 | + return mb_encode_numericentity( $xmlDocument->saveHTML(), [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
346 | + //return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
347 | + } |
|
348 | + } else { |
|
349 | + return $xmlDocument->saveHTML(); |
|
350 | + } |
|
351 | + } |
|
352 | 352 | |
353 | 353 | /** |
354 | 354 | * String to lower. |
@@ -358,143 +358,143 @@ discard block |
||
358 | 358 | * @param array $m |
359 | 359 | * @return string |
360 | 360 | */ |
361 | - public function strtolower(array $m) { |
|
362 | - return strtolower($m[0]); |
|
363 | - } |
|
364 | - |
|
365 | - |
|
366 | - /** |
|
367 | - * This method merges old or existing name/value array with new name/value array. |
|
368 | - * and then generates a string of the combined style suitable for placing inline. |
|
369 | - * This becomes the single point for CSS string generation allowing for consistent. |
|
370 | - * CSS output no matter where the CSS originally came from. |
|
371 | - * @param array $oldStyles |
|
372 | - * @param array $newStyles |
|
373 | - * @return string |
|
374 | - */ |
|
375 | - private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
376 | - $combinedStyles = array_merge($oldStyles, $newStyles); |
|
377 | - $style = ''; |
|
378 | - foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
379 | - $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
380 | - } |
|
381 | - return trim($style); |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * Copies the media part from CSS array parts to $xmlDocument. |
|
387 | - * |
|
388 | - * @param array $cssParts |
|
389 | - * @param DOMDocument $xmlDocument |
|
390 | - */ |
|
391 | - public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
392 | - if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
393 | - $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
394 | - } |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Returns CSS content. |
|
399 | - * |
|
400 | - * @param DOMXPath $xpath |
|
401 | - * @return string |
|
402 | - */ |
|
403 | - private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
404 | - $styleNodes = $xpath->query('//style'); |
|
405 | - |
|
406 | - if ($styleNodes === false) { |
|
407 | - return ''; |
|
408 | - } |
|
409 | - |
|
410 | - $css = ''; |
|
411 | - /** @var $styleNode DOMNode */ |
|
412 | - foreach ($styleNodes as $styleNode) { |
|
413 | - $css .= "\n\n" . $styleNode->nodeValue; |
|
414 | - $styleNode->parentNode->removeChild($styleNode); |
|
415 | - } |
|
416 | - |
|
417 | - return $css; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Adds a style element with $css to $document. |
|
422 | - * |
|
423 | - * @param DOMDocument $document |
|
424 | - * @param string $css |
|
425 | - */ |
|
426 | - private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
427 | - $styleElement = $document->createElement('style', $css); |
|
428 | - $styleAttribute = $document->createAttribute('type'); |
|
429 | - $styleAttribute->value = 'text/css'; |
|
430 | - $styleElement->appendChild($styleAttribute); |
|
431 | - |
|
432 | - $head = $this->getOrCreateHeadElement($document); |
|
433 | - $head->appendChild($styleElement); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Returns the existing or creates a new head element in $document. |
|
438 | - * |
|
439 | - * @param DOMDocument $document |
|
440 | - * @return DOMNode the head element |
|
441 | - */ |
|
442 | - private function getOrCreateHeadElement(DOMDocument $document) { |
|
443 | - $head = $document->getElementsByTagName('head')->item(0); |
|
444 | - |
|
445 | - if ($head === null) { |
|
446 | - $head = $document->createElement('head'); |
|
447 | - $html = $document->getElementsByTagName('html')->item(0); |
|
448 | - $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
449 | - } |
|
450 | - |
|
451 | - return $head; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Splits input CSS code to an array where: |
|
456 | - * |
|
457 | - * - key "css" will be contains clean CSS code. |
|
458 | - * - key "media" will be contains all valuable media queries. |
|
459 | - * |
|
460 | - * Example: |
|
461 | - * |
|
462 | - * The CSS code. |
|
463 | - * |
|
464 | - * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
465 | - * |
|
466 | - * will be parsed into the following array: |
|
467 | - * |
|
468 | - * "css" => "h1 { color:red; }" |
|
469 | - * "media" => "@media { h1 {}}" |
|
470 | - * |
|
471 | - * @param string $css |
|
472 | - * @return array |
|
473 | - */ |
|
474 | - private function splitCssAndMediaQuery($css) { |
|
475 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
476 | - |
|
477 | - // filter the CSS |
|
478 | - $search = array( |
|
479 | - // get rid of css comment code |
|
480 | - '/\\/\\*.*\\*\\//sU', |
|
481 | - // strip out any import directives |
|
482 | - '/^\\s*@import\\s[^;]+;/misU', |
|
483 | - // strip remains media enclosures |
|
484 | - '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
485 | - ); |
|
486 | - |
|
487 | - $replace = array( |
|
488 | - '', |
|
489 | - '', |
|
490 | - '', |
|
491 | - ); |
|
492 | - |
|
493 | - // clean CSS before output |
|
494 | - $css = preg_replace($search, $replace, $css); |
|
495 | - |
|
496 | - return array('css' => $css, 'media' => self::$_media); |
|
497 | - } |
|
361 | + public function strtolower(array $m) { |
|
362 | + return strtolower($m[0]); |
|
363 | + } |
|
364 | + |
|
365 | + |
|
366 | + /** |
|
367 | + * This method merges old or existing name/value array with new name/value array. |
|
368 | + * and then generates a string of the combined style suitable for placing inline. |
|
369 | + * This becomes the single point for CSS string generation allowing for consistent. |
|
370 | + * CSS output no matter where the CSS originally came from. |
|
371 | + * @param array $oldStyles |
|
372 | + * @param array $newStyles |
|
373 | + * @return string |
|
374 | + */ |
|
375 | + private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
376 | + $combinedStyles = array_merge($oldStyles, $newStyles); |
|
377 | + $style = ''; |
|
378 | + foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
379 | + $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
380 | + } |
|
381 | + return trim($style); |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * Copies the media part from CSS array parts to $xmlDocument. |
|
387 | + * |
|
388 | + * @param array $cssParts |
|
389 | + * @param DOMDocument $xmlDocument |
|
390 | + */ |
|
391 | + public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
392 | + if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
393 | + $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
394 | + } |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Returns CSS content. |
|
399 | + * |
|
400 | + * @param DOMXPath $xpath |
|
401 | + * @return string |
|
402 | + */ |
|
403 | + private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
404 | + $styleNodes = $xpath->query('//style'); |
|
405 | + |
|
406 | + if ($styleNodes === false) { |
|
407 | + return ''; |
|
408 | + } |
|
409 | + |
|
410 | + $css = ''; |
|
411 | + /** @var $styleNode DOMNode */ |
|
412 | + foreach ($styleNodes as $styleNode) { |
|
413 | + $css .= "\n\n" . $styleNode->nodeValue; |
|
414 | + $styleNode->parentNode->removeChild($styleNode); |
|
415 | + } |
|
416 | + |
|
417 | + return $css; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Adds a style element with $css to $document. |
|
422 | + * |
|
423 | + * @param DOMDocument $document |
|
424 | + * @param string $css |
|
425 | + */ |
|
426 | + private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
427 | + $styleElement = $document->createElement('style', $css); |
|
428 | + $styleAttribute = $document->createAttribute('type'); |
|
429 | + $styleAttribute->value = 'text/css'; |
|
430 | + $styleElement->appendChild($styleAttribute); |
|
431 | + |
|
432 | + $head = $this->getOrCreateHeadElement($document); |
|
433 | + $head->appendChild($styleElement); |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Returns the existing or creates a new head element in $document. |
|
438 | + * |
|
439 | + * @param DOMDocument $document |
|
440 | + * @return DOMNode the head element |
|
441 | + */ |
|
442 | + private function getOrCreateHeadElement(DOMDocument $document) { |
|
443 | + $head = $document->getElementsByTagName('head')->item(0); |
|
444 | + |
|
445 | + if ($head === null) { |
|
446 | + $head = $document->createElement('head'); |
|
447 | + $html = $document->getElementsByTagName('html')->item(0); |
|
448 | + $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
449 | + } |
|
450 | + |
|
451 | + return $head; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Splits input CSS code to an array where: |
|
456 | + * |
|
457 | + * - key "css" will be contains clean CSS code. |
|
458 | + * - key "media" will be contains all valuable media queries. |
|
459 | + * |
|
460 | + * Example: |
|
461 | + * |
|
462 | + * The CSS code. |
|
463 | + * |
|
464 | + * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
465 | + * |
|
466 | + * will be parsed into the following array: |
|
467 | + * |
|
468 | + * "css" => "h1 { color:red; }" |
|
469 | + * "media" => "@media { h1 {}}" |
|
470 | + * |
|
471 | + * @param string $css |
|
472 | + * @return array |
|
473 | + */ |
|
474 | + private function splitCssAndMediaQuery($css) { |
|
475 | + $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
476 | + |
|
477 | + // filter the CSS |
|
478 | + $search = array( |
|
479 | + // get rid of css comment code |
|
480 | + '/\\/\\*.*\\*\\//sU', |
|
481 | + // strip out any import directives |
|
482 | + '/^\\s*@import\\s[^;]+;/misU', |
|
483 | + // strip remains media enclosures |
|
484 | + '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
485 | + ); |
|
486 | + |
|
487 | + $replace = array( |
|
488 | + '', |
|
489 | + '', |
|
490 | + '', |
|
491 | + ); |
|
492 | + |
|
493 | + // clean CSS before output |
|
494 | + $css = preg_replace($search, $replace, $css); |
|
495 | + |
|
496 | + return array('css' => $css, 'media' => self::$_media); |
|
497 | + } |
|
498 | 498 | |
499 | 499 | /** |
500 | 500 | * Media concat. |
@@ -503,306 +503,306 @@ discard block |
||
503 | 503 | * |
504 | 504 | * @param array $matches Matches result array. |
505 | 505 | */ |
506 | - private function _media_concat( $matches ) { |
|
507 | - self::$_media .= $matches[0]; |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Creates a DOMDocument instance with the current HTML. |
|
512 | - * |
|
513 | - * @return DOMDocument |
|
514 | - */ |
|
515 | - private function createXmlDocument() { |
|
516 | - $xmlDocument = new DOMDocument; |
|
517 | - $xmlDocument->encoding = self::ENCODING; |
|
518 | - $xmlDocument->strictErrorChecking = false; |
|
519 | - $xmlDocument->formatOutput = true; |
|
520 | - $libXmlState = libxml_use_internal_errors(true); |
|
521 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
522 | - /** @scrutinizer ignore-unhandled */ @$xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
523 | - libxml_clear_errors(); |
|
524 | - libxml_use_internal_errors($libXmlState); |
|
525 | - $xmlDocument->normalizeDocument(); |
|
526 | - |
|
527 | - return $xmlDocument; |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
532 | - * |
|
533 | - * @return string the unified HTML |
|
534 | - * |
|
535 | - * @throws BadMethodCallException |
|
536 | - */ |
|
537 | - private function getUnifiedHtml() { |
|
538 | - if (!empty($this->unprocessableHtmlTags)) { |
|
539 | - $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
540 | - $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
541 | - } else { |
|
542 | - $bodyWithoutUnprocessableTags = $this->html; |
|
543 | - } |
|
544 | - |
|
545 | - // Deprecated since PHP 8.2 |
|
546 | - if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
547 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
548 | - } else { |
|
549 | - return mb_encode_numericentity( $bodyWithoutUnprocessableTags, [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
550 | - //return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
551 | - } |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * @param array $a |
|
556 | - * @param array $b |
|
557 | - * |
|
558 | - * @return integer |
|
559 | - */ |
|
560 | - private function sortBySelectorPrecedence(array $a, array $b) { |
|
561 | - $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
562 | - $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
563 | - |
|
564 | - // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
565 | - // selectors with greater precedence get sorted last. |
|
566 | - // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
567 | - // arithmetic operators. |
|
568 | - // @see http://forge.typo3.org/issues/55605 |
|
569 | - $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
570 | - $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
571 | - return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * @param string $selector |
|
576 | - * |
|
577 | - * @return integer |
|
578 | - */ |
|
579 | - private function getCssSelectorPrecedence($selector) { |
|
580 | - $selectorKey = md5($selector); |
|
581 | - if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
582 | - $precedence = 0; |
|
583 | - $value = 100; |
|
584 | - // ids: worth 100, classes: worth 10, elements: worth 1 |
|
585 | - $search = array('\\#','\\.',''); |
|
586 | - |
|
587 | - foreach ($search as $s) { |
|
588 | - if (trim($selector == '')) { |
|
589 | - break; |
|
590 | - } |
|
591 | - $number = 0; |
|
592 | - $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
593 | - $precedence += ($value * $number); |
|
594 | - $value /= 10; |
|
595 | - } |
|
596 | - $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
597 | - } |
|
598 | - |
|
599 | - return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
604 | - * |
|
605 | - * @see http://plasmasturm.org/log/444/ |
|
606 | - * |
|
607 | - * @param string $paramCssSelector |
|
608 | - * |
|
609 | - * @return string |
|
610 | - */ |
|
611 | - private function translateCssToXpath($paramCssSelector) { |
|
612 | - $cssSelector = ' ' . $paramCssSelector . ' '; |
|
613 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
614 | - $cssSelector = trim($cssSelector); |
|
615 | - $xpathKey = md5($cssSelector); |
|
616 | - if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
617 | - // returns an Xpath selector |
|
618 | - $search = array( |
|
619 | - // Matches any element that is a child of parent. |
|
620 | - '/\\s+>\\s+/', |
|
621 | - // Matches any element that is an adjacent sibling. |
|
622 | - '/\\s+\\+\\s+/', |
|
623 | - // Matches any element that is a descendant of an parent element element. |
|
624 | - '/\\s+/', |
|
625 | - // first-child pseudo-selector |
|
626 | - '/([^\\/]+):first-child/i', |
|
627 | - // last-child pseudo-selector |
|
628 | - '/([^\\/]+):last-child/i', |
|
629 | - // Matches attribute only selector |
|
630 | - '/^\\[(\\w+)\\]/', |
|
631 | - // Matches element with attribute |
|
632 | - '/(\\w)\\[(\\w+)\\]/', |
|
633 | - // Matches element with EXACT attribute |
|
634 | - '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
635 | - ); |
|
636 | - $replace = array( |
|
637 | - '/', |
|
638 | - '/following-sibling::*[1]/self::', |
|
639 | - '//', |
|
640 | - '*[1]/self::\\1', |
|
641 | - '*[last()]/self::\\1', |
|
642 | - '*[@\\1]', |
|
643 | - '\\1[@\\2]', |
|
644 | - '\\1[@\\2="\\3"]', |
|
645 | - ); |
|
646 | - |
|
647 | - $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
648 | - |
|
649 | - $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
650 | - $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
651 | - |
|
652 | - // Advanced selectors are going to require a bit more advanced emogrification. |
|
653 | - // When we required PHP 5.3, we could do this with closures. |
|
654 | - $cssSelector = preg_replace_callback( |
|
655 | - '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
656 | - array($this, 'translateNthChild'), $cssSelector |
|
657 | - ); |
|
658 | - $cssSelector = preg_replace_callback( |
|
659 | - '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
660 | - array($this, 'translateNthOfType'), $cssSelector |
|
661 | - ); |
|
662 | - |
|
663 | - $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
664 | - } |
|
665 | - return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
666 | - } |
|
667 | - |
|
668 | - /** |
|
669 | - * @param array $match |
|
670 | - * |
|
671 | - * @return string |
|
672 | - */ |
|
673 | - private function matchIdAttributes(array $match) { |
|
674 | - return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
675 | - } |
|
676 | - |
|
677 | - /** |
|
678 | - * @param array $match |
|
679 | - * |
|
680 | - * @return string |
|
681 | - */ |
|
682 | - private function matchClassAttributes(array $match) { |
|
683 | - return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
684 | - implode( |
|
685 | - '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
686 | - explode('.', substr($match[2], 1)) |
|
687 | - ) . '"," "))]'; |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * @param array $match |
|
692 | - * |
|
693 | - * @return string |
|
694 | - */ |
|
695 | - private function translateNthChild(array $match) { |
|
696 | - $result = $this->parseNth($match); |
|
697 | - |
|
698 | - if (isset($result[self::MULTIPLIER])) { |
|
699 | - if ($result[self::MULTIPLIER] < 0) { |
|
700 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | - return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
702 | - } else { |
|
703 | - return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
704 | - } |
|
705 | - } else { |
|
706 | - return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
707 | - } |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * @param array $match |
|
712 | - * |
|
713 | - * @return string |
|
714 | - */ |
|
715 | - private function translateNthOfType(array $match) { |
|
716 | - $result = $this->parseNth($match); |
|
717 | - |
|
718 | - if (isset($result[self::MULTIPLIER])) { |
|
719 | - if ($result[self::MULTIPLIER] < 0) { |
|
720 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
721 | - return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
722 | - } else { |
|
723 | - return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
724 | - } |
|
725 | - } else { |
|
726 | - return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
727 | - } |
|
728 | - } |
|
729 | - |
|
730 | - /** |
|
731 | - * @param array $match |
|
732 | - * |
|
733 | - * @return array |
|
734 | - */ |
|
735 | - private function parseNth(array $match) { |
|
736 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
737 | - $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
738 | - return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
739 | - } elseif (stripos($match[2], 'n') === false) { |
|
740 | - // if there is a multiplier |
|
741 | - $index = intval(str_replace(' ', '', $match[2])); |
|
742 | - return array(self::INDEX => $index); |
|
743 | - } else { |
|
744 | - if (isset($match[3])) { |
|
745 | - $multipleTerm = str_replace($match[3], '', $match[2]); |
|
746 | - $index = intval(str_replace(' ', '', $match[3])); |
|
747 | - } else { |
|
748 | - $multipleTerm = $match[2]; |
|
749 | - $index = 0; |
|
750 | - } |
|
751 | - |
|
752 | - $multiplier = str_ireplace('n', '', $multipleTerm); |
|
753 | - |
|
754 | - if (!strlen($multiplier)) { |
|
755 | - $multiplier = 1; |
|
756 | - } elseif ($multiplier == 0) { |
|
757 | - return array(self::INDEX => $index); |
|
758 | - } else { |
|
759 | - $multiplier = intval($multiplier); |
|
760 | - } |
|
761 | - |
|
762 | - while ($index < 0) { |
|
763 | - $index += abs($multiplier); |
|
764 | - } |
|
765 | - |
|
766 | - return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
767 | - } |
|
768 | - } |
|
769 | - |
|
770 | - /** |
|
771 | - * Parses a CSS declaration block into property name/value pairs. |
|
772 | - * |
|
773 | - * Example: |
|
774 | - * |
|
775 | - * The declaration block. |
|
776 | - * |
|
777 | - * "color: #000; font-weight: bold;". |
|
778 | - * |
|
779 | - * will be parsed into the following array: |
|
780 | - * |
|
781 | - * "color" => "#000" |
|
782 | - * "font-weight" => "bold" |
|
783 | - * |
|
784 | - * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
785 | - * |
|
786 | - * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
787 | - */ |
|
788 | - private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
789 | - if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
790 | - return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
791 | - } |
|
792 | - |
|
793 | - $properties = array(); |
|
794 | - $declarations = explode(';', $cssDeclarationBlock); |
|
795 | - foreach ($declarations as $declaration) { |
|
796 | - $matches = array(); |
|
797 | - if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
798 | - continue; |
|
799 | - } |
|
800 | - $propertyName = strtolower($matches[1]); |
|
801 | - $propertyValue = $matches[2]; |
|
802 | - $properties[$propertyName] = $propertyValue; |
|
803 | - } |
|
804 | - $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
805 | - |
|
806 | - return $properties; |
|
807 | - } |
|
506 | + private function _media_concat( $matches ) { |
|
507 | + self::$_media .= $matches[0]; |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Creates a DOMDocument instance with the current HTML. |
|
512 | + * |
|
513 | + * @return DOMDocument |
|
514 | + */ |
|
515 | + private function createXmlDocument() { |
|
516 | + $xmlDocument = new DOMDocument; |
|
517 | + $xmlDocument->encoding = self::ENCODING; |
|
518 | + $xmlDocument->strictErrorChecking = false; |
|
519 | + $xmlDocument->formatOutput = true; |
|
520 | + $libXmlState = libxml_use_internal_errors(true); |
|
521 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
522 | + /** @scrutinizer ignore-unhandled */ @$xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
523 | + libxml_clear_errors(); |
|
524 | + libxml_use_internal_errors($libXmlState); |
|
525 | + $xmlDocument->normalizeDocument(); |
|
526 | + |
|
527 | + return $xmlDocument; |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
532 | + * |
|
533 | + * @return string the unified HTML |
|
534 | + * |
|
535 | + * @throws BadMethodCallException |
|
536 | + */ |
|
537 | + private function getUnifiedHtml() { |
|
538 | + if (!empty($this->unprocessableHtmlTags)) { |
|
539 | + $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
540 | + $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
541 | + } else { |
|
542 | + $bodyWithoutUnprocessableTags = $this->html; |
|
543 | + } |
|
544 | + |
|
545 | + // Deprecated since PHP 8.2 |
|
546 | + if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
547 | + return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
548 | + } else { |
|
549 | + return mb_encode_numericentity( $bodyWithoutUnprocessableTags, [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
550 | + //return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
551 | + } |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * @param array $a |
|
556 | + * @param array $b |
|
557 | + * |
|
558 | + * @return integer |
|
559 | + */ |
|
560 | + private function sortBySelectorPrecedence(array $a, array $b) { |
|
561 | + $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
562 | + $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
563 | + |
|
564 | + // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
565 | + // selectors with greater precedence get sorted last. |
|
566 | + // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
567 | + // arithmetic operators. |
|
568 | + // @see http://forge.typo3.org/issues/55605 |
|
569 | + $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
570 | + $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
571 | + return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * @param string $selector |
|
576 | + * |
|
577 | + * @return integer |
|
578 | + */ |
|
579 | + private function getCssSelectorPrecedence($selector) { |
|
580 | + $selectorKey = md5($selector); |
|
581 | + if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
582 | + $precedence = 0; |
|
583 | + $value = 100; |
|
584 | + // ids: worth 100, classes: worth 10, elements: worth 1 |
|
585 | + $search = array('\\#','\\.',''); |
|
586 | + |
|
587 | + foreach ($search as $s) { |
|
588 | + if (trim($selector == '')) { |
|
589 | + break; |
|
590 | + } |
|
591 | + $number = 0; |
|
592 | + $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
593 | + $precedence += ($value * $number); |
|
594 | + $value /= 10; |
|
595 | + } |
|
596 | + $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
597 | + } |
|
598 | + |
|
599 | + return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
604 | + * |
|
605 | + * @see http://plasmasturm.org/log/444/ |
|
606 | + * |
|
607 | + * @param string $paramCssSelector |
|
608 | + * |
|
609 | + * @return string |
|
610 | + */ |
|
611 | + private function translateCssToXpath($paramCssSelector) { |
|
612 | + $cssSelector = ' ' . $paramCssSelector . ' '; |
|
613 | + $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
614 | + $cssSelector = trim($cssSelector); |
|
615 | + $xpathKey = md5($cssSelector); |
|
616 | + if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
617 | + // returns an Xpath selector |
|
618 | + $search = array( |
|
619 | + // Matches any element that is a child of parent. |
|
620 | + '/\\s+>\\s+/', |
|
621 | + // Matches any element that is an adjacent sibling. |
|
622 | + '/\\s+\\+\\s+/', |
|
623 | + // Matches any element that is a descendant of an parent element element. |
|
624 | + '/\\s+/', |
|
625 | + // first-child pseudo-selector |
|
626 | + '/([^\\/]+):first-child/i', |
|
627 | + // last-child pseudo-selector |
|
628 | + '/([^\\/]+):last-child/i', |
|
629 | + // Matches attribute only selector |
|
630 | + '/^\\[(\\w+)\\]/', |
|
631 | + // Matches element with attribute |
|
632 | + '/(\\w)\\[(\\w+)\\]/', |
|
633 | + // Matches element with EXACT attribute |
|
634 | + '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
635 | + ); |
|
636 | + $replace = array( |
|
637 | + '/', |
|
638 | + '/following-sibling::*[1]/self::', |
|
639 | + '//', |
|
640 | + '*[1]/self::\\1', |
|
641 | + '*[last()]/self::\\1', |
|
642 | + '*[@\\1]', |
|
643 | + '\\1[@\\2]', |
|
644 | + '\\1[@\\2="\\3"]', |
|
645 | + ); |
|
646 | + |
|
647 | + $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
648 | + |
|
649 | + $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
650 | + $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
651 | + |
|
652 | + // Advanced selectors are going to require a bit more advanced emogrification. |
|
653 | + // When we required PHP 5.3, we could do this with closures. |
|
654 | + $cssSelector = preg_replace_callback( |
|
655 | + '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
656 | + array($this, 'translateNthChild'), $cssSelector |
|
657 | + ); |
|
658 | + $cssSelector = preg_replace_callback( |
|
659 | + '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
660 | + array($this, 'translateNthOfType'), $cssSelector |
|
661 | + ); |
|
662 | + |
|
663 | + $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
664 | + } |
|
665 | + return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
666 | + } |
|
667 | + |
|
668 | + /** |
|
669 | + * @param array $match |
|
670 | + * |
|
671 | + * @return string |
|
672 | + */ |
|
673 | + private function matchIdAttributes(array $match) { |
|
674 | + return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
675 | + } |
|
676 | + |
|
677 | + /** |
|
678 | + * @param array $match |
|
679 | + * |
|
680 | + * @return string |
|
681 | + */ |
|
682 | + private function matchClassAttributes(array $match) { |
|
683 | + return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
684 | + implode( |
|
685 | + '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
686 | + explode('.', substr($match[2], 1)) |
|
687 | + ) . '"," "))]'; |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * @param array $match |
|
692 | + * |
|
693 | + * @return string |
|
694 | + */ |
|
695 | + private function translateNthChild(array $match) { |
|
696 | + $result = $this->parseNth($match); |
|
697 | + |
|
698 | + if (isset($result[self::MULTIPLIER])) { |
|
699 | + if ($result[self::MULTIPLIER] < 0) { |
|
700 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | + return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
702 | + } else { |
|
703 | + return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
704 | + } |
|
705 | + } else { |
|
706 | + return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
707 | + } |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * @param array $match |
|
712 | + * |
|
713 | + * @return string |
|
714 | + */ |
|
715 | + private function translateNthOfType(array $match) { |
|
716 | + $result = $this->parseNth($match); |
|
717 | + |
|
718 | + if (isset($result[self::MULTIPLIER])) { |
|
719 | + if ($result[self::MULTIPLIER] < 0) { |
|
720 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
721 | + return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
722 | + } else { |
|
723 | + return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
724 | + } |
|
725 | + } else { |
|
726 | + return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
727 | + } |
|
728 | + } |
|
729 | + |
|
730 | + /** |
|
731 | + * @param array $match |
|
732 | + * |
|
733 | + * @return array |
|
734 | + */ |
|
735 | + private function parseNth(array $match) { |
|
736 | + if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
737 | + $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
738 | + return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
739 | + } elseif (stripos($match[2], 'n') === false) { |
|
740 | + // if there is a multiplier |
|
741 | + $index = intval(str_replace(' ', '', $match[2])); |
|
742 | + return array(self::INDEX => $index); |
|
743 | + } else { |
|
744 | + if (isset($match[3])) { |
|
745 | + $multipleTerm = str_replace($match[3], '', $match[2]); |
|
746 | + $index = intval(str_replace(' ', '', $match[3])); |
|
747 | + } else { |
|
748 | + $multipleTerm = $match[2]; |
|
749 | + $index = 0; |
|
750 | + } |
|
751 | + |
|
752 | + $multiplier = str_ireplace('n', '', $multipleTerm); |
|
753 | + |
|
754 | + if (!strlen($multiplier)) { |
|
755 | + $multiplier = 1; |
|
756 | + } elseif ($multiplier == 0) { |
|
757 | + return array(self::INDEX => $index); |
|
758 | + } else { |
|
759 | + $multiplier = intval($multiplier); |
|
760 | + } |
|
761 | + |
|
762 | + while ($index < 0) { |
|
763 | + $index += abs($multiplier); |
|
764 | + } |
|
765 | + |
|
766 | + return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
767 | + } |
|
768 | + } |
|
769 | + |
|
770 | + /** |
|
771 | + * Parses a CSS declaration block into property name/value pairs. |
|
772 | + * |
|
773 | + * Example: |
|
774 | + * |
|
775 | + * The declaration block. |
|
776 | + * |
|
777 | + * "color: #000; font-weight: bold;". |
|
778 | + * |
|
779 | + * will be parsed into the following array: |
|
780 | + * |
|
781 | + * "color" => "#000" |
|
782 | + * "font-weight" => "bold" |
|
783 | + * |
|
784 | + * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
785 | + * |
|
786 | + * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
787 | + */ |
|
788 | + private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
789 | + if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
790 | + return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
791 | + } |
|
792 | + |
|
793 | + $properties = array(); |
|
794 | + $declarations = explode(';', $cssDeclarationBlock); |
|
795 | + foreach ($declarations as $declaration) { |
|
796 | + $matches = array(); |
|
797 | + if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
798 | + continue; |
|
799 | + } |
|
800 | + $propertyName = strtolower($matches[1]); |
|
801 | + $propertyValue = $matches[2]; |
|
802 | + $properties[$propertyName] = $propertyValue; |
|
803 | + } |
|
804 | + $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
805 | + |
|
806 | + return $properties; |
|
807 | + } |
|
808 | 808 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | if ($nodesWithStyleAttributes !== false) { |
239 | 239 | /** @var $nodeWithStyleAttribute DOMNode */ |
240 | 240 | foreach ($nodesWithStyleAttributes as $node) { |
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
241 | + $normalizedOriginalStyle = preg_replace_callback('/[A-z\\-]+(?=\\:)/S', array($this, 'strtolower'), $node->getAttribute('style')); |
|
242 | 242 | |
243 | 243 | // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
244 | 244 | $nodePath = $node->getNodePath(); |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | } |
290 | 290 | |
291 | 291 | // now sort the selectors by precedence |
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
292 | + usort($allSelectors, array($this, 'sortBySelectorPrecedence')); |
|
293 | 293 | |
294 | 294 | $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
295 | 295 | } |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | if ($nodesWithStyleDisplayNone->length > 0) { |
330 | 330 | /** @var $node \DOMNode */ |
331 | 331 | foreach ($nodesWithStyleDisplayNone as $node) { |
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode, 'removeChild'))) { |
|
333 | 333 | $node->parentNode->removeChild($node); |
334 | 334 | } |
335 | 335 | } |
@@ -339,10 +339,10 @@ discard block |
||
339 | 339 | |
340 | 340 | if ($this->preserveEncoding) { |
341 | 341 | // Deprecated since PHP 8.2 |
342 | - if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
343 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
342 | + if (version_compare(PHP_VERSION, '8.2', '<') && function_exists('mb_convert_encoding')) { |
|
343 | + return mb_convert_encoding($xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES'); |
|
344 | 344 | } else { |
345 | - return mb_encode_numericentity( $xmlDocument->saveHTML(), [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
345 | + return mb_encode_numericentity($xmlDocument->saveHTML(), [0x80, 0x10FFFF, 0, ~0], self::ENCODING); |
|
346 | 346 | //return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
347 | 347 | } |
348 | 348 | } else { |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | * @return array |
473 | 473 | */ |
474 | 474 | private function splitCssAndMediaQuery($css) { |
475 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
475 | + $css = preg_replace_callback('#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array($this, '_media_concat'), $css); |
|
476 | 476 | |
477 | 477 | // filter the CSS |
478 | 478 | $search = array( |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | * |
504 | 504 | * @param array $matches Matches result array. |
505 | 505 | */ |
506 | - private function _media_concat( $matches ) { |
|
506 | + private function _media_concat($matches) { |
|
507 | 507 | self::$_media .= $matches[0]; |
508 | 508 | } |
509 | 509 | |
@@ -543,10 +543,10 @@ discard block |
||
543 | 543 | } |
544 | 544 | |
545 | 545 | // Deprecated since PHP 8.2 |
546 | - if ( version_compare( PHP_VERSION, '8.2', '<' ) && function_exists( 'mb_convert_encoding' ) ) { |
|
547 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
546 | + if (version_compare(PHP_VERSION, '8.2', '<') && function_exists('mb_convert_encoding')) { |
|
547 | + return mb_convert_encoding($bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING); |
|
548 | 548 | } else { |
549 | - return mb_encode_numericentity( $bodyWithoutUnprocessableTags, [0x80, 0x10FFFF, 0, ~0], self::ENCODING ); |
|
549 | + return mb_encode_numericentity($bodyWithoutUnprocessableTags, [0x80, 0x10FFFF, 0, ~0], self::ENCODING); |
|
550 | 550 | //return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
551 | 551 | } |
552 | 552 | } |
@@ -582,7 +582,7 @@ discard block |
||
582 | 582 | $precedence = 0; |
583 | 583 | $value = 100; |
584 | 584 | // ids: worth 100, classes: worth 10, elements: worth 1 |
585 | - $search = array('\\#','\\.',''); |
|
585 | + $search = array('\\#', '\\.', ''); |
|
586 | 586 | |
587 | 587 | foreach ($search as $s) { |
588 | 588 | if (trim($selector == '')) { |
@@ -610,7 +610,7 @@ discard block |
||
610 | 610 | */ |
611 | 611 | private function translateCssToXpath($paramCssSelector) { |
612 | 612 | $cssSelector = ' ' . $paramCssSelector . ' '; |
613 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
613 | + $cssSelector = preg_replace_callback('/\s+\w+\s+/', array($this, 'strtolower'), $cssSelector); |
|
614 | 614 | $cssSelector = trim($cssSelector); |
615 | 615 | $xpathKey = md5($cssSelector); |
616 | 616 | if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
@@ -733,7 +733,7 @@ discard block |
||
733 | 733 | * @return array |
734 | 734 | */ |
735 | 735 | private function parseNth(array $match) { |
736 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
736 | + if (in_array(strtolower($match[2]), array('even', 'odd'))) { |
|
737 | 737 | $index = strtolower($match[2]) == 'even' ? 0 : 1; |
738 | 738 | return array(self::MULTIPLIER => 2, self::INDEX => $index); |
739 | 739 | } elseif (stripos($match[2], 'n') === false) { |