@@ -21,145 +21,145 @@ |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Deactivator_Feedback { |
23 | 23 | |
24 | - /** |
|
25 | - * A {@link Wordlift_Log_Service} instance. |
|
26 | - * |
|
27 | - * @since 3.19.0 |
|
28 | - * @access private |
|
29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
30 | - */ |
|
31 | - private $log; |
|
32 | - |
|
33 | - /** |
|
34 | - * Wordlift_Deactivator_Feedback constructor. |
|
35 | - * |
|
36 | - * @since 3.19.0 |
|
37 | - */ |
|
38 | - public function __construct() { |
|
39 | - |
|
40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
41 | - |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Checks whether we have permissions to show the popup. |
|
46 | - * |
|
47 | - * @return bool `true` if we have permissions, false otherwise. |
|
48 | - * @version 3.19.0 |
|
49 | - */ |
|
50 | - private function has_permission_to_show_popup() { |
|
51 | - // Get the current page. |
|
52 | - global $pagenow; |
|
53 | - |
|
54 | - // Bail if the user doesn't have permissions |
|
55 | - // or if it's not the plugins page. |
|
56 | - if ( 'plugins.php' !== $pagenow ) { |
|
57 | - return false; |
|
58 | - } |
|
59 | - |
|
60 | - // Get the user preferences. We shouldn't show the feedback popup |
|
61 | - // if we don't have permissions for that. |
|
62 | - $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
63 | - |
|
64 | - // Bail. We don't have preferences to show the popup. |
|
65 | - if ( 'yes' !== $user_preferences ) { |
|
66 | - return false; |
|
67 | - } |
|
68 | - |
|
69 | - return true; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Render the feedback popup in the footer. |
|
74 | - * |
|
75 | - * @return void |
|
76 | - * @version 3.19.0 |
|
77 | - */ |
|
78 | - public function render_feedback_popup() { |
|
79 | - // Bail if we don't have permissions to show the popup. |
|
80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
81 | - return; |
|
82 | - } |
|
83 | - // Include the partial. |
|
84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Enqueue required popup scripts and styles. |
|
89 | - * |
|
90 | - * @return void |
|
91 | - * @version 3.19.0 |
|
92 | - */ |
|
93 | - public function enqueue_popup_scripts() { |
|
94 | - // Bail if we don't have permissions to show the popup. |
|
95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
96 | - return; |
|
97 | - } |
|
98 | - |
|
99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Handle the deactivation ajax call |
|
105 | - * and perform a request to external server. |
|
106 | - * |
|
107 | - * @return void |
|
108 | - * @version 3.19.0 |
|
109 | - */ |
|
110 | - public function wl_deactivation_feedback() { |
|
111 | - // Bail if the nonce is not valid. |
|
112 | - if ( |
|
113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
115 | - ) { |
|
116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
117 | - } |
|
118 | - |
|
119 | - // We allow user to deactivate without providing a reason |
|
120 | - // so bail and send success response. |
|
121 | - if ( empty( $_POST['code'] ) ) { |
|
122 | - wp_send_json_success(); |
|
123 | - } |
|
124 | - |
|
125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
126 | - |
|
127 | - // Prepare the options. |
|
128 | - $options = array( |
|
129 | - // The deactivation reason. |
|
130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
131 | - // Additional information if provided. |
|
132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
133 | - // The website url. |
|
134 | - 'url' => get_bloginfo( 'url' ), |
|
135 | - // WP version. |
|
136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
137 | - // WL version. |
|
138 | - 'wordliftVersion' => $plugin_data['Version'], |
|
139 | - // The admin email. |
|
140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
141 | - ); |
|
142 | - |
|
143 | - $response = wp_remote_post( |
|
144 | - Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
145 | - array( |
|
146 | - 'method' => 'POST', |
|
147 | - 'body' => wp_json_encode( $options ), |
|
148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
149 | - ) |
|
150 | - ); |
|
151 | - |
|
152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
154 | - |
|
155 | - // Add message to the error log if the response code is not 200. |
|
156 | - if ( 201 !== $code ) { |
|
157 | - // Write the error in the logs. |
|
158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
159 | - } |
|
160 | - |
|
161 | - // We should send success message even when the feedback is not |
|
162 | - // send, because otherwise the plugin cannot be deactivated. |
|
163 | - wp_send_json_success(); |
|
164 | - } |
|
24 | + /** |
|
25 | + * A {@link Wordlift_Log_Service} instance. |
|
26 | + * |
|
27 | + * @since 3.19.0 |
|
28 | + * @access private |
|
29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
30 | + */ |
|
31 | + private $log; |
|
32 | + |
|
33 | + /** |
|
34 | + * Wordlift_Deactivator_Feedback constructor. |
|
35 | + * |
|
36 | + * @since 3.19.0 |
|
37 | + */ |
|
38 | + public function __construct() { |
|
39 | + |
|
40 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
41 | + |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Checks whether we have permissions to show the popup. |
|
46 | + * |
|
47 | + * @return bool `true` if we have permissions, false otherwise. |
|
48 | + * @version 3.19.0 |
|
49 | + */ |
|
50 | + private function has_permission_to_show_popup() { |
|
51 | + // Get the current page. |
|
52 | + global $pagenow; |
|
53 | + |
|
54 | + // Bail if the user doesn't have permissions |
|
55 | + // or if it's not the plugins page. |
|
56 | + if ( 'plugins.php' !== $pagenow ) { |
|
57 | + return false; |
|
58 | + } |
|
59 | + |
|
60 | + // Get the user preferences. We shouldn't show the feedback popup |
|
61 | + // if we don't have permissions for that. |
|
62 | + $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
63 | + |
|
64 | + // Bail. We don't have preferences to show the popup. |
|
65 | + if ( 'yes' !== $user_preferences ) { |
|
66 | + return false; |
|
67 | + } |
|
68 | + |
|
69 | + return true; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Render the feedback popup in the footer. |
|
74 | + * |
|
75 | + * @return void |
|
76 | + * @version 3.19.0 |
|
77 | + */ |
|
78 | + public function render_feedback_popup() { |
|
79 | + // Bail if we don't have permissions to show the popup. |
|
80 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
81 | + return; |
|
82 | + } |
|
83 | + // Include the partial. |
|
84 | + include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Enqueue required popup scripts and styles. |
|
89 | + * |
|
90 | + * @return void |
|
91 | + * @version 3.19.0 |
|
92 | + */ |
|
93 | + public function enqueue_popup_scripts() { |
|
94 | + // Bail if we don't have permissions to show the popup. |
|
95 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
96 | + return; |
|
97 | + } |
|
98 | + |
|
99 | + wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | + wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Handle the deactivation ajax call |
|
105 | + * and perform a request to external server. |
|
106 | + * |
|
107 | + * @return void |
|
108 | + * @version 3.19.0 |
|
109 | + */ |
|
110 | + public function wl_deactivation_feedback() { |
|
111 | + // Bail if the nonce is not valid. |
|
112 | + if ( |
|
113 | + empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
115 | + ) { |
|
116 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
117 | + } |
|
118 | + |
|
119 | + // We allow user to deactivate without providing a reason |
|
120 | + // so bail and send success response. |
|
121 | + if ( empty( $_POST['code'] ) ) { |
|
122 | + wp_send_json_success(); |
|
123 | + } |
|
124 | + |
|
125 | + $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
126 | + |
|
127 | + // Prepare the options. |
|
128 | + $options = array( |
|
129 | + // The deactivation reason. |
|
130 | + 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
131 | + // Additional information if provided. |
|
132 | + 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
133 | + // The website url. |
|
134 | + 'url' => get_bloginfo( 'url' ), |
|
135 | + // WP version. |
|
136 | + 'wordpressVersion' => get_bloginfo( 'version' ), |
|
137 | + // WL version. |
|
138 | + 'wordliftVersion' => $plugin_data['Version'], |
|
139 | + // The admin email. |
|
140 | + 'email' => get_bloginfo( 'admin_email' ), |
|
141 | + ); |
|
142 | + |
|
143 | + $response = wp_remote_post( |
|
144 | + Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
145 | + array( |
|
146 | + 'method' => 'POST', |
|
147 | + 'body' => wp_json_encode( $options ), |
|
148 | + 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
149 | + ) |
|
150 | + ); |
|
151 | + |
|
152 | + $code = wp_remote_retrieve_response_code( $response ); |
|
153 | + $message = wp_remote_retrieve_response_message( $response ); |
|
154 | + |
|
155 | + // Add message to the error log if the response code is not 200. |
|
156 | + if ( 201 !== $code ) { |
|
157 | + // Write the error in the logs. |
|
158 | + $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
159 | + } |
|
160 | + |
|
161 | + // We should send success message even when the feedback is not |
|
162 | + // send, because otherwise the plugin cannot be deactivated. |
|
163 | + wp_send_json_success(); |
|
164 | + } |
|
165 | 165 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public function __construct() { |
39 | 39 | |
40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
40 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Deactivator_Feedback'); |
|
41 | 41 | |
42 | 42 | } |
43 | 43 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | |
54 | 54 | // Bail if the user doesn't have permissions |
55 | 55 | // or if it's not the plugins page. |
56 | - if ( 'plugins.php' !== $pagenow ) { |
|
56 | + if ('plugins.php' !== $pagenow) { |
|
57 | 57 | return false; |
58 | 58 | } |
59 | 59 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
63 | 63 | |
64 | 64 | // Bail. We don't have preferences to show the popup. |
65 | - if ( 'yes' !== $user_preferences ) { |
|
65 | + if ('yes' !== $user_preferences) { |
|
66 | 66 | return false; |
67 | 67 | } |
68 | 68 | |
@@ -77,11 +77,11 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function render_feedback_popup() { |
79 | 79 | // Bail if we don't have permissions to show the popup. |
80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
80 | + if ( ! $this->has_permission_to_show_popup()) { |
|
81 | 81 | return; |
82 | 82 | } |
83 | 83 | // Include the partial. |
84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
84 | + include plugin_dir_path(__FILE__).'../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -92,12 +92,12 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function enqueue_popup_scripts() { |
94 | 94 | // Bail if we don't have permissions to show the popup. |
95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
95 | + if ( ! $this->has_permission_to_show_popup()) { |
|
96 | 96 | return; |
97 | 97 | } |
98 | 98 | |
99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
99 | + wp_enqueue_style('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION); |
|
100 | + wp_enqueue_script('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/js/wordlift-admin-feedback-popup.js', array('jquery'), WORDLIFT_VERSION, false); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -110,52 +110,52 @@ discard block |
||
110 | 110 | public function wl_deactivation_feedback() { |
111 | 111 | // Bail if the nonce is not valid. |
112 | 112 | if ( |
113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
113 | + empty($_POST['wl_deactivation_feedback_nonce']) || // The nonce doens't exists. |
|
114 | + ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wl_deactivation_feedback_nonce'])), 'wl_deactivation_feedback_nonce') // The nonce is invalid. |
|
115 | 115 | ) { |
116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
116 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | // We allow user to deactivate without providing a reason |
120 | 120 | // so bail and send success response. |
121 | - if ( empty( $_POST['code'] ) ) { |
|
121 | + if (empty($_POST['code'])) { |
|
122 | 122 | wp_send_json_success(); |
123 | 123 | } |
124 | 124 | |
125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
125 | + $plugin_data = get_plugin_data(plugin_dir_path(__DIR__).'wordlift.php', false, false); |
|
126 | 126 | |
127 | 127 | // Prepare the options. |
128 | 128 | $options = array( |
129 | 129 | // The deactivation reason. |
130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
130 | + 'code' => sanitize_text_field(wp_unslash((string) $_POST['code'])), |
|
131 | 131 | // Additional information if provided. |
132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
132 | + 'details' => ( ! empty($_POST['details'])) ? sanitize_text_field(wp_unslash((string) $_POST['details'])) : '', |
|
133 | 133 | // The website url. |
134 | - 'url' => get_bloginfo( 'url' ), |
|
134 | + 'url' => get_bloginfo('url'), |
|
135 | 135 | // WP version. |
136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
136 | + 'wordpressVersion' => get_bloginfo('version'), |
|
137 | 137 | // WL version. |
138 | 138 | 'wordliftVersion' => $plugin_data['Version'], |
139 | 139 | // The admin email. |
140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
140 | + 'email' => get_bloginfo('admin_email'), |
|
141 | 141 | ); |
142 | 142 | |
143 | 143 | $response = wp_remote_post( |
144 | 144 | Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
145 | 145 | array( |
146 | 146 | 'method' => 'POST', |
147 | - 'body' => wp_json_encode( $options ), |
|
148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
147 | + 'body' => wp_json_encode($options), |
|
148 | + 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), |
|
149 | 149 | ) |
150 | 150 | ); |
151 | 151 | |
152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
152 | + $code = wp_remote_retrieve_response_code($response); |
|
153 | + $message = wp_remote_retrieve_response_message($response); |
|
154 | 154 | |
155 | 155 | // Add message to the error log if the response code is not 200. |
156 | - if ( 201 !== $code ) { |
|
156 | + if (201 !== $code) { |
|
157 | 157 | // Write the error in the logs. |
158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
158 | + $this->log->error('An error occurred while requesting a feedback endpoint error_code: '.$code.' message: '.$message); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | // We should send success message even when the feedback is not |
@@ -23,107 +23,107 @@ |
||
23 | 23 | */ |
24 | 24 | class Wordlift_Loader { |
25 | 25 | |
26 | - /** |
|
27 | - * The array of actions registered with WordPress. |
|
28 | - * |
|
29 | - * @since 1.0.0 |
|
30 | - * @access protected |
|
31 | - * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
32 | - */ |
|
33 | - protected $actions; |
|
34 | - |
|
35 | - /** |
|
36 | - * The array of filters registered with WordPress. |
|
37 | - * |
|
38 | - * @since 1.0.0 |
|
39 | - * @access protected |
|
40 | - * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
41 | - */ |
|
42 | - protected $filters; |
|
43 | - |
|
44 | - /** |
|
45 | - * Initialize the collections used to maintain the actions and filters. |
|
46 | - * |
|
47 | - * @since 1.0.0 |
|
48 | - */ |
|
49 | - public function __construct() { |
|
50 | - |
|
51 | - $this->actions = array(); |
|
52 | - $this->filters = array(); |
|
53 | - |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Add a new action to the collection to be registered with WordPress. |
|
58 | - * |
|
59 | - * @since 1.0.0 |
|
60 | - * @param string $hook The name of the WordPress action that is being registered. |
|
61 | - * @param object $component A reference to the instance of the object on which the action is defined. |
|
62 | - * @param string $callback The name of the function definition on the $component. |
|
63 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
64 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
65 | - */ |
|
66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Add a new filter to the collection to be registered with WordPress. |
|
72 | - * |
|
73 | - * @since 1.0.0 |
|
74 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
75 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
76 | - * @param string $callback The name of the function definition on the $component. |
|
77 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
78 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
79 | - */ |
|
80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * A utility function that is used to register the actions and hooks into a single |
|
86 | - * collection. |
|
87 | - * |
|
88 | - * @since 1.0.0 |
|
89 | - * @access private |
|
90 | - * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
91 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
92 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
93 | - * @param string $callback The name of the function definition on the $component. |
|
94 | - * @param int $priority The priority at which the function should be fired. |
|
95 | - * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
96 | - * @return array The collection of actions and filters registered with WordPress. |
|
97 | - */ |
|
98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
99 | - |
|
100 | - $hooks[] = array( |
|
101 | - 'hook' => $hook, |
|
102 | - 'component' => $component, |
|
103 | - 'callback' => $callback, |
|
104 | - 'priority' => $priority, |
|
105 | - 'accepted_args' => $accepted_args, |
|
106 | - ); |
|
107 | - |
|
108 | - return $hooks; |
|
109 | - |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Register the filters and actions with WordPress. |
|
114 | - * |
|
115 | - * @since 1.0.0 |
|
116 | - */ |
|
117 | - public function run() { |
|
118 | - |
|
119 | - foreach ( $this->filters as $hook ) { |
|
120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
121 | - } |
|
122 | - |
|
123 | - foreach ( $this->actions as $hook ) { |
|
124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
26 | + /** |
|
27 | + * The array of actions registered with WordPress. |
|
28 | + * |
|
29 | + * @since 1.0.0 |
|
30 | + * @access protected |
|
31 | + * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
32 | + */ |
|
33 | + protected $actions; |
|
34 | + |
|
35 | + /** |
|
36 | + * The array of filters registered with WordPress. |
|
37 | + * |
|
38 | + * @since 1.0.0 |
|
39 | + * @access protected |
|
40 | + * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
41 | + */ |
|
42 | + protected $filters; |
|
43 | + |
|
44 | + /** |
|
45 | + * Initialize the collections used to maintain the actions and filters. |
|
46 | + * |
|
47 | + * @since 1.0.0 |
|
48 | + */ |
|
49 | + public function __construct() { |
|
50 | + |
|
51 | + $this->actions = array(); |
|
52 | + $this->filters = array(); |
|
53 | + |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Add a new action to the collection to be registered with WordPress. |
|
58 | + * |
|
59 | + * @since 1.0.0 |
|
60 | + * @param string $hook The name of the WordPress action that is being registered. |
|
61 | + * @param object $component A reference to the instance of the object on which the action is defined. |
|
62 | + * @param string $callback The name of the function definition on the $component. |
|
63 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
64 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
65 | + */ |
|
66 | + public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | + add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Add a new filter to the collection to be registered with WordPress. |
|
72 | + * |
|
73 | + * @since 1.0.0 |
|
74 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
75 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
76 | + * @param string $callback The name of the function definition on the $component. |
|
77 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
78 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
79 | + */ |
|
80 | + public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | + add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * A utility function that is used to register the actions and hooks into a single |
|
86 | + * collection. |
|
87 | + * |
|
88 | + * @since 1.0.0 |
|
89 | + * @access private |
|
90 | + * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
91 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
92 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
93 | + * @param string $callback The name of the function definition on the $component. |
|
94 | + * @param int $priority The priority at which the function should be fired. |
|
95 | + * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
96 | + * @return array The collection of actions and filters registered with WordPress. |
|
97 | + */ |
|
98 | + private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
99 | + |
|
100 | + $hooks[] = array( |
|
101 | + 'hook' => $hook, |
|
102 | + 'component' => $component, |
|
103 | + 'callback' => $callback, |
|
104 | + 'priority' => $priority, |
|
105 | + 'accepted_args' => $accepted_args, |
|
106 | + ); |
|
107 | + |
|
108 | + return $hooks; |
|
109 | + |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Register the filters and actions with WordPress. |
|
114 | + * |
|
115 | + * @since 1.0.0 |
|
116 | + */ |
|
117 | + public function run() { |
|
118 | + |
|
119 | + foreach ( $this->filters as $hook ) { |
|
120 | + add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
121 | + } |
|
122 | + |
|
123 | + foreach ( $this->actions as $hook ) { |
|
124 | + add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | 128 | |
129 | 129 | } |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
64 | 64 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
65 | 65 | */ |
66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
66 | + public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
67 | + add_action($hook, array($component, $callback), $priority, $accepted_args); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -77,8 +77,8 @@ discard block |
||
77 | 77 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
78 | 78 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
79 | 79 | */ |
80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
80 | + public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
81 | + add_filter($hook, array($component, $callback), $priority, $accepted_args); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @param int $accepted_args The number of arguments that should be passed to the $callback. |
96 | 96 | * @return array The collection of actions and filters registered with WordPress. |
97 | 97 | */ |
98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
98 | + private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) { |
|
99 | 99 | |
100 | 100 | $hooks[] = array( |
101 | 101 | 'hook' => $hook, |
@@ -116,12 +116,12 @@ discard block |
||
116 | 116 | */ |
117 | 117 | public function run() { |
118 | 118 | |
119 | - foreach ( $this->filters as $hook ) { |
|
120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
119 | + foreach ($this->filters as $hook) { |
|
120 | + add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
121 | 121 | } |
122 | 122 | |
123 | - foreach ( $this->actions as $hook ) { |
|
124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
123 | + foreach ($this->actions as $hook) { |
|
124 | + add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | } |
@@ -17,202 +17,202 @@ |
||
17 | 17 | */ |
18 | 18 | class Wordlift_Api_Service { |
19 | 19 | |
20 | - /** |
|
21 | - * The {@link Wordlift_Api_Service} singleton instance. |
|
22 | - * |
|
23 | - * @since 3.20.0 |
|
24 | - * @access private |
|
25 | - * @var \Wordlift_Api_Service $instance The singleton instance. |
|
26 | - */ |
|
27 | - private static $instance; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Api_Headers_Service|null |
|
31 | - */ |
|
32 | - private $headers_service; |
|
33 | - |
|
34 | - /** |
|
35 | - * Create a {@link Wordlift_Api_Service} instance. |
|
36 | - * |
|
37 | - * @since 3.20.0 |
|
38 | - */ |
|
39 | - public function __construct() { |
|
40 | - self::$instance = $this; |
|
41 | - $this->headers_service = Api_Headers_Service::get_instance(); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Get the {@link Wordlift_Api_Service} singleton instance. |
|
46 | - * |
|
47 | - * @return \Wordlift_Api_Service The {@link Wordlift_Api_Service} singleton instance. |
|
48 | - * @since 3.20.0 |
|
49 | - */ |
|
50 | - public static function get_instance() { |
|
51 | - |
|
52 | - return self::$instance; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Perform a `GET` request towards the requested path. |
|
57 | - * |
|
58 | - * @param string $path The relative path. |
|
59 | - * |
|
60 | - * @return array|WP_Error |
|
61 | - * @since 3.20.0 |
|
62 | - */ |
|
63 | - public function get( $path ) { |
|
64 | - |
|
65 | - // Prepare the target URL. |
|
66 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
67 | - |
|
68 | - // Get the response value. |
|
69 | - $response = wp_remote_get( |
|
70 | - $url, |
|
71 | - array( |
|
72 | - 'user-agent' => User_Agent::get_user_agent(), |
|
73 | - 'headers' => array( |
|
74 | - 'X-Authorization' => Wordlift_Configuration_Service::get_instance()->get_key(), |
|
75 | - ) + $this->headers_service->get_wp_headers(), |
|
76 | - /* |
|
20 | + /** |
|
21 | + * The {@link Wordlift_Api_Service} singleton instance. |
|
22 | + * |
|
23 | + * @since 3.20.0 |
|
24 | + * @access private |
|
25 | + * @var \Wordlift_Api_Service $instance The singleton instance. |
|
26 | + */ |
|
27 | + private static $instance; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Api_Headers_Service|null |
|
31 | + */ |
|
32 | + private $headers_service; |
|
33 | + |
|
34 | + /** |
|
35 | + * Create a {@link Wordlift_Api_Service} instance. |
|
36 | + * |
|
37 | + * @since 3.20.0 |
|
38 | + */ |
|
39 | + public function __construct() { |
|
40 | + self::$instance = $this; |
|
41 | + $this->headers_service = Api_Headers_Service::get_instance(); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Get the {@link Wordlift_Api_Service} singleton instance. |
|
46 | + * |
|
47 | + * @return \Wordlift_Api_Service The {@link Wordlift_Api_Service} singleton instance. |
|
48 | + * @since 3.20.0 |
|
49 | + */ |
|
50 | + public static function get_instance() { |
|
51 | + |
|
52 | + return self::$instance; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Perform a `GET` request towards the requested path. |
|
57 | + * |
|
58 | + * @param string $path The relative path. |
|
59 | + * |
|
60 | + * @return array|WP_Error |
|
61 | + * @since 3.20.0 |
|
62 | + */ |
|
63 | + public function get( $path ) { |
|
64 | + |
|
65 | + // Prepare the target URL. |
|
66 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
67 | + |
|
68 | + // Get the response value. |
|
69 | + $response = wp_remote_get( |
|
70 | + $url, |
|
71 | + array( |
|
72 | + 'user-agent' => User_Agent::get_user_agent(), |
|
73 | + 'headers' => array( |
|
74 | + 'X-Authorization' => Wordlift_Configuration_Service::get_instance()->get_key(), |
|
75 | + ) + $this->headers_service->get_wp_headers(), |
|
76 | + /* |
|
77 | 77 | * Increase the timeout from the default of 5 to 30 secs. |
78 | 78 | * |
79 | 79 | * @see https://github.com/insideout10/wordlift-plugin/issues/906 |
80 | 80 | * |
81 | 81 | * @since 3.20.1 |
82 | 82 | */ |
83 | - 'timeout' => 30, |
|
84 | - ) |
|
85 | - ); |
|
86 | - |
|
87 | - return self::get_message_or_error( $response ); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Perform a `POST` request towards the requested path. |
|
92 | - * |
|
93 | - * @param string $path The relative path. |
|
94 | - * @param array|object $body The request body (will be serialized to JSON). |
|
95 | - * |
|
96 | - * @return array|WP_Error |
|
97 | - * @since 3.20.0 |
|
98 | - */ |
|
99 | - public function post( $path, $body ) { |
|
100 | - |
|
101 | - return $this->post_custom_content_type( |
|
102 | - $path, |
|
103 | - wp_json_encode( $body ), |
|
104 | - 'application/json; ' . get_bloginfo( 'charset' ) |
|
105 | - ); |
|
106 | - } |
|
107 | - |
|
108 | - public function post_custom_content_type( $path, $body, $content_type ) { |
|
109 | - |
|
110 | - // Prepare the target URL. |
|
111 | - $url = apply_filters( 'wl_api_service_api_url_path', Wordlift_Configuration_Service::get_instance()->get_api_url() . $path ); |
|
112 | - |
|
113 | - // Give some time for the operation to complete, more than the time we give to the HTTP operation to complete. |
|
114 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
115 | - @set_time_limit( 90 ); |
|
116 | - |
|
117 | - // Get the response value. |
|
118 | - $key = Wordlift_Configuration_Service::get_instance()->get_key(); |
|
119 | - $response = wp_remote_post( |
|
120 | - $url, |
|
121 | - array( |
|
122 | - 'timeout' => 60, |
|
123 | - 'user-agent' => User_Agent::get_user_agent(), |
|
124 | - 'headers' => array( |
|
125 | - 'Content-Type' => $content_type, |
|
126 | - 'X-Authorization' => $key, |
|
127 | - 'Authorization' => "Key $key", |
|
128 | - /* |
|
83 | + 'timeout' => 30, |
|
84 | + ) |
|
85 | + ); |
|
86 | + |
|
87 | + return self::get_message_or_error( $response ); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Perform a `POST` request towards the requested path. |
|
92 | + * |
|
93 | + * @param string $path The relative path. |
|
94 | + * @param array|object $body The request body (will be serialized to JSON). |
|
95 | + * |
|
96 | + * @return array|WP_Error |
|
97 | + * @since 3.20.0 |
|
98 | + */ |
|
99 | + public function post( $path, $body ) { |
|
100 | + |
|
101 | + return $this->post_custom_content_type( |
|
102 | + $path, |
|
103 | + wp_json_encode( $body ), |
|
104 | + 'application/json; ' . get_bloginfo( 'charset' ) |
|
105 | + ); |
|
106 | + } |
|
107 | + |
|
108 | + public function post_custom_content_type( $path, $body, $content_type ) { |
|
109 | + |
|
110 | + // Prepare the target URL. |
|
111 | + $url = apply_filters( 'wl_api_service_api_url_path', Wordlift_Configuration_Service::get_instance()->get_api_url() . $path ); |
|
112 | + |
|
113 | + // Give some time for the operation to complete, more than the time we give to the HTTP operation to complete. |
|
114 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
115 | + @set_time_limit( 90 ); |
|
116 | + |
|
117 | + // Get the response value. |
|
118 | + $key = Wordlift_Configuration_Service::get_instance()->get_key(); |
|
119 | + $response = wp_remote_post( |
|
120 | + $url, |
|
121 | + array( |
|
122 | + 'timeout' => 60, |
|
123 | + 'user-agent' => User_Agent::get_user_agent(), |
|
124 | + 'headers' => array( |
|
125 | + 'Content-Type' => $content_type, |
|
126 | + 'X-Authorization' => $key, |
|
127 | + 'Authorization' => "Key $key", |
|
128 | + /* |
|
129 | 129 | * This is required to avoid CURL receiving 502 Bad Gateway errors. |
130 | 130 | * |
131 | 131 | * @see https://stackoverflow.com/questions/30601075/curl-to-google-compute-load-balancer-gets-error-502 |
132 | 132 | */ |
133 | - 'Expect' => '', |
|
134 | - ) + $this->headers_service->get_wp_headers(), |
|
135 | - 'body' => $body, |
|
136 | - ) |
|
137 | - ); |
|
138 | - |
|
139 | - return self::get_message_or_error( $response ); |
|
140 | - } |
|
141 | - |
|
142 | - public function delete( $path ) { |
|
143 | - |
|
144 | - // Prepare the target URL. |
|
145 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
146 | - |
|
147 | - // Get the response value. |
|
148 | - $response = wp_remote_request( |
|
149 | - $url, |
|
150 | - array( |
|
151 | - 'method' => 'DELETE', |
|
152 | - 'user-agent' => User_Agent::get_user_agent(), |
|
153 | - 'headers' => array( |
|
154 | - 'X-Authorization' => Wordlift_Configuration_Service::get_instance()->get_key(), |
|
155 | - ) + $this->headers_service->get_wp_headers(), |
|
156 | - ) |
|
157 | - ); |
|
158 | - |
|
159 | - return self::get_message_or_error( $response ); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Return the {@link WP_Error} in case of error or the actual reply if successful. |
|
164 | - * |
|
165 | - * @param array|WP_Error $response The response of an http call. |
|
166 | - * |
|
167 | - * @return string|object|WP_Error A {@link WP_Error} instance or the actual response content. |
|
168 | - * @since 3.20.0 |
|
169 | - */ |
|
170 | - private static function get_message_or_error( $response ) { |
|
171 | - |
|
172 | - // Result is WP_Error. |
|
173 | - if ( is_wp_error( $response ) ) { |
|
174 | - return $response; |
|
175 | - } |
|
176 | - |
|
177 | - // `code` not set or not numeric. |
|
178 | - $code = wp_remote_retrieve_response_code( $response ); |
|
179 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
180 | - $message = @wp_remote_retrieve_response_message( $response ); |
|
181 | - |
|
182 | - if ( empty( $code ) || ! is_numeric( $code ) ) { |
|
183 | - return new WP_Error( 0, $message ); |
|
184 | - } |
|
185 | - |
|
186 | - // Code not 2xx. |
|
187 | - if ( 2 !== intval( $code / 100 ) ) { |
|
188 | - return new WP_Error( $code, $message ); |
|
189 | - } |
|
190 | - |
|
191 | - // Everything's fine, return the message. |
|
192 | - return self::try_json_decode( $response ); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Try to decode the json response |
|
197 | - * |
|
198 | - * @param array $response The response array. |
|
199 | - * |
|
200 | - * @return array|mixed|object The decoded response or the original response body. |
|
201 | - * @since 3.20.0 |
|
202 | - */ |
|
203 | - private static function try_json_decode( $response ) { |
|
204 | - |
|
205 | - // Get the headers. |
|
206 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
207 | - $body = wp_remote_retrieve_body( $response ); |
|
208 | - |
|
209 | - // If it's not an `application/json` return the plain response body. |
|
210 | - if ( 0 !== strpos( strtolower( $content_type ), 'application/json' ) ) { |
|
211 | - return $body; |
|
212 | - } |
|
213 | - |
|
214 | - // Decode and return the structured result. |
|
215 | - return json_decode( $body ); |
|
216 | - } |
|
133 | + 'Expect' => '', |
|
134 | + ) + $this->headers_service->get_wp_headers(), |
|
135 | + 'body' => $body, |
|
136 | + ) |
|
137 | + ); |
|
138 | + |
|
139 | + return self::get_message_or_error( $response ); |
|
140 | + } |
|
141 | + |
|
142 | + public function delete( $path ) { |
|
143 | + |
|
144 | + // Prepare the target URL. |
|
145 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
146 | + |
|
147 | + // Get the response value. |
|
148 | + $response = wp_remote_request( |
|
149 | + $url, |
|
150 | + array( |
|
151 | + 'method' => 'DELETE', |
|
152 | + 'user-agent' => User_Agent::get_user_agent(), |
|
153 | + 'headers' => array( |
|
154 | + 'X-Authorization' => Wordlift_Configuration_Service::get_instance()->get_key(), |
|
155 | + ) + $this->headers_service->get_wp_headers(), |
|
156 | + ) |
|
157 | + ); |
|
158 | + |
|
159 | + return self::get_message_or_error( $response ); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Return the {@link WP_Error} in case of error or the actual reply if successful. |
|
164 | + * |
|
165 | + * @param array|WP_Error $response The response of an http call. |
|
166 | + * |
|
167 | + * @return string|object|WP_Error A {@link WP_Error} instance or the actual response content. |
|
168 | + * @since 3.20.0 |
|
169 | + */ |
|
170 | + private static function get_message_or_error( $response ) { |
|
171 | + |
|
172 | + // Result is WP_Error. |
|
173 | + if ( is_wp_error( $response ) ) { |
|
174 | + return $response; |
|
175 | + } |
|
176 | + |
|
177 | + // `code` not set or not numeric. |
|
178 | + $code = wp_remote_retrieve_response_code( $response ); |
|
179 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
180 | + $message = @wp_remote_retrieve_response_message( $response ); |
|
181 | + |
|
182 | + if ( empty( $code ) || ! is_numeric( $code ) ) { |
|
183 | + return new WP_Error( 0, $message ); |
|
184 | + } |
|
185 | + |
|
186 | + // Code not 2xx. |
|
187 | + if ( 2 !== intval( $code / 100 ) ) { |
|
188 | + return new WP_Error( $code, $message ); |
|
189 | + } |
|
190 | + |
|
191 | + // Everything's fine, return the message. |
|
192 | + return self::try_json_decode( $response ); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Try to decode the json response |
|
197 | + * |
|
198 | + * @param array $response The response array. |
|
199 | + * |
|
200 | + * @return array|mixed|object The decoded response or the original response body. |
|
201 | + * @since 3.20.0 |
|
202 | + */ |
|
203 | + private static function try_json_decode( $response ) { |
|
204 | + |
|
205 | + // Get the headers. |
|
206 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
207 | + $body = wp_remote_retrieve_body( $response ); |
|
208 | + |
|
209 | + // If it's not an `application/json` return the plain response body. |
|
210 | + if ( 0 !== strpos( strtolower( $content_type ), 'application/json' ) ) { |
|
211 | + return $body; |
|
212 | + } |
|
213 | + |
|
214 | + // Decode and return the structured result. |
|
215 | + return json_decode( $body ); |
|
216 | + } |
|
217 | 217 | |
218 | 218 | } |
@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | * @return array|WP_Error |
61 | 61 | * @since 3.20.0 |
62 | 62 | */ |
63 | - public function get( $path ) { |
|
63 | + public function get($path) { |
|
64 | 64 | |
65 | 65 | // Prepare the target URL. |
66 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
66 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url().$path; |
|
67 | 67 | |
68 | 68 | // Get the response value. |
69 | 69 | $response = wp_remote_get( |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | ) |
85 | 85 | ); |
86 | 86 | |
87 | - return self::get_message_or_error( $response ); |
|
87 | + return self::get_message_or_error($response); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -96,23 +96,23 @@ discard block |
||
96 | 96 | * @return array|WP_Error |
97 | 97 | * @since 3.20.0 |
98 | 98 | */ |
99 | - public function post( $path, $body ) { |
|
99 | + public function post($path, $body) { |
|
100 | 100 | |
101 | 101 | return $this->post_custom_content_type( |
102 | 102 | $path, |
103 | - wp_json_encode( $body ), |
|
104 | - 'application/json; ' . get_bloginfo( 'charset' ) |
|
103 | + wp_json_encode($body), |
|
104 | + 'application/json; '.get_bloginfo('charset') |
|
105 | 105 | ); |
106 | 106 | } |
107 | 107 | |
108 | - public function post_custom_content_type( $path, $body, $content_type ) { |
|
108 | + public function post_custom_content_type($path, $body, $content_type) { |
|
109 | 109 | |
110 | 110 | // Prepare the target URL. |
111 | - $url = apply_filters( 'wl_api_service_api_url_path', Wordlift_Configuration_Service::get_instance()->get_api_url() . $path ); |
|
111 | + $url = apply_filters('wl_api_service_api_url_path', Wordlift_Configuration_Service::get_instance()->get_api_url().$path); |
|
112 | 112 | |
113 | 113 | // Give some time for the operation to complete, more than the time we give to the HTTP operation to complete. |
114 | 114 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
115 | - @set_time_limit( 90 ); |
|
115 | + @set_time_limit(90); |
|
116 | 116 | |
117 | 117 | // Get the response value. |
118 | 118 | $key = Wordlift_Configuration_Service::get_instance()->get_key(); |
@@ -136,13 +136,13 @@ discard block |
||
136 | 136 | ) |
137 | 137 | ); |
138 | 138 | |
139 | - return self::get_message_or_error( $response ); |
|
139 | + return self::get_message_or_error($response); |
|
140 | 140 | } |
141 | 141 | |
142 | - public function delete( $path ) { |
|
142 | + public function delete($path) { |
|
143 | 143 | |
144 | 144 | // Prepare the target URL. |
145 | - $url = Wordlift_Configuration_Service::get_instance()->get_api_url() . $path; |
|
145 | + $url = Wordlift_Configuration_Service::get_instance()->get_api_url().$path; |
|
146 | 146 | |
147 | 147 | // Get the response value. |
148 | 148 | $response = wp_remote_request( |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | ) |
157 | 157 | ); |
158 | 158 | |
159 | - return self::get_message_or_error( $response ); |
|
159 | + return self::get_message_or_error($response); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -167,29 +167,29 @@ discard block |
||
167 | 167 | * @return string|object|WP_Error A {@link WP_Error} instance or the actual response content. |
168 | 168 | * @since 3.20.0 |
169 | 169 | */ |
170 | - private static function get_message_or_error( $response ) { |
|
170 | + private static function get_message_or_error($response) { |
|
171 | 171 | |
172 | 172 | // Result is WP_Error. |
173 | - if ( is_wp_error( $response ) ) { |
|
173 | + if (is_wp_error($response)) { |
|
174 | 174 | return $response; |
175 | 175 | } |
176 | 176 | |
177 | 177 | // `code` not set or not numeric. |
178 | - $code = wp_remote_retrieve_response_code( $response ); |
|
178 | + $code = wp_remote_retrieve_response_code($response); |
|
179 | 179 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
180 | - $message = @wp_remote_retrieve_response_message( $response ); |
|
180 | + $message = @wp_remote_retrieve_response_message($response); |
|
181 | 181 | |
182 | - if ( empty( $code ) || ! is_numeric( $code ) ) { |
|
183 | - return new WP_Error( 0, $message ); |
|
182 | + if (empty($code) || ! is_numeric($code)) { |
|
183 | + return new WP_Error(0, $message); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Code not 2xx. |
187 | - if ( 2 !== intval( $code / 100 ) ) { |
|
188 | - return new WP_Error( $code, $message ); |
|
187 | + if (2 !== intval($code / 100)) { |
|
188 | + return new WP_Error($code, $message); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | // Everything's fine, return the message. |
192 | - return self::try_json_decode( $response ); |
|
192 | + return self::try_json_decode($response); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | /** |
@@ -200,19 +200,19 @@ discard block |
||
200 | 200 | * @return array|mixed|object The decoded response or the original response body. |
201 | 201 | * @since 3.20.0 |
202 | 202 | */ |
203 | - private static function try_json_decode( $response ) { |
|
203 | + private static function try_json_decode($response) { |
|
204 | 204 | |
205 | 205 | // Get the headers. |
206 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
207 | - $body = wp_remote_retrieve_body( $response ); |
|
206 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
207 | + $body = wp_remote_retrieve_body($response); |
|
208 | 208 | |
209 | 209 | // If it's not an `application/json` return the plain response body. |
210 | - if ( 0 !== strpos( strtolower( $content_type ), 'application/json' ) ) { |
|
210 | + if (0 !== strpos(strtolower($content_type), 'application/json')) { |
|
211 | 211 | return $body; |
212 | 212 | } |
213 | 213 | |
214 | 214 | // Decode and return the structured result. |
215 | - return json_decode( $body ); |
|
215 | + return json_decode($body); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | } |
@@ -18,103 +18,103 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Google_Analytics_Export_Service { |
20 | 20 | |
21 | - /** |
|
22 | - * Export the site data that could be imported in Google Analytics. |
|
23 | - * It will works only when permalink structure is set to "Postname". |
|
24 | - * |
|
25 | - * @since 3.16.0 |
|
26 | - * |
|
27 | - * @return void |
|
28 | - */ |
|
29 | - public function export() { |
|
30 | - // Bail if the permalink structure is different from "Post name". |
|
31 | - if ( ! $this->is_postname_permalink_structure() ) { |
|
32 | - wp_die( 'The current permalink structure do not allow to export your data. Please change the permalink structure to "Post name".' ); |
|
33 | - } |
|
34 | - |
|
35 | - // Output the file data. @codingStandardsIgnoreLine |
|
36 | - @ob_end_clean(); |
|
37 | - |
|
38 | - // Generate unique filename using current timestamp. |
|
39 | - $filename = 'wl-ga-export-' . gmdate( 'Y-m-d-H-i-s' ) . '.csv'; |
|
40 | - |
|
41 | - // Add proper file headers. |
|
42 | - header( "Content-Disposition: attachment; filename=$filename" ); |
|
43 | - header( 'Content-Type: text/csv; charset=' . get_bloginfo( 'charset' ) ); |
|
44 | - |
|
45 | - // Do not cache the file. |
|
46 | - header( 'Pragma: no-cache' ); |
|
47 | - header( 'Expires: 0' ); |
|
48 | - |
|
49 | - // Build the CSV file. |
|
50 | - $this->create_csv(); |
|
51 | - |
|
52 | - wp_die(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Return site path. Some installations are in subdirectories |
|
57 | - * and we need add them to expported permalinks. |
|
58 | - * |
|
59 | - * @since 3.16.0 |
|
60 | - * |
|
61 | - * @return string The site path. |
|
62 | - */ |
|
63 | - public function get_site_path() { |
|
64 | - // Get home url from database. |
|
65 | - $home_url = home_url( '/' ); |
|
66 | - |
|
67 | - // Parse the url. |
|
68 | - $parsed = wp_parse_url( $home_url ); |
|
69 | - |
|
70 | - // Return the path. |
|
71 | - return $parsed['path']; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Check if the current permalink structure is set to "Post name". |
|
76 | - * |
|
77 | - * @since 3.16.0 |
|
78 | - * |
|
79 | - * @return bool whether the structure is "Post name" or not. |
|
80 | - */ |
|
81 | - public static function is_postname_permalink_structure() { |
|
82 | - // Get current permalink structure. |
|
83 | - $structure = get_option( 'permalink_structure' ); |
|
84 | - |
|
85 | - // The regular expression. It will check if the site structure contains postname. |
|
86 | - $regex = '~^/\%postname\%/$~'; |
|
87 | - |
|
88 | - // Check if the site structure match the rquired one. |
|
89 | - preg_match( $regex, $structure, $matches ); |
|
90 | - |
|
91 | - // Bail if the site have different structure. |
|
92 | - if ( empty( $matches ) ) { |
|
93 | - return false; |
|
94 | - } |
|
95 | - |
|
96 | - return true; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Generate array data, that should be exported as csv. |
|
101 | - * The data contains the post/page title, entity name and type. |
|
102 | - * |
|
103 | - * @since 3.16.0 |
|
104 | - * |
|
105 | - * @return array $items Content data. |
|
106 | - */ |
|
107 | - public function get_content_data() { |
|
108 | - // Get the global $wpdb. |
|
109 | - global $wpdb; |
|
110 | - |
|
111 | - // Site path (optional). |
|
112 | - $path = $this->get_site_path(); |
|
113 | - |
|
114 | - // Get the data. |
|
115 | - $items = $wpdb->get_results( |
|
116 | - $wpdb->prepare( |
|
117 | - "SELECT |
|
21 | + /** |
|
22 | + * Export the site data that could be imported in Google Analytics. |
|
23 | + * It will works only when permalink structure is set to "Postname". |
|
24 | + * |
|
25 | + * @since 3.16.0 |
|
26 | + * |
|
27 | + * @return void |
|
28 | + */ |
|
29 | + public function export() { |
|
30 | + // Bail if the permalink structure is different from "Post name". |
|
31 | + if ( ! $this->is_postname_permalink_structure() ) { |
|
32 | + wp_die( 'The current permalink structure do not allow to export your data. Please change the permalink structure to "Post name".' ); |
|
33 | + } |
|
34 | + |
|
35 | + // Output the file data. @codingStandardsIgnoreLine |
|
36 | + @ob_end_clean(); |
|
37 | + |
|
38 | + // Generate unique filename using current timestamp. |
|
39 | + $filename = 'wl-ga-export-' . gmdate( 'Y-m-d-H-i-s' ) . '.csv'; |
|
40 | + |
|
41 | + // Add proper file headers. |
|
42 | + header( "Content-Disposition: attachment; filename=$filename" ); |
|
43 | + header( 'Content-Type: text/csv; charset=' . get_bloginfo( 'charset' ) ); |
|
44 | + |
|
45 | + // Do not cache the file. |
|
46 | + header( 'Pragma: no-cache' ); |
|
47 | + header( 'Expires: 0' ); |
|
48 | + |
|
49 | + // Build the CSV file. |
|
50 | + $this->create_csv(); |
|
51 | + |
|
52 | + wp_die(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Return site path. Some installations are in subdirectories |
|
57 | + * and we need add them to expported permalinks. |
|
58 | + * |
|
59 | + * @since 3.16.0 |
|
60 | + * |
|
61 | + * @return string The site path. |
|
62 | + */ |
|
63 | + public function get_site_path() { |
|
64 | + // Get home url from database. |
|
65 | + $home_url = home_url( '/' ); |
|
66 | + |
|
67 | + // Parse the url. |
|
68 | + $parsed = wp_parse_url( $home_url ); |
|
69 | + |
|
70 | + // Return the path. |
|
71 | + return $parsed['path']; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Check if the current permalink structure is set to "Post name". |
|
76 | + * |
|
77 | + * @since 3.16.0 |
|
78 | + * |
|
79 | + * @return bool whether the structure is "Post name" or not. |
|
80 | + */ |
|
81 | + public static function is_postname_permalink_structure() { |
|
82 | + // Get current permalink structure. |
|
83 | + $structure = get_option( 'permalink_structure' ); |
|
84 | + |
|
85 | + // The regular expression. It will check if the site structure contains postname. |
|
86 | + $regex = '~^/\%postname\%/$~'; |
|
87 | + |
|
88 | + // Check if the site structure match the rquired one. |
|
89 | + preg_match( $regex, $structure, $matches ); |
|
90 | + |
|
91 | + // Bail if the site have different structure. |
|
92 | + if ( empty( $matches ) ) { |
|
93 | + return false; |
|
94 | + } |
|
95 | + |
|
96 | + return true; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Generate array data, that should be exported as csv. |
|
101 | + * The data contains the post/page title, entity name and type. |
|
102 | + * |
|
103 | + * @since 3.16.0 |
|
104 | + * |
|
105 | + * @return array $items Content data. |
|
106 | + */ |
|
107 | + public function get_content_data() { |
|
108 | + // Get the global $wpdb. |
|
109 | + global $wpdb; |
|
110 | + |
|
111 | + // Site path (optional). |
|
112 | + $path = $this->get_site_path(); |
|
113 | + |
|
114 | + // Get the data. |
|
115 | + $items = $wpdb->get_results( |
|
116 | + $wpdb->prepare( |
|
117 | + "SELECT |
|
118 | 118 | CONCAT( %s, p.post_name, '/' ) AS 'post_name', |
119 | 119 | p1.post_name AS 'entity_name', |
120 | 120 | t.slug AS 'entity_type' |
@@ -131,43 +131,43 @@ discard block |
||
131 | 131 | INNER JOIN {$wpdb->prefix}terms t |
132 | 132 | ON t.term_id = tt.term_id |
133 | 133 | WHERE p.post_type IN ( 'page', 'post' );", |
134 | - $path |
|
135 | - ) |
|
136 | - ); // db call ok; no-cache ok. |
|
137 | - |
|
138 | - return $items; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Create the CSV file that will be downloaded. |
|
143 | - * |
|
144 | - * @since 3.16.0 |
|
145 | - * |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - public function create_csv() { |
|
149 | - // Create a file pointer connected to the output stream. |
|
150 | - // Ignoring linter notices below that complain about file output which do not actually happen. |
|
151 | - $file = fopen( 'php://output', 'w' ); |
|
152 | - |
|
153 | - // Add the column headers. @codingStandardsIgnoreLine |
|
154 | - fputcsv( |
|
155 | - $file, |
|
156 | - array( |
|
157 | - 'ga:pagePath', |
|
158 | - 'ga:dimension1', |
|
159 | - 'ga:dimension2', |
|
160 | - ) |
|
161 | - ); |
|
162 | - |
|
163 | - // Cycle through items and add each item data to the file. |
|
164 | - foreach ( $this->get_content_data() as $row ) { |
|
165 | - // Add new line in the file. @codingStandardsIgnoreLine |
|
166 | - fputcsv( |
|
167 | - $file, |
|
168 | - (array) $row // convert the object to array. |
|
169 | - ); |
|
170 | - } |
|
171 | - } |
|
134 | + $path |
|
135 | + ) |
|
136 | + ); // db call ok; no-cache ok. |
|
137 | + |
|
138 | + return $items; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Create the CSV file that will be downloaded. |
|
143 | + * |
|
144 | + * @since 3.16.0 |
|
145 | + * |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + public function create_csv() { |
|
149 | + // Create a file pointer connected to the output stream. |
|
150 | + // Ignoring linter notices below that complain about file output which do not actually happen. |
|
151 | + $file = fopen( 'php://output', 'w' ); |
|
152 | + |
|
153 | + // Add the column headers. @codingStandardsIgnoreLine |
|
154 | + fputcsv( |
|
155 | + $file, |
|
156 | + array( |
|
157 | + 'ga:pagePath', |
|
158 | + 'ga:dimension1', |
|
159 | + 'ga:dimension2', |
|
160 | + ) |
|
161 | + ); |
|
162 | + |
|
163 | + // Cycle through items and add each item data to the file. |
|
164 | + foreach ( $this->get_content_data() as $row ) { |
|
165 | + // Add new line in the file. @codingStandardsIgnoreLine |
|
166 | + fputcsv( |
|
167 | + $file, |
|
168 | + (array) $row // convert the object to array. |
|
169 | + ); |
|
170 | + } |
|
171 | + } |
|
172 | 172 | |
173 | 173 | } |
@@ -28,23 +28,23 @@ discard block |
||
28 | 28 | */ |
29 | 29 | public function export() { |
30 | 30 | // Bail if the permalink structure is different from "Post name". |
31 | - if ( ! $this->is_postname_permalink_structure() ) { |
|
32 | - wp_die( 'The current permalink structure do not allow to export your data. Please change the permalink structure to "Post name".' ); |
|
31 | + if ( ! $this->is_postname_permalink_structure()) { |
|
32 | + wp_die('The current permalink structure do not allow to export your data. Please change the permalink structure to "Post name".'); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | // Output the file data. @codingStandardsIgnoreLine |
36 | 36 | @ob_end_clean(); |
37 | 37 | |
38 | 38 | // Generate unique filename using current timestamp. |
39 | - $filename = 'wl-ga-export-' . gmdate( 'Y-m-d-H-i-s' ) . '.csv'; |
|
39 | + $filename = 'wl-ga-export-'.gmdate('Y-m-d-H-i-s').'.csv'; |
|
40 | 40 | |
41 | 41 | // Add proper file headers. |
42 | - header( "Content-Disposition: attachment; filename=$filename" ); |
|
43 | - header( 'Content-Type: text/csv; charset=' . get_bloginfo( 'charset' ) ); |
|
42 | + header("Content-Disposition: attachment; filename=$filename"); |
|
43 | + header('Content-Type: text/csv; charset='.get_bloginfo('charset')); |
|
44 | 44 | |
45 | 45 | // Do not cache the file. |
46 | - header( 'Pragma: no-cache' ); |
|
47 | - header( 'Expires: 0' ); |
|
46 | + header('Pragma: no-cache'); |
|
47 | + header('Expires: 0'); |
|
48 | 48 | |
49 | 49 | // Build the CSV file. |
50 | 50 | $this->create_csv(); |
@@ -62,10 +62,10 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function get_site_path() { |
64 | 64 | // Get home url from database. |
65 | - $home_url = home_url( '/' ); |
|
65 | + $home_url = home_url('/'); |
|
66 | 66 | |
67 | 67 | // Parse the url. |
68 | - $parsed = wp_parse_url( $home_url ); |
|
68 | + $parsed = wp_parse_url($home_url); |
|
69 | 69 | |
70 | 70 | // Return the path. |
71 | 71 | return $parsed['path']; |
@@ -80,16 +80,16 @@ discard block |
||
80 | 80 | */ |
81 | 81 | public static function is_postname_permalink_structure() { |
82 | 82 | // Get current permalink structure. |
83 | - $structure = get_option( 'permalink_structure' ); |
|
83 | + $structure = get_option('permalink_structure'); |
|
84 | 84 | |
85 | 85 | // The regular expression. It will check if the site structure contains postname. |
86 | 86 | $regex = '~^/\%postname\%/$~'; |
87 | 87 | |
88 | 88 | // Check if the site structure match the rquired one. |
89 | - preg_match( $regex, $structure, $matches ); |
|
89 | + preg_match($regex, $structure, $matches); |
|
90 | 90 | |
91 | 91 | // Bail if the site have different structure. |
92 | - if ( empty( $matches ) ) { |
|
92 | + if (empty($matches)) { |
|
93 | 93 | return false; |
94 | 94 | } |
95 | 95 | |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | public function create_csv() { |
149 | 149 | // Create a file pointer connected to the output stream. |
150 | 150 | // Ignoring linter notices below that complain about file output which do not actually happen. |
151 | - $file = fopen( 'php://output', 'w' ); |
|
151 | + $file = fopen('php://output', 'w'); |
|
152 | 152 | |
153 | 153 | // Add the column headers. @codingStandardsIgnoreLine |
154 | 154 | fputcsv( |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | ); |
162 | 162 | |
163 | 163 | // Cycle through items and add each item data to the file. |
164 | - foreach ( $this->get_content_data() as $row ) { |
|
164 | + foreach ($this->get_content_data() as $row) { |
|
165 | 165 | // Add new line in the file. @codingStandardsIgnoreLine |
166 | 166 | fputcsv( |
167 | 167 | $file, |
@@ -19,31 +19,31 @@ |
||
19 | 19 | */ |
20 | 20 | class Wordlift_Duration_Property_Service extends Wordlift_Simple_Property_Service { |
21 | 21 | |
22 | - /** |
|
23 | - * {@inheritdoc} |
|
24 | - */ |
|
25 | - public function get( $id, $meta_key, $type ) { |
|
22 | + /** |
|
23 | + * {@inheritdoc} |
|
24 | + */ |
|
25 | + public function get( $id, $meta_key, $type ) { |
|
26 | 26 | |
27 | - // Get the values and filter out the empty ones (or the ones with 00:00). |
|
28 | - $values = array_filter( |
|
29 | - parent::get( $id, $meta_key, $type ), |
|
30 | - function ( $item ) { |
|
31 | - return ! empty( $item ) && '00:00' !== $item; |
|
32 | - } |
|
33 | - ); |
|
27 | + // Get the values and filter out the empty ones (or the ones with 00:00). |
|
28 | + $values = array_filter( |
|
29 | + parent::get( $id, $meta_key, $type ), |
|
30 | + function ( $item ) { |
|
31 | + return ! empty( $item ) && '00:00' !== $item; |
|
32 | + } |
|
33 | + ); |
|
34 | 34 | |
35 | - /* |
|
35 | + /* |
|
36 | 36 | * Map the value in the meta |
37 | 37 | * The UI for the meta date enable two forms, a number of minutes |
38 | 38 | * or an h:mm format. |
39 | 39 | * Both needs to be adjusted to the iso format. |
40 | 40 | */ |
41 | - return array_map( |
|
42 | - function ( $value ) { |
|
43 | - return 'PT' . str_replace( ':', 'H', $value ) . 'M'; |
|
44 | - }, |
|
45 | - $values |
|
46 | - ); |
|
47 | - } |
|
41 | + return array_map( |
|
42 | + function ( $value ) { |
|
43 | + return 'PT' . str_replace( ':', 'H', $value ) . 'M'; |
|
44 | + }, |
|
45 | + $values |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | } |
@@ -22,13 +22,13 @@ discard block |
||
22 | 22 | /** |
23 | 23 | * {@inheritdoc} |
24 | 24 | */ |
25 | - public function get( $id, $meta_key, $type ) { |
|
25 | + public function get($id, $meta_key, $type) { |
|
26 | 26 | |
27 | 27 | // Get the values and filter out the empty ones (or the ones with 00:00). |
28 | 28 | $values = array_filter( |
29 | - parent::get( $id, $meta_key, $type ), |
|
30 | - function ( $item ) { |
|
31 | - return ! empty( $item ) && '00:00' !== $item; |
|
29 | + parent::get($id, $meta_key, $type), |
|
30 | + function($item) { |
|
31 | + return ! empty($item) && '00:00' !== $item; |
|
32 | 32 | } |
33 | 33 | ); |
34 | 34 | |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | * Both needs to be adjusted to the iso format. |
40 | 40 | */ |
41 | 41 | return array_map( |
42 | - function ( $value ) { |
|
43 | - return 'PT' . str_replace( ':', 'H', $value ) . 'M'; |
|
42 | + function($value) { |
|
43 | + return 'PT'.str_replace(':', 'H', $value).'M'; |
|
44 | 44 | }, |
45 | 45 | $values |
46 | 46 | ); |
@@ -9,74 +9,74 @@ |
||
9 | 9 | */ |
10 | 10 | class Wordlift_Property_Getter { |
11 | 11 | |
12 | - /** |
|
13 | - * An array of {@link Wordlift_Simple_Property_Service}s which can access a |
|
14 | - * property. |
|
15 | - * |
|
16 | - * @since 3.8.0 |
|
17 | - * @access private |
|
18 | - * @var Wordlift_Simple_Property_Service[] $services An array of {@link Wordlift_Simple_Property_Service}s. |
|
19 | - */ |
|
20 | - private $services = array(); |
|
12 | + /** |
|
13 | + * An array of {@link Wordlift_Simple_Property_Service}s which can access a |
|
14 | + * property. |
|
15 | + * |
|
16 | + * @since 3.8.0 |
|
17 | + * @access private |
|
18 | + * @var Wordlift_Simple_Property_Service[] $services An array of {@link Wordlift_Simple_Property_Service}s. |
|
19 | + */ |
|
20 | + private $services = array(); |
|
21 | 21 | |
22 | - /** |
|
23 | - * The default {@link Wordlift_Simple_Property_Service} which is used to access |
|
24 | - * a property when no specific {@link Wordlift_Simple_Property_Service} is found |
|
25 | - * in the {@see $services} array. |
|
26 | - * |
|
27 | - * @var Wordlift_Simple_Property_Service |
|
28 | - */ |
|
29 | - private $default; |
|
22 | + /** |
|
23 | + * The default {@link Wordlift_Simple_Property_Service} which is used to access |
|
24 | + * a property when no specific {@link Wordlift_Simple_Property_Service} is found |
|
25 | + * in the {@see $services} array. |
|
26 | + * |
|
27 | + * @var Wordlift_Simple_Property_Service |
|
28 | + */ |
|
29 | + private $default; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Create a property service with the provided {@link Wordlift_Simple_Property_Service} |
|
33 | - * as default. |
|
34 | - * |
|
35 | - * @param $default |
|
36 | - * |
|
37 | - * @since 3.8.0 |
|
38 | - */ |
|
39 | - public function __construct( $default ) { |
|
31 | + /** |
|
32 | + * Create a property service with the provided {@link Wordlift_Simple_Property_Service} |
|
33 | + * as default. |
|
34 | + * |
|
35 | + * @param $default |
|
36 | + * |
|
37 | + * @since 3.8.0 |
|
38 | + */ |
|
39 | + public function __construct( $default ) { |
|
40 | 40 | |
41 | - $this->default = $default; |
|
41 | + $this->default = $default; |
|
42 | 42 | |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Register a {@link Wordlift_Simple_Property_Service} for the specified meta keys. |
|
47 | - * |
|
48 | - * @param \Wordlift_Simple_Property_Service $property_service A {@link Wordlift_Simple_Property_Service} instance. |
|
49 | - * @param array $meta_keys An array of meta keys that the provided {@link Wordlift_Simple_Property_Service} will handle. |
|
50 | - * |
|
51 | - * @since 3.8.0 |
|
52 | - */ |
|
53 | - public function register( $property_service, $meta_keys ) { |
|
45 | + /** |
|
46 | + * Register a {@link Wordlift_Simple_Property_Service} for the specified meta keys. |
|
47 | + * |
|
48 | + * @param \Wordlift_Simple_Property_Service $property_service A {@link Wordlift_Simple_Property_Service} instance. |
|
49 | + * @param array $meta_keys An array of meta keys that the provided {@link Wordlift_Simple_Property_Service} will handle. |
|
50 | + * |
|
51 | + * @since 3.8.0 |
|
52 | + */ |
|
53 | + public function register( $property_service, $meta_keys ) { |
|
54 | 54 | |
55 | - // Register the specified property service for each meta key. |
|
56 | - foreach ( $meta_keys as $meta_key ) { |
|
57 | - $this->services[ $meta_key ] = $property_service; |
|
58 | - } |
|
55 | + // Register the specified property service for each meta key. |
|
56 | + foreach ( $meta_keys as $meta_key ) { |
|
57 | + $this->services[ $meta_key ] = $property_service; |
|
58 | + } |
|
59 | 59 | |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get the value for the specified entity post id and WP's meta key. |
|
64 | - * |
|
65 | - * @param int $post_id The post id. |
|
66 | - * @param string $meta_key The meta key. |
|
67 | - * |
|
68 | - * @param int $type Term or Post, by default Post is used. |
|
69 | - * |
|
70 | - * @return mixed|null The property value or null. |
|
71 | - * @since 3.8.0 |
|
72 | - */ |
|
73 | - public function get( $post_id, $meta_key, $type ) { |
|
62 | + /** |
|
63 | + * Get the value for the specified entity post id and WP's meta key. |
|
64 | + * |
|
65 | + * @param int $post_id The post id. |
|
66 | + * @param string $meta_key The meta key. |
|
67 | + * |
|
68 | + * @param int $type Term or Post, by default Post is used. |
|
69 | + * |
|
70 | + * @return mixed|null The property value or null. |
|
71 | + * @since 3.8.0 |
|
72 | + */ |
|
73 | + public function get( $post_id, $meta_key, $type ) { |
|
74 | 74 | |
75 | - return isset( $this->services[ $meta_key ] ) |
|
76 | - // Use a specific property service. |
|
77 | - ? $this->services[ $meta_key ]->get( $post_id, $meta_key, $type ) |
|
78 | - // Use the default property service. |
|
79 | - : $this->default->get( $post_id, $meta_key, $type ); |
|
80 | - } |
|
75 | + return isset( $this->services[ $meta_key ] ) |
|
76 | + // Use a specific property service. |
|
77 | + ? $this->services[ $meta_key ]->get( $post_id, $meta_key, $type ) |
|
78 | + // Use the default property service. |
|
79 | + : $this->default->get( $post_id, $meta_key, $type ); |
|
80 | + } |
|
81 | 81 | |
82 | 82 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @since 3.8.0 |
38 | 38 | */ |
39 | - public function __construct( $default ) { |
|
39 | + public function __construct($default) { |
|
40 | 40 | |
41 | 41 | $this->default = $default; |
42 | 42 | |
@@ -50,11 +50,11 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @since 3.8.0 |
52 | 52 | */ |
53 | - public function register( $property_service, $meta_keys ) { |
|
53 | + public function register($property_service, $meta_keys) { |
|
54 | 54 | |
55 | 55 | // Register the specified property service for each meta key. |
56 | - foreach ( $meta_keys as $meta_key ) { |
|
57 | - $this->services[ $meta_key ] = $property_service; |
|
56 | + foreach ($meta_keys as $meta_key) { |
|
57 | + $this->services[$meta_key] = $property_service; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | } |
@@ -70,13 +70,13 @@ discard block |
||
70 | 70 | * @return mixed|null The property value or null. |
71 | 71 | * @since 3.8.0 |
72 | 72 | */ |
73 | - public function get( $post_id, $meta_key, $type ) { |
|
73 | + public function get($post_id, $meta_key, $type) { |
|
74 | 74 | |
75 | - return isset( $this->services[ $meta_key ] ) |
|
75 | + return isset($this->services[$meta_key]) |
|
76 | 76 | // Use a specific property service. |
77 | - ? $this->services[ $meta_key ]->get( $post_id, $meta_key, $type ) |
|
77 | + ? $this->services[$meta_key]->get($post_id, $meta_key, $type) |
|
78 | 78 | // Use the default property service. |
79 | - : $this->default->get( $post_id, $meta_key, $type ); |
|
79 | + : $this->default->get($post_id, $meta_key, $type); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | } |
@@ -26,60 +26,60 @@ |
||
26 | 26 | */ |
27 | 27 | class Wordlift_Property_Getter_Factory { |
28 | 28 | |
29 | - /** |
|
30 | - * Create a {@link Wordlift_Property_Getter} instance. |
|
31 | - * |
|
32 | - * @return \Wordlift_Property_Getter A {@link Wordlift_Property_Getter} instance. |
|
33 | - * @since 3.8.0 |
|
34 | - */ |
|
35 | - public static function create() { |
|
29 | + /** |
|
30 | + * Create a {@link Wordlift_Property_Getter} instance. |
|
31 | + * |
|
32 | + * @return \Wordlift_Property_Getter A {@link Wordlift_Property_Getter} instance. |
|
33 | + * @since 3.8.0 |
|
34 | + */ |
|
35 | + public static function create() { |
|
36 | 36 | |
37 | - $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
38 | - $property_getter->register( |
|
39 | - new Wordlift_Entity_Property_Service(), |
|
40 | - array( |
|
41 | - Wordlift_Schema_Service::FIELD_FOUNDER, |
|
42 | - Wordlift_Schema_Service::FIELD_AUTHOR, |
|
43 | - Wordlift_Schema_Service::FIELD_KNOWS, |
|
44 | - Wordlift_Schema_Service::FIELD_BIRTH_PLACE, |
|
45 | - Wordlift_Schema_Service::FIELD_AFFILIATION, |
|
46 | - Wordlift_Schema_Service::FIELD_PERFORMER, |
|
47 | - Wordlift_Schema_Service::FIELD_OFFERS, |
|
48 | - Wordlift_Schema_Service::FIELD_ITEM_OFFERED, |
|
49 | - ) |
|
50 | - ); |
|
51 | - $property_getter->register( |
|
52 | - new Wordlift_Location_Property_Service(), |
|
53 | - array( |
|
54 | - Wordlift_Schema_Service::FIELD_LOCATION, |
|
55 | - ) |
|
56 | - ); |
|
57 | - $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
58 | - $property_getter->register( |
|
59 | - new Wordlift_Double_Property_Service(), |
|
60 | - array( |
|
61 | - Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
62 | - Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
63 | - ) |
|
64 | - ); |
|
37 | + $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
38 | + $property_getter->register( |
|
39 | + new Wordlift_Entity_Property_Service(), |
|
40 | + array( |
|
41 | + Wordlift_Schema_Service::FIELD_FOUNDER, |
|
42 | + Wordlift_Schema_Service::FIELD_AUTHOR, |
|
43 | + Wordlift_Schema_Service::FIELD_KNOWS, |
|
44 | + Wordlift_Schema_Service::FIELD_BIRTH_PLACE, |
|
45 | + Wordlift_Schema_Service::FIELD_AFFILIATION, |
|
46 | + Wordlift_Schema_Service::FIELD_PERFORMER, |
|
47 | + Wordlift_Schema_Service::FIELD_OFFERS, |
|
48 | + Wordlift_Schema_Service::FIELD_ITEM_OFFERED, |
|
49 | + ) |
|
50 | + ); |
|
51 | + $property_getter->register( |
|
52 | + new Wordlift_Location_Property_Service(), |
|
53 | + array( |
|
54 | + Wordlift_Schema_Service::FIELD_LOCATION, |
|
55 | + ) |
|
56 | + ); |
|
57 | + $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
58 | + $property_getter->register( |
|
59 | + new Wordlift_Double_Property_Service(), |
|
60 | + array( |
|
61 | + Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
62 | + Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
63 | + ) |
|
64 | + ); |
|
65 | 65 | |
66 | - $property_getter->register( |
|
67 | - new Wordlift_Duration_Property_Service(), |
|
68 | - array( |
|
69 | - Wordlift_Schema_Service::FIELD_PREP_TIME, |
|
70 | - Wordlift_Schema_Service::FIELD_COOK_TIME, |
|
71 | - Wordlift_Schema_Service::FIELD_TOTAL_TIME, |
|
72 | - ) |
|
73 | - ); |
|
66 | + $property_getter->register( |
|
67 | + new Wordlift_Duration_Property_Service(), |
|
68 | + array( |
|
69 | + Wordlift_Schema_Service::FIELD_PREP_TIME, |
|
70 | + Wordlift_Schema_Service::FIELD_COOK_TIME, |
|
71 | + Wordlift_Schema_Service::FIELD_TOTAL_TIME, |
|
72 | + ) |
|
73 | + ); |
|
74 | 74 | |
75 | - add_action( |
|
76 | - 'after_setup_theme', |
|
77 | - function () use ( $property_getter ) { |
|
78 | - $property_getter->register( new Wordlift_Required_Property_Service(), apply_filters( 'wl_required_property', array() ) ); |
|
79 | - } |
|
80 | - ); |
|
75 | + add_action( |
|
76 | + 'after_setup_theme', |
|
77 | + function () use ( $property_getter ) { |
|
78 | + $property_getter->register( new Wordlift_Required_Property_Service(), apply_filters( 'wl_required_property', array() ) ); |
|
79 | + } |
|
80 | + ); |
|
81 | 81 | |
82 | - return $property_getter; |
|
83 | - } |
|
82 | + return $property_getter; |
|
83 | + } |
|
84 | 84 | |
85 | 85 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public static function create() { |
36 | 36 | |
37 | - $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
37 | + $property_getter = new Wordlift_Property_Getter(new Wordlift_Simple_Property_Service()); |
|
38 | 38 | $property_getter->register( |
39 | 39 | new Wordlift_Entity_Property_Service(), |
40 | 40 | array( |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | Wordlift_Schema_Service::FIELD_LOCATION, |
55 | 55 | ) |
56 | 56 | ); |
57 | - $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
57 | + $property_getter->register(new Wordlift_Url_Property_Service(), array(Wordlift_Url_Property_Service::META_KEY)); |
|
58 | 58 | $property_getter->register( |
59 | 59 | new Wordlift_Double_Property_Service(), |
60 | 60 | array( |
@@ -74,8 +74,8 @@ discard block |
||
74 | 74 | |
75 | 75 | add_action( |
76 | 76 | 'after_setup_theme', |
77 | - function () use ( $property_getter ) { |
|
78 | - $property_getter->register( new Wordlift_Required_Property_Service(), apply_filters( 'wl_required_property', array() ) ); |
|
77 | + function() use ($property_getter) { |
|
78 | + $property_getter->register(new Wordlift_Required_Property_Service(), apply_filters('wl_required_property', array())); |
|
79 | 79 | } |
80 | 80 | ); |
81 | 81 |
@@ -12,33 +12,33 @@ |
||
12 | 12 | */ |
13 | 13 | class Wordlift_Simple_Property_Service { |
14 | 14 | |
15 | - /** |
|
16 | - * The meta key for this property service. |
|
17 | - * |
|
18 | - * @since 3.8.0 |
|
19 | - */ |
|
20 | - const META_KEY = '*'; |
|
15 | + /** |
|
16 | + * The meta key for this property service. |
|
17 | + * |
|
18 | + * @since 3.8.0 |
|
19 | + */ |
|
20 | + const META_KEY = '*'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Get the property value for the specified post id and meta with the specified key. |
|
24 | - * |
|
25 | - * @param int $id The post id. |
|
26 | - * @param string $meta_key The meta key. |
|
27 | - * |
|
28 | - * @param $type int Post or Term |
|
29 | - * |
|
30 | - * @return mixed|null The property value. |
|
31 | - * @since 3.8.0 |
|
32 | - */ |
|
33 | - public function get( $id, $meta_key, $type ) { |
|
22 | + /** |
|
23 | + * Get the property value for the specified post id and meta with the specified key. |
|
24 | + * |
|
25 | + * @param int $id The post id. |
|
26 | + * @param string $meta_key The meta key. |
|
27 | + * |
|
28 | + * @param $type int Post or Term |
|
29 | + * |
|
30 | + * @return mixed|null The property value. |
|
31 | + * @since 3.8.0 |
|
32 | + */ |
|
33 | + public function get( $id, $meta_key, $type ) { |
|
34 | 34 | |
35 | - if ( Object_Type_Enum::POST === $type ) { |
|
36 | - // Get the value stored in WP. |
|
37 | - return get_post_meta( $id, $meta_key ); |
|
38 | - } elseif ( Object_Type_Enum::TERM === $type ) { |
|
39 | - return get_term_meta( $id, $meta_key ); |
|
40 | - } |
|
41 | - return null; |
|
42 | - } |
|
35 | + if ( Object_Type_Enum::POST === $type ) { |
|
36 | + // Get the value stored in WP. |
|
37 | + return get_post_meta( $id, $meta_key ); |
|
38 | + } elseif ( Object_Type_Enum::TERM === $type ) { |
|
39 | + return get_term_meta( $id, $meta_key ); |
|
40 | + } |
|
41 | + return null; |
|
42 | + } |
|
43 | 43 | |
44 | 44 | } |
@@ -30,13 +30,13 @@ |
||
30 | 30 | * @return mixed|null The property value. |
31 | 31 | * @since 3.8.0 |
32 | 32 | */ |
33 | - public function get( $id, $meta_key, $type ) { |
|
33 | + public function get($id, $meta_key, $type) { |
|
34 | 34 | |
35 | - if ( Object_Type_Enum::POST === $type ) { |
|
35 | + if (Object_Type_Enum::POST === $type) { |
|
36 | 36 | // Get the value stored in WP. |
37 | - return get_post_meta( $id, $meta_key ); |
|
38 | - } elseif ( Object_Type_Enum::TERM === $type ) { |
|
39 | - return get_term_meta( $id, $meta_key ); |
|
37 | + return get_post_meta($id, $meta_key); |
|
38 | + } elseif (Object_Type_Enum::TERM === $type) { |
|
39 | + return get_term_meta($id, $meta_key); |
|
40 | 40 | } |
41 | 41 | return null; |
42 | 42 | } |
@@ -16,18 +16,18 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Double_Property_Service extends Wordlift_Simple_Property_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * @inheritdoc |
|
21 | - */ |
|
22 | - public function get( $id, $meta_key, $type ) { |
|
19 | + /** |
|
20 | + * @inheritdoc |
|
21 | + */ |
|
22 | + public function get( $id, $meta_key, $type ) { |
|
23 | 23 | |
24 | - // Map the result to a numeric value when possible. |
|
25 | - return array_map( |
|
26 | - function ( $value ) { |
|
27 | - return is_numeric( $value ) ? (float) $value : $value; |
|
28 | - }, |
|
29 | - parent::get( $id, $meta_key, $type ) |
|
30 | - ); |
|
31 | - } |
|
24 | + // Map the result to a numeric value when possible. |
|
25 | + return array_map( |
|
26 | + function ( $value ) { |
|
27 | + return is_numeric( $value ) ? (float) $value : $value; |
|
28 | + }, |
|
29 | + parent::get( $id, $meta_key, $type ) |
|
30 | + ); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | } |
@@ -19,14 +19,14 @@ |
||
19 | 19 | /** |
20 | 20 | * @inheritdoc |
21 | 21 | */ |
22 | - public function get( $id, $meta_key, $type ) { |
|
22 | + public function get($id, $meta_key, $type) { |
|
23 | 23 | |
24 | 24 | // Map the result to a numeric value when possible. |
25 | 25 | return array_map( |
26 | - function ( $value ) { |
|
27 | - return is_numeric( $value ) ? (float) $value : $value; |
|
26 | + function($value) { |
|
27 | + return is_numeric($value) ? (float) $value : $value; |
|
28 | 28 | }, |
29 | - parent::get( $id, $meta_key, $type ) |
|
29 | + parent::get($id, $meta_key, $type) |
|
30 | 30 | ); |
31 | 31 | } |
32 | 32 |