@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | - 'version' => 'fc6f111cb519e60a3fdeb386b0f5c705', |
|
| 2 | + 'dependencies' => array( 'react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | + 'version' => 'fc6f111cb519e60a3fdeb386b0f5c705', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | - 'version' => '785c0df61237d9c3e6f334647c8aa361', |
|
| 2 | + 'dependencies' => array( 'react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | + 'version' => '785c0df61237d9c3e6f334647c8aa361', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | - 'version' => '56c7e19783d48802a6ed15efe6b13573', |
|
| 2 | + 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | + 'version' => '56c7e19783d48802a6ed15efe6b13573', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | - 'version' => '3b00ce2f141cd55045223317c8ef6372', |
|
| 2 | + 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | + 'version' => '3b00ce2f141cd55045223317c8ef6372', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | - 'version' => 'd7a2eb289641d71000bf54ce8827acb5', |
|
| 2 | + 'dependencies' => array( 'react', 'wp-polyfill', 'wp-rich-text' ), |
|
| 3 | + 'version' => 'd7a2eb289641d71000bf54ce8827acb5', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-hooks', 'wp-polyfill' ), |
|
| 3 | - 'version' => 'b5e96a8d088d58b28796107eb369d196', |
|
| 2 | + 'dependencies' => array( 'react', 'react-dom', 'wp-hooks', 'wp-polyfill' ), |
|
| 3 | + 'version' => 'b5e96a8d088d58b28796107eb369d196', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'wp-polyfill' ), |
|
| 3 | - 'version' => '30d03f4caf7434e88b88658c29d9b3d9', |
|
| 2 | + 'dependencies' => array( 'react', 'wp-polyfill' ), |
|
| 3 | + 'version' => '30d03f4caf7434e88b88658c29d9b3d9', |
|
| 4 | 4 | ); |
@@ -10,59 +10,59 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Avada_Builder_Support { |
| 12 | 12 | |
| 13 | - public function __construct() { |
|
| 14 | - add_filter( 'wl_post_excerpt_post_content', array( $this, 'post_excerpt_post_content' ), 10, 3 ); |
|
| 15 | - add_filter( |
|
| 16 | - 'wl_no_editor_analysis_should_be_enabled_for_post_id', |
|
| 17 | - array( $this, 'is_avada_builder_active_no_analysis' ), |
|
| 18 | - 10, |
|
| 19 | - 2 |
|
| 20 | - ); |
|
| 21 | - } |
|
| 13 | + public function __construct() { |
|
| 14 | + add_filter( 'wl_post_excerpt_post_content', array( $this, 'post_excerpt_post_content' ), 10, 3 ); |
|
| 15 | + add_filter( |
|
| 16 | + 'wl_no_editor_analysis_should_be_enabled_for_post_id', |
|
| 17 | + array( $this, 'is_avada_builder_active_no_analysis' ), |
|
| 18 | + 10, |
|
| 19 | + 2 |
|
| 20 | + ); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Filter the post content before sending it to WordLift API. |
|
| 25 | - * |
|
| 26 | - * @param $post_body string The post content sent from WordPress editor. ( with strip shortcodes ) |
|
| 27 | - * @param $post_id int The post id. |
|
| 28 | - * @param $post_content string The post content. |
|
| 29 | - * |
|
| 30 | - * @return string The post content after removing Avada Builder's shortcodes. |
|
| 31 | - */ |
|
| 32 | - public function post_excerpt_post_content( $post_body, $post_id, $post_content ) { |
|
| 23 | + /** |
|
| 24 | + * Filter the post content before sending it to WordLift API. |
|
| 25 | + * |
|
| 26 | + * @param $post_body string The post content sent from WordPress editor. ( with strip shortcodes ) |
|
| 27 | + * @param $post_id int The post id. |
|
| 28 | + * @param $post_content string The post content. |
|
| 29 | + * |
|
| 30 | + * @return string The post content after removing Avada Builder's shortcodes. |
|
| 31 | + */ |
|
| 32 | + public function post_excerpt_post_content( $post_body, $post_id, $post_content ) { |
|
| 33 | 33 | |
| 34 | - if ( $this->is_avada_builder_active( $post_id ) ) { // Check if its Avada Builder's Content. |
|
| 35 | - return wp_strip_all_tags( do_shortcode( $post_content ), true ); |
|
| 36 | - } |
|
| 37 | - return $post_body; |
|
| 34 | + if ( $this->is_avada_builder_active( $post_id ) ) { // Check if its Avada Builder's Content. |
|
| 35 | + return wp_strip_all_tags( do_shortcode( $post_content ), true ); |
|
| 36 | + } |
|
| 37 | + return $post_body; |
|
| 38 | 38 | |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Check if Avada Builder is active for the post. |
|
| 43 | - * |
|
| 44 | - * @param $post_id int The post id. |
|
| 45 | - * |
|
| 46 | - * @return bool True if Avada Builder is active for the post. |
|
| 47 | - */ |
|
| 48 | - public function is_avada_builder_active( $post_id ) { |
|
| 41 | + /** |
|
| 42 | + * Check if Avada Builder is active for the post. |
|
| 43 | + * |
|
| 44 | + * @param $post_id int The post id. |
|
| 45 | + * |
|
| 46 | + * @return bool True if Avada Builder is active for the post. |
|
| 47 | + */ |
|
| 48 | + public function is_avada_builder_active( $post_id ) { |
|
| 49 | 49 | |
| 50 | - if ( ! $post_id || ! class_exists( '\FusionBuilder' ) ) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 50 | + if ( ! $post_id || ! class_exists( '\FusionBuilder' ) ) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - return 'active' === get_post_meta( $post_id, 'fusion_builder_status', true ); |
|
| 55 | - } |
|
| 54 | + return 'active' === get_post_meta( $post_id, 'fusion_builder_status', true ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Check if Avada Builder is active for the post ( no analysis ). |
|
| 59 | - * |
|
| 60 | - * @param $is_enabled bool Is Enabled. |
|
| 61 | - * @param $post_id int The post id. |
|
| 62 | - * |
|
| 63 | - * @return bool True if Avada Builder is active for the post. |
|
| 64 | - */ |
|
| 65 | - public function is_avada_builder_active_no_analysis( $is_enabled, $post_id ) { |
|
| 66 | - return $is_enabled ? $is_enabled : $this->is_avada_builder_active( $post_id ); |
|
| 67 | - } |
|
| 57 | + /** |
|
| 58 | + * Check if Avada Builder is active for the post ( no analysis ). |
|
| 59 | + * |
|
| 60 | + * @param $is_enabled bool Is Enabled. |
|
| 61 | + * @param $post_id int The post id. |
|
| 62 | + * |
|
| 63 | + * @return bool True if Avada Builder is active for the post. |
|
| 64 | + */ |
|
| 65 | + public function is_avada_builder_active_no_analysis( $is_enabled, $post_id ) { |
|
| 66 | + return $is_enabled ? $is_enabled : $this->is_avada_builder_active( $post_id ); |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -18,218 +18,218 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Post_Excerpt_Rest_Controller { |
| 20 | 20 | |
| 21 | - const POST_EXCERPT_NAMESPACE = 'post-excerpt'; |
|
| 22 | - /** |
|
| 23 | - * Key for storing the meta data for the wordlift post excerpt. |
|
| 24 | - */ |
|
| 25 | - const POST_EXCERPT_META_KEY = '_wl_post_excerpt_meta'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Url for getting the post excerpt data from wordlift api. |
|
| 29 | - */ |
|
| 30 | - const WORDLIFT_POST_EXCERPT_ENDPOINT = '/summarize'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Wordlift returns excerpt in response using this key.. |
|
| 34 | - */ |
|
| 35 | - const WORDLIFT_POST_EXCERPT_RESPONSE_KEY = 'summary'; |
|
| 36 | - |
|
| 37 | - public static function register_routes() { |
|
| 38 | - add_action( 'rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback' ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Determines whether we need to get the excerpt from wordlift api, |
|
| 43 | - * or just use the one we already obtained by generating the hash and comparing it |
|
| 44 | - * with the previous one. |
|
| 45 | - * |
|
| 46 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 47 | - * |
|
| 48 | - * @return array Post excerpt data. |
|
| 49 | - */ |
|
| 50 | - public static function get_post_excerpt( $request ) { |
|
| 51 | - $data = $request->get_params(); |
|
| 52 | - $post_id = $data['post_id']; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param $data['post_body'] string The post content sent from WordPress editor. ( with strip shortcodes ) |
|
| 56 | - * @param $post_id int The post id. |
|
| 57 | - * @param $data['post_body'] string The post content. |
|
| 58 | - * |
|
| 59 | - * @since 3.33.5 |
|
| 60 | - * Allow post content sent to excerpt api to be filtered. |
|
| 61 | - */ |
|
| 62 | - $post_body = apply_filters( |
|
| 63 | - 'wl_post_excerpt_post_content', |
|
| 64 | - strip_shortcodes( $data['post_body'] ), |
|
| 65 | - $post_id, |
|
| 66 | - $data['post_body'] |
|
| 67 | - ); |
|
| 68 | - $current_hash = md5( $post_body ); |
|
| 69 | - $server_response = self::get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ); |
|
| 70 | - if ( empty( $server_response ) || ! array_key_exists( 'post_excerpt', $server_response ) ) { |
|
| 71 | - return array( |
|
| 72 | - 'status' => 'error', |
|
| 73 | - 'message' => __( 'Error While Generating Post Excerpt.', 'wordlift' ), |
|
| 74 | - ); |
|
| 75 | - } else { |
|
| 76 | - return array( |
|
| 77 | - 'status' => 'success', |
|
| 78 | - 'post_excerpt' => $server_response['post_excerpt'], |
|
| 79 | - 'from_cache' => $server_response['from_cache'], |
|
| 80 | - 'message' => __( 'Excerpt successfully generated.', 'wordlift' ), |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * This function determines whether to get the excerpt from the server or from the meta cache. |
|
| 88 | - * |
|
| 89 | - * @param $post_id int The Post id. |
|
| 90 | - * @param $post_body string The post content |
|
| 91 | - * @param $current_hash string md5 hash of the current post body. |
|
| 92 | - * |
|
| 93 | - * @return array|bool|null |
|
| 94 | - */ |
|
| 95 | - public static function get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ) { |
|
| 96 | - $previous_data = get_post_meta( $post_id, self::POST_EXCERPT_META_KEY, true ); |
|
| 97 | - $server_response = null; |
|
| 98 | - if ( '' === $previous_data ) { |
|
| 99 | - // There is no data in meta, so just fetch the data from remote server. |
|
| 100 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 101 | - } else { |
|
| 102 | - // If there is data in meta, get the previous hash and compare. |
|
| 103 | - $previous_hash = $previous_data['post_body_hash']; |
|
| 104 | - |
|
| 105 | - if ( $current_hash === $previous_hash ) { |
|
| 106 | - // then return the previous value. |
|
| 107 | - $server_response = array( |
|
| 108 | - 'post_excerpt' => $previous_data['post_excerpt'], |
|
| 109 | - 'from_cache' => true, |
|
| 110 | - ); |
|
| 111 | - } else { |
|
| 112 | - // send the request to external API and then send the response. |
|
| 113 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return $server_response; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Sends the remote request to the wordlift API and saves the response in meta for |
|
| 122 | - * future use. |
|
| 123 | - * |
|
| 124 | - * @param $post_id int Post id which the post excerpt belongs to |
|
| 125 | - * @param $post_body string Total text content of the post body. |
|
| 126 | - * |
|
| 127 | - * @return array|bool |
|
| 128 | - */ |
|
| 129 | - public static function get_post_excerpt_from_remote_server( $post_id, $post_body ) { |
|
| 130 | - // The configuration is constant for now, it might be changing in future. |
|
| 131 | - $configuration = array( |
|
| 132 | - 'ratio' => 0.0005, |
|
| 133 | - 'min_length' => 60, |
|
| 134 | - ); |
|
| 135 | - // Construct the url with the configuration |
|
| 136 | - $endpoint = add_query_arg( $configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT ); |
|
| 137 | - $api_service = Default_Api_Service::get_instance(); |
|
| 138 | - $response = $api_service->request( |
|
| 139 | - 'POST', |
|
| 140 | - $endpoint, |
|
| 141 | - array( 'Content-Type' => 'text/plain' ), |
|
| 142 | - $post_body, |
|
| 143 | - null, |
|
| 144 | - null, |
|
| 145 | - array( 'data_format' => 'body' ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - return self::save_response_to_meta_on_success( $post_id, $post_body, $response ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Save the post excerpt to meta if the response is successful. |
|
| 153 | - * |
|
| 154 | - * @param $post_id int The post id |
|
| 155 | - * @param $post_body string Full text content of the post. |
|
| 156 | - * @param $response Response instance |
|
| 157 | - * |
|
| 158 | - * @return array|bool |
|
| 159 | - */ |
|
| 160 | - public static function save_response_to_meta_on_success( $post_id, $post_body, $response ) { |
|
| 161 | - // If body exists then decode the body. |
|
| 162 | - $body = json_decode( $response->get_body(), true ); |
|
| 163 | - if ( empty( $body ) || ! array_key_exists( self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body ) ) { |
|
| 164 | - // Bail out if we get an incorrect response |
|
| 165 | - return false; |
|
| 166 | - } else { |
|
| 167 | - $post_excerpt = (string) $body[ self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY ]; |
|
| 168 | - // Save it to meta. |
|
| 169 | - self::save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ); |
|
| 170 | - |
|
| 171 | - return array( |
|
| 172 | - 'post_excerpt' => $post_excerpt, |
|
| 173 | - 'from_cache' => false, |
|
| 174 | - ); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Saves the excerpt in the post meta. |
|
| 180 | - * |
|
| 181 | - * @param $post_id int Post id which the post excerpt belongs to |
|
| 182 | - * @param $post_excerpt string Post excerpt returned by the server |
|
| 183 | - * @param $post_body string Total text content of the post body. |
|
| 184 | - * |
|
| 185 | - * @return void |
|
| 186 | - */ |
|
| 187 | - public static function save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ) { |
|
| 188 | - // hash the post body and save it. |
|
| 189 | - $data = array( |
|
| 190 | - 'post_body_hash' => md5( $post_body ), |
|
| 191 | - 'post_excerpt' => $post_excerpt, |
|
| 192 | - ); |
|
| 193 | - update_post_meta( $post_id, self::POST_EXCERPT_META_KEY, $data ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * This call back is invoked by the Rest api action. |
|
| 198 | - */ |
|
| 199 | - public static function register_route_callback() { |
|
| 200 | - /** @var $post_id_validation_settings array Settings used to validate post id */ |
|
| 201 | - $post_id_validation_settings = array( |
|
| 202 | - 'required' => true, |
|
| 203 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 204 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 205 | - return is_numeric( $param ); |
|
| 206 | - }, |
|
| 207 | - ); |
|
| 208 | - $post_body_validation_settings = array( |
|
| 209 | - 'required' => true, |
|
| 210 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 211 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 212 | - return is_string( $param ); |
|
| 213 | - }, |
|
| 214 | - ); |
|
| 215 | - /** |
|
| 216 | - * Rest route for getting the excerpt from wordlift api. |
|
| 217 | - */ |
|
| 218 | - register_rest_route( |
|
| 219 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 220 | - '/' . self::POST_EXCERPT_NAMESPACE . '/(?P<post_id>\d+)', |
|
| 221 | - array( |
|
| 222 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 223 | - 'callback' => 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::get_post_excerpt', |
|
| 224 | - 'permission_callback' => function () { |
|
| 225 | - return current_user_can( 'publish_posts' ); |
|
| 226 | - }, |
|
| 227 | - 'args' => array( |
|
| 228 | - 'post_id' => $post_id_validation_settings, |
|
| 229 | - 'post_body' => $post_body_validation_settings, |
|
| 230 | - ), |
|
| 231 | - ) |
|
| 232 | - ); |
|
| 233 | - } |
|
| 21 | + const POST_EXCERPT_NAMESPACE = 'post-excerpt'; |
|
| 22 | + /** |
|
| 23 | + * Key for storing the meta data for the wordlift post excerpt. |
|
| 24 | + */ |
|
| 25 | + const POST_EXCERPT_META_KEY = '_wl_post_excerpt_meta'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Url for getting the post excerpt data from wordlift api. |
|
| 29 | + */ |
|
| 30 | + const WORDLIFT_POST_EXCERPT_ENDPOINT = '/summarize'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Wordlift returns excerpt in response using this key.. |
|
| 34 | + */ |
|
| 35 | + const WORDLIFT_POST_EXCERPT_RESPONSE_KEY = 'summary'; |
|
| 36 | + |
|
| 37 | + public static function register_routes() { |
|
| 38 | + add_action( 'rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback' ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Determines whether we need to get the excerpt from wordlift api, |
|
| 43 | + * or just use the one we already obtained by generating the hash and comparing it |
|
| 44 | + * with the previous one. |
|
| 45 | + * |
|
| 46 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 47 | + * |
|
| 48 | + * @return array Post excerpt data. |
|
| 49 | + */ |
|
| 50 | + public static function get_post_excerpt( $request ) { |
|
| 51 | + $data = $request->get_params(); |
|
| 52 | + $post_id = $data['post_id']; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param $data['post_body'] string The post content sent from WordPress editor. ( with strip shortcodes ) |
|
| 56 | + * @param $post_id int The post id. |
|
| 57 | + * @param $data['post_body'] string The post content. |
|
| 58 | + * |
|
| 59 | + * @since 3.33.5 |
|
| 60 | + * Allow post content sent to excerpt api to be filtered. |
|
| 61 | + */ |
|
| 62 | + $post_body = apply_filters( |
|
| 63 | + 'wl_post_excerpt_post_content', |
|
| 64 | + strip_shortcodes( $data['post_body'] ), |
|
| 65 | + $post_id, |
|
| 66 | + $data['post_body'] |
|
| 67 | + ); |
|
| 68 | + $current_hash = md5( $post_body ); |
|
| 69 | + $server_response = self::get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ); |
|
| 70 | + if ( empty( $server_response ) || ! array_key_exists( 'post_excerpt', $server_response ) ) { |
|
| 71 | + return array( |
|
| 72 | + 'status' => 'error', |
|
| 73 | + 'message' => __( 'Error While Generating Post Excerpt.', 'wordlift' ), |
|
| 74 | + ); |
|
| 75 | + } else { |
|
| 76 | + return array( |
|
| 77 | + 'status' => 'success', |
|
| 78 | + 'post_excerpt' => $server_response['post_excerpt'], |
|
| 79 | + 'from_cache' => $server_response['from_cache'], |
|
| 80 | + 'message' => __( 'Excerpt successfully generated.', 'wordlift' ), |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * This function determines whether to get the excerpt from the server or from the meta cache. |
|
| 88 | + * |
|
| 89 | + * @param $post_id int The Post id. |
|
| 90 | + * @param $post_body string The post content |
|
| 91 | + * @param $current_hash string md5 hash of the current post body. |
|
| 92 | + * |
|
| 93 | + * @return array|bool|null |
|
| 94 | + */ |
|
| 95 | + public static function get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ) { |
|
| 96 | + $previous_data = get_post_meta( $post_id, self::POST_EXCERPT_META_KEY, true ); |
|
| 97 | + $server_response = null; |
|
| 98 | + if ( '' === $previous_data ) { |
|
| 99 | + // There is no data in meta, so just fetch the data from remote server. |
|
| 100 | + $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 101 | + } else { |
|
| 102 | + // If there is data in meta, get the previous hash and compare. |
|
| 103 | + $previous_hash = $previous_data['post_body_hash']; |
|
| 104 | + |
|
| 105 | + if ( $current_hash === $previous_hash ) { |
|
| 106 | + // then return the previous value. |
|
| 107 | + $server_response = array( |
|
| 108 | + 'post_excerpt' => $previous_data['post_excerpt'], |
|
| 109 | + 'from_cache' => true, |
|
| 110 | + ); |
|
| 111 | + } else { |
|
| 112 | + // send the request to external API and then send the response. |
|
| 113 | + $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return $server_response; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Sends the remote request to the wordlift API and saves the response in meta for |
|
| 122 | + * future use. |
|
| 123 | + * |
|
| 124 | + * @param $post_id int Post id which the post excerpt belongs to |
|
| 125 | + * @param $post_body string Total text content of the post body. |
|
| 126 | + * |
|
| 127 | + * @return array|bool |
|
| 128 | + */ |
|
| 129 | + public static function get_post_excerpt_from_remote_server( $post_id, $post_body ) { |
|
| 130 | + // The configuration is constant for now, it might be changing in future. |
|
| 131 | + $configuration = array( |
|
| 132 | + 'ratio' => 0.0005, |
|
| 133 | + 'min_length' => 60, |
|
| 134 | + ); |
|
| 135 | + // Construct the url with the configuration |
|
| 136 | + $endpoint = add_query_arg( $configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT ); |
|
| 137 | + $api_service = Default_Api_Service::get_instance(); |
|
| 138 | + $response = $api_service->request( |
|
| 139 | + 'POST', |
|
| 140 | + $endpoint, |
|
| 141 | + array( 'Content-Type' => 'text/plain' ), |
|
| 142 | + $post_body, |
|
| 143 | + null, |
|
| 144 | + null, |
|
| 145 | + array( 'data_format' => 'body' ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + return self::save_response_to_meta_on_success( $post_id, $post_body, $response ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Save the post excerpt to meta if the response is successful. |
|
| 153 | + * |
|
| 154 | + * @param $post_id int The post id |
|
| 155 | + * @param $post_body string Full text content of the post. |
|
| 156 | + * @param $response Response instance |
|
| 157 | + * |
|
| 158 | + * @return array|bool |
|
| 159 | + */ |
|
| 160 | + public static function save_response_to_meta_on_success( $post_id, $post_body, $response ) { |
|
| 161 | + // If body exists then decode the body. |
|
| 162 | + $body = json_decode( $response->get_body(), true ); |
|
| 163 | + if ( empty( $body ) || ! array_key_exists( self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body ) ) { |
|
| 164 | + // Bail out if we get an incorrect response |
|
| 165 | + return false; |
|
| 166 | + } else { |
|
| 167 | + $post_excerpt = (string) $body[ self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY ]; |
|
| 168 | + // Save it to meta. |
|
| 169 | + self::save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ); |
|
| 170 | + |
|
| 171 | + return array( |
|
| 172 | + 'post_excerpt' => $post_excerpt, |
|
| 173 | + 'from_cache' => false, |
|
| 174 | + ); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Saves the excerpt in the post meta. |
|
| 180 | + * |
|
| 181 | + * @param $post_id int Post id which the post excerpt belongs to |
|
| 182 | + * @param $post_excerpt string Post excerpt returned by the server |
|
| 183 | + * @param $post_body string Total text content of the post body. |
|
| 184 | + * |
|
| 185 | + * @return void |
|
| 186 | + */ |
|
| 187 | + public static function save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ) { |
|
| 188 | + // hash the post body and save it. |
|
| 189 | + $data = array( |
|
| 190 | + 'post_body_hash' => md5( $post_body ), |
|
| 191 | + 'post_excerpt' => $post_excerpt, |
|
| 192 | + ); |
|
| 193 | + update_post_meta( $post_id, self::POST_EXCERPT_META_KEY, $data ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * This call back is invoked by the Rest api action. |
|
| 198 | + */ |
|
| 199 | + public static function register_route_callback() { |
|
| 200 | + /** @var $post_id_validation_settings array Settings used to validate post id */ |
|
| 201 | + $post_id_validation_settings = array( |
|
| 202 | + 'required' => true, |
|
| 203 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 204 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 205 | + return is_numeric( $param ); |
|
| 206 | + }, |
|
| 207 | + ); |
|
| 208 | + $post_body_validation_settings = array( |
|
| 209 | + 'required' => true, |
|
| 210 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 211 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 212 | + return is_string( $param ); |
|
| 213 | + }, |
|
| 214 | + ); |
|
| 215 | + /** |
|
| 216 | + * Rest route for getting the excerpt from wordlift api. |
|
| 217 | + */ |
|
| 218 | + register_rest_route( |
|
| 219 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 220 | + '/' . self::POST_EXCERPT_NAMESPACE . '/(?P<post_id>\d+)', |
|
| 221 | + array( |
|
| 222 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 223 | + 'callback' => 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::get_post_excerpt', |
|
| 224 | + 'permission_callback' => function () { |
|
| 225 | + return current_user_can( 'publish_posts' ); |
|
| 226 | + }, |
|
| 227 | + 'args' => array( |
|
| 228 | + 'post_id' => $post_id_validation_settings, |
|
| 229 | + 'post_body' => $post_body_validation_settings, |
|
| 230 | + ), |
|
| 231 | + ) |
|
| 232 | + ); |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | 235 | } |