@@ -19,148 +19,148 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Key_Validation_Service { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * A {@link Wordlift_Log_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.14.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $log; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.14.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $configuration_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 42 | - * |
|
| 43 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.14.0 |
|
| 46 | - */ |
|
| 47 | - public function __construct( $configuration_service ) { |
|
| 48 | - |
|
| 49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 50 | - |
|
| 51 | - $this->configuration_service = $configuration_service; |
|
| 52 | - |
|
| 53 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 54 | - /** |
|
| 55 | - * Filter: wl_feature__enable__notices. |
|
| 56 | - * |
|
| 57 | - * @param bool whether the notices needs to be enabled or not. |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - * @since 3.27.6 |
|
| 61 | - */ |
|
| 62 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 63 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Validate the provided key. |
|
| 70 | - * |
|
| 71 | - * @param string $key WordLift's key to validate. |
|
| 72 | - * |
|
| 73 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 74 | - * @since 3.9.0 |
|
| 75 | - * |
|
| 76 | - */ |
|
| 77 | - public function get_account_info( $key ) { |
|
| 78 | - |
|
| 79 | - $this->log->debug( 'Validating key...' ); |
|
| 80 | - |
|
| 81 | - return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 82 | - 'Authorization' => "Key $key", |
|
| 22 | + /** |
|
| 23 | + * A {@link Wordlift_Log_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.14.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $log; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.14.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $configuration_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 42 | + * |
|
| 43 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.14.0 |
|
| 46 | + */ |
|
| 47 | + public function __construct( $configuration_service ) { |
|
| 48 | + |
|
| 49 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 50 | + |
|
| 51 | + $this->configuration_service = $configuration_service; |
|
| 52 | + |
|
| 53 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 54 | + /** |
|
| 55 | + * Filter: wl_feature__enable__notices. |
|
| 56 | + * |
|
| 57 | + * @param bool whether the notices needs to be enabled or not. |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + * @since 3.27.6 |
|
| 61 | + */ |
|
| 62 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 63 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Validate the provided key. |
|
| 70 | + * |
|
| 71 | + * @param string $key WordLift's key to validate. |
|
| 72 | + * |
|
| 73 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 74 | + * @since 3.9.0 |
|
| 75 | + * |
|
| 76 | + */ |
|
| 77 | + public function get_account_info( $key ) { |
|
| 78 | + |
|
| 79 | + $this->log->debug( 'Validating key...' ); |
|
| 80 | + |
|
| 81 | + return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 82 | + 'Authorization' => "Key $key", |
|
| 83 | 83 | ) )->get_response(); |
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 88 | - * |
|
| 89 | - * @since 3.9.0 |
|
| 90 | - */ |
|
| 91 | - public function validate_key() { |
|
| 92 | - |
|
| 93 | - // Ensure we don't have garbage before us. |
|
| 94 | - ob_clean(); |
|
| 95 | - |
|
| 96 | - // Check if we have a key. |
|
| 97 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 98 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $response = $this->get_account_info( $_POST['key'] ); |
|
| 102 | - |
|
| 103 | - // If we got an error, return invalid. |
|
| 104 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 105 | - wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 109 | - |
|
| 110 | - // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 111 | - $url = $res_body['url']; |
|
| 112 | - |
|
| 113 | - // If the URL isn't set or matches, then it's valid. |
|
| 114 | - if ( is_null( $url ) || $url === get_option( 'home' ) ) { |
|
| 115 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 119 | - if ( $url !== get_option( 'home' ) ) { |
|
| 120 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 121 | - wp_send_json_success( array( |
|
| 122 | - 'valid' => false, |
|
| 123 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 124 | - ) ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // Set a response with valid set to true or false according to the key validity with message. |
|
| 128 | - wp_send_json_success( array( |
|
| 129 | - 'valid' => false, |
|
| 130 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 131 | - ) ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 136 | - * |
|
| 137 | - */ |
|
| 138 | - public function wl_load_plugin() { |
|
| 139 | - |
|
| 140 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 141 | - $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 142 | - |
|
| 143 | - if ( ! $wl_blog_url ) { |
|
| 144 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 145 | - } else if ( $wl_blog_url !== $home_url ) { |
|
| 146 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 147 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 148 | - 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 ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 155 | - * |
|
| 156 | - */ |
|
| 157 | - public function wl_key_update_notice() { |
|
| 158 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 159 | - ?> |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 88 | + * |
|
| 89 | + * @since 3.9.0 |
|
| 90 | + */ |
|
| 91 | + public function validate_key() { |
|
| 92 | + |
|
| 93 | + // Ensure we don't have garbage before us. |
|
| 94 | + ob_clean(); |
|
| 95 | + |
|
| 96 | + // Check if we have a key. |
|
| 97 | + if ( ! isset( $_POST['key'] ) ) { |
|
| 98 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $response = $this->get_account_info( $_POST['key'] ); |
|
| 102 | + |
|
| 103 | + // If we got an error, return invalid. |
|
| 104 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 105 | + wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 109 | + |
|
| 110 | + // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 111 | + $url = $res_body['url']; |
|
| 112 | + |
|
| 113 | + // If the URL isn't set or matches, then it's valid. |
|
| 114 | + if ( is_null( $url ) || $url === get_option( 'home' ) ) { |
|
| 115 | + wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 119 | + if ( $url !== get_option( 'home' ) ) { |
|
| 120 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 121 | + wp_send_json_success( array( |
|
| 122 | + 'valid' => false, |
|
| 123 | + 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 124 | + ) ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // Set a response with valid set to true or false according to the key validity with message. |
|
| 128 | + wp_send_json_success( array( |
|
| 129 | + 'valid' => false, |
|
| 130 | + 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 131 | + ) ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | + public function wl_load_plugin() { |
|
| 139 | + |
|
| 140 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 141 | + $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 142 | + |
|
| 143 | + if ( ! $wl_blog_url ) { |
|
| 144 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 145 | + } else if ( $wl_blog_url !== $home_url ) { |
|
| 146 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 147 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 148 | + 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 ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 155 | + * |
|
| 156 | + */ |
|
| 157 | + public function wl_key_update_notice() { |
|
| 158 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 159 | + ?> |
|
| 160 | 160 | <div class="updated notice is-dismissible error"> |
| 161 | 161 | <p><?php _e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
| 162 | 162 | </div> |
| 163 | 163 | <?php |
| 164 | - } |
|
| 165 | - } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @since 3.14.0 |
| 46 | 46 | */ |
| 47 | - public function __construct( $configuration_service ) { |
|
| 47 | + public function __construct($configuration_service) { |
|
| 48 | 48 | |
| 49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 49 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service'); |
|
| 50 | 50 | |
| 51 | 51 | $this->configuration_service = $configuration_service; |
| 52 | 52 | |
| 53 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 53 | + add_action('admin_init', array($this, 'wl_load_plugin')); |
|
| 54 | 54 | /** |
| 55 | 55 | * Filter: wl_feature__enable__notices. |
| 56 | 56 | * |
@@ -59,8 +59,8 @@ discard block |
||
| 59 | 59 | * @return bool |
| 60 | 60 | * @since 3.27.6 |
| 61 | 61 | */ |
| 62 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 63 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 62 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 63 | + add_action('admin_notices', array($this, 'wl_key_update_notice')); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | } |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | * @since 3.9.0 |
| 75 | 75 | * |
| 76 | 76 | */ |
| 77 | - public function get_account_info( $key ) { |
|
| 77 | + public function get_account_info($key) { |
|
| 78 | 78 | |
| 79 | - $this->log->debug( 'Validating key...' ); |
|
| 79 | + $this->log->debug('Validating key...'); |
|
| 80 | 80 | |
| 81 | - return Default_Api_Service::get_instance()->get( '/accounts/info', array( |
|
| 81 | + return Default_Api_Service::get_instance()->get('/accounts/info', array( |
|
| 82 | 82 | 'Authorization' => "Key $key", |
| 83 | - ) )->get_response(); |
|
| 83 | + ))->get_response(); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -94,41 +94,41 @@ discard block |
||
| 94 | 94 | ob_clean(); |
| 95 | 95 | |
| 96 | 96 | // Check if we have a key. |
| 97 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 98 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 97 | + if ( ! isset($_POST['key'])) { |
|
| 98 | + wp_send_json_error('The key parameter is required.'); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $response = $this->get_account_info( $_POST['key'] ); |
|
| 101 | + $response = $this->get_account_info($_POST['key']); |
|
| 102 | 102 | |
| 103 | 103 | // If we got an error, return invalid. |
| 104 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 105 | - wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 104 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
| 105 | + wp_send_json_success(array('valid' => false, 'message' => '')); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 108 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
| 109 | 109 | |
| 110 | 110 | // The URL stored in WLS. If this is the initial install the URL may be null. |
| 111 | 111 | $url = $res_body['url']; |
| 112 | 112 | |
| 113 | 113 | // If the URL isn't set or matches, then it's valid. |
| 114 | - if ( is_null( $url ) || $url === get_option( 'home' ) ) { |
|
| 115 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 114 | + if (is_null($url) || $url === get_option('home')) { |
|
| 115 | + wp_send_json_success(array('valid' => true, 'message' => '')); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // If the URL doesn't match it means that this key has been configured elsewhere already. |
| 119 | - if ( $url !== get_option( 'home' ) ) { |
|
| 120 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 121 | - wp_send_json_success( array( |
|
| 119 | + if ($url !== get_option('home')) { |
|
| 120 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 121 | + wp_send_json_success(array( |
|
| 122 | 122 | 'valid' => false, |
| 123 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 124 | - ) ); |
|
| 123 | + 'message' => __('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift'), |
|
| 124 | + )); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | // Set a response with valid set to true or false according to the key validity with message. |
| 128 | - wp_send_json_success( array( |
|
| 128 | + wp_send_json_success(array( |
|
| 129 | 129 | 'valid' => false, |
| 130 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 131 | - ) ); |
|
| 130 | + 'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'), |
|
| 131 | + )); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -137,15 +137,15 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function wl_load_plugin() { |
| 139 | 139 | |
| 140 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 141 | - $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 140 | + $wl_blog_url = get_option('_wl_blog_url'); |
|
| 141 | + $home_url = defined('WP_HOME') ? WP_HOME : get_option('home'); |
|
| 142 | 142 | |
| 143 | - if ( ! $wl_blog_url ) { |
|
| 144 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 145 | - } else if ( $wl_blog_url !== $home_url ) { |
|
| 146 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 147 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 148 | - 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 ); |
|
| 143 | + if ( ! $wl_blog_url) { |
|
| 144 | + update_option('_wl_blog_url', $home_url, true); |
|
| 145 | + } else if ($wl_blog_url !== $home_url) { |
|
| 146 | + update_option('_wl_blog_url', $home_url, true); |
|
| 147 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 148 | + 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); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | } |
@@ -155,10 +155,10 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | */ |
| 157 | 157 | public function wl_key_update_notice() { |
| 158 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 158 | + if (get_transient('wl-key-error-msg')) { |
|
| 159 | 159 | ?> |
| 160 | 160 | <div class="updated notice is-dismissible error"> |
| 161 | - <p><?php _e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
|
| 161 | + <p><?php _e(get_transient('wl-key-error-msg'), 'wordlift'); ?></p> |
|
| 162 | 162 | </div> |
| 163 | 163 | <?php |
| 164 | 164 | } |
@@ -9,15 +9,15 @@ discard block |
||
| 9 | 9 | * @since 3.27.6 |
| 10 | 10 | */ |
| 11 | 11 | function wl_ajax_analyze_disabled_action() { |
| 12 | - // adding the below header for debugging purpose. |
|
| 13 | - if ( ! headers_sent() ) { |
|
| 14 | - header( 'X-WordLift-Analysis: OFF' ); |
|
| 15 | - } |
|
| 16 | - wp_send_json_success( array( |
|
| 17 | - 'entities' => array(), |
|
| 18 | - 'annotations' => array(), |
|
| 19 | - 'topics' => array() |
|
| 20 | - ) ); |
|
| 12 | + // adding the below header for debugging purpose. |
|
| 13 | + if ( ! headers_sent() ) { |
|
| 14 | + header( 'X-WordLift-Analysis: OFF' ); |
|
| 15 | + } |
|
| 16 | + wp_send_json_success( array( |
|
| 17 | + 'entities' => array(), |
|
| 18 | + 'annotations' => array(), |
|
| 19 | + 'topics' => array() |
|
| 20 | + ) ); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | function wl_ajax_analyze_action() { |
| 33 | 33 | |
| 34 | - check_admin_referer( 'wl_analyze' ); |
|
| 34 | + check_admin_referer( 'wl_analyze' ); |
|
| 35 | 35 | |
| 36 | - // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
|
| 37 | - // `$_POST`. |
|
| 38 | - $data = $_POST['data']; |
|
| 36 | + // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
|
| 37 | + // `$_POST`. |
|
| 38 | + $data = $_POST['data']; |
|
| 39 | 39 | |
| 40 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 40 | + wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -59,62 +59,62 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | function wl_analyze_content( $data, $content_type ) { |
| 61 | 61 | |
| 62 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 63 | - // Using stripslashes since 3.27.7 as $_POST['data'] is escaped |
|
| 64 | - $request_body = json_decode( stripslashes($data), true ); |
|
| 65 | - $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 66 | - $data = wp_json_encode( $request_body ); |
|
| 67 | - |
|
| 68 | - // If dataset is not enabled, return a locally prepared response without analysis API. |
|
| 69 | - if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 70 | - |
|
| 71 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 72 | - ->create( $default_response ) |
|
| 73 | - ->make_entities_local() |
|
| 74 | - ->add_occurrences( $request_body['content'] ) |
|
| 75 | - ->add_local_entities() |
|
| 76 | - ->get_json(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 80 | - $json = Wordlift_Api_Service::get_instance() |
|
| 81 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 82 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 83 | - |
|
| 84 | - // If it's an error log it. |
|
| 85 | - if ( is_wp_error( $json ) ) { |
|
| 86 | - |
|
| 87 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 88 | - ->create( $default_response ) |
|
| 89 | - ->make_entities_local() |
|
| 90 | - ->add_occurrences( $request_body['content'] ) |
|
| 91 | - ->get_json(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /* |
|
| 62 | + $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 63 | + // Using stripslashes since 3.27.7 as $_POST['data'] is escaped |
|
| 64 | + $request_body = json_decode( stripslashes($data), true ); |
|
| 65 | + $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 66 | + $data = wp_json_encode( $request_body ); |
|
| 67 | + |
|
| 68 | + // If dataset is not enabled, return a locally prepared response without analysis API. |
|
| 69 | + if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 70 | + |
|
| 71 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 72 | + ->create( $default_response ) |
|
| 73 | + ->make_entities_local() |
|
| 74 | + ->add_occurrences( $request_body['content'] ) |
|
| 75 | + ->add_local_entities() |
|
| 76 | + ->get_json(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 80 | + $json = Wordlift_Api_Service::get_instance() |
|
| 81 | + ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 82 | + remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 83 | + |
|
| 84 | + // If it's an error log it. |
|
| 85 | + if ( is_wp_error( $json ) ) { |
|
| 86 | + |
|
| 87 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 88 | + ->create( $default_response ) |
|
| 89 | + ->make_entities_local() |
|
| 90 | + ->add_occurrences( $request_body['content'] ) |
|
| 91 | + ->get_json(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /* |
|
| 95 | 95 | * We pass the response to the Analysis_Response_Ops to ensure that we make remote entities local. |
| 96 | 96 | * |
| 97 | 97 | * @see https://github.com/insideout10/wordlift-plugin/issues/944 |
| 98 | 98 | * @since 3.21.5 |
| 99 | 99 | */ |
| 100 | 100 | |
| 101 | - // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 102 | - // the occurrences for the local entities. |
|
| 103 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 104 | - $request_content = $request_body['content']; |
|
| 105 | - } else { |
|
| 106 | - $request_content = $data; |
|
| 107 | - } |
|
| 101 | + // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 102 | + // the occurrences for the local entities. |
|
| 103 | + if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 104 | + $request_content = $request_body['content']; |
|
| 105 | + } else { |
|
| 106 | + $request_content = $data; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 110 | - ->create( $json ) |
|
| 111 | - ->make_entities_local() |
|
| 112 | - ->add_occurrences( $request_content ) |
|
| 113 | - ->get_json(); |
|
| 109 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 110 | + ->create( $json ) |
|
| 111 | + ->make_entities_local() |
|
| 112 | + ->add_occurrences( $request_content ) |
|
| 113 | + ->get_json(); |
|
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | function wl_use_analysis_on_api_wordlift_io( $value ) { |
| 118 | 118 | |
| 119 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 119 | + return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 120 | 120 | } |
@@ -10,14 +10,14 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_ajax_analyze_disabled_action() { |
| 12 | 12 | // adding the below header for debugging purpose. |
| 13 | - if ( ! headers_sent() ) { |
|
| 14 | - header( 'X-WordLift-Analysis: OFF' ); |
|
| 13 | + if ( ! headers_sent()) { |
|
| 14 | + header('X-WordLift-Analysis: OFF'); |
|
| 15 | 15 | } |
| 16 | - wp_send_json_success( array( |
|
| 16 | + wp_send_json_success(array( |
|
| 17 | 17 | 'entities' => array(), |
| 18 | 18 | 'annotations' => array(), |
| 19 | 19 | 'topics' => array() |
| 20 | - ) ); |
|
| 20 | + )); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | function wl_ajax_analyze_action() { |
| 33 | 33 | |
| 34 | - check_admin_referer( 'wl_analyze' ); |
|
| 34 | + check_admin_referer('wl_analyze'); |
|
| 35 | 35 | |
| 36 | 36 | // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
| 37 | 37 | // `$_POST`. |
| 38 | 38 | $data = $_POST['data']; |
| 39 | 39 | |
| 40 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 40 | + wp_send_json_success(wl_analyze_content($data, 'application/json; charset='.strtolower(get_bloginfo('charset')))); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -57,37 +57,37 @@ discard block |
||
| 57 | 57 | * @since 3.24.2 We don't return an error anymore, but an empty analysis response. This is required to allow the editor |
| 58 | 58 | * to manage entities or to manually add them even when analysis isn't available. |
| 59 | 59 | */ |
| 60 | -function wl_analyze_content( $data, $content_type ) { |
|
| 60 | +function wl_analyze_content($data, $content_type) { |
|
| 61 | 61 | |
| 62 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 62 | + $default_response = json_decode('{ "entities": {}, "annotations": {}, "topics": {} }'); |
|
| 63 | 63 | // Using stripslashes since 3.27.7 as $_POST['data'] is escaped |
| 64 | - $request_body = json_decode( stripslashes($data), true ); |
|
| 64 | + $request_body = json_decode(stripslashes($data), true); |
|
| 65 | 65 | $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
| 66 | - $data = wp_json_encode( $request_body ); |
|
| 66 | + $data = wp_json_encode($request_body); |
|
| 67 | 67 | |
| 68 | 68 | // If dataset is not enabled, return a locally prepared response without analysis API. |
| 69 | - if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 69 | + if ( ! apply_filters('wl_features__enable__dataset', true)) { |
|
| 70 | 70 | |
| 71 | 71 | return Analysis_Response_Ops_Factory::get_instance() |
| 72 | - ->create( $default_response ) |
|
| 72 | + ->create($default_response) |
|
| 73 | 73 | ->make_entities_local() |
| 74 | - ->add_occurrences( $request_body['content'] ) |
|
| 74 | + ->add_occurrences($request_body['content']) |
|
| 75 | 75 | ->add_local_entities() |
| 76 | 76 | ->get_json(); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 79 | + add_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 80 | 80 | $json = Wordlift_Api_Service::get_instance() |
| 81 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 82 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 81 | + ->post_custom_content_type('analysis/single', $data, $content_type); |
|
| 82 | + remove_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 83 | 83 | |
| 84 | 84 | // If it's an error log it. |
| 85 | - if ( is_wp_error( $json ) ) { |
|
| 85 | + if (is_wp_error($json)) { |
|
| 86 | 86 | |
| 87 | 87 | return Analysis_Response_Ops_Factory::get_instance() |
| 88 | - ->create( $default_response ) |
|
| 88 | + ->create($default_response) |
|
| 89 | 89 | ->make_entities_local() |
| 90 | - ->add_occurrences( $request_body['content'] ) |
|
| 90 | + ->add_occurrences($request_body['content']) |
|
| 91 | 91 | ->get_json(); |
| 92 | 92 | } |
| 93 | 93 | |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
| 102 | 102 | // the occurrences for the local entities. |
| 103 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 103 | + if (0 === strpos($content_type, 'application/json')) { |
|
| 104 | 104 | $request_content = $request_body['content']; |
| 105 | 105 | } else { |
| 106 | 106 | $request_content = $data; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | return Analysis_Response_Ops_Factory::get_instance() |
| 110 | - ->create( $json ) |
|
| 110 | + ->create($json) |
|
| 111 | 111 | ->make_entities_local() |
| 112 | - ->add_occurrences( $request_content ) |
|
| 112 | + ->add_occurrences($request_content) |
|
| 113 | 113 | ->get_json(); |
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | -function wl_use_analysis_on_api_wordlift_io( $value ) { |
|
| 117 | +function wl_use_analysis_on_api_wordlift_io($value) { |
|
| 118 | 118 | |
| 119 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 119 | + return preg_replace('|https://api\.wordlift\.it/|', apply_filters('wl_api_base_url', 'https://api.wordlift.io').'/', $value); |
|
| 120 | 120 | } |