@@ -4,186 +4,186 @@ |
||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | class Config { |
| 7 | - /** |
|
| 8 | - * @var \Wordlift_Admin_Setup |
|
| 9 | - */ |
|
| 10 | - private $admin_setup; |
|
| 11 | - /** |
|
| 12 | - * @var \Wordlift_Key_Validation_Service |
|
| 13 | - */ |
|
| 14 | - private $key_validation_service; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Config constructor. |
|
| 18 | - * |
|
| 19 | - * @param $admin_setup \Wordlift_Admin_Setup |
|
| 20 | - * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 21 | - */ |
|
| 22 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 23 | - |
|
| 24 | - $this->admin_setup = $admin_setup; |
|
| 25 | - $this->key_validation_service = $key_validation_service; |
|
| 26 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 27 | - |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Check if the key is valid and also not bound to any domain. |
|
| 32 | - * |
|
| 33 | - * @param $key string |
|
| 34 | - * |
|
| 35 | - * @return bool |
|
| 36 | - */ |
|
| 37 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * we need to check if the key is not associated with any account |
|
| 42 | - * before setting it, we should check if the url is null. |
|
| 43 | - */ |
|
| 44 | - if ( is_wp_error( $account_info ) |
|
| 45 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $account_info_json = $account_info['body']; |
|
| 50 | - |
|
| 51 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 52 | - |
|
| 53 | - if ( ! $account_info_data ) { |
|
| 54 | - // Invalid json returned by api. |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - if ( $account_info_data['url'] === null ) { |
|
| 62 | - return true; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Check if the key belongs to same site. |
|
| 66 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 67 | - // key already associated with another account. |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Return true if the key domain and site domain are the same. |
|
| 72 | - return true; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - public function config() { |
|
| 77 | - |
|
| 78 | - // Perform validation check for all the parameters. |
|
| 79 | - $required_fields = array( |
|
| 80 | - 'diagnostic', |
|
| 81 | - 'vocabulary', |
|
| 7 | + /** |
|
| 8 | + * @var \Wordlift_Admin_Setup |
|
| 9 | + */ |
|
| 10 | + private $admin_setup; |
|
| 11 | + /** |
|
| 12 | + * @var \Wordlift_Key_Validation_Service |
|
| 13 | + */ |
|
| 14 | + private $key_validation_service; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Config constructor. |
|
| 18 | + * |
|
| 19 | + * @param $admin_setup \Wordlift_Admin_Setup |
|
| 20 | + * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 21 | + */ |
|
| 22 | + public function __construct( $admin_setup, $key_validation_service ) { |
|
| 23 | + |
|
| 24 | + $this->admin_setup = $admin_setup; |
|
| 25 | + $this->key_validation_service = $key_validation_service; |
|
| 26 | + add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 27 | + |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Check if the key is valid and also not bound to any domain. |
|
| 32 | + * |
|
| 33 | + * @param $key string |
|
| 34 | + * |
|
| 35 | + * @return bool |
|
| 36 | + */ |
|
| 37 | + private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | + $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * we need to check if the key is not associated with any account |
|
| 42 | + * before setting it, we should check if the url is null. |
|
| 43 | + */ |
|
| 44 | + if ( is_wp_error( $account_info ) |
|
| 45 | + || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $account_info_json = $account_info['body']; |
|
| 50 | + |
|
| 51 | + $account_info_data = json_decode( $account_info_json, true ); |
|
| 52 | + |
|
| 53 | + if ( ! $account_info_data ) { |
|
| 54 | + // Invalid json returned by api. |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + if ( $account_info_data['url'] === null ) { |
|
| 62 | + return true; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Check if the key belongs to same site. |
|
| 66 | + if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 67 | + // key already associated with another account. |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Return true if the key domain and site domain are the same. |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + public function config() { |
|
| 77 | + |
|
| 78 | + // Perform validation check for all the parameters. |
|
| 79 | + $required_fields = array( |
|
| 80 | + 'diagnostic', |
|
| 81 | + 'vocabulary', |
|
| 82 | 82 | // Don't ask for language from webapp. |
| 83 | 83 | // 'language', |
| 84 | - 'country', |
|
| 85 | - 'publisherName', |
|
| 86 | - 'publisher', |
|
| 87 | - 'license' |
|
| 88 | - ); |
|
| 84 | + 'country', |
|
| 85 | + 'publisherName', |
|
| 86 | + 'publisher', |
|
| 87 | + 'license' |
|
| 88 | + ); |
|
| 89 | 89 | |
| 90 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 90 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 91 | 91 | |
| 92 | - // validate all the fields before processing |
|
| 93 | - foreach ( $required_fields as $field ) { |
|
| 94 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 92 | + // validate all the fields before processing |
|
| 93 | + foreach ( $required_fields as $field ) { |
|
| 94 | + if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | + wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 96 | 96 | |
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - } |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - $key = (string) $_POST['license']; |
|
| 101 | + $key = (string) $_POST['license']; |
|
| 102 | 102 | |
| 103 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | + wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 105 | 105 | |
| 106 | - // exit if not valid. |
|
| 107 | - return; |
|
| 108 | - } |
|
| 106 | + // exit if not valid. |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - // check if key is already configured, if yes then dont save settings. |
|
| 112 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 111 | + // check if key is already configured, if yes then dont save settings. |
|
| 112 | + if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | + wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 114 | 114 | |
| 115 | - // key already configured |
|
| 116 | - return; |
|
| 117 | - } |
|
| 115 | + // key already configured |
|
| 116 | + return; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 119 | + $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 123 | - } |
|
| 122 | + wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - private function get_params() { |
|
| 125 | + /** |
|
| 126 | + * |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + private function get_params() { |
|
| 130 | 130 | |
| 131 | - $attachment_id = $this->may_be_get_attachment_id(); |
|
| 131 | + $attachment_id = $this->may_be_get_attachment_id(); |
|
| 132 | 132 | |
| 133 | - $params = array( |
|
| 134 | - 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 135 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 136 | - 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 137 | - 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 138 | - 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 139 | - 'logo' => $attachment_id |
|
| 140 | - ); |
|
| 133 | + $params = array( |
|
| 134 | + 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 135 | + 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 136 | + 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 137 | + 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 138 | + 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 139 | + 'logo' => $attachment_id |
|
| 140 | + ); |
|
| 141 | 141 | |
| 142 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | - if ( $diagnostic ) { |
|
| 144 | - $params['share-diagnostic'] = 'on'; |
|
| 145 | - } |
|
| 142 | + $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | + if ( $diagnostic ) { |
|
| 144 | + $params['share-diagnostic'] = 'on'; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return $params; |
|
| 148 | - } |
|
| 147 | + return $params; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @return int | bool |
|
| 152 | - */ |
|
| 153 | - private function may_be_get_attachment_id() { |
|
| 150 | + /** |
|
| 151 | + * @return int | bool |
|
| 152 | + */ |
|
| 153 | + private function may_be_get_attachment_id() { |
|
| 154 | 154 | |
| 155 | - // if image or image extension not posted then return false. |
|
| 156 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 155 | + // if image or image extension not posted then return false. |
|
| 156 | + if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 161 | - $image_string = (string) $_POST['image']; |
|
| 162 | - $image_ext = (string) $_POST['imageExtension']; |
|
| 160 | + $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 161 | + $image_string = (string) $_POST['image']; |
|
| 162 | + $image_ext = (string) $_POST['imageExtension']; |
|
| 163 | 163 | |
| 164 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 164 | + if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 168 | + $image_decoded_string = base64_decode( $image_string ); |
|
| 169 | 169 | |
| 170 | - $upload_dir = wp_upload_dir(); |
|
| 170 | + $upload_dir = wp_upload_dir(); |
|
| 171 | 171 | |
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 172 | + $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 173 | 173 | |
| 174 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 174 | + file_put_contents( $file_path, $image_decoded_string ); |
|
| 175 | 175 | |
| 176 | - $attachment_id = wp_insert_attachment( array( |
|
| 177 | - 'post_status' => 'inherit', |
|
| 178 | - 'post_mime_type' => "image/$image_ext" |
|
| 179 | - ), $file_path ); |
|
| 176 | + $attachment_id = wp_insert_attachment( array( |
|
| 177 | + 'post_status' => 'inherit', |
|
| 178 | + 'post_mime_type' => "image/$image_ext" |
|
| 179 | + ), $file_path ); |
|
| 180 | 180 | |
| 181 | - // Generate the metadata for the attachment, and update the database record. |
|
| 182 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 183 | - // Update the attachment metadata. |
|
| 184 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 181 | + // Generate the metadata for the attachment, and update the database record. |
|
| 182 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 183 | + // Update the attachment metadata. |
|
| 184 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 185 | 185 | |
| 186 | - return $attachment_id; |
|
| 187 | - } |
|
| 186 | + return $attachment_id; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | } |
@@ -19,11 +19,11 @@ discard block |
||
| 19 | 19 | * @param $admin_setup \Wordlift_Admin_Setup |
| 20 | 20 | * @param $key_validation_service \Wordlift_Key_Validation_Service |
| 21 | 21 | */ |
| 22 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 22 | + public function __construct($admin_setup, $key_validation_service) { |
|
| 23 | 23 | |
| 24 | 24 | $this->admin_setup = $admin_setup; |
| 25 | 25 | $this->key_validation_service = $key_validation_service; |
| 26 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 26 | + add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config')); |
|
| 27 | 27 | |
| 28 | 28 | } |
| 29 | 29 | |
@@ -34,36 +34,36 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @return bool |
| 36 | 36 | */ |
| 37 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 38 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 37 | + private function is_key_valid_and_not_bound_to_any_domain($key) { |
|
| 38 | + $account_info = $this->key_validation_service->get_account_info($key); |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * we need to check if the key is not associated with any account |
| 42 | 42 | * before setting it, we should check if the url is null. |
| 43 | 43 | */ |
| 44 | - if ( is_wp_error( $account_info ) |
|
| 45 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 44 | + if (is_wp_error($account_info) |
|
| 45 | + || wp_remote_retrieve_response_code($account_info) !== 200) { |
|
| 46 | 46 | return false; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $account_info_json = $account_info['body']; |
| 50 | 50 | |
| 51 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 51 | + $account_info_data = json_decode($account_info_json, true); |
|
| 52 | 52 | |
| 53 | - if ( ! $account_info_data ) { |
|
| 53 | + if ( ! $account_info_data) { |
|
| 54 | 54 | // Invalid json returned by api. |
| 55 | 55 | return false; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 58 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - if ( $account_info_data['url'] === null ) { |
|
| 61 | + if ($account_info_data['url'] === null) { |
|
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | // Check if the key belongs to same site. |
| 66 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 66 | + if ($site_url !== untrailingslashit($account_info_data['url'])) { |
|
| 67 | 67 | // key already associated with another account. |
| 68 | 68 | return false; |
| 69 | 69 | } |
@@ -87,12 +87,12 @@ discard block |
||
| 87 | 87 | 'license' |
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 90 | + header('Access-Control-Allow-Origin: *'); |
|
| 91 | 91 | |
| 92 | 92 | // validate all the fields before processing |
| 93 | - foreach ( $required_fields as $field ) { |
|
| 94 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 95 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 93 | + foreach ($required_fields as $field) { |
|
| 94 | + if ( ! array_key_exists($field, $_POST)) { |
|
| 95 | + wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422); |
|
| 96 | 96 | |
| 97 | 97 | return; |
| 98 | 98 | } |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | $key = (string) $_POST['license']; |
| 102 | 102 | |
| 103 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 104 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) { |
|
| 104 | + wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403); |
|
| 105 | 105 | |
| 106 | 106 | // exit if not valid. |
| 107 | 107 | return; |
@@ -109,17 +109,17 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | |
| 111 | 111 | // check if key is already configured, if yes then dont save settings. |
| 112 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 113 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 112 | + if (\Wordlift_Configuration_Service::get_instance()->get_key()) { |
|
| 113 | + wp_send_json_error(__('Key already configured.', 'wordlift'), 403); |
|
| 114 | 114 | |
| 115 | 115 | // key already configured |
| 116 | 116 | return; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 119 | + $this->admin_setup->save_configuration($this->get_params()); |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 122 | + wp_send_json_success(__('Configuration Saved', 'wordlift')); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -131,16 +131,16 @@ discard block |
||
| 131 | 131 | $attachment_id = $this->may_be_get_attachment_id(); |
| 132 | 132 | |
| 133 | 133 | $params = array( |
| 134 | - 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 135 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 136 | - 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 137 | - 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 138 | - 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 134 | + 'key' => isset($_POST['license']) ? (string) $_POST['license'] : '', |
|
| 135 | + 'vocabulary' => isset($_POST['vocabulary']) ? (string) $_POST['vocabulary'] : '', |
|
| 136 | + 'wl-country-code' => isset($_POST['country']) ? (string) $_POST['country'] : '', |
|
| 137 | + 'name' => isset($_POST['publisherName']) ? (string) $_POST['publisherName'] : '', |
|
| 138 | + 'user_type' => isset($_POST['publisher']) ? (string) $_POST['publisher'] : '', |
|
| 139 | 139 | 'logo' => $attachment_id |
| 140 | 140 | ); |
| 141 | 141 | |
| 142 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | - if ( $diagnostic ) { |
|
| 142 | + $diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false; |
|
| 143 | + if ($diagnostic) { |
|
| 144 | 144 | $params['share-diagnostic'] = 'on'; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -153,35 +153,35 @@ discard block |
||
| 153 | 153 | private function may_be_get_attachment_id() { |
| 154 | 154 | |
| 155 | 155 | // if image or image extension not posted then return false. |
| 156 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 156 | + if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) { |
|
| 157 | 157 | return false; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 160 | + $allowed_extensions = array('png', 'jpeg', 'jpg'); |
|
| 161 | 161 | $image_string = (string) $_POST['image']; |
| 162 | 162 | $image_ext = (string) $_POST['imageExtension']; |
| 163 | 163 | |
| 164 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 164 | + if ( ! in_array($image_ext, $allowed_extensions)) { |
|
| 165 | 165 | return false; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 168 | + $image_decoded_string = base64_decode($image_string); |
|
| 169 | 169 | |
| 170 | 170 | $upload_dir = wp_upload_dir(); |
| 171 | 171 | |
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 172 | + $file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).".".$image_ext; |
|
| 173 | 173 | |
| 174 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 174 | + file_put_contents($file_path, $image_decoded_string); |
|
| 175 | 175 | |
| 176 | - $attachment_id = wp_insert_attachment( array( |
|
| 176 | + $attachment_id = wp_insert_attachment(array( |
|
| 177 | 177 | 'post_status' => 'inherit', |
| 178 | 178 | 'post_mime_type' => "image/$image_ext" |
| 179 | - ), $file_path ); |
|
| 179 | + ), $file_path); |
|
| 180 | 180 | |
| 181 | 181 | // Generate the metadata for the attachment, and update the database record. |
| 182 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 182 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); |
|
| 183 | 183 | // Update the attachment metadata. |
| 184 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 184 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 185 | 185 | |
| 186 | 186 | return $attachment_id; |
| 187 | 187 | } |
@@ -19,254 +19,254 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Post_Adapter { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The post id to which the adopter relates. |
|
| 24 | - * |
|
| 25 | - * @since 3.14.0 |
|
| 26 | - * |
|
| 27 | - * @var integer $post_id . |
|
| 28 | - */ |
|
| 29 | - private $post_id; |
|
| 30 | - |
|
| 31 | - const TYPE_ENTITY_LINK = 0; |
|
| 32 | - const TYPE_TERM_LINK = 1; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 36 | - * |
|
| 37 | - * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 38 | - * |
|
| 39 | - * @since 3.14.0 |
|
| 40 | - * |
|
| 41 | - */ |
|
| 42 | - public function __construct( $post_id ) { |
|
| 43 | - |
|
| 44 | - $this->post_id = $post_id; |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Get the word count of the post content. |
|
| 50 | - * |
|
| 51 | - * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 52 | - * |
|
| 53 | - * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 54 | - * @since 3.14.0 |
|
| 55 | - * |
|
| 56 | - */ |
|
| 57 | - public function word_count() { |
|
| 58 | - |
|
| 59 | - $post = get_post( $this->post_id ); |
|
| 60 | - |
|
| 61 | - /* |
|
| 22 | + /** |
|
| 23 | + * The post id to which the adopter relates. |
|
| 24 | + * |
|
| 25 | + * @since 3.14.0 |
|
| 26 | + * |
|
| 27 | + * @var integer $post_id . |
|
| 28 | + */ |
|
| 29 | + private $post_id; |
|
| 30 | + |
|
| 31 | + const TYPE_ENTITY_LINK = 0; |
|
| 32 | + const TYPE_TERM_LINK = 1; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 36 | + * |
|
| 37 | + * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 38 | + * |
|
| 39 | + * @since 3.14.0 |
|
| 40 | + * |
|
| 41 | + */ |
|
| 42 | + public function __construct( $post_id ) { |
|
| 43 | + |
|
| 44 | + $this->post_id = $post_id; |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Get the word count of the post content. |
|
| 50 | + * |
|
| 51 | + * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 52 | + * |
|
| 53 | + * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 54 | + * @since 3.14.0 |
|
| 55 | + * |
|
| 56 | + */ |
|
| 57 | + public function word_count() { |
|
| 58 | + |
|
| 59 | + $post = get_post( $this->post_id ); |
|
| 60 | + |
|
| 61 | + /* |
|
| 62 | 62 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 63 | 63 | * because the content is written elsewhere. |
| 64 | 64 | * |
| 65 | 65 | * @since 3.20.0 |
| 66 | 66 | */ |
| 67 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 68 | - |
|
| 69 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Count words in the string, taking into account UTF-8 characters. |
|
| 74 | - * |
|
| 75 | - * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
| 76 | - * |
|
| 77 | - * @since 3.20.0 |
|
| 78 | - * |
|
| 79 | - * @param string $str The target string. |
|
| 80 | - * |
|
| 81 | - * @return int The number of words. |
|
| 82 | - */ |
|
| 83 | - private static function str_word_count_utf8( $str ) { |
|
| 84 | - |
|
| 85 | - return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 90 | - * |
|
| 91 | - * @param int $post_id Post ID. |
|
| 92 | - * |
|
| 93 | - * @return string The post permalink. |
|
| 94 | - * @since 3.20.0 |
|
| 95 | - * |
|
| 96 | - */ |
|
| 97 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 98 | - |
|
| 99 | - $object_link_service = Object_Link_Provider::get_instance(); |
|
| 100 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
| 104 | - * |
|
| 105 | - * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
| 106 | - * post permalink if we find them. |
|
| 107 | - */ |
|
| 108 | - global $wp_version; |
|
| 109 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 110 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 115 | - * when the production environment is copied over from a staging environment with staging |
|
| 116 | - * URLs. |
|
| 117 | - * |
|
| 118 | - * @param string $permalink_url The default permalink. |
|
| 119 | - * @param int $post_id The post id. |
|
| 120 | - * |
|
| 121 | - * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
| 122 | - * @since 3.20.0 |
|
| 123 | - * |
|
| 124 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - $uris = $object_link_service->get_same_as_uris( |
|
| 130 | - $post_id, |
|
| 131 | - $object_type |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
| 135 | - $terms = array(); |
|
| 136 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 137 | - // Try to find one term matching the entity. |
|
| 138 | - $terms = get_terms( array( |
|
| 139 | - 'number' => 1, |
|
| 140 | - 'hide_empty' => false, |
|
| 141 | - 'update_term_meta_cache' => false, |
|
| 142 | - 'meta_key' => '_wl_entity_id', |
|
| 143 | - 'meta_value' => $uris, |
|
| 144 | - 'meta_compare' => 'IN', |
|
| 145 | - ) ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $type = self::TYPE_ENTITY_LINK; |
|
| 149 | - $term = null; |
|
| 150 | - // If found use the term link, otherwise the permalink. |
|
| 151 | - if ( 1 === count( $terms ) ) { |
|
| 152 | - $term = current( $terms ); |
|
| 153 | - $permalink = get_term_link( $term ); |
|
| 154 | - $type = self::TYPE_TERM_LINK; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Apply the `wl_production_permalink` filter. |
|
| 159 | - * |
|
| 160 | - * @param string $permalink The permalink. |
|
| 161 | - * @param int $post_id The post id. |
|
| 162 | - * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
| 163 | - * @param WP_Term $term The term if type is term link, otherwise null. |
|
| 164 | - * |
|
| 165 | - * @since 3.23.0 add the permalink type and term parameters. |
|
| 166 | - * |
|
| 167 | - */ |
|
| 168 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get comma separated tags to be used as keywords |
|
| 173 | - * |
|
| 174 | - * @return string|void Comma separated tags |
|
| 175 | - * |
|
| 176 | - * @since 3.27.2 |
|
| 177 | - */ |
|
| 178 | - public function keywords() { |
|
| 179 | - $tags = get_the_tags( $this->post_id ); |
|
| 180 | - |
|
| 181 | - if ( empty( $tags ) ) { |
|
| 182 | - return; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - return implode( ',', array_map( function ( $tag ) { |
|
| 186 | - return $tag->name; |
|
| 187 | - }, $tags ) ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Get comma separated categories to be used as article section |
|
| 192 | - * |
|
| 193 | - * @return string[] Comma separated categories |
|
| 194 | - * |
|
| 195 | - * @since 3.27.2 |
|
| 196 | - */ |
|
| 197 | - public function article_section() { |
|
| 198 | - $categories = get_the_category( $this->post_id ); |
|
| 199 | - |
|
| 200 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Get comment count |
|
| 205 | - * |
|
| 206 | - * @return string|void Comment count |
|
| 207 | - * |
|
| 208 | - * @since 3.27.2 |
|
| 209 | - */ |
|
| 210 | - public function comment_count() { |
|
| 211 | - return get_comments_number( $this->post_id ); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get Language |
|
| 216 | - * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
| 217 | - * |
|
| 218 | - * @return string|void Language code (locale) |
|
| 219 | - * |
|
| 220 | - * @since 3.27.2 |
|
| 221 | - */ |
|
| 222 | - public function locale() { |
|
| 223 | - $language = 'en-US'; |
|
| 224 | - |
|
| 225 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 226 | - // WPML handling |
|
| 227 | - // WPML: Updated function signature. |
|
| 228 | - // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
| 229 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
| 230 | - if ( ! $post_language instanceof WP_Error ) { |
|
| 231 | - $language = $post_language['locale']; |
|
| 232 | - } |
|
| 233 | - } else if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 234 | - // Polylang handling |
|
| 235 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
| 236 | - } else { |
|
| 237 | - $language = get_locale(); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - return str_replace( '_', '-', $language ); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Add mentions to an article. |
|
| 245 | - * To add mentions we just push the referenced entities to mentions. |
|
| 246 | - * |
|
| 247 | - * @param $post_id int |
|
| 248 | - * @param $references int[] |
|
| 249 | - */ |
|
| 250 | - public function add_references( $post_id, &$references ) { |
|
| 251 | - $tags = get_the_tags( $post_id ); |
|
| 252 | - |
|
| 253 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
| 254 | - // Loop through the tags and push it to references. |
|
| 255 | - foreach ( $tags as $tag ) { |
|
| 256 | - /** |
|
| 257 | - * @var $tag WP_Term |
|
| 258 | - */ |
|
| 259 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 260 | - foreach ( $entity_uris as $uri ) { |
|
| 261 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 262 | - if ( $referenced_entity instanceof WP_Post ) { |
|
| 263 | - // push the referenced entities to references. |
|
| 264 | - $references[] = $referenced_entity->ID; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - } |
|
| 67 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 68 | + |
|
| 69 | + return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Count words in the string, taking into account UTF-8 characters. |
|
| 74 | + * |
|
| 75 | + * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
| 76 | + * |
|
| 77 | + * @since 3.20.0 |
|
| 78 | + * |
|
| 79 | + * @param string $str The target string. |
|
| 80 | + * |
|
| 81 | + * @return int The number of words. |
|
| 82 | + */ |
|
| 83 | + private static function str_word_count_utf8( $str ) { |
|
| 84 | + |
|
| 85 | + return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 90 | + * |
|
| 91 | + * @param int $post_id Post ID. |
|
| 92 | + * |
|
| 93 | + * @return string The post permalink. |
|
| 94 | + * @since 3.20.0 |
|
| 95 | + * |
|
| 96 | + */ |
|
| 97 | + public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 98 | + |
|
| 99 | + $object_link_service = Object_Link_Provider::get_instance(); |
|
| 100 | + $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
| 104 | + * |
|
| 105 | + * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
| 106 | + * post permalink if we find them. |
|
| 107 | + */ |
|
| 108 | + global $wp_version; |
|
| 109 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 110 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 115 | + * when the production environment is copied over from a staging environment with staging |
|
| 116 | + * URLs. |
|
| 117 | + * |
|
| 118 | + * @param string $permalink_url The default permalink. |
|
| 119 | + * @param int $post_id The post id. |
|
| 120 | + * |
|
| 121 | + * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
| 122 | + * @since 3.20.0 |
|
| 123 | + * |
|
| 124 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + $uris = $object_link_service->get_same_as_uris( |
|
| 130 | + $post_id, |
|
| 131 | + $object_type |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
| 135 | + $terms = array(); |
|
| 136 | + if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 137 | + // Try to find one term matching the entity. |
|
| 138 | + $terms = get_terms( array( |
|
| 139 | + 'number' => 1, |
|
| 140 | + 'hide_empty' => false, |
|
| 141 | + 'update_term_meta_cache' => false, |
|
| 142 | + 'meta_key' => '_wl_entity_id', |
|
| 143 | + 'meta_value' => $uris, |
|
| 144 | + 'meta_compare' => 'IN', |
|
| 145 | + ) ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $type = self::TYPE_ENTITY_LINK; |
|
| 149 | + $term = null; |
|
| 150 | + // If found use the term link, otherwise the permalink. |
|
| 151 | + if ( 1 === count( $terms ) ) { |
|
| 152 | + $term = current( $terms ); |
|
| 153 | + $permalink = get_term_link( $term ); |
|
| 154 | + $type = self::TYPE_TERM_LINK; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Apply the `wl_production_permalink` filter. |
|
| 159 | + * |
|
| 160 | + * @param string $permalink The permalink. |
|
| 161 | + * @param int $post_id The post id. |
|
| 162 | + * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
| 163 | + * @param WP_Term $term The term if type is term link, otherwise null. |
|
| 164 | + * |
|
| 165 | + * @since 3.23.0 add the permalink type and term parameters. |
|
| 166 | + * |
|
| 167 | + */ |
|
| 168 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get comma separated tags to be used as keywords |
|
| 173 | + * |
|
| 174 | + * @return string|void Comma separated tags |
|
| 175 | + * |
|
| 176 | + * @since 3.27.2 |
|
| 177 | + */ |
|
| 178 | + public function keywords() { |
|
| 179 | + $tags = get_the_tags( $this->post_id ); |
|
| 180 | + |
|
| 181 | + if ( empty( $tags ) ) { |
|
| 182 | + return; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + return implode( ',', array_map( function ( $tag ) { |
|
| 186 | + return $tag->name; |
|
| 187 | + }, $tags ) ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Get comma separated categories to be used as article section |
|
| 192 | + * |
|
| 193 | + * @return string[] Comma separated categories |
|
| 194 | + * |
|
| 195 | + * @since 3.27.2 |
|
| 196 | + */ |
|
| 197 | + public function article_section() { |
|
| 198 | + $categories = get_the_category( $this->post_id ); |
|
| 199 | + |
|
| 200 | + return wp_list_pluck( $categories, 'cat_name' ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Get comment count |
|
| 205 | + * |
|
| 206 | + * @return string|void Comment count |
|
| 207 | + * |
|
| 208 | + * @since 3.27.2 |
|
| 209 | + */ |
|
| 210 | + public function comment_count() { |
|
| 211 | + return get_comments_number( $this->post_id ); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get Language |
|
| 216 | + * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
| 217 | + * |
|
| 218 | + * @return string|void Language code (locale) |
|
| 219 | + * |
|
| 220 | + * @since 3.27.2 |
|
| 221 | + */ |
|
| 222 | + public function locale() { |
|
| 223 | + $language = 'en-US'; |
|
| 224 | + |
|
| 225 | + if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 226 | + // WPML handling |
|
| 227 | + // WPML: Updated function signature. |
|
| 228 | + // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
| 229 | + $post_language = wpml_get_language_information( null, $this->post_id ); |
|
| 230 | + if ( ! $post_language instanceof WP_Error ) { |
|
| 231 | + $language = $post_language['locale']; |
|
| 232 | + } |
|
| 233 | + } else if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 234 | + // Polylang handling |
|
| 235 | + $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
| 236 | + } else { |
|
| 237 | + $language = get_locale(); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + return str_replace( '_', '-', $language ); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Add mentions to an article. |
|
| 245 | + * To add mentions we just push the referenced entities to mentions. |
|
| 246 | + * |
|
| 247 | + * @param $post_id int |
|
| 248 | + * @param $references int[] |
|
| 249 | + */ |
|
| 250 | + public function add_references( $post_id, &$references ) { |
|
| 251 | + $tags = get_the_tags( $post_id ); |
|
| 252 | + |
|
| 253 | + if ( $tags && ! is_wp_error( $tags ) ) { |
|
| 254 | + // Loop through the tags and push it to references. |
|
| 255 | + foreach ( $tags as $tag ) { |
|
| 256 | + /** |
|
| 257 | + * @var $tag WP_Term |
|
| 258 | + */ |
|
| 259 | + $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 260 | + foreach ( $entity_uris as $uri ) { |
|
| 261 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 262 | + if ( $referenced_entity instanceof WP_Post ) { |
|
| 263 | + // push the referenced entities to references. |
|
| 264 | + $references[] = $referenced_entity->ID; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | 272 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @since 3.14.0 |
| 40 | 40 | * |
| 41 | 41 | */ |
| 42 | - public function __construct( $post_id ) { |
|
| 42 | + public function __construct($post_id) { |
|
| 43 | 43 | |
| 44 | 44 | $this->post_id = $post_id; |
| 45 | 45 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function word_count() { |
| 58 | 58 | |
| 59 | - $post = get_post( $this->post_id ); |
|
| 59 | + $post = get_post($this->post_id); |
|
| 60 | 60 | |
| 61 | 61 | /* |
| 62 | 62 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @since 3.20.0 |
| 66 | 66 | */ |
| 67 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 67 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 68 | 68 | |
| 69 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
| 69 | + return self::str_word_count_utf8(wp_strip_all_tags(strip_shortcodes($post_content))); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return int The number of words. |
| 82 | 82 | */ |
| 83 | - private static function str_word_count_utf8( $str ) { |
|
| 83 | + private static function str_word_count_utf8($str) { |
|
| 84 | 84 | |
| 85 | - return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) ); |
|
| 85 | + return count(preg_split('~[^\p{L}\p{N}\']+~u', $str)); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -94,10 +94,10 @@ discard block |
||
| 94 | 94 | * @since 3.20.0 |
| 95 | 95 | * |
| 96 | 96 | */ |
| 97 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 97 | + public static function get_production_permalink($post_id, $object_type = Object_Type_Enum::POST) { |
|
| 98 | 98 | |
| 99 | 99 | $object_link_service = Object_Link_Provider::get_instance(); |
| 100 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
| 100 | + $permalink = $object_link_service->get_permalink($post_id, $object_type); |
|
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | 103 | * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
@@ -106,8 +106,8 @@ discard block |
||
| 106 | 106 | * post permalink if we find them. |
| 107 | 107 | */ |
| 108 | 108 | global $wp_version; |
| 109 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 110 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
| 109 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 110 | + return apply_filters('wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -133,24 +133,24 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
| 135 | 135 | $terms = array(); |
| 136 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
| 136 | + if (defined('WL_ENABLE_TERM_LINKING') && WL_ENABLE_TERM_LINKING) { |
|
| 137 | 137 | // Try to find one term matching the entity. |
| 138 | - $terms = get_terms( array( |
|
| 138 | + $terms = get_terms(array( |
|
| 139 | 139 | 'number' => 1, |
| 140 | 140 | 'hide_empty' => false, |
| 141 | 141 | 'update_term_meta_cache' => false, |
| 142 | 142 | 'meta_key' => '_wl_entity_id', |
| 143 | 143 | 'meta_value' => $uris, |
| 144 | 144 | 'meta_compare' => 'IN', |
| 145 | - ) ); |
|
| 145 | + )); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $type = self::TYPE_ENTITY_LINK; |
| 149 | 149 | $term = null; |
| 150 | 150 | // If found use the term link, otherwise the permalink. |
| 151 | - if ( 1 === count( $terms ) ) { |
|
| 152 | - $term = current( $terms ); |
|
| 153 | - $permalink = get_term_link( $term ); |
|
| 151 | + if (1 === count($terms)) { |
|
| 152 | + $term = current($terms); |
|
| 153 | + $permalink = get_term_link($term); |
|
| 154 | 154 | $type = self::TYPE_TERM_LINK; |
| 155 | 155 | } |
| 156 | 156 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | * @since 3.23.0 add the permalink type and term parameters. |
| 166 | 166 | * |
| 167 | 167 | */ |
| 168 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
| 168 | + return apply_filters('wl_production_permalink', $permalink, $post_id, $type, $term); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -176,15 +176,15 @@ discard block |
||
| 176 | 176 | * @since 3.27.2 |
| 177 | 177 | */ |
| 178 | 178 | public function keywords() { |
| 179 | - $tags = get_the_tags( $this->post_id ); |
|
| 179 | + $tags = get_the_tags($this->post_id); |
|
| 180 | 180 | |
| 181 | - if ( empty( $tags ) ) { |
|
| 181 | + if (empty($tags)) { |
|
| 182 | 182 | return; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - return implode( ',', array_map( function ( $tag ) { |
|
| 185 | + return implode(',', array_map(function($tag) { |
|
| 186 | 186 | return $tag->name; |
| 187 | - }, $tags ) ); |
|
| 187 | + }, $tags)); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -195,9 +195,9 @@ discard block |
||
| 195 | 195 | * @since 3.27.2 |
| 196 | 196 | */ |
| 197 | 197 | public function article_section() { |
| 198 | - $categories = get_the_category( $this->post_id ); |
|
| 198 | + $categories = get_the_category($this->post_id); |
|
| 199 | 199 | |
| 200 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
| 200 | + return wp_list_pluck($categories, 'cat_name'); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | * @since 3.27.2 |
| 209 | 209 | */ |
| 210 | 210 | public function comment_count() { |
| 211 | - return get_comments_number( $this->post_id ); |
|
| 211 | + return get_comments_number($this->post_id); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
@@ -222,22 +222,22 @@ discard block |
||
| 222 | 222 | public function locale() { |
| 223 | 223 | $language = 'en-US'; |
| 224 | 224 | |
| 225 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
| 225 | + if (function_exists('wpml_get_language_information')) { |
|
| 226 | 226 | // WPML handling |
| 227 | 227 | // WPML: Updated function signature. |
| 228 | 228 | // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
| 229 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
| 230 | - if ( ! $post_language instanceof WP_Error ) { |
|
| 229 | + $post_language = wpml_get_language_information(null, $this->post_id); |
|
| 230 | + if ( ! $post_language instanceof WP_Error) { |
|
| 231 | 231 | $language = $post_language['locale']; |
| 232 | 232 | } |
| 233 | - } else if ( function_exists( 'pll_get_post_language' ) ) { |
|
| 233 | + } else if (function_exists('pll_get_post_language')) { |
|
| 234 | 234 | // Polylang handling |
| 235 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
| 235 | + $language = pll_get_post_language($this->post_id, 'locale'); |
|
| 236 | 236 | } else { |
| 237 | 237 | $language = get_locale(); |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - return str_replace( '_', '-', $language ); |
|
| 240 | + return str_replace('_', '-', $language); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
@@ -247,19 +247,19 @@ discard block |
||
| 247 | 247 | * @param $post_id int |
| 248 | 248 | * @param $references int[] |
| 249 | 249 | */ |
| 250 | - public function add_references( $post_id, &$references ) { |
|
| 251 | - $tags = get_the_tags( $post_id ); |
|
| 250 | + public function add_references($post_id, &$references) { |
|
| 251 | + $tags = get_the_tags($post_id); |
|
| 252 | 252 | |
| 253 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
| 253 | + if ($tags && ! is_wp_error($tags)) { |
|
| 254 | 254 | // Loop through the tags and push it to references. |
| 255 | - foreach ( $tags as $tag ) { |
|
| 255 | + foreach ($tags as $tag) { |
|
| 256 | 256 | /** |
| 257 | 257 | * @var $tag WP_Term |
| 258 | 258 | */ |
| 259 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
| 260 | - foreach ( $entity_uris as $uri ) { |
|
| 261 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
| 262 | - if ( $referenced_entity instanceof WP_Post ) { |
|
| 259 | + $entity_uris = get_term_meta($tag->term_id, '_wl_entity_id'); |
|
| 260 | + foreach ($entity_uris as $uri) { |
|
| 261 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity($uri); |
|
| 262 | + if ($referenced_entity instanceof WP_Post) { |
|
| 263 | 263 | // push the referenced entities to references. |
| 264 | 264 | $references[] = $referenced_entity->ID; |
| 265 | 265 | } |