@@ -21,149 +21,149 @@ |
||
| 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 | - */ |
|
| 51 | - private function has_permission_to_show_popup() { |
|
| 52 | - // Get the current page. |
|
| 53 | - global $pagenow; |
|
| 54 | - |
|
| 55 | - // Bail if the user doesn't have permissions |
|
| 56 | - // or if it's not the plugins page. |
|
| 57 | - if ( 'plugins.php' !== $pagenow ) { |
|
| 58 | - return false; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Get the user preferences. We shouldn't show the feedback popup |
|
| 62 | - // if we don't have permissions for that. |
|
| 63 | - $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
| 64 | - |
|
| 65 | - // Bail. We don't have preferences to show the popup. |
|
| 66 | - if ( 'yes' !== $user_preferences ) { |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return true; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Render the feedback popup in the footer. |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - * @version 3.19.0 |
|
| 78 | - * |
|
| 79 | - */ |
|
| 80 | - public function render_feedback_popup() { |
|
| 81 | - // Bail if we don't have permissions to show the popup. |
|
| 82 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 83 | - return; |
|
| 84 | - } |
|
| 85 | - // Include the partial. |
|
| 86 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Enqueue required popup scripts and styles. |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - * @version 3.19.0 |
|
| 94 | - * |
|
| 95 | - */ |
|
| 96 | - public function enqueue_popup_scripts() { |
|
| 97 | - // Bail if we don't have permissions to show the popup. |
|
| 98 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/css/wordlift-admin-feedback-popup.css', array() ); |
|
| 103 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ) ); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Handle the deactivation ajax call |
|
| 108 | - * and perform a request to external server. |
|
| 109 | - * |
|
| 110 | - * @return void |
|
| 111 | - * @version 3.19.0 |
|
| 112 | - * |
|
| 113 | - */ |
|
| 114 | - public function wl_deactivation_feedback() { |
|
| 115 | - // Bail if the nonce is not valid. |
|
| 116 | - if ( |
|
| 117 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 118 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 119 | - ) { |
|
| 120 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // We allow user to deactivate without providing a reason |
|
| 124 | - // so bail and send success response. |
|
| 125 | - if ( empty( $_POST['code'] ) ) { |
|
| 126 | - wp_send_json_success(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $plugin_data = get_plugin_data( plugin_dir_path( dirname( __FILE__ ) ) . 'wordlift.php', false, false ); |
|
| 130 | - |
|
| 131 | - // Prepare the options. |
|
| 132 | - $options = array( |
|
| 133 | - // The deactivation reason. |
|
| 134 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 135 | - // Additional information if provided. |
|
| 136 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 137 | - // The website url. |
|
| 138 | - 'url' => get_bloginfo( 'url' ), |
|
| 139 | - // WP version. |
|
| 140 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 141 | - // WL version. |
|
| 142 | - 'wordliftVersion' => $plugin_data['Version'], |
|
| 143 | - // The admin email. |
|
| 144 | - 'email' => get_bloginfo( 'admin_email' ), |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - $response = wp_remote_post( |
|
| 148 | - Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
| 149 | - array( |
|
| 150 | - 'method' => 'POST', |
|
| 151 | - 'body' => json_encode( $options ), |
|
| 152 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 153 | - ) |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 157 | - $message = wp_remote_retrieve_response_message( $response ); |
|
| 158 | - |
|
| 159 | - // Add message to the error log if the response code is not 200. |
|
| 160 | - if ( 201 !== $code ) { |
|
| 161 | - // Write the error in the logs. |
|
| 162 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // We should send success message even when the feedback is not |
|
| 166 | - // send, because otherwise the plugin cannot be deactivated. |
|
| 167 | - wp_send_json_success(); |
|
| 168 | - } |
|
| 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 | + */ |
|
| 51 | + private function has_permission_to_show_popup() { |
|
| 52 | + // Get the current page. |
|
| 53 | + global $pagenow; |
|
| 54 | + |
|
| 55 | + // Bail if the user doesn't have permissions |
|
| 56 | + // or if it's not the plugins page. |
|
| 57 | + if ( 'plugins.php' !== $pagenow ) { |
|
| 58 | + return false; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Get the user preferences. We shouldn't show the feedback popup |
|
| 62 | + // if we don't have permissions for that. |
|
| 63 | + $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
| 64 | + |
|
| 65 | + // Bail. We don't have preferences to show the popup. |
|
| 66 | + if ( 'yes' !== $user_preferences ) { |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Render the feedback popup in the footer. |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + * @version 3.19.0 |
|
| 78 | + * |
|
| 79 | + */ |
|
| 80 | + public function render_feedback_popup() { |
|
| 81 | + // Bail if we don't have permissions to show the popup. |
|
| 82 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
| 83 | + return; |
|
| 84 | + } |
|
| 85 | + // Include the partial. |
|
| 86 | + include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Enqueue required popup scripts and styles. |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + * @version 3.19.0 |
|
| 94 | + * |
|
| 95 | + */ |
|
| 96 | + public function enqueue_popup_scripts() { |
|
| 97 | + // Bail if we don't have permissions to show the popup. |
|
| 98 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/css/wordlift-admin-feedback-popup.css', array() ); |
|
| 103 | + wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ) ); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Handle the deactivation ajax call |
|
| 108 | + * and perform a request to external server. |
|
| 109 | + * |
|
| 110 | + * @return void |
|
| 111 | + * @version 3.19.0 |
|
| 112 | + * |
|
| 113 | + */ |
|
| 114 | + public function wl_deactivation_feedback() { |
|
| 115 | + // Bail if the nonce is not valid. |
|
| 116 | + if ( |
|
| 117 | + empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 118 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 119 | + ) { |
|
| 120 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // We allow user to deactivate without providing a reason |
|
| 124 | + // so bail and send success response. |
|
| 125 | + if ( empty( $_POST['code'] ) ) { |
|
| 126 | + wp_send_json_success(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $plugin_data = get_plugin_data( plugin_dir_path( dirname( __FILE__ ) ) . 'wordlift.php', false, false ); |
|
| 130 | + |
|
| 131 | + // Prepare the options. |
|
| 132 | + $options = array( |
|
| 133 | + // The deactivation reason. |
|
| 134 | + 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 135 | + // Additional information if provided. |
|
| 136 | + 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 137 | + // The website url. |
|
| 138 | + 'url' => get_bloginfo( 'url' ), |
|
| 139 | + // WP version. |
|
| 140 | + 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 141 | + // WL version. |
|
| 142 | + 'wordliftVersion' => $plugin_data['Version'], |
|
| 143 | + // The admin email. |
|
| 144 | + 'email' => get_bloginfo( 'admin_email' ), |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + $response = wp_remote_post( |
|
| 148 | + Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
| 149 | + array( |
|
| 150 | + 'method' => 'POST', |
|
| 151 | + 'body' => json_encode( $options ), |
|
| 152 | + 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 153 | + ) |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + $code = wp_remote_retrieve_response_code( $response ); |
|
| 157 | + $message = wp_remote_retrieve_response_message( $response ); |
|
| 158 | + |
|
| 159 | + // Add message to the error log if the response code is not 200. |
|
| 160 | + if ( 201 !== $code ) { |
|
| 161 | + // Write the error in the logs. |
|
| 162 | + $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // We should send success message even when the feedback is not |
|
| 166 | + // send, because otherwise the plugin cannot be deactivated. |
|
| 167 | + wp_send_json_success(); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -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 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | // Bail if the user doesn't have permissions |
| 56 | 56 | // or if it's not the plugins page. |
| 57 | - if ( 'plugins.php' !== $pagenow ) { |
|
| 57 | + if ('plugins.php' !== $pagenow) { |
|
| 58 | 58 | return false; |
| 59 | 59 | } |
| 60 | 60 | |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
| 64 | 64 | |
| 65 | 65 | // Bail. We don't have preferences to show the popup. |
| 66 | - if ( 'yes' !== $user_preferences ) { |
|
| 66 | + if ('yes' !== $user_preferences) { |
|
| 67 | 67 | return false; |
| 68 | 68 | } |
| 69 | 69 | |
@@ -79,11 +79,11 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | public function render_feedback_popup() { |
| 81 | 81 | // Bail if we don't have permissions to show the popup. |
| 82 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 82 | + if ( ! $this->has_permission_to_show_popup()) { |
|
| 83 | 83 | return; |
| 84 | 84 | } |
| 85 | 85 | // Include the partial. |
| 86 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 86 | + include plugin_dir_path(__FILE__).'../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function enqueue_popup_scripts() { |
| 97 | 97 | // Bail if we don't have permissions to show the popup. |
| 98 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
| 98 | + if ( ! $this->has_permission_to_show_popup()) { |
|
| 99 | 99 | return; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/css/wordlift-admin-feedback-popup.css', array() ); |
|
| 103 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ) ); |
|
| 102 | + wp_enqueue_style('wordlift-admin-feedback-popup', plugin_dir_url(dirname(__FILE__)).'admin/css/wordlift-admin-feedback-popup.css', array()); |
|
| 103 | + wp_enqueue_script('wordlift-admin-feedback-popup', plugin_dir_url(dirname(__FILE__)).'admin/js/wordlift-admin-feedback-popup.js', array('jquery')); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -114,52 +114,52 @@ discard block |
||
| 114 | 114 | public function wl_deactivation_feedback() { |
| 115 | 115 | // Bail if the nonce is not valid. |
| 116 | 116 | if ( |
| 117 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
| 118 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
| 117 | + empty($_POST['wl_deactivation_feedback_nonce']) || // The nonce doens't exists. |
|
| 118 | + ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wl_deactivation_feedback_nonce'])), 'wl_deactivation_feedback_nonce') // The nonce is invalid. |
|
| 119 | 119 | ) { |
| 120 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 120 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | // We allow user to deactivate without providing a reason |
| 124 | 124 | // so bail and send success response. |
| 125 | - if ( empty( $_POST['code'] ) ) { |
|
| 125 | + if (empty($_POST['code'])) { |
|
| 126 | 126 | wp_send_json_success(); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - $plugin_data = get_plugin_data( plugin_dir_path( dirname( __FILE__ ) ) . 'wordlift.php', false, false ); |
|
| 129 | + $plugin_data = get_plugin_data(plugin_dir_path(dirname(__FILE__)).'wordlift.php', false, false); |
|
| 130 | 130 | |
| 131 | 131 | // Prepare the options. |
| 132 | 132 | $options = array( |
| 133 | 133 | // The deactivation reason. |
| 134 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
| 134 | + 'code' => sanitize_text_field(wp_unslash((string) $_POST['code'])), |
|
| 135 | 135 | // Additional information if provided. |
| 136 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
| 136 | + 'details' => ( ! empty($_POST['details'])) ? sanitize_text_field(wp_unslash((string) $_POST['details'])) : '', |
|
| 137 | 137 | // The website url. |
| 138 | - 'url' => get_bloginfo( 'url' ), |
|
| 138 | + 'url' => get_bloginfo('url'), |
|
| 139 | 139 | // WP version. |
| 140 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
| 140 | + 'wordpressVersion' => get_bloginfo('version'), |
|
| 141 | 141 | // WL version. |
| 142 | 142 | 'wordliftVersion' => $plugin_data['Version'], |
| 143 | 143 | // The admin email. |
| 144 | - 'email' => get_bloginfo( 'admin_email' ), |
|
| 144 | + 'email' => get_bloginfo('admin_email'), |
|
| 145 | 145 | ); |
| 146 | 146 | |
| 147 | 147 | $response = wp_remote_post( |
| 148 | 148 | Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
| 149 | 149 | array( |
| 150 | 150 | 'method' => 'POST', |
| 151 | - 'body' => json_encode( $options ), |
|
| 152 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
| 151 | + 'body' => json_encode($options), |
|
| 152 | + 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), |
|
| 153 | 153 | ) |
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | - $code = wp_remote_retrieve_response_code( $response ); |
|
| 157 | - $message = wp_remote_retrieve_response_message( $response ); |
|
| 156 | + $code = wp_remote_retrieve_response_code($response); |
|
| 157 | + $message = wp_remote_retrieve_response_message($response); |
|
| 158 | 158 | |
| 159 | 159 | // Add message to the error log if the response code is not 200. |
| 160 | - if ( 201 !== $code ) { |
|
| 160 | + if (201 !== $code) { |
|
| 161 | 161 | // Write the error in the logs. |
| 162 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
| 162 | + $this->log->error('An error occurred while requesting a feedback endpoint error_code: '.$code.' message: '.$message); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // We should send success message even when the feedback is not |
@@ -20,181 +20,181 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Key_Validation_Service { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.14.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var Ttl_Cache |
|
| 34 | - */ |
|
| 35 | - private $ttl_cache_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.14.0 |
|
| 41 | - */ |
|
| 42 | - public function __construct() { |
|
| 43 | - |
|
| 44 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 45 | - |
|
| 46 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 47 | - /** |
|
| 48 | - * Filter: wl_feature__enable__notices. |
|
| 49 | - * |
|
| 50 | - * @param bool whether notices need to be enabled or not. |
|
| 51 | - * |
|
| 52 | - * @return bool |
|
| 53 | - * @since 3.27.6 |
|
| 54 | - */ |
|
| 55 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 56 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 60 | - |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Validate the provided key. |
|
| 65 | - * |
|
| 66 | - * @param string $key WordLift's key to validate. |
|
| 67 | - * |
|
| 68 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 69 | - * @since 3.9.0 |
|
| 70 | - * |
|
| 71 | - */ |
|
| 72 | - public function get_account_info( $key ) { |
|
| 73 | - |
|
| 74 | - $this->log->debug( 'Validating key...' ); |
|
| 75 | - |
|
| 76 | - return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 77 | - 'Authorization' => "Key $key", |
|
| 78 | - ) )->get_response(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Check if key is valid |
|
| 83 | - * |
|
| 84 | - * @param $key string |
|
| 85 | - * |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function is_key_valid( $key ) { |
|
| 89 | - |
|
| 90 | - $response = $this->get_account_info( $key ); |
|
| 91 | - |
|
| 92 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 93 | - return false; |
|
| 94 | - } |
|
| 95 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 96 | - |
|
| 97 | - $url = $res_body['url']; |
|
| 98 | - |
|
| 99 | - // Considering that production URL may be filtered. |
|
| 100 | - $home_url = get_option( 'home' ); |
|
| 101 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 102 | - if ( is_null( $url ) || $url === $site_url ) { |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 111 | - * |
|
| 112 | - * @since 3.9.0 |
|
| 113 | - */ |
|
| 114 | - public function validate_key() { |
|
| 115 | - |
|
| 116 | - // Ensure we don't have garbage before us. |
|
| 117 | - ob_clean(); |
|
| 118 | - |
|
| 119 | - // Check if we have a key. |
|
| 120 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 121 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); |
|
| 125 | - |
|
| 126 | - // If we got an error, return invalid. |
|
| 127 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 128 | - wp_send_json_success( array( |
|
| 129 | - 'valid' => false, |
|
| 130 | - 'message' => '', |
|
| 131 | - 'response' => $response, |
|
| 132 | - 'api_url' => Default_Api_Service::get_instance()->get_base_url() |
|
| 133 | - ) ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 137 | - |
|
| 138 | - // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 139 | - $url = $res_body['url']; |
|
| 140 | - |
|
| 141 | - // Considering that production URL may be filtered. |
|
| 142 | - $home_url = get_option( 'home' ); |
|
| 143 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 144 | - |
|
| 145 | - // If the URL isn't set or matches, then it's valid. |
|
| 146 | - if ( is_null( $url ) || $url === $site_url ) { |
|
| 147 | - // Invalidate the cache key |
|
| 148 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 149 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 153 | - if ( $url !== $site_url ) { |
|
| 154 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 155 | - wp_send_json_success( array( |
|
| 156 | - 'valid' => false, |
|
| 157 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 158 | - ) ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Set a response with valid set to true or false according to the key validity with message. |
|
| 162 | - wp_send_json_success( array( |
|
| 163 | - 'valid' => false, |
|
| 164 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 165 | - ) ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 170 | - * |
|
| 171 | - */ |
|
| 172 | - public function wl_load_plugin() { |
|
| 173 | - |
|
| 174 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 175 | - $home_url = get_option( 'home' ); |
|
| 176 | - |
|
| 177 | - if ( ! $wl_blog_url ) { |
|
| 178 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 179 | - } else if ( $wl_blog_url !== $home_url ) { |
|
| 180 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 181 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 182 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 189 | - * |
|
| 190 | - */ |
|
| 191 | - public function wl_key_update_notice() { |
|
| 192 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 193 | - ?> |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.14.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var Ttl_Cache |
|
| 34 | + */ |
|
| 35 | + private $ttl_cache_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.14.0 |
|
| 41 | + */ |
|
| 42 | + public function __construct() { |
|
| 43 | + |
|
| 44 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 45 | + |
|
| 46 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 47 | + /** |
|
| 48 | + * Filter: wl_feature__enable__notices. |
|
| 49 | + * |
|
| 50 | + * @param bool whether notices need to be enabled or not. |
|
| 51 | + * |
|
| 52 | + * @return bool |
|
| 53 | + * @since 3.27.6 |
|
| 54 | + */ |
|
| 55 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 56 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Validate the provided key. |
|
| 65 | + * |
|
| 66 | + * @param string $key WordLift's key to validate. |
|
| 67 | + * |
|
| 68 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 69 | + * @since 3.9.0 |
|
| 70 | + * |
|
| 71 | + */ |
|
| 72 | + public function get_account_info( $key ) { |
|
| 73 | + |
|
| 74 | + $this->log->debug( 'Validating key...' ); |
|
| 75 | + |
|
| 76 | + return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 77 | + 'Authorization' => "Key $key", |
|
| 78 | + ) )->get_response(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Check if key is valid |
|
| 83 | + * |
|
| 84 | + * @param $key string |
|
| 85 | + * |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function is_key_valid( $key ) { |
|
| 89 | + |
|
| 90 | + $response = $this->get_account_info( $key ); |
|
| 91 | + |
|
| 92 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 95 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 96 | + |
|
| 97 | + $url = $res_body['url']; |
|
| 98 | + |
|
| 99 | + // Considering that production URL may be filtered. |
|
| 100 | + $home_url = get_option( 'home' ); |
|
| 101 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 102 | + if ( is_null( $url ) || $url === $site_url ) { |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 111 | + * |
|
| 112 | + * @since 3.9.0 |
|
| 113 | + */ |
|
| 114 | + public function validate_key() { |
|
| 115 | + |
|
| 116 | + // Ensure we don't have garbage before us. |
|
| 117 | + ob_clean(); |
|
| 118 | + |
|
| 119 | + // Check if we have a key. |
|
| 120 | + if ( ! isset( $_POST['key'] ) ) { |
|
| 121 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); |
|
| 125 | + |
|
| 126 | + // If we got an error, return invalid. |
|
| 127 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 128 | + wp_send_json_success( array( |
|
| 129 | + 'valid' => false, |
|
| 130 | + 'message' => '', |
|
| 131 | + 'response' => $response, |
|
| 132 | + 'api_url' => Default_Api_Service::get_instance()->get_base_url() |
|
| 133 | + ) ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 137 | + |
|
| 138 | + // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 139 | + $url = $res_body['url']; |
|
| 140 | + |
|
| 141 | + // Considering that production URL may be filtered. |
|
| 142 | + $home_url = get_option( 'home' ); |
|
| 143 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 144 | + |
|
| 145 | + // If the URL isn't set or matches, then it's valid. |
|
| 146 | + if ( is_null( $url ) || $url === $site_url ) { |
|
| 147 | + // Invalidate the cache key |
|
| 148 | + $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 149 | + wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 153 | + if ( $url !== $site_url ) { |
|
| 154 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 155 | + wp_send_json_success( array( |
|
| 156 | + 'valid' => false, |
|
| 157 | + 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 158 | + ) ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Set a response with valid set to true or false according to the key validity with message. |
|
| 162 | + wp_send_json_success( array( |
|
| 163 | + 'valid' => false, |
|
| 164 | + 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 165 | + ) ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 170 | + * |
|
| 171 | + */ |
|
| 172 | + public function wl_load_plugin() { |
|
| 173 | + |
|
| 174 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 175 | + $home_url = get_option( 'home' ); |
|
| 176 | + |
|
| 177 | + if ( ! $wl_blog_url ) { |
|
| 178 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 179 | + } else if ( $wl_blog_url !== $home_url ) { |
|
| 180 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 181 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 182 | + set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 189 | + * |
|
| 190 | + */ |
|
| 191 | + public function wl_key_update_notice() { |
|
| 192 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 193 | + ?> |
|
| 194 | 194 | <div class="updated notice is-dismissible error"> |
| 195 | 195 | <p><?php esc_html_e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
| 196 | 196 | </div> |
| 197 | 197 | <?php |
| 198 | - } |
|
| 199 | - } |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | 200 | } |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function __construct() { |
| 43 | 43 | |
| 44 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 44 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service'); |
|
| 45 | 45 | |
| 46 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 46 | + add_action('admin_init', array($this, 'wl_load_plugin')); |
|
| 47 | 47 | /** |
| 48 | 48 | * Filter: wl_feature__enable__notices. |
| 49 | 49 | * |
@@ -52,11 +52,11 @@ discard block |
||
| 52 | 52 | * @return bool |
| 53 | 53 | * @since 3.27.6 |
| 54 | 54 | */ |
| 55 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 56 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 55 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 56 | + add_action('admin_notices', array($this, 'wl_key_update_notice')); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 59 | + $this->ttl_cache_service = new Ttl_Cache('key-validation-notification'); |
|
| 60 | 60 | |
| 61 | 61 | } |
| 62 | 62 | |
@@ -69,13 +69,13 @@ discard block |
||
| 69 | 69 | * @since 3.9.0 |
| 70 | 70 | * |
| 71 | 71 | */ |
| 72 | - public function get_account_info( $key ) { |
|
| 72 | + public function get_account_info($key) { |
|
| 73 | 73 | |
| 74 | - $this->log->debug( 'Validating key...' ); |
|
| 74 | + $this->log->debug('Validating key...'); |
|
| 75 | 75 | |
| 76 | - return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 76 | + return Default_Api_Service::get_instance()->get('/accounts/info', array( |
|
| 77 | 77 | 'Authorization' => "Key $key", |
| 78 | - ) )->get_response(); |
|
| 78 | + ))->get_response(); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -85,21 +85,21 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return bool |
| 87 | 87 | */ |
| 88 | - public function is_key_valid( $key ) { |
|
| 88 | + public function is_key_valid($key) { |
|
| 89 | 89 | |
| 90 | - $response = $this->get_account_info( $key ); |
|
| 90 | + $response = $this->get_account_info($key); |
|
| 91 | 91 | |
| 92 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 92 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
| 93 | 93 | return false; |
| 94 | 94 | } |
| 95 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 95 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
| 96 | 96 | |
| 97 | 97 | $url = $res_body['url']; |
| 98 | 98 | |
| 99 | 99 | // Considering that production URL may be filtered. |
| 100 | - $home_url = get_option( 'home' ); |
|
| 101 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 102 | - if ( is_null( $url ) || $url === $site_url ) { |
|
| 100 | + $home_url = get_option('home'); |
|
| 101 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
| 102 | + if (is_null($url) || $url === $site_url) { |
|
| 103 | 103 | return true; |
| 104 | 104 | } |
| 105 | 105 | |
@@ -117,52 +117,52 @@ discard block |
||
| 117 | 117 | ob_clean(); |
| 118 | 118 | |
| 119 | 119 | // Check if we have a key. |
| 120 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 121 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 120 | + if ( ! isset($_POST['key'])) { |
|
| 121 | + wp_send_json_error('The key parameter is required.'); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); |
|
| 124 | + $response = $this->get_account_info(sanitize_text_field(wp_unslash((string) $_POST['key']))); |
|
| 125 | 125 | |
| 126 | 126 | // If we got an error, return invalid. |
| 127 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 128 | - wp_send_json_success( array( |
|
| 127 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
| 128 | + wp_send_json_success(array( |
|
| 129 | 129 | 'valid' => false, |
| 130 | 130 | 'message' => '', |
| 131 | 131 | 'response' => $response, |
| 132 | 132 | 'api_url' => Default_Api_Service::get_instance()->get_base_url() |
| 133 | - ) ); |
|
| 133 | + )); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 136 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
| 137 | 137 | |
| 138 | 138 | // The URL stored in WLS. If this is the initial install the URL may be null. |
| 139 | 139 | $url = $res_body['url']; |
| 140 | 140 | |
| 141 | 141 | // Considering that production URL may be filtered. |
| 142 | - $home_url = get_option( 'home' ); |
|
| 143 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 142 | + $home_url = get_option('home'); |
|
| 143 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
| 144 | 144 | |
| 145 | 145 | // If the URL isn't set or matches, then it's valid. |
| 146 | - if ( is_null( $url ) || $url === $site_url ) { |
|
| 146 | + if (is_null($url) || $url === $site_url) { |
|
| 147 | 147 | // Invalidate the cache key |
| 148 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 149 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 148 | + $this->ttl_cache_service->delete('is_key_valid'); |
|
| 149 | + wp_send_json_success(array('valid' => true, 'message' => '')); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // If the URL doesn't match it means that this key has been configured elsewhere already. |
| 153 | - if ( $url !== $site_url ) { |
|
| 154 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 155 | - wp_send_json_success( array( |
|
| 153 | + if ($url !== $site_url) { |
|
| 154 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 155 | + wp_send_json_success(array( |
|
| 156 | 156 | 'valid' => false, |
| 157 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 158 | - ) ); |
|
| 157 | + 'message' => __('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift'), |
|
| 158 | + )); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Set a response with valid set to true or false according to the key validity with message. |
| 162 | - wp_send_json_success( array( |
|
| 162 | + wp_send_json_success(array( |
|
| 163 | 163 | 'valid' => false, |
| 164 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 165 | - ) ); |
|
| 164 | + 'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'), |
|
| 165 | + )); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -171,15 +171,15 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | public function wl_load_plugin() { |
| 173 | 173 | |
| 174 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 175 | - $home_url = get_option( 'home' ); |
|
| 174 | + $wl_blog_url = get_option('_wl_blog_url'); |
|
| 175 | + $home_url = get_option('home'); |
|
| 176 | 176 | |
| 177 | - if ( ! $wl_blog_url ) { |
|
| 178 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 179 | - } else if ( $wl_blog_url !== $home_url ) { |
|
| 180 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 181 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 182 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 177 | + if ( ! $wl_blog_url) { |
|
| 178 | + update_option('_wl_blog_url', $home_url, true); |
|
| 179 | + } else if ($wl_blog_url !== $home_url) { |
|
| 180 | + update_option('_wl_blog_url', $home_url, true); |
|
| 181 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 182 | + set_transient('wl-key-error-msg', __("Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift'), 10); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | } |
@@ -189,10 +189,10 @@ discard block |
||
| 189 | 189 | * |
| 190 | 190 | */ |
| 191 | 191 | public function wl_key_update_notice() { |
| 192 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 192 | + if (get_transient('wl-key-error-msg')) { |
|
| 193 | 193 | ?> |
| 194 | 194 | <div class="updated notice is-dismissible error"> |
| 195 | - <p><?php esc_html_e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
|
| 195 | + <p><?php esc_html_e(get_transient('wl-key-error-msg'), 'wordlift'); ?></p> |
|
| 196 | 196 | </div> |
| 197 | 197 | <?php |
| 198 | 198 | } |
@@ -7,85 +7,85 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_UI_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The button element HTML code. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - */ |
|
| 15 | - const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>'; |
|
| 10 | + /** |
|
| 11 | + * The button element HTML code. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + */ |
|
| 15 | + const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The template HTML code. |
|
| 19 | - * |
|
| 20 | - * @since 3.2.0 |
|
| 21 | - */ |
|
| 22 | - const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>'; |
|
| 17 | + /** |
|
| 18 | + * The template HTML code. |
|
| 19 | + * |
|
| 20 | + * @since 3.2.0 |
|
| 21 | + */ |
|
| 22 | + const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the button HTML. |
|
| 26 | - * |
|
| 27 | - * @param string $element_id The button element id. |
|
| 28 | - * @param string $label The button (translated) label. |
|
| 29 | - * |
|
| 30 | - * @return string The button HTML code. |
|
| 31 | - * @since 3.2.0 |
|
| 32 | - * |
|
| 33 | - */ |
|
| 34 | - public static function get_button_html( $element_id, $label ) { |
|
| 24 | + /** |
|
| 25 | + * Get the button HTML. |
|
| 26 | + * |
|
| 27 | + * @param string $element_id The button element id. |
|
| 28 | + * @param string $label The button (translated) label. |
|
| 29 | + * |
|
| 30 | + * @return string The button HTML code. |
|
| 31 | + * @since 3.2.0 |
|
| 32 | + * |
|
| 33 | + */ |
|
| 34 | + public static function get_button_html( $element_id, $label ) { |
|
| 35 | 35 | |
| 36 | - return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 37 | - } |
|
| 36 | + return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Echo the button HTML. |
|
| 41 | - * |
|
| 42 | - * @param string $element_id The button element id. |
|
| 43 | - * @param string $label The button (translated) label. |
|
| 44 | - * |
|
| 45 | - * @return string The button HTML code. |
|
| 46 | - * @since 3.2.0 |
|
| 47 | - * |
|
| 48 | - */ |
|
| 49 | - public static function print_button( $element_id, $label ) { |
|
| 39 | + /** |
|
| 40 | + * Echo the button HTML. |
|
| 41 | + * |
|
| 42 | + * @param string $element_id The button element id. |
|
| 43 | + * @param string $label The button (translated) label. |
|
| 44 | + * |
|
| 45 | + * @return string The button HTML code. |
|
| 46 | + * @since 3.2.0 |
|
| 47 | + * |
|
| 48 | + */ |
|
| 49 | + public static function print_button( $element_id, $label ) { |
|
| 50 | 50 | |
| 51 | - echo wp_kses( self::get_button_html( $element_id, $label ), array( |
|
| 52 | - 'a' => array( |
|
| 53 | - 'id' => array(), |
|
| 54 | - 'class' => array() |
|
| 55 | - ) |
|
| 56 | - ) ); |
|
| 51 | + echo wp_kses( self::get_button_html( $element_id, $label ), array( |
|
| 52 | + 'a' => array( |
|
| 53 | + 'id' => array(), |
|
| 54 | + 'class' => array() |
|
| 55 | + ) |
|
| 56 | + ) ); |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Get the HTML code for a template tag. |
|
| 62 | - * |
|
| 63 | - * @param string $element_id The element id. |
|
| 64 | - * @param string $body The element content. |
|
| 65 | - * |
|
| 66 | - * @return string The HTML code. |
|
| 67 | - * @since 3.2.0 |
|
| 68 | - * |
|
| 69 | - */ |
|
| 70 | - public static function get_template_html( $element_id, $body ) { |
|
| 60 | + /** |
|
| 61 | + * Get the HTML code for a template tag. |
|
| 62 | + * |
|
| 63 | + * @param string $element_id The element id. |
|
| 64 | + * @param string $body The element content. |
|
| 65 | + * |
|
| 66 | + * @return string The HTML code. |
|
| 67 | + * @since 3.2.0 |
|
| 68 | + * |
|
| 69 | + */ |
|
| 70 | + public static function get_template_html( $element_id, $body ) { |
|
| 71 | 71 | |
| 72 | - return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 73 | - } |
|
| 72 | + return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Echo the HTML code for a template tag. |
|
| 77 | - * |
|
| 78 | - * @param string $element_id The element id. |
|
| 79 | - * @param string $body The element content. |
|
| 80 | - * |
|
| 81 | - * @return string øThe HTML code. |
|
| 82 | - * @since 3.2.0 |
|
| 83 | - * |
|
| 84 | - */ |
|
| 85 | - public static function print_template( $element_id, $body ) { |
|
| 75 | + /** |
|
| 76 | + * Echo the HTML code for a template tag. |
|
| 77 | + * |
|
| 78 | + * @param string $element_id The element id. |
|
| 79 | + * @param string $body The element content. |
|
| 80 | + * |
|
| 81 | + * @return string øThe HTML code. |
|
| 82 | + * @since 3.2.0 |
|
| 83 | + * |
|
| 84 | + */ |
|
| 85 | + public static function print_template( $element_id, $body ) { |
|
| 86 | 86 | |
| 87 | - echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 87 | + echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 88 | 88 | |
| 89 | - } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | } |
@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | * @since 3.2.0 |
| 32 | 32 | * |
| 33 | 33 | */ |
| 34 | - public static function get_button_html( $element_id, $label ) { |
|
| 34 | + public static function get_button_html($element_id, $label) { |
|
| 35 | 35 | |
| 36 | - return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 36 | + return sprintf(self::BUTTON_HTML, $element_id, esc_html($label)); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | * @since 3.2.0 |
| 47 | 47 | * |
| 48 | 48 | */ |
| 49 | - public static function print_button( $element_id, $label ) { |
|
| 49 | + public static function print_button($element_id, $label) { |
|
| 50 | 50 | |
| 51 | - echo wp_kses( self::get_button_html( $element_id, $label ), array( |
|
| 51 | + echo wp_kses(self::get_button_html($element_id, $label), array( |
|
| 52 | 52 | 'a' => array( |
| 53 | 53 | 'id' => array(), |
| 54 | 54 | 'class' => array() |
| 55 | 55 | ) |
| 56 | - ) ); |
|
| 56 | + )); |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * @since 3.2.0 |
| 68 | 68 | * |
| 69 | 69 | */ |
| 70 | - public static function get_template_html( $element_id, $body ) { |
|
| 70 | + public static function get_template_html($element_id, $body) { |
|
| 71 | 71 | |
| 72 | - return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 72 | + return sprintf(self::TEMPLATE_HTML, $element_id, $body); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | * @since 3.2.0 |
| 83 | 83 | * |
| 84 | 84 | */ |
| 85 | - public static function print_template( $element_id, $body ) { |
|
| 85 | + public static function print_template($element_id, $body) { |
|
| 86 | 86 | |
| 87 | - echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 87 | + echo(self::get_template_html($element_id, $body)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 88 | 88 | |
| 89 | 89 | } |
| 90 | 90 | |
@@ -14,135 +14,135 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Batch_Operation_Ajax_Adapter { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The access levels. |
|
| 19 | - * |
|
| 20 | - * @since 3.20.0 |
|
| 21 | - */ |
|
| 22 | - const ACCESS_ANONYMOUS = 1; |
|
| 23 | - const ACCESS_ADMIN = 2; |
|
| 24 | - const ACCESS_ALL = 3; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.20.0 |
|
| 30 | - * @access private |
|
| 31 | - * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 32 | - */ |
|
| 33 | - private $operation; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The ajax action name. |
|
| 37 | - * |
|
| 38 | - * @since 3.20.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var string $action The ajax action name. |
|
| 41 | - */ |
|
| 42 | - private $action; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Wordlift_Batch_Operation_Ajax_Adapter constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Wordlift_Batch_Operation_Interface $operation The batch operation. |
|
| 48 | - * @param string $action The action name. |
|
| 49 | - * @param int $access The access level. |
|
| 50 | - */ |
|
| 51 | - public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 52 | - |
|
| 53 | - $this->operation = $operation; |
|
| 54 | - |
|
| 55 | - if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | - add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | - add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 58 | - |
|
| 59 | - // Add the nonce for the `schemaorg_sync` action. |
|
| 60 | - add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | - add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | - add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $this->action = $action; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Hook to `wl_admin_settings`, adds the nonce. |
|
| 73 | - * |
|
| 74 | - * @param array $params An array of settings. |
|
| 75 | - * |
|
| 76 | - * @return array The updated array of settings. |
|
| 77 | - * @since 3.20.0 |
|
| 78 | - * |
|
| 79 | - */ |
|
| 80 | - public function add_nonce( $params ) { |
|
| 81 | - |
|
| 82 | - return array_merge( $params, array( |
|
| 83 | - "{$this->action}_nonce" => $this->create_nonce(), |
|
| 84 | - ) ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Process the requested operation. |
|
| 89 | - * |
|
| 90 | - * @since 3.20.0 |
|
| 91 | - */ |
|
| 92 | - public function process() { |
|
| 93 | - $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 94 | - // Validate the nonce. |
|
| 95 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | - $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 101 | - |
|
| 102 | - // Run the batch operation. |
|
| 103 | - $result = $this->operation->process( $offset, $limit ); |
|
| 104 | - |
|
| 105 | - // Send the results along with a potentially updated nonce. |
|
| 106 | - wp_send_json_success( array_merge( $result, array( |
|
| 107 | - '_nonce' => $this->create_nonce(), |
|
| 108 | - ) ) ); |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Count the number of elements that would be affected by the operation. |
|
| 114 | - * |
|
| 115 | - * @since 3.20.0 |
|
| 116 | - */ |
|
| 117 | - public function count() { |
|
| 118 | - |
|
| 119 | - // Validate the nonce. |
|
| 120 | - $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 121 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Run the batch operation. |
|
| 126 | - $result = $this->operation->count(); |
|
| 127 | - |
|
| 128 | - // Send the results along with a potentially updated nonce. |
|
| 129 | - wp_send_json_success( array( |
|
| 130 | - 'count' => $result, |
|
| 131 | - '_nonce' => $this->create_nonce(), |
|
| 132 | - ) ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Create a nonce for the ajax operation. |
|
| 138 | - * |
|
| 139 | - * @return string The nonce. |
|
| 140 | - * @since 3.20.0 |
|
| 141 | - * |
|
| 142 | - */ |
|
| 143 | - public function create_nonce() { |
|
| 144 | - |
|
| 145 | - return wp_create_nonce( $this->action ); |
|
| 146 | - } |
|
| 17 | + /** |
|
| 18 | + * The access levels. |
|
| 19 | + * |
|
| 20 | + * @since 3.20.0 |
|
| 21 | + */ |
|
| 22 | + const ACCESS_ANONYMOUS = 1; |
|
| 23 | + const ACCESS_ADMIN = 2; |
|
| 24 | + const ACCESS_ALL = 3; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.20.0 |
|
| 30 | + * @access private |
|
| 31 | + * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 32 | + */ |
|
| 33 | + private $operation; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The ajax action name. |
|
| 37 | + * |
|
| 38 | + * @since 3.20.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var string $action The ajax action name. |
|
| 41 | + */ |
|
| 42 | + private $action; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Wordlift_Batch_Operation_Ajax_Adapter constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Wordlift_Batch_Operation_Interface $operation The batch operation. |
|
| 48 | + * @param string $action The action name. |
|
| 49 | + * @param int $access The access level. |
|
| 50 | + */ |
|
| 51 | + public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 52 | + |
|
| 53 | + $this->operation = $operation; |
|
| 54 | + |
|
| 55 | + if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | + add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | + add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 58 | + |
|
| 59 | + // Add the nonce for the `schemaorg_sync` action. |
|
| 60 | + add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | + add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | + add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $this->action = $action; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Hook to `wl_admin_settings`, adds the nonce. |
|
| 73 | + * |
|
| 74 | + * @param array $params An array of settings. |
|
| 75 | + * |
|
| 76 | + * @return array The updated array of settings. |
|
| 77 | + * @since 3.20.0 |
|
| 78 | + * |
|
| 79 | + */ |
|
| 80 | + public function add_nonce( $params ) { |
|
| 81 | + |
|
| 82 | + return array_merge( $params, array( |
|
| 83 | + "{$this->action}_nonce" => $this->create_nonce(), |
|
| 84 | + ) ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Process the requested operation. |
|
| 89 | + * |
|
| 90 | + * @since 3.20.0 |
|
| 91 | + */ |
|
| 92 | + public function process() { |
|
| 93 | + $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 94 | + // Validate the nonce. |
|
| 95 | + if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | + wp_send_json_error( 'Invalid nonce.' ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | + $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 101 | + |
|
| 102 | + // Run the batch operation. |
|
| 103 | + $result = $this->operation->process( $offset, $limit ); |
|
| 104 | + |
|
| 105 | + // Send the results along with a potentially updated nonce. |
|
| 106 | + wp_send_json_success( array_merge( $result, array( |
|
| 107 | + '_nonce' => $this->create_nonce(), |
|
| 108 | + ) ) ); |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Count the number of elements that would be affected by the operation. |
|
| 114 | + * |
|
| 115 | + * @since 3.20.0 |
|
| 116 | + */ |
|
| 117 | + public function count() { |
|
| 118 | + |
|
| 119 | + // Validate the nonce. |
|
| 120 | + $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 121 | + if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | + wp_send_json_error( 'Invalid nonce.' ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Run the batch operation. |
|
| 126 | + $result = $this->operation->count(); |
|
| 127 | + |
|
| 128 | + // Send the results along with a potentially updated nonce. |
|
| 129 | + wp_send_json_success( array( |
|
| 130 | + 'count' => $result, |
|
| 131 | + '_nonce' => $this->create_nonce(), |
|
| 132 | + ) ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Create a nonce for the ajax operation. |
|
| 138 | + * |
|
| 139 | + * @return string The nonce. |
|
| 140 | + * @since 3.20.0 |
|
| 141 | + * |
|
| 142 | + */ |
|
| 143 | + public function create_nonce() { |
|
| 144 | + |
|
| 145 | + return wp_create_nonce( $this->action ); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | } |
@@ -48,21 +48,21 @@ discard block |
||
| 48 | 48 | * @param string $action The action name. |
| 49 | 49 | * @param int $access The access level. |
| 50 | 50 | */ |
| 51 | - public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 51 | + public function __construct($operation, $action, $access = self::ACCESS_ADMIN) { |
|
| 52 | 52 | |
| 53 | 53 | $this->operation = $operation; |
| 54 | 54 | |
| 55 | - if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | - add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | - add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 55 | + if ($access & self::ACCESS_ADMIN) { |
|
| 56 | + add_action("wp_ajax_$action", array($this, 'process')); |
|
| 57 | + add_action("wp_ajax_{$action}_count", array($this, 'count')); |
|
| 58 | 58 | |
| 59 | 59 | // Add the nonce for the `schemaorg_sync` action. |
| 60 | - add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 60 | + add_filter('wl_admin_settings', array($this, 'add_nonce')); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | - add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | - add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 63 | + if ($access & self::ACCESS_ANONYMOUS) { |
|
| 64 | + add_action("wp_ajax_nopriv_$action", array($this, 'process')); |
|
| 65 | + add_action("wp_ajax_nopriv_{$action}_count", array($this, 'count')); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | $this->action = $action; |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | * @since 3.20.0 |
| 78 | 78 | * |
| 79 | 79 | */ |
| 80 | - public function add_nonce( $params ) { |
|
| 80 | + public function add_nonce($params) { |
|
| 81 | 81 | |
| 82 | - return array_merge( $params, array( |
|
| 82 | + return array_merge($params, array( |
|
| 83 | 83 | "{$this->action}_nonce" => $this->create_nonce(), |
| 84 | - ) ); |
|
| 84 | + )); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -90,22 +90,22 @@ discard block |
||
| 90 | 90 | * @since 3.20.0 |
| 91 | 91 | */ |
| 92 | 92 | public function process() { |
| 93 | - $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 93 | + $nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : ''; |
|
| 94 | 94 | // Validate the nonce. |
| 95 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 95 | + if ( ! wp_verify_nonce($nonce, $this->action)) { |
|
| 96 | + wp_send_json_error('Invalid nonce.'); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | - $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 99 | + $offset = isset($_POST['offset']) ? (int) $_POST['offset'] : 0; |
|
| 100 | + $limit = isset($_POST['limit']) ? (int) $_POST['limit'] : 10; |
|
| 101 | 101 | |
| 102 | 102 | // Run the batch operation. |
| 103 | - $result = $this->operation->process( $offset, $limit ); |
|
| 103 | + $result = $this->operation->process($offset, $limit); |
|
| 104 | 104 | |
| 105 | 105 | // Send the results along with a potentially updated nonce. |
| 106 | - wp_send_json_success( array_merge( $result, array( |
|
| 106 | + wp_send_json_success(array_merge($result, array( |
|
| 107 | 107 | '_nonce' => $this->create_nonce(), |
| 108 | - ) ) ); |
|
| 108 | + ))); |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
@@ -117,19 +117,19 @@ discard block |
||
| 117 | 117 | public function count() { |
| 118 | 118 | |
| 119 | 119 | // Validate the nonce. |
| 120 | - $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : ''; |
|
| 121 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 120 | + $nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : ''; |
|
| 121 | + if ( ! wp_verify_nonce($nonce, $this->action)) { |
|
| 122 | + wp_send_json_error('Invalid nonce.'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Run the batch operation. |
| 126 | 126 | $result = $this->operation->count(); |
| 127 | 127 | |
| 128 | 128 | // Send the results along with a potentially updated nonce. |
| 129 | - wp_send_json_success( array( |
|
| 129 | + wp_send_json_success(array( |
|
| 130 | 130 | 'count' => $result, |
| 131 | 131 | '_nonce' => $this->create_nonce(), |
| 132 | - ) ); |
|
| 132 | + )); |
|
| 133 | 133 | |
| 134 | 134 | } |
| 135 | 135 | |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | public function create_nonce() { |
| 144 | 144 | |
| 145 | - return wp_create_nonce( $this->action ); |
|
| 145 | + return wp_create_nonce($this->action); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | } |
@@ -16,344 +16,344 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Timeline_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.1.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
| 30 | - * and used by a filter. |
|
| 31 | - * |
|
| 32 | - * @since 3.7.0 |
|
| 33 | - * @access private |
|
| 34 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 35 | - */ |
|
| 36 | - private $excerpt_length; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
| 40 | - * |
|
| 41 | - * @since 3.1.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 44 | - */ |
|
| 45 | - private static $instance; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Create a Wordlift_Timeline_Service instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.1.0 |
|
| 51 | - * |
|
| 52 | - */ |
|
| 53 | - public function __construct() { |
|
| 54 | - |
|
| 55 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 56 | - |
|
| 57 | - self::$instance = $this; |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 63 | - * |
|
| 64 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 65 | - * @since 3.1.0 |
|
| 66 | - * |
|
| 67 | - */ |
|
| 68 | - public static function get_instance() { |
|
| 69 | - |
|
| 70 | - return self::$instance; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Retrieve timeline events and output them in JSON. |
|
| 75 | - * |
|
| 76 | - * @since 3.1.0 |
|
| 77 | - */ |
|
| 78 | - public function ajax_timeline() { |
|
| 79 | - |
|
| 80 | - // Get the ID of the post who requested the timeline. |
|
| 81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 82 | - |
|
| 83 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
| 84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Retrieve timeline events. |
|
| 90 | - * |
|
| 91 | - * @param int $post_id The post ID. |
|
| 92 | - * |
|
| 93 | - * @return array An array of event posts. |
|
| 94 | - * @since 3.1.0 |
|
| 95 | - * |
|
| 96 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - public function get_events( $post_id = null ) { |
|
| 100 | - |
|
| 101 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 102 | - // posts if no post has been specified. |
|
| 103 | - $ids = ( is_numeric( $post_id ) |
|
| 104 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 106 | - |
|
| 107 | - // Add the post itself if it's an entity. |
|
| 108 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 109 | - $ids[] = $post_id; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // If there's no entities, return an empty array right away. |
|
| 113 | - if ( 0 === sizeof( $ids ) ) { |
|
| 114 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 115 | - |
|
| 116 | - return array(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 120 | - |
|
| 121 | - $args = array( |
|
| 122 | - 'post__in' => $ids, |
|
| 123 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 124 | - 'post_status' => 'publish', |
|
| 125 | - 'posts_per_page' => - 1, |
|
| 126 | - 'meta_query' => array( |
|
| 127 | - 'relation' => 'AND', |
|
| 128 | - array( |
|
| 129 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 130 | - 'value' => null, |
|
| 131 | - 'compare' => '!=', |
|
| 132 | - ), |
|
| 133 | - array( |
|
| 134 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 135 | - 'value' => null, |
|
| 136 | - 'compare' => '!=', |
|
| 137 | - ), |
|
| 138 | - ), |
|
| 139 | - 'tax_query' => array( |
|
| 140 | - array( |
|
| 141 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 142 | - 'field' => 'slug', |
|
| 143 | - 'terms' => 'event', |
|
| 144 | - ), |
|
| 145 | - ), |
|
| 146 | - 'orderby' => 'meta_value', |
|
| 147 | - 'order' => 'DESC', |
|
| 148 | - 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - return get_posts( $args ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Convert timeline events to JSON. This function sets the global post in order |
|
| 156 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 157 | - * we're not taking care of restoring any previous post: there isn't any. |
|
| 158 | - * |
|
| 159 | - * @param array $posts An array of posts. |
|
| 160 | - * |
|
| 161 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 162 | - * @since 3.1.0 |
|
| 163 | - * |
|
| 164 | - */ |
|
| 165 | - public function to_json( $posts ) { |
|
| 166 | - |
|
| 167 | - // If there are no events, return empty JSON |
|
| 168 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 169 | - return ''; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 173 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
| 174 | - |
|
| 175 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
| 176 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
| 177 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 178 | - |
|
| 179 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 180 | - // a link to the post itself. |
|
| 181 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 182 | - |
|
| 183 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 184 | - $start_at_slide = 0; |
|
| 185 | - $event_index = - 1; |
|
| 186 | - $now = time(); |
|
| 187 | - |
|
| 188 | - // Prepare the timeline variable. |
|
| 189 | - $timeline = array(); |
|
| 190 | - |
|
| 191 | - // Populate the arrays. |
|
| 192 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 193 | - |
|
| 194 | - // Get the start and end dates. |
|
| 195 | - // We have to remove double quotes from date to make timeline work properly |
|
| 196 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | - $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 198 | - |
|
| 199 | - // Set the starting slide. |
|
| 200 | - $event_index ++; |
|
| 201 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 202 | - $start_at_slide = $event_index; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - // Load thumbnail |
|
| 206 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 209 | - ) { |
|
| 210 | - |
|
| 211 | - // Set the thumbnail URL. |
|
| 212 | - if ( 'background' === $display_images_as ) { |
|
| 213 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 215 | - } else { |
|
| 216 | - $date['media'] = array( |
|
| 217 | - 'url' => $attachment[0], |
|
| 218 | - 'thumbnail' => $attachment[0], |
|
| 219 | - ); |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
| 224 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 226 | - |
|
| 227 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 228 | - |
|
| 229 | - $more_link_text = sprintf( |
|
| 230 | - '<span aria-label="%1$s">%2$s</span>', |
|
| 231 | - sprintf( |
|
| 232 | - /* translators: %s: Name of current post */ |
|
| 233 | - __( 'Continue reading %s' ), |
|
| 234 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 235 | - ), |
|
| 236 | - __( '(more…)' ) |
|
| 237 | - ); |
|
| 238 | - |
|
| 239 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 240 | - $date['text'] = array( |
|
| 241 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 242 | - ); |
|
| 243 | - |
|
| 244 | - // If we have an excerpt, set it. |
|
| 245 | - if ( 0 < $excerpt_length ) { |
|
| 246 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $date; |
|
| 250 | - |
|
| 251 | - }, $posts ); |
|
| 252 | - |
|
| 253 | - // Finally remove the excerpt filter. |
|
| 254 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 255 | - |
|
| 256 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 257 | - return array( |
|
| 258 | - 'timeline' => $timeline, |
|
| 259 | - 'start_at_slide' => $start_at_slide, |
|
| 260 | - ); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
| 265 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 266 | - * |
|
| 267 | - * @param string $excerpt_more The excerpt more preset. |
|
| 268 | - * |
|
| 269 | - * @return string An empty string. |
|
| 270 | - * @since 3.7.0 |
|
| 271 | - * |
|
| 272 | - */ |
|
| 273 | - public function excerpt_more( $excerpt_more ) { |
|
| 274 | - |
|
| 275 | - return ''; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 280 | - * how many words to return according to the client setting. |
|
| 281 | - * |
|
| 282 | - * @param int $length The preset number of words. |
|
| 283 | - * |
|
| 284 | - * @return int The number of words for the preset. |
|
| 285 | - * @since 3.7.0 |
|
| 286 | - * |
|
| 287 | - */ |
|
| 288 | - public function excerpt_length( $length ) { |
|
| 289 | - |
|
| 290 | - return $this->excerpt_length; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Convert the date to a date array. |
|
| 296 | - * |
|
| 297 | - * @param $value int A date value. |
|
| 298 | - * |
|
| 299 | - * @return array An array containing year, month and day values. |
|
| 300 | - * @since 3.7.0 |
|
| 301 | - * |
|
| 302 | - */ |
|
| 303 | - public static function date( $value ) { |
|
| 304 | - |
|
| 305 | - return array( |
|
| 306 | - 'year' => (int) date( 'Y', $value ), |
|
| 307 | - 'month' => (int) date( 'm', $value ), |
|
| 308 | - 'day' => (int) date( 'd', $value ), |
|
| 309 | - |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 315 | - * its specific function). |
|
| 316 | - * |
|
| 317 | - * @return array An array of post IDs. |
|
| 318 | - * @since 3.1.0 |
|
| 319 | - * |
|
| 320 | - */ |
|
| 321 | - public function get_all_related_to_last_50_published_posts() { |
|
| 322 | - |
|
| 323 | - // Global timeline. Get entities from the latest posts. |
|
| 324 | - $latest_posts_ids = get_posts( array( |
|
| 325 | - 'numberposts' => 50, |
|
| 326 | - 'fields' => 'ids', //only get post IDs |
|
| 327 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 328 | - 'tax_query' => array( |
|
| 329 | - 'relation' => 'OR', |
|
| 330 | - array( |
|
| 331 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 332 | - 'operator' => 'NOT EXISTS', |
|
| 333 | - ), |
|
| 334 | - array( |
|
| 335 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 336 | - 'field' => 'slug', |
|
| 337 | - 'terms' => 'article', |
|
| 338 | - ), |
|
| 339 | - ), |
|
| 340 | - 'post_status' => 'publish', |
|
| 341 | - ) ); |
|
| 342 | - |
|
| 343 | - if ( empty( $latest_posts_ids ) ) { |
|
| 344 | - // There are no posts. |
|
| 345 | - return array(); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - // Collect entities related to latest posts |
|
| 349 | - $entity_ids = array(); |
|
| 350 | - foreach ( $latest_posts_ids as $id ) { |
|
| 351 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 352 | - 'status' => 'publish', |
|
| 353 | - ) ) ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return $entity_ids; |
|
| 357 | - } |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.1.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
| 30 | + * and used by a filter. |
|
| 31 | + * |
|
| 32 | + * @since 3.7.0 |
|
| 33 | + * @access private |
|
| 34 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 35 | + */ |
|
| 36 | + private $excerpt_length; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
| 40 | + * |
|
| 41 | + * @since 3.1.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 44 | + */ |
|
| 45 | + private static $instance; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Create a Wordlift_Timeline_Service instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.1.0 |
|
| 51 | + * |
|
| 52 | + */ |
|
| 53 | + public function __construct() { |
|
| 54 | + |
|
| 55 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 56 | + |
|
| 57 | + self::$instance = $this; |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 63 | + * |
|
| 64 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 65 | + * @since 3.1.0 |
|
| 66 | + * |
|
| 67 | + */ |
|
| 68 | + public static function get_instance() { |
|
| 69 | + |
|
| 70 | + return self::$instance; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Retrieve timeline events and output them in JSON. |
|
| 75 | + * |
|
| 76 | + * @since 3.1.0 |
|
| 77 | + */ |
|
| 78 | + public function ajax_timeline() { |
|
| 79 | + |
|
| 80 | + // Get the ID of the post who requested the timeline. |
|
| 81 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 82 | + |
|
| 83 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
| 84 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Retrieve timeline events. |
|
| 90 | + * |
|
| 91 | + * @param int $post_id The post ID. |
|
| 92 | + * |
|
| 93 | + * @return array An array of event posts. |
|
| 94 | + * @since 3.1.0 |
|
| 95 | + * |
|
| 96 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + public function get_events( $post_id = null ) { |
|
| 100 | + |
|
| 101 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 102 | + // posts if no post has been specified. |
|
| 103 | + $ids = ( is_numeric( $post_id ) |
|
| 104 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
| 106 | + |
|
| 107 | + // Add the post itself if it's an entity. |
|
| 108 | + if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 109 | + $ids[] = $post_id; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // If there's no entities, return an empty array right away. |
|
| 113 | + if ( 0 === sizeof( $ids ) ) { |
|
| 114 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 115 | + |
|
| 116 | + return array(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 120 | + |
|
| 121 | + $args = array( |
|
| 122 | + 'post__in' => $ids, |
|
| 123 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 124 | + 'post_status' => 'publish', |
|
| 125 | + 'posts_per_page' => - 1, |
|
| 126 | + 'meta_query' => array( |
|
| 127 | + 'relation' => 'AND', |
|
| 128 | + array( |
|
| 129 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 130 | + 'value' => null, |
|
| 131 | + 'compare' => '!=', |
|
| 132 | + ), |
|
| 133 | + array( |
|
| 134 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 135 | + 'value' => null, |
|
| 136 | + 'compare' => '!=', |
|
| 137 | + ), |
|
| 138 | + ), |
|
| 139 | + 'tax_query' => array( |
|
| 140 | + array( |
|
| 141 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 142 | + 'field' => 'slug', |
|
| 143 | + 'terms' => 'event', |
|
| 144 | + ), |
|
| 145 | + ), |
|
| 146 | + 'orderby' => 'meta_value', |
|
| 147 | + 'order' => 'DESC', |
|
| 148 | + 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + return get_posts( $args ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Convert timeline events to JSON. This function sets the global post in order |
|
| 156 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 157 | + * we're not taking care of restoring any previous post: there isn't any. |
|
| 158 | + * |
|
| 159 | + * @param array $posts An array of posts. |
|
| 160 | + * |
|
| 161 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 162 | + * @since 3.1.0 |
|
| 163 | + * |
|
| 164 | + */ |
|
| 165 | + public function to_json( $posts ) { |
|
| 166 | + |
|
| 167 | + // If there are no events, return empty JSON |
|
| 168 | + if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 169 | + return ''; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 173 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
| 174 | + |
|
| 175 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
| 176 | + $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
| 177 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 178 | + |
|
| 179 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 180 | + // a link to the post itself. |
|
| 181 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 182 | + |
|
| 183 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 184 | + $start_at_slide = 0; |
|
| 185 | + $event_index = - 1; |
|
| 186 | + $now = time(); |
|
| 187 | + |
|
| 188 | + // Prepare the timeline variable. |
|
| 189 | + $timeline = array(); |
|
| 190 | + |
|
| 191 | + // Populate the arrays. |
|
| 192 | + $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 193 | + |
|
| 194 | + // Get the start and end dates. |
|
| 195 | + // We have to remove double quotes from date to make timeline work properly |
|
| 196 | + $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | + $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 198 | + |
|
| 199 | + // Set the starting slide. |
|
| 200 | + $event_index ++; |
|
| 201 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 202 | + $start_at_slide = $event_index; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + // Load thumbnail |
|
| 206 | + $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | + && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 209 | + ) { |
|
| 210 | + |
|
| 211 | + // Set the thumbnail URL. |
|
| 212 | + if ( 'background' === $display_images_as ) { |
|
| 213 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 215 | + } else { |
|
| 216 | + $date['media'] = array( |
|
| 217 | + 'url' => $attachment[0], |
|
| 218 | + 'thumbnail' => $attachment[0], |
|
| 219 | + ); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
| 224 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 226 | + |
|
| 227 | + setup_postdata( $GLOBALS['post'] = $item ); |
|
| 228 | + |
|
| 229 | + $more_link_text = sprintf( |
|
| 230 | + '<span aria-label="%1$s">%2$s</span>', |
|
| 231 | + sprintf( |
|
| 232 | + /* translators: %s: Name of current post */ |
|
| 233 | + __( 'Continue reading %s' ), |
|
| 234 | + the_title_attribute( array( 'echo' => false ) ) |
|
| 235 | + ), |
|
| 236 | + __( '(more…)' ) |
|
| 237 | + ); |
|
| 238 | + |
|
| 239 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 240 | + $date['text'] = array( |
|
| 241 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 242 | + ); |
|
| 243 | + |
|
| 244 | + // If we have an excerpt, set it. |
|
| 245 | + if ( 0 < $excerpt_length ) { |
|
| 246 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $date; |
|
| 250 | + |
|
| 251 | + }, $posts ); |
|
| 252 | + |
|
| 253 | + // Finally remove the excerpt filter. |
|
| 254 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 255 | + |
|
| 256 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 257 | + return array( |
|
| 258 | + 'timeline' => $timeline, |
|
| 259 | + 'start_at_slide' => $start_at_slide, |
|
| 260 | + ); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
| 265 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 266 | + * |
|
| 267 | + * @param string $excerpt_more The excerpt more preset. |
|
| 268 | + * |
|
| 269 | + * @return string An empty string. |
|
| 270 | + * @since 3.7.0 |
|
| 271 | + * |
|
| 272 | + */ |
|
| 273 | + public function excerpt_more( $excerpt_more ) { |
|
| 274 | + |
|
| 275 | + return ''; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 280 | + * how many words to return according to the client setting. |
|
| 281 | + * |
|
| 282 | + * @param int $length The preset number of words. |
|
| 283 | + * |
|
| 284 | + * @return int The number of words for the preset. |
|
| 285 | + * @since 3.7.0 |
|
| 286 | + * |
|
| 287 | + */ |
|
| 288 | + public function excerpt_length( $length ) { |
|
| 289 | + |
|
| 290 | + return $this->excerpt_length; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Convert the date to a date array. |
|
| 296 | + * |
|
| 297 | + * @param $value int A date value. |
|
| 298 | + * |
|
| 299 | + * @return array An array containing year, month and day values. |
|
| 300 | + * @since 3.7.0 |
|
| 301 | + * |
|
| 302 | + */ |
|
| 303 | + public static function date( $value ) { |
|
| 304 | + |
|
| 305 | + return array( |
|
| 306 | + 'year' => (int) date( 'Y', $value ), |
|
| 307 | + 'month' => (int) date( 'm', $value ), |
|
| 308 | + 'day' => (int) date( 'd', $value ), |
|
| 309 | + |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 315 | + * its specific function). |
|
| 316 | + * |
|
| 317 | + * @return array An array of post IDs. |
|
| 318 | + * @since 3.1.0 |
|
| 319 | + * |
|
| 320 | + */ |
|
| 321 | + public function get_all_related_to_last_50_published_posts() { |
|
| 322 | + |
|
| 323 | + // Global timeline. Get entities from the latest posts. |
|
| 324 | + $latest_posts_ids = get_posts( array( |
|
| 325 | + 'numberposts' => 50, |
|
| 326 | + 'fields' => 'ids', //only get post IDs |
|
| 327 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 328 | + 'tax_query' => array( |
|
| 329 | + 'relation' => 'OR', |
|
| 330 | + array( |
|
| 331 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 332 | + 'operator' => 'NOT EXISTS', |
|
| 333 | + ), |
|
| 334 | + array( |
|
| 335 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 336 | + 'field' => 'slug', |
|
| 337 | + 'terms' => 'article', |
|
| 338 | + ), |
|
| 339 | + ), |
|
| 340 | + 'post_status' => 'publish', |
|
| 341 | + ) ); |
|
| 342 | + |
|
| 343 | + if ( empty( $latest_posts_ids ) ) { |
|
| 344 | + // There are no posts. |
|
| 345 | + return array(); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + // Collect entities related to latest posts |
|
| 349 | + $entity_ids = array(); |
|
| 350 | + foreach ( $latest_posts_ids as $id ) { |
|
| 351 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 352 | + 'status' => 'publish', |
|
| 353 | + ) ) ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return $entity_ids; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function __construct() { |
| 54 | 54 | |
| 55 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 55 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service'); |
|
| 56 | 56 | |
| 57 | 57 | self::$instance = $this; |
| 58 | 58 | |
@@ -78,10 +78,10 @@ discard block |
||
| 78 | 78 | public function ajax_timeline() { |
| 79 | 79 | |
| 80 | 80 | // Get the ID of the post who requested the timeline. |
| 81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 81 | + $post_id = (isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : null); |
|
| 82 | 82 | |
| 83 | 83 | // Get the events and transform them for the JSON response, then send them to the client. |
| 84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 84 | + wp_send_json($this->to_json($this->get_events($post_id))); |
|
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
@@ -96,33 +96,33 @@ discard block |
||
| 96 | 96 | * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
| 97 | 97 | * |
| 98 | 98 | */ |
| 99 | - public function get_events( $post_id = null ) { |
|
| 99 | + public function get_events($post_id = null) { |
|
| 100 | 100 | |
| 101 | 101 | // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
| 102 | 102 | // posts if no post has been specified. |
| 103 | - $ids = ( is_numeric( $post_id ) |
|
| 104 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 105 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 103 | + $ids = (is_numeric($post_id) |
|
| 104 | + ? wl_core_get_related_entity_ids($post_id) |
|
| 105 | + : $this->get_all_related_to_last_50_published_posts()); |
|
| 106 | 106 | |
| 107 | 107 | // Add the post itself if it's an entity. |
| 108 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 108 | + if (is_numeric($post_id) && Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
| 109 | 109 | $ids[] = $post_id; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // If there's no entities, return an empty array right away. |
| 113 | - if ( 0 === sizeof( $ids ) ) { |
|
| 114 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 113 | + if (0 === sizeof($ids)) { |
|
| 114 | + $this->log->trace("No events found [ post id :: $post_id ]"); |
|
| 115 | 115 | |
| 116 | 116 | return array(); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 119 | + $this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]'); |
|
| 120 | 120 | |
| 121 | 121 | $args = array( |
| 122 | 122 | 'post__in' => $ids, |
| 123 | 123 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 124 | 124 | 'post_status' => 'publish', |
| 125 | - 'posts_per_page' => - 1, |
|
| 125 | + 'posts_per_page' => -1, |
|
| 126 | 126 | 'meta_query' => array( |
| 127 | 127 | 'relation' => 'AND', |
| 128 | 128 | array( |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
| 149 | 149 | ); |
| 150 | 150 | |
| 151 | - return get_posts( $args ); |
|
| 151 | + return get_posts($args); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -162,23 +162,23 @@ discard block |
||
| 162 | 162 | * @since 3.1.0 |
| 163 | 163 | * |
| 164 | 164 | */ |
| 165 | - public function to_json( $posts ) { |
|
| 165 | + public function to_json($posts) { |
|
| 166 | 166 | |
| 167 | 167 | // If there are no events, return empty JSON |
| 168 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 168 | + if (empty($posts) || is_null($posts)) { |
|
| 169 | 169 | return ''; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
| 173 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
| 173 | + $display_images_as = isset($_REQUEST['display_images_as']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['display_images_as'])) : 'media'; |
|
| 174 | 174 | |
| 175 | 175 | // The number of words for the excerpt (by default 55, as WordPress). |
| 176 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
| 177 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 176 | + $this->excerpt_length = $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? sanitize_text_field(wp_unslash($_REQUEST['excerpt_length'])) : 55; |
|
| 177 | + add_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 178 | 178 | |
| 179 | 179 | // Add a filter to remove the [...] after excerpts, since we're adding |
| 180 | 180 | // a link to the post itself. |
| 181 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 181 | + add_filter('excerpt_more', array($this, 'excerpt_more')); |
|
| 182 | 182 | |
| 183 | 183 | // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
| 184 | 184 | $start_at_slide = 0; |
@@ -189,29 +189,29 @@ discard block |
||
| 189 | 189 | $timeline = array(); |
| 190 | 190 | |
| 191 | 191 | // Populate the arrays. |
| 192 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 192 | + $timeline['events'] = array_map(function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) { |
|
| 193 | 193 | |
| 194 | 194 | // Get the start and end dates. |
| 195 | 195 | // We have to remove double quotes from date to make timeline work properly |
| 196 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
| 197 | - $end_date = strtotime( str_replace( '"', '',get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
| 196 | + $start_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true))); |
|
| 197 | + $end_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true))); |
|
| 198 | 198 | |
| 199 | 199 | // Set the starting slide. |
| 200 | - $event_index ++; |
|
| 201 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 200 | + $event_index++; |
|
| 201 | + if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) { |
|
| 202 | 202 | $start_at_slide = $event_index; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | // Load thumbnail |
| 206 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 207 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 206 | + $thumbnail_id = get_post_thumbnail_id($item->ID); |
|
| 207 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 208 | + && false !== ($attachment = wp_get_attachment_image_src($thumbnail_id)) |
|
| 209 | 209 | ) { |
| 210 | 210 | |
| 211 | 211 | // Set the thumbnail URL. |
| 212 | - if ( 'background' === $display_images_as ) { |
|
| 213 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 214 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 212 | + if ('background' === $display_images_as) { |
|
| 213 | + $date['background'] = array('url' => $attachment[0]); |
|
| 214 | + $date['media'] = array('thumbnail' => $attachment[0]); |
|
| 215 | 215 | } else { |
| 216 | 216 | $date['media'] = array( |
| 217 | 217 | 'url' => $attachment[0], |
@@ -221,37 +221,37 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // Set the start/end dates by converting them to TimelineJS required format. |
| 224 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 225 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 224 | + $date['start_date'] = Wordlift_Timeline_Service::date($start_date); |
|
| 225 | + $date['end_date'] = Wordlift_Timeline_Service::date($end_date); |
|
| 226 | 226 | |
| 227 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 227 | + setup_postdata($GLOBALS['post'] = $item); |
|
| 228 | 228 | |
| 229 | 229 | $more_link_text = sprintf( |
| 230 | 230 | '<span aria-label="%1$s">%2$s</span>', |
| 231 | 231 | sprintf( |
| 232 | 232 | /* translators: %s: Name of current post */ |
| 233 | - __( 'Continue reading %s' ), |
|
| 234 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 233 | + __('Continue reading %s'), |
|
| 234 | + the_title_attribute(array('echo' => false)) |
|
| 235 | 235 | ), |
| 236 | - __( '(more…)' ) |
|
| 236 | + __('(more…)') |
|
| 237 | 237 | ); |
| 238 | 238 | |
| 239 | 239 | // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
| 240 | 240 | $date['text'] = array( |
| 241 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 241 | + 'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>', |
|
| 242 | 242 | ); |
| 243 | 243 | |
| 244 | 244 | // If we have an excerpt, set it. |
| 245 | - if ( 0 < $excerpt_length ) { |
|
| 246 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 245 | + if (0 < $excerpt_length) { |
|
| 246 | + $date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | return $date; |
| 250 | 250 | |
| 251 | - }, $posts ); |
|
| 251 | + }, $posts); |
|
| 252 | 252 | |
| 253 | 253 | // Finally remove the excerpt filter. |
| 254 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 254 | + remove_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 255 | 255 | |
| 256 | 256 | // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
| 257 | 257 | return array( |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | * @since 3.7.0 |
| 271 | 271 | * |
| 272 | 272 | */ |
| 273 | - public function excerpt_more( $excerpt_more ) { |
|
| 273 | + public function excerpt_more($excerpt_more) { |
|
| 274 | 274 | |
| 275 | 275 | return ''; |
| 276 | 276 | } |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * @since 3.7.0 |
| 286 | 286 | * |
| 287 | 287 | */ |
| 288 | - public function excerpt_length( $length ) { |
|
| 288 | + public function excerpt_length($length) { |
|
| 289 | 289 | |
| 290 | 290 | return $this->excerpt_length; |
| 291 | 291 | } |
@@ -300,12 +300,12 @@ discard block |
||
| 300 | 300 | * @since 3.7.0 |
| 301 | 301 | * |
| 302 | 302 | */ |
| 303 | - public static function date( $value ) { |
|
| 303 | + public static function date($value) { |
|
| 304 | 304 | |
| 305 | 305 | return array( |
| 306 | - 'year' => (int) date( 'Y', $value ), |
|
| 307 | - 'month' => (int) date( 'm', $value ), |
|
| 308 | - 'day' => (int) date( 'd', $value ), |
|
| 306 | + 'year' => (int) date('Y', $value), |
|
| 307 | + 'month' => (int) date('m', $value), |
|
| 308 | + 'day' => (int) date('d', $value), |
|
| 309 | 309 | |
| 310 | 310 | ); |
| 311 | 311 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | public function get_all_related_to_last_50_published_posts() { |
| 322 | 322 | |
| 323 | 323 | // Global timeline. Get entities from the latest posts. |
| 324 | - $latest_posts_ids = get_posts( array( |
|
| 324 | + $latest_posts_ids = get_posts(array( |
|
| 325 | 325 | 'numberposts' => 50, |
| 326 | 326 | 'fields' => 'ids', //only get post IDs |
| 327 | 327 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
@@ -338,19 +338,19 @@ discard block |
||
| 338 | 338 | ), |
| 339 | 339 | ), |
| 340 | 340 | 'post_status' => 'publish', |
| 341 | - ) ); |
|
| 341 | + )); |
|
| 342 | 342 | |
| 343 | - if ( empty( $latest_posts_ids ) ) { |
|
| 343 | + if (empty($latest_posts_ids)) { |
|
| 344 | 344 | // There are no posts. |
| 345 | 345 | return array(); |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // Collect entities related to latest posts |
| 349 | 349 | $entity_ids = array(); |
| 350 | - foreach ( $latest_posts_ids as $id ) { |
|
| 351 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 350 | + foreach ($latest_posts_ids as $id) { |
|
| 351 | + $entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array( |
|
| 352 | 352 | 'status' => 'publish', |
| 353 | - ) ) ); |
|
| 353 | + ))); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | return $entity_ids; |
@@ -21,499 +21,499 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Entity_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The Log service. |
|
| 26 | - * |
|
| 27 | - * @since 3.2.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $relation_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.16.3 |
|
| 46 | - * @access private |
|
| 47 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | - */ |
|
| 49 | - private $entity_uri_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The entity post type name. |
|
| 53 | - * |
|
| 54 | - * @since 3.1.0 |
|
| 55 | - */ |
|
| 56 | - const TYPE_NAME = 'entity'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The alternative label meta key. |
|
| 60 | - * |
|
| 61 | - * @since 3.2.0 |
|
| 62 | - */ |
|
| 63 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The alternative label input template. |
|
| 67 | - * |
|
| 68 | - * @since 3.2.0 |
|
| 69 | - */ |
|
| 70 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 24 | + /** |
|
| 25 | + * The Log service. |
|
| 26 | + * |
|
| 27 | + * @since 3.2.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $relation_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.16.3 |
|
| 46 | + * @access private |
|
| 47 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | + */ |
|
| 49 | + private $entity_uri_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The entity post type name. |
|
| 53 | + * |
|
| 54 | + * @since 3.1.0 |
|
| 55 | + */ |
|
| 56 | + const TYPE_NAME = 'entity'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The alternative label meta key. |
|
| 60 | + * |
|
| 61 | + * @since 3.2.0 |
|
| 62 | + */ |
|
| 63 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The alternative label input template. |
|
| 67 | + * |
|
| 68 | + * @since 3.2.0 |
|
| 69 | + */ |
|
| 70 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 72 | 72 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 73 | 73 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 74 | 74 | <button class="button wl-delete-button">%s</button> |
| 75 | 75 | </div>'; |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Entity_Service instance. |
|
| 79 | - * |
|
| 80 | - * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | - * @since 3.2.0 |
|
| 82 | - */ |
|
| 83 | - protected function __construct() { |
|
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | - |
|
| 86 | - $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | - $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | - |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * A singleton instance of the Entity service. |
|
| 93 | - * |
|
| 94 | - * @since 3.2.0 |
|
| 95 | - * @access private |
|
| 96 | - * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | - */ |
|
| 98 | - private static $instance = null; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the singleton instance of the Entity service. |
|
| 102 | - * |
|
| 103 | - * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | - * @since 3.2.0 |
|
| 105 | - */ |
|
| 106 | - public static function get_instance() { |
|
| 107 | - |
|
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 109 | - self::$instance = new self(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return self::$instance; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | - * something which is not an article. |
|
| 118 | - * |
|
| 119 | - * @param int $post_id A post id. |
|
| 120 | - * |
|
| 121 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | - * @since 3.1.0 |
|
| 123 | - * |
|
| 124 | - */ |
|
| 125 | - public function is_entity( $post_id ) { |
|
| 126 | - |
|
| 127 | - // Improve performance by giving for granted that a product is an entity. |
|
| 128 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 129 | - return true; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 133 | - |
|
| 134 | - if ( is_wp_error( $terms ) ) { |
|
| 135 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 136 | - |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ( empty( $terms ) ) { |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /* |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Entity_Service instance. |
|
| 79 | + * |
|
| 80 | + * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | + * @since 3.2.0 |
|
| 82 | + */ |
|
| 83 | + protected function __construct() { |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | + |
|
| 86 | + $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | + $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | + |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * A singleton instance of the Entity service. |
|
| 93 | + * |
|
| 94 | + * @since 3.2.0 |
|
| 95 | + * @access private |
|
| 96 | + * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | + */ |
|
| 98 | + private static $instance = null; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the singleton instance of the Entity service. |
|
| 102 | + * |
|
| 103 | + * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | + * @since 3.2.0 |
|
| 105 | + */ |
|
| 106 | + public static function get_instance() { |
|
| 107 | + |
|
| 108 | + if ( ! isset( self::$instance ) ) { |
|
| 109 | + self::$instance = new self(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return self::$instance; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | + * something which is not an article. |
|
| 118 | + * |
|
| 119 | + * @param int $post_id A post id. |
|
| 120 | + * |
|
| 121 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | + * @since 3.1.0 |
|
| 123 | + * |
|
| 124 | + */ |
|
| 125 | + public function is_entity( $post_id ) { |
|
| 126 | + |
|
| 127 | + // Improve performance by giving for granted that a product is an entity. |
|
| 128 | + if ( 'product' === get_post_type( $post_id ) ) { |
|
| 129 | + return true; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 133 | + |
|
| 134 | + if ( is_wp_error( $terms ) ) { |
|
| 135 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 136 | + |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ( empty( $terms ) ) { |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /* |
|
| 145 | 145 | * We don't consider an `article` to be an entity. |
| 146 | 146 | * |
| 147 | 147 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 150 | 150 | */ |
| 151 | - foreach ( $terms as $term ) { |
|
| 152 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Get the proper classification scope for a given entity post |
|
| 162 | - * |
|
| 163 | - * @param integer $post_id An entity post id. |
|
| 164 | - * |
|
| 165 | - * @param string $default The default classification scope, `what` if not |
|
| 166 | - * provided. |
|
| 167 | - * |
|
| 168 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 169 | - * @since 3.5.0 |
|
| 170 | - * |
|
| 171 | - */ |
|
| 172 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 173 | - |
|
| 174 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 175 | - return $default; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // Retrieve the entity type |
|
| 179 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 181 | - // Retrieve classification boxes configuration |
|
| 182 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | - foreach ( $classification_boxes as $cb ) { |
|
| 184 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | - return $cb['id']; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $default; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Check whether a {@link WP_Post} is used. |
|
| 194 | - * |
|
| 195 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 196 | - * |
|
| 197 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 198 | - */ |
|
| 199 | - public function is_used( $post_id ) { |
|
| 200 | - |
|
| 201 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 202 | - return null; |
|
| 203 | - } |
|
| 204 | - // Retrieve the post |
|
| 205 | - $entity = get_post( $post_id ); |
|
| 206 | - |
|
| 207 | - global $wpdb; |
|
| 208 | - // Retrieve Wordlift relation instances table name |
|
| 209 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 210 | - |
|
| 211 | - // Check is it's referenced / related to another post / entity |
|
| 212 | - $stmt = $wpdb->prepare( |
|
| 213 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 214 | - $entity->ID |
|
| 215 | - ); |
|
| 216 | - |
|
| 217 | - // Perform the query |
|
| 218 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 219 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 220 | - if ( 0 < $relation_instances ) { |
|
| 221 | - return true; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // Check if the entity uri is used as meta_value |
|
| 225 | - $stmt = $wpdb->prepare( |
|
| 226 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 227 | - $entity->ID, |
|
| 228 | - wl_get_entity_uri( $entity->ID ) |
|
| 229 | - ); |
|
| 230 | - // Perform the query |
|
| 231 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 232 | - |
|
| 233 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 234 | - if ( 0 < $meta_instances ) { |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // If we are here, it means the current entity is not used at the moment |
|
| 239 | - return false; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 244 | - * |
|
| 245 | - * @param string $uri The entity URI. |
|
| 246 | - * |
|
| 247 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 248 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 249 | - * |
|
| 250 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 251 | - * @since 3.2.0 |
|
| 252 | - * |
|
| 253 | - */ |
|
| 254 | - public function get_entity_post_by_uri( $uri ) { |
|
| 255 | - |
|
| 256 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 261 | - * we check that the post we're saving is the current post. |
|
| 262 | - * |
|
| 263 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 264 | - * |
|
| 265 | - * @since 3.2.0 |
|
| 266 | - * |
|
| 267 | - * @param int $post_id Post ID. |
|
| 268 | - * @param WP_Post $post Post object. |
|
| 269 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 270 | - */ |
|
| 271 | - public function save_post( $post_id, $post, $update ) { |
|
| 272 | - |
|
| 273 | - // Avoid doing anything if post is autosave or a revision. |
|
| 274 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // We're setting the alternative label that have been provided via the UI |
|
| 279 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 280 | - // programmatically by some other function: we need to check therefore if |
|
| 281 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 282 | - // |
|
| 283 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 284 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 285 | - return; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // Get the alt labels from the request (or empty array). |
|
| 289 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | - |
|
| 291 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 292 | - // This is via classic editor, so set the alternative labels. |
|
| 293 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Set the alternative labels. |
|
| 301 | - * |
|
| 302 | - * @param int $post_id The post id. |
|
| 303 | - * @param array $alt_labels An array of labels. |
|
| 304 | - * |
|
| 305 | - * @since 3.2.0 |
|
| 306 | - * |
|
| 307 | - */ |
|
| 308 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 309 | - |
|
| 310 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 311 | - // instead of post id. |
|
| 312 | - if ( ! is_numeric( $post_id ) ) { |
|
| 313 | - return; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - // Force $alt_labels to be an array |
|
| 317 | - if ( ! is_array( $alt_labels ) ) { |
|
| 318 | - $alt_labels = array( $alt_labels ); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 322 | - |
|
| 323 | - // Delete all the existing alternate labels. |
|
| 324 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 325 | - |
|
| 326 | - // Save only unique synonymns. |
|
| 327 | - $alt_labels = array_unique( $alt_labels ); |
|
| 328 | - |
|
| 329 | - // Set the alternative labels. |
|
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - |
|
| 332 | - // Strip html code from synonym. |
|
| 333 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 334 | - |
|
| 335 | - if ( ! empty( $alt_label ) ) { |
|
| 336 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 343 | - |
|
| 344 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 345 | - |
|
| 346 | - // Append new synonyms to the end. |
|
| 347 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 348 | - |
|
| 349 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 350 | - |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Retrieve the alternate labels. |
|
| 355 | - * |
|
| 356 | - * @param int $post_id Post id. |
|
| 357 | - * |
|
| 358 | - * @return mixed An array of alternative labels. |
|
| 359 | - * @since 3.2.0 |
|
| 360 | - * |
|
| 361 | - */ |
|
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 363 | - |
|
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | - * |
|
| 370 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | - * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | - * |
|
| 373 | - * @return array An array with the entity title and labels. |
|
| 374 | - * @since 3.12.0 |
|
| 375 | - */ |
|
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Term Reference dont have synonyms yet. |
|
| 382 | - return array(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | - * |
|
| 388 | - * @param WP_Post $post Post object. |
|
| 389 | - * |
|
| 390 | - * @since 3.2.0 |
|
| 391 | - * |
|
| 392 | - */ |
|
| 393 | - public function edit_form_before_permalink( $post ) { |
|
| 394 | - |
|
| 395 | - // If it's not an entity, return. |
|
| 396 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 397 | - return; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - // If disabled by filter, return. |
|
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - // Print the input template. |
|
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | - |
|
| 408 | - // Print all the currently set alternative labels. |
|
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | - |
|
| 411 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html('post') ); |
|
| 412 | - |
|
| 413 | - }; |
|
| 414 | - |
|
| 415 | - // Print the button. |
|
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | - |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | - $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | - |
|
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | - try { |
|
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 432 | - return null; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $entity_id; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Get the alternative label input HTML code. |
|
| 441 | - * |
|
| 442 | - * @param string $value The input value. |
|
| 443 | - * |
|
| 444 | - * @return string The input HTML code. |
|
| 445 | - * @since 3.2.0 |
|
| 446 | - * |
|
| 447 | - */ |
|
| 448 | - private function get_alternative_label_input( $value = '' ) { |
|
| 449 | - |
|
| 450 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get the number of entity posts published in this blog. |
|
| 455 | - * |
|
| 456 | - * @return int The number of published entity posts. |
|
| 457 | - * @since 3.6.0 |
|
| 458 | - * |
|
| 459 | - */ |
|
| 460 | - public function count() { |
|
| 461 | - global $wpdb; |
|
| 462 | - |
|
| 463 | - // Try to get the count from the transient. |
|
| 464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | - if ( false !== $count ) { |
|
| 466 | - return $count; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - // Query the count. |
|
| 470 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 471 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 472 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 473 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 474 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 475 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 476 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 477 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 478 | - 'article' |
|
| 479 | - ) ); |
|
| 480 | - |
|
| 481 | - // Store the count in cache. |
|
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | - |
|
| 484 | - return $count; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | - * call. |
|
| 490 | - * |
|
| 491 | - * @param array $args The arguments for a `get_posts` call. |
|
| 492 | - * |
|
| 493 | - * @return array The arguments for a `get_posts` call. |
|
| 494 | - * @since 3.15.0 |
|
| 495 | - * |
|
| 496 | - */ |
|
| 497 | - public static function add_criterias( $args ) { |
|
| 498 | - |
|
| 499 | - // Build an optimal tax-query. |
|
| 500 | - $tax_query = array( |
|
| 501 | - 'relation' => 'AND', |
|
| 502 | - array( |
|
| 503 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 504 | - 'operator' => 'EXISTS', |
|
| 505 | - ), |
|
| 506 | - array( |
|
| 507 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 508 | - 'field' => 'slug', |
|
| 509 | - 'terms' => 'article', |
|
| 510 | - 'operator' => 'NOT IN', |
|
| 511 | - ), |
|
| 512 | - ); |
|
| 513 | - |
|
| 514 | - return $args + array( |
|
| 515 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 516 | - /* |
|
| 151 | + foreach ( $terms as $term ) { |
|
| 152 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Get the proper classification scope for a given entity post |
|
| 162 | + * |
|
| 163 | + * @param integer $post_id An entity post id. |
|
| 164 | + * |
|
| 165 | + * @param string $default The default classification scope, `what` if not |
|
| 166 | + * provided. |
|
| 167 | + * |
|
| 168 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 169 | + * @since 3.5.0 |
|
| 170 | + * |
|
| 171 | + */ |
|
| 172 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 173 | + |
|
| 174 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 175 | + return $default; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // Retrieve the entity type |
|
| 179 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 181 | + // Retrieve classification boxes configuration |
|
| 182 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | + foreach ( $classification_boxes as $cb ) { |
|
| 184 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | + return $cb['id']; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $default; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Check whether a {@link WP_Post} is used. |
|
| 194 | + * |
|
| 195 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 196 | + * |
|
| 197 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 198 | + */ |
|
| 199 | + public function is_used( $post_id ) { |
|
| 200 | + |
|
| 201 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 202 | + return null; |
|
| 203 | + } |
|
| 204 | + // Retrieve the post |
|
| 205 | + $entity = get_post( $post_id ); |
|
| 206 | + |
|
| 207 | + global $wpdb; |
|
| 208 | + // Retrieve Wordlift relation instances table name |
|
| 209 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 210 | + |
|
| 211 | + // Check is it's referenced / related to another post / entity |
|
| 212 | + $stmt = $wpdb->prepare( |
|
| 213 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 214 | + $entity->ID |
|
| 215 | + ); |
|
| 216 | + |
|
| 217 | + // Perform the query |
|
| 218 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 219 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 220 | + if ( 0 < $relation_instances ) { |
|
| 221 | + return true; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // Check if the entity uri is used as meta_value |
|
| 225 | + $stmt = $wpdb->prepare( |
|
| 226 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 227 | + $entity->ID, |
|
| 228 | + wl_get_entity_uri( $entity->ID ) |
|
| 229 | + ); |
|
| 230 | + // Perform the query |
|
| 231 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 232 | + |
|
| 233 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 234 | + if ( 0 < $meta_instances ) { |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // If we are here, it means the current entity is not used at the moment |
|
| 239 | + return false; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 244 | + * |
|
| 245 | + * @param string $uri The entity URI. |
|
| 246 | + * |
|
| 247 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 248 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 249 | + * |
|
| 250 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 251 | + * @since 3.2.0 |
|
| 252 | + * |
|
| 253 | + */ |
|
| 254 | + public function get_entity_post_by_uri( $uri ) { |
|
| 255 | + |
|
| 256 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 261 | + * we check that the post we're saving is the current post. |
|
| 262 | + * |
|
| 263 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 264 | + * |
|
| 265 | + * @since 3.2.0 |
|
| 266 | + * |
|
| 267 | + * @param int $post_id Post ID. |
|
| 268 | + * @param WP_Post $post Post object. |
|
| 269 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 270 | + */ |
|
| 271 | + public function save_post( $post_id, $post, $update ) { |
|
| 272 | + |
|
| 273 | + // Avoid doing anything if post is autosave or a revision. |
|
| 274 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // We're setting the alternative label that have been provided via the UI |
|
| 279 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 280 | + // programmatically by some other function: we need to check therefore if |
|
| 281 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 282 | + // |
|
| 283 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 284 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 285 | + return; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // Get the alt labels from the request (or empty array). |
|
| 289 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | + |
|
| 291 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 292 | + // This is via classic editor, so set the alternative labels. |
|
| 293 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Set the alternative labels. |
|
| 301 | + * |
|
| 302 | + * @param int $post_id The post id. |
|
| 303 | + * @param array $alt_labels An array of labels. |
|
| 304 | + * |
|
| 305 | + * @since 3.2.0 |
|
| 306 | + * |
|
| 307 | + */ |
|
| 308 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 309 | + |
|
| 310 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 311 | + // instead of post id. |
|
| 312 | + if ( ! is_numeric( $post_id ) ) { |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + // Force $alt_labels to be an array |
|
| 317 | + if ( ! is_array( $alt_labels ) ) { |
|
| 318 | + $alt_labels = array( $alt_labels ); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 322 | + |
|
| 323 | + // Delete all the existing alternate labels. |
|
| 324 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 325 | + |
|
| 326 | + // Save only unique synonymns. |
|
| 327 | + $alt_labels = array_unique( $alt_labels ); |
|
| 328 | + |
|
| 329 | + // Set the alternative labels. |
|
| 330 | + foreach ( $alt_labels as $alt_label ) { |
|
| 331 | + |
|
| 332 | + // Strip html code from synonym. |
|
| 333 | + $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 334 | + |
|
| 335 | + if ( ! empty( $alt_label ) ) { |
|
| 336 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 343 | + |
|
| 344 | + $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 345 | + |
|
| 346 | + // Append new synonyms to the end. |
|
| 347 | + $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 348 | + |
|
| 349 | + $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 350 | + |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Retrieve the alternate labels. |
|
| 355 | + * |
|
| 356 | + * @param int $post_id Post id. |
|
| 357 | + * |
|
| 358 | + * @return mixed An array of alternative labels. |
|
| 359 | + * @since 3.2.0 |
|
| 360 | + * |
|
| 361 | + */ |
|
| 362 | + public function get_alternative_labels( $post_id ) { |
|
| 363 | + |
|
| 364 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | + * |
|
| 370 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | + * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | + * |
|
| 373 | + * @return array An array with the entity title and labels. |
|
| 374 | + * @since 3.12.0 |
|
| 375 | + */ |
|
| 376 | + public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Term Reference dont have synonyms yet. |
|
| 382 | + return array(); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | + * |
|
| 388 | + * @param WP_Post $post Post object. |
|
| 389 | + * |
|
| 390 | + * @since 3.2.0 |
|
| 391 | + * |
|
| 392 | + */ |
|
| 393 | + public function edit_form_before_permalink( $post ) { |
|
| 394 | + |
|
| 395 | + // If it's not an entity, return. |
|
| 396 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 397 | + return; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + // If disabled by filter, return. |
|
| 401 | + if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + // Print the input template. |
|
| 406 | + Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | + |
|
| 408 | + // Print all the currently set alternative labels. |
|
| 409 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | + |
|
| 411 | + echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html('post') ); |
|
| 412 | + |
|
| 413 | + }; |
|
| 414 | + |
|
| 415 | + // Print the button. |
|
| 416 | + Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | + |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | + $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | + |
|
| 425 | + if ( ! isset( $entity_id ) || |
|
| 426 | + ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | + try { |
|
| 429 | + $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | + } catch ( Exception $e ) { |
|
| 432 | + return null; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $entity_id; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Get the alternative label input HTML code. |
|
| 441 | + * |
|
| 442 | + * @param string $value The input value. |
|
| 443 | + * |
|
| 444 | + * @return string The input HTML code. |
|
| 445 | + * @since 3.2.0 |
|
| 446 | + * |
|
| 447 | + */ |
|
| 448 | + private function get_alternative_label_input( $value = '' ) { |
|
| 449 | + |
|
| 450 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get the number of entity posts published in this blog. |
|
| 455 | + * |
|
| 456 | + * @return int The number of published entity posts. |
|
| 457 | + * @since 3.6.0 |
|
| 458 | + * |
|
| 459 | + */ |
|
| 460 | + public function count() { |
|
| 461 | + global $wpdb; |
|
| 462 | + |
|
| 463 | + // Try to get the count from the transient. |
|
| 464 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | + if ( false !== $count ) { |
|
| 466 | + return $count; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + // Query the count. |
|
| 470 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 471 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 472 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 473 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 474 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 475 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 476 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 477 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 478 | + 'article' |
|
| 479 | + ) ); |
|
| 480 | + |
|
| 481 | + // Store the count in cache. |
|
| 482 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | + |
|
| 484 | + return $count; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | + * call. |
|
| 490 | + * |
|
| 491 | + * @param array $args The arguments for a `get_posts` call. |
|
| 492 | + * |
|
| 493 | + * @return array The arguments for a `get_posts` call. |
|
| 494 | + * @since 3.15.0 |
|
| 495 | + * |
|
| 496 | + */ |
|
| 497 | + public static function add_criterias( $args ) { |
|
| 498 | + |
|
| 499 | + // Build an optimal tax-query. |
|
| 500 | + $tax_query = array( |
|
| 501 | + 'relation' => 'AND', |
|
| 502 | + array( |
|
| 503 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 504 | + 'operator' => 'EXISTS', |
|
| 505 | + ), |
|
| 506 | + array( |
|
| 507 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 508 | + 'field' => 'slug', |
|
| 509 | + 'terms' => 'article', |
|
| 510 | + 'operator' => 'NOT IN', |
|
| 511 | + ), |
|
| 512 | + ); |
|
| 513 | + |
|
| 514 | + return $args + array( |
|
| 515 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 516 | + /* |
|
| 517 | 517 | * Ensure compatibility with Polylang. |
| 518 | 518 | * |
| 519 | 519 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -521,102 +521,102 @@ discard block |
||
| 521 | 521 | * |
| 522 | 522 | * @since 3.19.5 |
| 523 | 523 | */ |
| 524 | - 'lang' => '', |
|
| 525 | - 'tax_query' => $tax_query, |
|
| 526 | - ); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Create a new entity. |
|
| 531 | - * |
|
| 532 | - * @param string $name The entity name. |
|
| 533 | - * @param string $type_uri The entity's type URI. |
|
| 534 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 535 | - * @param string $status The post status, by default 'publish'. |
|
| 536 | - * |
|
| 537 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 538 | - * @since 3.9.0 |
|
| 539 | - * |
|
| 540 | - */ |
|
| 541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 542 | - |
|
| 543 | - // Create an entity for the publisher. |
|
| 544 | - $post_id = @wp_insert_post( array( |
|
| 545 | - 'post_type' => self::TYPE_NAME, |
|
| 546 | - 'post_title' => $name, |
|
| 547 | - 'post_status' => $status, |
|
| 548 | - 'post_content' => '', |
|
| 549 | - ) ); |
|
| 550 | - |
|
| 551 | - // Return the error if any. |
|
| 552 | - if ( is_wp_error( $post_id ) ) { |
|
| 553 | - return $post_id; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - // Set the entity logo. |
|
| 557 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | - set_post_thumbnail( $post_id, $logo ); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - // Set the entity type. |
|
| 562 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 563 | - |
|
| 564 | - return $post_id; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * Get the entities related to the one with the specified id. By default only |
|
| 569 | - * published entities will be returned. |
|
| 570 | - * |
|
| 571 | - * @param int $id The post id. |
|
| 572 | - * @param string $post_status The target post status (default = publish). |
|
| 573 | - * |
|
| 574 | - * @return array An array of post ids. |
|
| 575 | - * @since 3.10.0 |
|
| 576 | - * |
|
| 577 | - */ |
|
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | - |
|
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get the list of entities. |
|
| 585 | - * |
|
| 586 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | - * |
|
| 588 | - * @return array An array of entity posts. |
|
| 589 | - * @since 3.12.2 |
|
| 590 | - * |
|
| 591 | - */ |
|
| 592 | - public function get( $params = array() ) { |
|
| 593 | - |
|
| 594 | - // Set the defaults. |
|
| 595 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 596 | - |
|
| 597 | - // Merge the defaults with the provided parameters. |
|
| 598 | - $args = wp_parse_args( $params, $defaults ); |
|
| 599 | - |
|
| 600 | - // Call the `get_posts` function. |
|
| 601 | - return get_posts( $args ); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * The list of post type names which can be used for entities |
|
| 606 | - * |
|
| 607 | - * Criteria is that the post type is public. The list of valid post types |
|
| 608 | - * can be overridden with a filter. |
|
| 609 | - * |
|
| 610 | - * @return array Array containing the names of the valid post types. |
|
| 611 | - * @since 3.15.0 |
|
| 612 | - * |
|
| 613 | - */ |
|
| 614 | - static function valid_entity_post_types() { |
|
| 615 | - |
|
| 616 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 618 | - |
|
| 619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 620 | - } |
|
| 524 | + 'lang' => '', |
|
| 525 | + 'tax_query' => $tax_query, |
|
| 526 | + ); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Create a new entity. |
|
| 531 | + * |
|
| 532 | + * @param string $name The entity name. |
|
| 533 | + * @param string $type_uri The entity's type URI. |
|
| 534 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 535 | + * @param string $status The post status, by default 'publish'. |
|
| 536 | + * |
|
| 537 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 538 | + * @since 3.9.0 |
|
| 539 | + * |
|
| 540 | + */ |
|
| 541 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 542 | + |
|
| 543 | + // Create an entity for the publisher. |
|
| 544 | + $post_id = @wp_insert_post( array( |
|
| 545 | + 'post_type' => self::TYPE_NAME, |
|
| 546 | + 'post_title' => $name, |
|
| 547 | + 'post_status' => $status, |
|
| 548 | + 'post_content' => '', |
|
| 549 | + ) ); |
|
| 550 | + |
|
| 551 | + // Return the error if any. |
|
| 552 | + if ( is_wp_error( $post_id ) ) { |
|
| 553 | + return $post_id; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + // Set the entity logo. |
|
| 557 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | + set_post_thumbnail( $post_id, $logo ); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + // Set the entity type. |
|
| 562 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 563 | + |
|
| 564 | + return $post_id; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * Get the entities related to the one with the specified id. By default only |
|
| 569 | + * published entities will be returned. |
|
| 570 | + * |
|
| 571 | + * @param int $id The post id. |
|
| 572 | + * @param string $post_status The target post status (default = publish). |
|
| 573 | + * |
|
| 574 | + * @return array An array of post ids. |
|
| 575 | + * @since 3.10.0 |
|
| 576 | + * |
|
| 577 | + */ |
|
| 578 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | + |
|
| 580 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get the list of entities. |
|
| 585 | + * |
|
| 586 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | + * |
|
| 588 | + * @return array An array of entity posts. |
|
| 589 | + * @since 3.12.2 |
|
| 590 | + * |
|
| 591 | + */ |
|
| 592 | + public function get( $params = array() ) { |
|
| 593 | + |
|
| 594 | + // Set the defaults. |
|
| 595 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 596 | + |
|
| 597 | + // Merge the defaults with the provided parameters. |
|
| 598 | + $args = wp_parse_args( $params, $defaults ); |
|
| 599 | + |
|
| 600 | + // Call the `get_posts` function. |
|
| 601 | + return get_posts( $args ); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * The list of post type names which can be used for entities |
|
| 606 | + * |
|
| 607 | + * Criteria is that the post type is public. The list of valid post types |
|
| 608 | + * can be overridden with a filter. |
|
| 609 | + * |
|
| 610 | + * @return array Array containing the names of the valid post types. |
|
| 611 | + * @since 3.15.0 |
|
| 612 | + * |
|
| 613 | + */ |
|
| 614 | + static function valid_entity_post_types() { |
|
| 615 | + |
|
| 616 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 617 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 618 | + |
|
| 619 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 620 | + } |
|
| 621 | 621 | |
| 622 | 622 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @since 3.2.0 |
| 82 | 82 | */ |
| 83 | 83 | protected function __construct() { |
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 85 | 85 | |
| 86 | 86 | $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
| 87 | 87 | $this->relation_service = Wordlift_Relation_Service::get_instance(); |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public static function get_instance() { |
| 107 | 107 | |
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 108 | + if ( ! isset(self::$instance)) { |
|
| 109 | 109 | self::$instance = new self(); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -122,22 +122,22 @@ discard block |
||
| 122 | 122 | * @since 3.1.0 |
| 123 | 123 | * |
| 124 | 124 | */ |
| 125 | - public function is_entity( $post_id ) { |
|
| 125 | + public function is_entity($post_id) { |
|
| 126 | 126 | |
| 127 | 127 | // Improve performance by giving for granted that a product is an entity. |
| 128 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 128 | + if ('product' === get_post_type($post_id)) { |
|
| 129 | 129 | return true; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 132 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 133 | 133 | |
| 134 | - if ( is_wp_error( $terms ) ) { |
|
| 135 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 134 | + if (is_wp_error($terms)) { |
|
| 135 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 136 | 136 | |
| 137 | 137 | return false; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - if ( empty( $terms ) ) { |
|
| 140 | + if (empty($terms)) { |
|
| 141 | 141 | return false; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -148,8 +148,8 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 150 | 150 | */ |
| 151 | - foreach ( $terms as $term ) { |
|
| 152 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 151 | + foreach ($terms as $term) { |
|
| 152 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
| 153 | 153 | return true; |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -169,19 +169,19 @@ discard block |
||
| 169 | 169 | * @since 3.5.0 |
| 170 | 170 | * |
| 171 | 171 | */ |
| 172 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 172 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 173 | 173 | |
| 174 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 174 | + if (false === $this->is_entity($post_id)) { |
|
| 175 | 175 | return $default; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | // Retrieve the entity type |
| 179 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 179 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 180 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 181 | 181 | // Retrieve classification boxes configuration |
| 182 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | - foreach ( $classification_boxes as $cb ) { |
|
| 184 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 182 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 183 | + foreach ($classification_boxes as $cb) { |
|
| 184 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 185 | 185 | return $cb['id']; |
| 186 | 186 | } |
| 187 | 187 | } |
@@ -196,13 +196,13 @@ discard block |
||
| 196 | 196 | * |
| 197 | 197 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 198 | 198 | */ |
| 199 | - public function is_used( $post_id ) { |
|
| 199 | + public function is_used($post_id) { |
|
| 200 | 200 | |
| 201 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 201 | + if (false === $this->is_entity($post_id)) { |
|
| 202 | 202 | return null; |
| 203 | 203 | } |
| 204 | 204 | // Retrieve the post |
| 205 | - $entity = get_post( $post_id ); |
|
| 205 | + $entity = get_post($post_id); |
|
| 206 | 206 | |
| 207 | 207 | global $wpdb; |
| 208 | 208 | // Retrieve Wordlift relation instances table name |
@@ -215,9 +215,9 @@ discard block |
||
| 215 | 215 | ); |
| 216 | 216 | |
| 217 | 217 | // Perform the query |
| 218 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 218 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 219 | 219 | // If there is at least one relation instance for the current entity, then it's used |
| 220 | - if ( 0 < $relation_instances ) { |
|
| 220 | + if (0 < $relation_instances) { |
|
| 221 | 221 | return true; |
| 222 | 222 | } |
| 223 | 223 | |
@@ -225,13 +225,13 @@ discard block |
||
| 225 | 225 | $stmt = $wpdb->prepare( |
| 226 | 226 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 227 | 227 | $entity->ID, |
| 228 | - wl_get_entity_uri( $entity->ID ) |
|
| 228 | + wl_get_entity_uri($entity->ID) |
|
| 229 | 229 | ); |
| 230 | 230 | // Perform the query |
| 231 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 231 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 232 | 232 | |
| 233 | 233 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 234 | - if ( 0 < $meta_instances ) { |
|
| 234 | + if (0 < $meta_instances) { |
|
| 235 | 235 | return true; |
| 236 | 236 | } |
| 237 | 237 | |
@@ -251,9 +251,9 @@ discard block |
||
| 251 | 251 | * @since 3.2.0 |
| 252 | 252 | * |
| 253 | 253 | */ |
| 254 | - public function get_entity_post_by_uri( $uri ) { |
|
| 254 | + public function get_entity_post_by_uri($uri) { |
|
| 255 | 255 | |
| 256 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 256 | + return $this->entity_uri_service->get_entity($uri); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -268,10 +268,10 @@ discard block |
||
| 268 | 268 | * @param WP_Post $post Post object. |
| 269 | 269 | * @param bool $update Whether this is an existing post being updated or not. |
| 270 | 270 | */ |
| 271 | - public function save_post( $post_id, $post, $update ) { |
|
| 271 | + public function save_post($post_id, $post, $update) { |
|
| 272 | 272 | |
| 273 | 273 | // Avoid doing anything if post is autosave or a revision. |
| 274 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 274 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 275 | 275 | return; |
| 276 | 276 | } |
| 277 | 277 | |
@@ -281,16 +281,16 @@ discard block |
||
| 281 | 281 | // the $post_id in the save_post call matches the post id set in the request. |
| 282 | 282 | // |
| 283 | 283 | // If this is not the current post being saved or if it's not an entity, return. |
| 284 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 284 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 285 | 285 | return; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // Get the alt labels from the request (or empty array). |
| 289 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 289 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | 290 | |
| 291 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 291 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 292 | 292 | // This is via classic editor, so set the alternative labels. |
| 293 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 293 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | |
@@ -305,48 +305,48 @@ discard block |
||
| 305 | 305 | * @since 3.2.0 |
| 306 | 306 | * |
| 307 | 307 | */ |
| 308 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 308 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 309 | 309 | |
| 310 | 310 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 311 | 311 | // instead of post id. |
| 312 | - if ( ! is_numeric( $post_id ) ) { |
|
| 312 | + if ( ! is_numeric($post_id)) { |
|
| 313 | 313 | return; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | // Force $alt_labels to be an array |
| 317 | - if ( ! is_array( $alt_labels ) ) { |
|
| 318 | - $alt_labels = array( $alt_labels ); |
|
| 317 | + if ( ! is_array($alt_labels)) { |
|
| 318 | + $alt_labels = array($alt_labels); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 321 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 322 | 322 | |
| 323 | 323 | // Delete all the existing alternate labels. |
| 324 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 325 | 325 | |
| 326 | 326 | // Save only unique synonymns. |
| 327 | - $alt_labels = array_unique( $alt_labels ); |
|
| 327 | + $alt_labels = array_unique($alt_labels); |
|
| 328 | 328 | |
| 329 | 329 | // Set the alternative labels. |
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 330 | + foreach ($alt_labels as $alt_label) { |
|
| 331 | 331 | |
| 332 | 332 | // Strip html code from synonym. |
| 333 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 333 | + $alt_label = wp_strip_all_tags($alt_label); |
|
| 334 | 334 | |
| 335 | - if ( ! empty( $alt_label ) ) { |
|
| 336 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 335 | + if ( ! empty($alt_label)) { |
|
| 336 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label); |
|
| 337 | 337 | } |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 342 | + public function append_alternative_labels($post_id, $labels_to_append) { |
|
| 343 | 343 | |
| 344 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 344 | + $merged_labels = $this->get_alternative_labels($post_id); |
|
| 345 | 345 | |
| 346 | 346 | // Append new synonyms to the end. |
| 347 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 347 | + $merged_labels = array_merge($merged_labels, $labels_to_append); |
|
| 348 | 348 | |
| 349 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 349 | + $this->set_alternative_labels($post_id, $merged_labels); |
|
| 350 | 350 | |
| 351 | 351 | } |
| 352 | 352 | |
@@ -359,9 +359,9 @@ discard block |
||
| 359 | 359 | * @since 3.2.0 |
| 360 | 360 | * |
| 361 | 361 | */ |
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 362 | + public function get_alternative_labels($post_id) { |
|
| 363 | 363 | |
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 364 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -373,9 +373,9 @@ discard block |
||
| 373 | 373 | * @return array An array with the entity title and labels. |
| 374 | 374 | * @since 3.12.0 |
| 375 | 375 | */ |
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 376 | + public function get_labels($post_id, $object_type = Object_Type_Enum::POST) { |
|
| 377 | + if ($object_type === Object_Type_Enum::POST) { |
|
| 378 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // Term Reference dont have synonyms yet. |
@@ -390,45 +390,45 @@ discard block |
||
| 390 | 390 | * @since 3.2.0 |
| 391 | 391 | * |
| 392 | 392 | */ |
| 393 | - public function edit_form_before_permalink( $post ) { |
|
| 393 | + public function edit_form_before_permalink($post) { |
|
| 394 | 394 | |
| 395 | 395 | // If it's not an entity, return. |
| 396 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 396 | + if ( ! $this->is_entity($post->ID)) { |
|
| 397 | 397 | return; |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | // If disabled by filter, return. |
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 401 | + if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) { |
|
| 402 | 402 | return; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | // Print the input template. |
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 406 | + Wordlift_UI_Service::print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 407 | 407 | |
| 408 | 408 | // Print all the currently set alternative labels. |
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 409 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 410 | 410 | |
| 411 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), wp_kses_allowed_html('post') ); |
|
| 411 | + echo wp_kses($this->get_alternative_label_input($alt_label), wp_kses_allowed_html('post')); |
|
| 412 | 412 | |
| 413 | 413 | }; |
| 414 | 414 | |
| 415 | 415 | // Print the button. |
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 416 | + Wordlift_UI_Service::print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 417 | 417 | |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 420 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
| 421 | 421 | $content_service = Wordpress_Content_Service::get_instance(); |
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 422 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 423 | 423 | $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
| 424 | 424 | |
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 425 | + if ( ! isset($entity_id) || |
|
| 426 | + ( ! empty($dataset_uri) && 0 !== strpos($entity_id, $dataset_uri))) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri($type, $object_id); |
|
| 428 | 428 | try { |
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 429 | + $content_service->set_entity_id(new Wordpress_Content_Id($object_id, $type), $rel_uri); |
|
| 430 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 431 | + } catch (Exception $e) { |
|
| 432 | 432 | return null; |
| 433 | 433 | } |
| 434 | 434 | } |
@@ -445,9 +445,9 @@ discard block |
||
| 445 | 445 | * @since 3.2.0 |
| 446 | 446 | * |
| 447 | 447 | */ |
| 448 | - private function get_alternative_label_input( $value = '' ) { |
|
| 448 | + private function get_alternative_label_input($value = '') { |
|
| 449 | 449 | |
| 450 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 450 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | /** |
@@ -461,13 +461,13 @@ discard block |
||
| 461 | 461 | global $wpdb; |
| 462 | 462 | |
| 463 | 463 | // Try to get the count from the transient. |
| 464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | - if ( false !== $count ) { |
|
| 464 | + $count = get_transient('_wl_entity_service__count'); |
|
| 465 | + if (false !== $count) { |
|
| 466 | 466 | return $count; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | // Query the count. |
| 470 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 470 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 471 | 471 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 472 | 472 | . " FROM {$wpdb->term_relationships} tr" |
| 473 | 473 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -476,10 +476,10 @@ discard block |
||
| 476 | 476 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 477 | 477 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 478 | 478 | 'article' |
| 479 | - ) ); |
|
| 479 | + )); |
|
| 480 | 480 | |
| 481 | 481 | // Store the count in cache. |
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 482 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 483 | 483 | |
| 484 | 484 | return $count; |
| 485 | 485 | } |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | * @since 3.15.0 |
| 495 | 495 | * |
| 496 | 496 | */ |
| 497 | - public static function add_criterias( $args ) { |
|
| 497 | + public static function add_criterias($args) { |
|
| 498 | 498 | |
| 499 | 499 | // Build an optimal tax-query. |
| 500 | 500 | $tax_query = array( |
@@ -538,28 +538,28 @@ discard block |
||
| 538 | 538 | * @since 3.9.0 |
| 539 | 539 | * |
| 540 | 540 | */ |
| 541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 541 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 542 | 542 | |
| 543 | 543 | // Create an entity for the publisher. |
| 544 | - $post_id = @wp_insert_post( array( |
|
| 544 | + $post_id = @wp_insert_post(array( |
|
| 545 | 545 | 'post_type' => self::TYPE_NAME, |
| 546 | 546 | 'post_title' => $name, |
| 547 | 547 | 'post_status' => $status, |
| 548 | 548 | 'post_content' => '', |
| 549 | - ) ); |
|
| 549 | + )); |
|
| 550 | 550 | |
| 551 | 551 | // Return the error if any. |
| 552 | - if ( is_wp_error( $post_id ) ) { |
|
| 552 | + if (is_wp_error($post_id)) { |
|
| 553 | 553 | return $post_id; |
| 554 | 554 | } |
| 555 | 555 | |
| 556 | 556 | // Set the entity logo. |
| 557 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | - set_post_thumbnail( $post_id, $logo ); |
|
| 557 | + if ($logo && is_numeric($logo)) { |
|
| 558 | + set_post_thumbnail($post_id, $logo); |
|
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | // Set the entity type. |
| 562 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 562 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 563 | 563 | |
| 564 | 564 | return $post_id; |
| 565 | 565 | } |
@@ -575,9 +575,9 @@ discard block |
||
| 575 | 575 | * @since 3.10.0 |
| 576 | 576 | * |
| 577 | 577 | */ |
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 578 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 579 | 579 | |
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 580 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /** |
@@ -589,16 +589,16 @@ discard block |
||
| 589 | 589 | * @since 3.12.2 |
| 590 | 590 | * |
| 591 | 591 | */ |
| 592 | - public function get( $params = array() ) { |
|
| 592 | + public function get($params = array()) { |
|
| 593 | 593 | |
| 594 | 594 | // Set the defaults. |
| 595 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 595 | + $defaults = array('post_type' => 'entity'); |
|
| 596 | 596 | |
| 597 | 597 | // Merge the defaults with the provided parameters. |
| 598 | - $args = wp_parse_args( $params, $defaults ); |
|
| 598 | + $args = wp_parse_args($params, $defaults); |
|
| 599 | 599 | |
| 600 | 600 | // Call the `get_posts` function. |
| 601 | - return get_posts( $args ); |
|
| 601 | + return get_posts($args); |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | /** |
@@ -614,9 +614,9 @@ discard block |
||
| 614 | 614 | static function valid_entity_post_types() { |
| 615 | 615 | |
| 616 | 616 | // Ignore builtins in the call to avoid getting attachments. |
| 617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 617 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 618 | 618 | |
| 619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 619 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 620 | 620 | } |
| 621 | 621 | |
| 622 | 622 | } |
@@ -16,178 +16,178 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Http_Api { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A {@link Wordlift_Log_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.15.3 |
|
| 23 | - * |
|
| 24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.3 |
|
| 32 | - */ |
|
| 33 | - public function __construct() { |
|
| 34 | - |
|
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | - |
|
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | - |
|
| 40 | - //region SAMPLE ACTIONS. |
|
| 41 | - add_action( 'admin_post_wl_hello_world', array( |
|
| 42 | - $this, |
|
| 43 | - 'hello_world', |
|
| 44 | - ) ); |
|
| 45 | - add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 46 | - $this, |
|
| 47 | - 'nopriv_hello_world', |
|
| 48 | - ) ); |
|
| 49 | - //endregion |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Add the `wl-api` rewrite end-point. |
|
| 55 | - * |
|
| 56 | - * @since 3.15.3 |
|
| 57 | - */ |
|
| 58 | - public function add_rewrite_endpoint() { |
|
| 59 | - |
|
| 60 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 61 | - $this->ensure_rewrite_rules_are_flushed(); |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Handle `template_redirect` hooks. |
|
| 67 | - * |
|
| 68 | - * @since 3.15.3 |
|
| 69 | - */ |
|
| 70 | - public function template_redirect() { |
|
| 71 | - |
|
| 72 | - global $wp_query; |
|
| 73 | - |
|
| 74 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 75 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 76 | - |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ): ''; |
|
| 81 | - $this->do_action( $action ); |
|
| 82 | - |
|
| 83 | - exit; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Do the requested action. |
|
| 89 | - * |
|
| 90 | - * @param string $action The action to execute. |
|
| 91 | - * |
|
| 92 | - * @since 3.15.3 |
|
| 93 | - * |
|
| 94 | - */ |
|
| 95 | - private function do_action( $action ) { |
|
| 96 | - |
|
| 97 | - if ( empty( $action ) ) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 102 | - /** |
|
| 103 | - * Fires on a non-authenticated admin post request for the given action. |
|
| 104 | - * |
|
| 105 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 106 | - * request action. |
|
| 107 | - * |
|
| 108 | - * @since 2.6.0 |
|
| 109 | - */ |
|
| 110 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 111 | - } else { |
|
| 112 | - /** |
|
| 113 | - * Fires on an authenticated admin post request for the given action. |
|
| 114 | - * |
|
| 115 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 116 | - * request action. |
|
| 117 | - * |
|
| 118 | - * @since 2.6.0 |
|
| 119 | - */ |
|
| 120 | - do_action( "admin_post_{$action}" ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Test function, anonymous. |
|
| 127 | - * |
|
| 128 | - * @since 3.15.3 |
|
| 129 | - */ |
|
| 130 | - public function nopriv_hello_world() { |
|
| 131 | - |
|
| 132 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Test function, authenticated. |
|
| 138 | - * |
|
| 139 | - * @since 3.15.3 |
|
| 140 | - */ |
|
| 141 | - public function hello_world() { |
|
| 142 | - |
|
| 143 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Ensure that the rewrite rules are flushed the first time. |
|
| 149 | - * |
|
| 150 | - * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
| 151 | - * @since 3.15.3 |
|
| 152 | - */ |
|
| 153 | - public static function ensure_rewrite_rules_are_flushed() { |
|
| 154 | - |
|
| 155 | - // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
| 156 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 157 | - update_option( 'wl_http_api', 'yes' ); |
|
| 158 | - add_action( 'wp_loaded', function () { |
|
| 159 | - flush_rewrite_rules(); |
|
| 160 | - } ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
| 167 | - * reinitialize the `wl-api` route. |
|
| 168 | - * |
|
| 169 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 170 | - * |
|
| 171 | - * @since 3.19.2 |
|
| 172 | - */ |
|
| 173 | - public static function activate() { |
|
| 174 | - |
|
| 175 | - // Force the plugin to reinitialize the rewrite rules. |
|
| 176 | - update_option( 'wl_http_api', 'no' ); |
|
| 177 | - |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Delete the option when the plugin is deactivated. |
|
| 182 | - * |
|
| 183 | - * @since 3.19.4 |
|
| 184 | - * |
|
| 185 | - * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
| 186 | - */ |
|
| 187 | - public static function deactivate() { |
|
| 188 | - |
|
| 189 | - delete_option( 'wl_http_api' ); |
|
| 190 | - |
|
| 191 | - } |
|
| 19 | + /** |
|
| 20 | + * A {@link Wordlift_Log_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.15.3 |
|
| 23 | + * |
|
| 24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.3 |
|
| 32 | + */ |
|
| 33 | + public function __construct() { |
|
| 34 | + |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | + |
|
| 37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | + |
|
| 40 | + //region SAMPLE ACTIONS. |
|
| 41 | + add_action( 'admin_post_wl_hello_world', array( |
|
| 42 | + $this, |
|
| 43 | + 'hello_world', |
|
| 44 | + ) ); |
|
| 45 | + add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 46 | + $this, |
|
| 47 | + 'nopriv_hello_world', |
|
| 48 | + ) ); |
|
| 49 | + //endregion |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Add the `wl-api` rewrite end-point. |
|
| 55 | + * |
|
| 56 | + * @since 3.15.3 |
|
| 57 | + */ |
|
| 58 | + public function add_rewrite_endpoint() { |
|
| 59 | + |
|
| 60 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 61 | + $this->ensure_rewrite_rules_are_flushed(); |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Handle `template_redirect` hooks. |
|
| 67 | + * |
|
| 68 | + * @since 3.15.3 |
|
| 69 | + */ |
|
| 70 | + public function template_redirect() { |
|
| 71 | + |
|
| 72 | + global $wp_query; |
|
| 73 | + |
|
| 74 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 75 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 76 | + |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ): ''; |
|
| 81 | + $this->do_action( $action ); |
|
| 82 | + |
|
| 83 | + exit; |
|
| 84 | + |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Do the requested action. |
|
| 89 | + * |
|
| 90 | + * @param string $action The action to execute. |
|
| 91 | + * |
|
| 92 | + * @since 3.15.3 |
|
| 93 | + * |
|
| 94 | + */ |
|
| 95 | + private function do_action( $action ) { |
|
| 96 | + |
|
| 97 | + if ( empty( $action ) ) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 102 | + /** |
|
| 103 | + * Fires on a non-authenticated admin post request for the given action. |
|
| 104 | + * |
|
| 105 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 106 | + * request action. |
|
| 107 | + * |
|
| 108 | + * @since 2.6.0 |
|
| 109 | + */ |
|
| 110 | + do_action( "admin_post_nopriv_{$action}" ); |
|
| 111 | + } else { |
|
| 112 | + /** |
|
| 113 | + * Fires on an authenticated admin post request for the given action. |
|
| 114 | + * |
|
| 115 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 116 | + * request action. |
|
| 117 | + * |
|
| 118 | + * @since 2.6.0 |
|
| 119 | + */ |
|
| 120 | + do_action( "admin_post_{$action}" ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Test function, anonymous. |
|
| 127 | + * |
|
| 128 | + * @since 3.15.3 |
|
| 129 | + */ |
|
| 130 | + public function nopriv_hello_world() { |
|
| 131 | + |
|
| 132 | + wp_die( 'Hello World! (from anonymous)' ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Test function, authenticated. |
|
| 138 | + * |
|
| 139 | + * @since 3.15.3 |
|
| 140 | + */ |
|
| 141 | + public function hello_world() { |
|
| 142 | + |
|
| 143 | + wp_die( 'Hello World! (from authenticated)' ); |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Ensure that the rewrite rules are flushed the first time. |
|
| 149 | + * |
|
| 150 | + * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
| 151 | + * @since 3.15.3 |
|
| 152 | + */ |
|
| 153 | + public static function ensure_rewrite_rules_are_flushed() { |
|
| 154 | + |
|
| 155 | + // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
| 156 | + if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 157 | + update_option( 'wl_http_api', 'yes' ); |
|
| 158 | + add_action( 'wp_loaded', function () { |
|
| 159 | + flush_rewrite_rules(); |
|
| 160 | + } ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
| 167 | + * reinitialize the `wl-api` route. |
|
| 168 | + * |
|
| 169 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 170 | + * |
|
| 171 | + * @since 3.19.2 |
|
| 172 | + */ |
|
| 173 | + public static function activate() { |
|
| 174 | + |
|
| 175 | + // Force the plugin to reinitialize the rewrite rules. |
|
| 176 | + update_option( 'wl_http_api', 'no' ); |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Delete the option when the plugin is deactivated. |
|
| 182 | + * |
|
| 183 | + * @since 3.19.4 |
|
| 184 | + * |
|
| 185 | + * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
| 186 | + */ |
|
| 187 | + public static function deactivate() { |
|
| 188 | + |
|
| 189 | + delete_option( 'wl_http_api' ); |
|
| 190 | + |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | } |
@@ -32,20 +32,20 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function __construct() { |
| 34 | 34 | |
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 36 | 36 | |
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
| 38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
| 39 | 39 | |
| 40 | 40 | //region SAMPLE ACTIONS. |
| 41 | - add_action( 'admin_post_wl_hello_world', array( |
|
| 41 | + add_action('admin_post_wl_hello_world', array( |
|
| 42 | 42 | $this, |
| 43 | 43 | 'hello_world', |
| 44 | - ) ); |
|
| 45 | - add_action( 'admin_post_nopriv_wl_hello_world', array( |
|
| 44 | + )); |
|
| 45 | + add_action('admin_post_nopriv_wl_hello_world', array( |
|
| 46 | 46 | $this, |
| 47 | 47 | 'nopriv_hello_world', |
| 48 | - ) ); |
|
| 48 | + )); |
|
| 49 | 49 | //endregion |
| 50 | 50 | |
| 51 | 51 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function add_rewrite_endpoint() { |
| 59 | 59 | |
| 60 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 60 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
| 61 | 61 | $this->ensure_rewrite_rules_are_flushed(); |
| 62 | 62 | |
| 63 | 63 | } |
@@ -71,14 +71,14 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | global $wp_query; |
| 73 | 73 | |
| 74 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 75 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 74 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
| 75 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
| 76 | 76 | |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ): ''; |
|
| 81 | - $this->do_action( $action ); |
|
| 80 | + $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; |
|
| 81 | + $this->do_action($action); |
|
| 82 | 82 | |
| 83 | 83 | exit; |
| 84 | 84 | |
@@ -92,13 +92,13 @@ discard block |
||
| 92 | 92 | * @since 3.15.3 |
| 93 | 93 | * |
| 94 | 94 | */ |
| 95 | - private function do_action( $action ) { |
|
| 95 | + private function do_action($action) { |
|
| 96 | 96 | |
| 97 | - if ( empty( $action ) ) { |
|
| 97 | + if (empty($action)) { |
|
| 98 | 98 | return; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 101 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
| 102 | 102 | /** |
| 103 | 103 | * Fires on a non-authenticated admin post request for the given action. |
| 104 | 104 | * |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @since 2.6.0 |
| 109 | 109 | */ |
| 110 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 110 | + do_action("admin_post_nopriv_{$action}"); |
|
| 111 | 111 | } else { |
| 112 | 112 | /** |
| 113 | 113 | * Fires on an authenticated admin post request for the given action. |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @since 2.6.0 |
| 119 | 119 | */ |
| 120 | - do_action( "admin_post_{$action}" ); |
|
| 120 | + do_action("admin_post_{$action}"); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public function nopriv_hello_world() { |
| 131 | 131 | |
| 132 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 132 | + wp_die('Hello World! (from anonymous)'); |
|
| 133 | 133 | |
| 134 | 134 | } |
| 135 | 135 | |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | public function hello_world() { |
| 142 | 142 | |
| 143 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 143 | + wp_die('Hello World! (from authenticated)'); |
|
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | |
@@ -153,9 +153,9 @@ discard block |
||
| 153 | 153 | public static function ensure_rewrite_rules_are_flushed() { |
| 154 | 154 | |
| 155 | 155 | // See https://github.com/insideout10/wordlift-plugin/issues/698. |
| 156 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
| 157 | - update_option( 'wl_http_api', 'yes' ); |
|
| 158 | - add_action( 'wp_loaded', function () { |
|
| 156 | + if ('yes' !== get_option('wl_http_api')) { |
|
| 157 | + update_option('wl_http_api', 'yes'); |
|
| 158 | + add_action('wp_loaded', function() { |
|
| 159 | 159 | flush_rewrite_rules(); |
| 160 | 160 | } ); |
| 161 | 161 | } |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | public static function activate() { |
| 174 | 174 | |
| 175 | 175 | // Force the plugin to reinitialize the rewrite rules. |
| 176 | - update_option( 'wl_http_api', 'no' ); |
|
| 176 | + update_option('wl_http_api', 'no'); |
|
| 177 | 177 | |
| 178 | 178 | } |
| 179 | 179 | |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | public static function deactivate() { |
| 188 | 188 | |
| 189 | - delete_option( 'wl_http_api' ); |
|
| 189 | + delete_option('wl_http_api'); |
|
| 190 | 190 | |
| 191 | 191 | } |
| 192 | 192 | |
@@ -8,83 +8,83 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Wordlift_Mapping_Ajax_Adapter { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * The {@link Wordlift_Mapping_Service} instance. |
|
| 13 | - * |
|
| 14 | - * @since 3.20.0 |
|
| 15 | - * @access private |
|
| 16 | - * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
| 17 | - */ |
|
| 18 | - private $mapping_service; |
|
| 11 | + /** |
|
| 12 | + * The {@link Wordlift_Mapping_Service} instance. |
|
| 13 | + * |
|
| 14 | + * @since 3.20.0 |
|
| 15 | + * @access private |
|
| 16 | + * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
| 17 | + */ |
|
| 18 | + private $mapping_service; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
| 22 | - * |
|
| 23 | - * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.20.0 |
|
| 26 | - */ |
|
| 27 | - public function __construct( $mapping_service ) { |
|
| 20 | + /** |
|
| 21 | + * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
| 22 | + * |
|
| 23 | + * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.20.0 |
|
| 26 | + */ |
|
| 27 | + public function __construct( $mapping_service ) { |
|
| 28 | 28 | |
| 29 | - $this->mapping_service = $mapping_service; |
|
| 29 | + $this->mapping_service = $mapping_service; |
|
| 30 | 30 | |
| 31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
| 32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
| 31 | + add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
| 32 | + add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
| 33 | 33 | |
| 34 | - } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - */ |
|
| 39 | - public function set_entity_types_for_post_type() { |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + */ |
|
| 39 | + public function set_entity_types_for_post_type() { |
|
| 40 | 40 | |
| 41 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { |
|
| 42 | - return; |
|
| 43 | - } |
|
| 41 | + if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
|
| 46 | - $entity_types = array_map( 'esc_html', wp_unslash( (array) $_REQUEST['entity_types'] ) ); |
|
| 45 | + $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
|
| 46 | + $entity_types = array_map( 'esc_html', wp_unslash( (array) $_REQUEST['entity_types'] ) ); |
|
| 47 | 47 | |
| 48 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 48 | + $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 49 | 49 | |
| 50 | - wp_send_json_success(); |
|
| 50 | + wp_send_json_success(); |
|
| 51 | 51 | |
| 52 | - } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function update_post_type_entity_types() { |
|
| 54 | + public function update_post_type_entity_types() { |
|
| 55 | 55 | |
| 56 | - // If the nonce is invalid, return an error. |
|
| 57 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ): ''; |
|
| 58 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 59 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 60 | - } |
|
| 56 | + // If the nonce is invalid, return an error. |
|
| 57 | + $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ): ''; |
|
| 58 | + if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 59 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 63 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 64 | - } |
|
| 62 | + if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 63 | + wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 67 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 68 | - } |
|
| 66 | + if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 67 | + wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - // Get the post type. |
|
| 71 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field(wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
| 70 | + // Get the post type. |
|
| 71 | + $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field(wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
| 72 | 72 | |
| 73 | - // Get the entity types URIs. |
|
| 74 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? array_map( 'esc_url_raw', wp_unslash( (array) $_REQUEST['entity_types'] ) ) : array(); |
|
| 73 | + // Get the entity types URIs. |
|
| 74 | + $entity_types = isset( $_REQUEST['entity_types'] ) ? array_map( 'esc_url_raw', wp_unslash( (array) $_REQUEST['entity_types'] ) ) : array(); |
|
| 75 | 75 | |
| 76 | - // Get the offset. |
|
| 77 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 76 | + // Get the offset. |
|
| 77 | + $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 78 | 78 | |
| 79 | - // Update and get the results. |
|
| 80 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 79 | + // Update and get the results. |
|
| 80 | + $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 81 | 81 | |
| 82 | - // Add our nonce to the result. |
|
| 83 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 82 | + // Add our nonce to the result. |
|
| 83 | + $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 84 | 84 | |
| 85 | - // Finally send the results. |
|
| 86 | - wp_send_json_success( $result ); |
|
| 85 | + // Finally send the results. |
|
| 86 | + wp_send_json_success( $result ); |
|
| 87 | 87 | |
| 88 | - } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
@@ -24,12 +24,12 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @since 3.20.0 |
| 26 | 26 | */ |
| 27 | - public function __construct( $mapping_service ) { |
|
| 27 | + public function __construct($mapping_service) { |
|
| 28 | 28 | |
| 29 | 29 | $this->mapping_service = $mapping_service; |
| 30 | 30 | |
| 31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
| 32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
| 31 | + add_action('wp_ajax_wl_set_entity_types_for_post_type', array($this, 'set_entity_types_for_post_type')); |
|
| 32 | + add_action('wp_ajax_wl_update_post_type_entity_types', array($this, 'update_post_type_entity_types')); |
|
| 33 | 33 | |
| 34 | 34 | } |
| 35 | 35 | |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function set_entity_types_for_post_type() { |
| 40 | 40 | |
| 41 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { |
|
| 41 | + if ( ! isset($_REQUEST['post_type']) || ! isset($_REQUEST['entity_types'])) { |
|
| 42 | 42 | return; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
|
| 46 | - $entity_types = array_map( 'esc_html', wp_unslash( (array) $_REQUEST['entity_types'] ) ); |
|
| 45 | + $post_type = sanitize_text_field(wp_unslash($_REQUEST['post_type'])); |
|
| 46 | + $entity_types = array_map('esc_html', wp_unslash((array) $_REQUEST['entity_types'])); |
|
| 47 | 47 | |
| 48 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 48 | + $this->mapping_service->set_entity_types_for_post_type($post_type, $entity_types); |
|
| 49 | 49 | |
| 50 | 50 | wp_send_json_success(); |
| 51 | 51 | |
@@ -54,36 +54,36 @@ discard block |
||
| 54 | 54 | public function update_post_type_entity_types() { |
| 55 | 55 | |
| 56 | 56 | // If the nonce is invalid, return an error. |
| 57 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ): ''; |
|
| 58 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 59 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 57 | + $nonce = isset($_REQUEST['_nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_nonce'])) : ''; |
|
| 58 | + if ( ! wp_verify_nonce($nonce, 'update_post_type_entity_types')) { |
|
| 59 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 63 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 62 | + if (empty($_REQUEST['post_type'])) { |
|
| 63 | + wp_send_json_error(__('`post_type` is required', 'wordlift')); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 67 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 66 | + if (empty($_REQUEST['entity_types'])) { |
|
| 67 | + wp_send_json_error(__('`entity_types` is required', 'wordlift')); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Get the post type. |
| 71 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field(wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
| 71 | + $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field(wp_unslash($_REQUEST['post_type'])) : ''; |
|
| 72 | 72 | |
| 73 | 73 | // Get the entity types URIs. |
| 74 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? array_map( 'esc_url_raw', wp_unslash( (array) $_REQUEST['entity_types'] ) ) : array(); |
|
| 74 | + $entity_types = isset($_REQUEST['entity_types']) ? array_map('esc_url_raw', wp_unslash((array) $_REQUEST['entity_types'])) : array(); |
|
| 75 | 75 | |
| 76 | 76 | // Get the offset. |
| 77 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 77 | + $offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0; |
|
| 78 | 78 | |
| 79 | 79 | // Update and get the results. |
| 80 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 80 | + $result = $this->mapping_service->update($post_type, $entity_types, $offset); |
|
| 81 | 81 | |
| 82 | 82 | // Add our nonce to the result. |
| 83 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 83 | + $result['_nonce'] = wp_create_nonce('update_post_type_entity_types'); |
|
| 84 | 84 | |
| 85 | 85 | // Finally send the results. |
| 86 | - wp_send_json_success( $result ); |
|
| 86 | + wp_send_json_success($result); |
|
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | |
@@ -16,56 +16,56 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Publisher_Ajax_Adapter { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The {@link Wordlift_Publisher_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.11.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $publisher_service; |
|
| 19 | + /** |
|
| 20 | + * The {@link Wordlift_Publisher_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.11.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $publisher_service; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
| 30 | - * |
|
| 31 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * |
|
| 35 | - */ |
|
| 36 | - function __construct( $publisher_service ) { |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
| 30 | + * |
|
| 31 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * |
|
| 35 | + */ |
|
| 36 | + function __construct( $publisher_service ) { |
|
| 37 | 37 | |
| 38 | - $this->publisher_service = $publisher_service; |
|
| 38 | + $this->publisher_service = $publisher_service; |
|
| 39 | 39 | |
| 40 | - } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
| 44 | - * action. |
|
| 45 | - * |
|
| 46 | - * @since 3.11.0 |
|
| 47 | - */ |
|
| 48 | - public function publisher() { |
|
| 42 | + /** |
|
| 43 | + * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
| 44 | + * action. |
|
| 45 | + * |
|
| 46 | + * @since 3.11.0 |
|
| 47 | + */ |
|
| 48 | + public function publisher() { |
|
| 49 | 49 | |
| 50 | - // Ensure we don't have garbage before us. |
|
| 51 | - ob_clean(); |
|
| 50 | + // Ensure we don't have garbage before us. |
|
| 51 | + ob_clean(); |
|
| 52 | 52 | |
| 53 | - // Check if the current user can `manage_options`. |
|
| 54 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 55 | - wp_send_json_error( 'Access denied.' ); |
|
| 56 | - } |
|
| 53 | + // Check if the current user can `manage_options`. |
|
| 54 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 55 | + wp_send_json_error( 'Access denied.' ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - // No actual search parameter was passed, bail out. |
|
| 59 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 60 | - wp_send_json_error( 'The q parameter is required.' ); |
|
| 61 | - } |
|
| 58 | + // No actual search parameter was passed, bail out. |
|
| 59 | + if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 60 | + wp_send_json_error( 'The q parameter is required.' ); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - // Get the response. |
|
| 64 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); |
|
| 63 | + // Get the response. |
|
| 64 | + $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); |
|
| 65 | 65 | |
| 66 | - // Finally output the response. |
|
| 67 | - wp_send_json_success( $response ); |
|
| 66 | + // Finally output the response. |
|
| 67 | + wp_send_json_success( $response ); |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * @since 3.11.0 |
| 34 | 34 | * |
| 35 | 35 | */ |
| 36 | - function __construct( $publisher_service ) { |
|
| 36 | + function __construct($publisher_service) { |
|
| 37 | 37 | |
| 38 | 38 | $this->publisher_service = $publisher_service; |
| 39 | 39 | |
@@ -51,20 +51,20 @@ discard block |
||
| 51 | 51 | ob_clean(); |
| 52 | 52 | |
| 53 | 53 | // Check if the current user can `manage_options`. |
| 54 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 55 | - wp_send_json_error( 'Access denied.' ); |
|
| 54 | + if ( ! current_user_can('manage_options')) { |
|
| 55 | + wp_send_json_error('Access denied.'); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // No actual search parameter was passed, bail out. |
| 59 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 60 | - wp_send_json_error( 'The q parameter is required.' ); |
|
| 59 | + if ( ! isset($_POST['q']) || empty($_POST['q'])) { |
|
| 60 | + wp_send_json_error('The q parameter is required.'); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Get the response. |
| 64 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); |
|
| 64 | + $response = $this->publisher_service->query(sanitize_text_field(wp_unslash((string) $_POST['q']))); |
|
| 65 | 65 | |
| 66 | 66 | // Finally output the response. |
| 67 | - wp_send_json_success( $response ); |
|
| 67 | + wp_send_json_success($response); |
|
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | |