@@ -7,172 +7,172 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Topic_Taxonomy_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.5.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log_service; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Taxonomy name. |
|
| 21 | - * |
|
| 22 | - * @since 3.5.0 |
|
| 23 | - */ |
|
| 24 | - const TAXONOMY_NAME = 'wl_topic'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Taxonomy object type. |
|
| 28 | - * |
|
| 29 | - * @since 3.5.0 |
|
| 30 | - */ |
|
| 31 | - const TAXONOMY_OBJECT_TYPE = 'post'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Taxonomy slug. |
|
| 35 | - * |
|
| 36 | - * @since 3.5.0 |
|
| 37 | - */ |
|
| 38 | - const TAXONOMY_SLUG = 'wl_topic'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
| 42 | - * |
|
| 43 | - * @since 3.5.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
| 46 | - */ |
|
| 47 | - private static $instance; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
| 51 | - * |
|
| 52 | - * @since 3.5.0 |
|
| 53 | - */ |
|
| 54 | - public function __construct() { |
|
| 55 | - |
|
| 56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 57 | - |
|
| 58 | - // Set the singleton instance. |
|
| 59 | - self::$instance = $this; |
|
| 60 | - |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the singleton instance of the Entity service. |
|
| 65 | - * |
|
| 66 | - * @since 3.5.0 |
|
| 67 | - * @return Wordlift_Topic_Taxonomy_Service |
|
| 68 | - */ |
|
| 69 | - public static function get_instance() { |
|
| 70 | - |
|
| 71 | - return self::$instance; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Just register the topic taxonomy. |
|
| 76 | - * |
|
| 77 | - * @since 3.5.0 |
|
| 78 | - */ |
|
| 79 | - public function init() { |
|
| 80 | - |
|
| 81 | - // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
| 82 | - $labels = array( |
|
| 83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
| 84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
| 85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
| 86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
| 87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
| 88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
| 89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
| 90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
| 91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
| 92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
| 93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - $capabilities = array( |
|
| 97 | - 'manage_terms' => null, |
|
| 98 | - 'edit_terms' => null, |
|
| 99 | - 'delete_terms' => null, |
|
| 100 | - 'assign_terms' => 'edit_posts', |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - $args = array( |
|
| 104 | - 'labels' => $labels, |
|
| 105 | - 'capabilities' => $capabilities, |
|
| 106 | - 'hierarchical' => true, |
|
| 107 | - 'show_admin_column' => false, |
|
| 108 | - 'show_ui' => false, |
|
| 109 | - 'rewrite' => array( |
|
| 110 | - 'slug' => self::TAXONOMY_SLUG, |
|
| 111 | - ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - // Register taxonomy |
|
| 115 | - register_taxonomy( |
|
| 116 | - self::TAXONOMY_NAME, |
|
| 117 | - self::TAXONOMY_OBJECT_TYPE, |
|
| 118 | - $args |
|
| 119 | - ); |
|
| 120 | - |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get or create a taxonomy term from a given entity topic. |
|
| 125 | - * |
|
| 126 | - * @since 3.5.0 |
|
| 127 | - */ |
|
| 128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
| 129 | - |
|
| 130 | - // Define taxonomy term slug |
|
| 131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
| 132 | - // Look for an existing taxonomy term with a given slug |
|
| 133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
| 134 | - if ( $term ) { |
|
| 135 | - return (int) $term->term_id; |
|
| 136 | - } |
|
| 137 | - // Otherwise create a new term and return it |
|
| 138 | - $result = wp_insert_term( |
|
| 139 | - $topic->post_title, |
|
| 140 | - self::TAXONOMY_NAME, |
|
| 141 | - array( |
|
| 142 | - 'slug' => $term_slug, |
|
| 143 | - 'description' => $topic->post_content, |
|
| 144 | - ) |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - return (int) $result['term_id']; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Set a topic for a given post. |
|
| 152 | - * |
|
| 153 | - * @since 3.5.0 |
|
| 154 | - */ |
|
| 155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
| 156 | - // Retrieve the topic entity post |
|
| 157 | - $topic_entity_post = get_post( $topic_id ); |
|
| 158 | - // If current topic does not exist in db false is returned |
|
| 159 | - if ( null === $topic_entity_post ) { |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - // Create the proper taxonomy term if needed |
|
| 163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
| 164 | - // Link the term to the current post |
|
| 165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Unlink any topic for a given post. |
|
| 171 | - * |
|
| 172 | - * @since 3.5.0 |
|
| 173 | - */ |
|
| 174 | - public function unlink_topic_for( $post_id ) { |
|
| 175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
| 176 | - } |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.5.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log_service; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Taxonomy name. |
|
| 21 | + * |
|
| 22 | + * @since 3.5.0 |
|
| 23 | + */ |
|
| 24 | + const TAXONOMY_NAME = 'wl_topic'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Taxonomy object type. |
|
| 28 | + * |
|
| 29 | + * @since 3.5.0 |
|
| 30 | + */ |
|
| 31 | + const TAXONOMY_OBJECT_TYPE = 'post'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Taxonomy slug. |
|
| 35 | + * |
|
| 36 | + * @since 3.5.0 |
|
| 37 | + */ |
|
| 38 | + const TAXONOMY_SLUG = 'wl_topic'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
| 42 | + * |
|
| 43 | + * @since 3.5.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
| 46 | + */ |
|
| 47 | + private static $instance; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
| 51 | + * |
|
| 52 | + * @since 3.5.0 |
|
| 53 | + */ |
|
| 54 | + public function __construct() { |
|
| 55 | + |
|
| 56 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 57 | + |
|
| 58 | + // Set the singleton instance. |
|
| 59 | + self::$instance = $this; |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the singleton instance of the Entity service. |
|
| 65 | + * |
|
| 66 | + * @since 3.5.0 |
|
| 67 | + * @return Wordlift_Topic_Taxonomy_Service |
|
| 68 | + */ |
|
| 69 | + public static function get_instance() { |
|
| 70 | + |
|
| 71 | + return self::$instance; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Just register the topic taxonomy. |
|
| 76 | + * |
|
| 77 | + * @since 3.5.0 |
|
| 78 | + */ |
|
| 79 | + public function init() { |
|
| 80 | + |
|
| 81 | + // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
| 82 | + $labels = array( |
|
| 83 | + 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
| 84 | + 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
| 85 | + 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
| 86 | + 'all_items' => __( 'All Topics', 'wordlift' ), |
|
| 87 | + 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
| 88 | + 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
| 89 | + 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
| 90 | + 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
| 91 | + 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
| 92 | + 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
| 93 | + 'menu_name' => __( 'Topics', 'wordlift' ), |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + $capabilities = array( |
|
| 97 | + 'manage_terms' => null, |
|
| 98 | + 'edit_terms' => null, |
|
| 99 | + 'delete_terms' => null, |
|
| 100 | + 'assign_terms' => 'edit_posts', |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + $args = array( |
|
| 104 | + 'labels' => $labels, |
|
| 105 | + 'capabilities' => $capabilities, |
|
| 106 | + 'hierarchical' => true, |
|
| 107 | + 'show_admin_column' => false, |
|
| 108 | + 'show_ui' => false, |
|
| 109 | + 'rewrite' => array( |
|
| 110 | + 'slug' => self::TAXONOMY_SLUG, |
|
| 111 | + ), |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + // Register taxonomy |
|
| 115 | + register_taxonomy( |
|
| 116 | + self::TAXONOMY_NAME, |
|
| 117 | + self::TAXONOMY_OBJECT_TYPE, |
|
| 118 | + $args |
|
| 119 | + ); |
|
| 120 | + |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get or create a taxonomy term from a given entity topic. |
|
| 125 | + * |
|
| 126 | + * @since 3.5.0 |
|
| 127 | + */ |
|
| 128 | + public function get_or_create_term_from_topic_entity( $topic ) { |
|
| 129 | + |
|
| 130 | + // Define taxonomy term slug |
|
| 131 | + $term_slug = sanitize_title( $topic->post_title ); |
|
| 132 | + // Look for an existing taxonomy term with a given slug |
|
| 133 | + $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
| 134 | + if ( $term ) { |
|
| 135 | + return (int) $term->term_id; |
|
| 136 | + } |
|
| 137 | + // Otherwise create a new term and return it |
|
| 138 | + $result = wp_insert_term( |
|
| 139 | + $topic->post_title, |
|
| 140 | + self::TAXONOMY_NAME, |
|
| 141 | + array( |
|
| 142 | + 'slug' => $term_slug, |
|
| 143 | + 'description' => $topic->post_content, |
|
| 144 | + ) |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + return (int) $result['term_id']; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Set a topic for a given post. |
|
| 152 | + * |
|
| 153 | + * @since 3.5.0 |
|
| 154 | + */ |
|
| 155 | + public function set_topic_for( $post_id, $topic_id ) { |
|
| 156 | + // Retrieve the topic entity post |
|
| 157 | + $topic_entity_post = get_post( $topic_id ); |
|
| 158 | + // If current topic does not exist in db false is returned |
|
| 159 | + if ( null === $topic_entity_post ) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + // Create the proper taxonomy term if needed |
|
| 163 | + $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
| 164 | + // Link the term to the current post |
|
| 165 | + wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Unlink any topic for a given post. |
|
| 171 | + * |
|
| 172 | + * @since 3.5.0 |
|
| 173 | + */ |
|
| 174 | + public function unlink_topic_for( $post_id ) { |
|
| 175 | + wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | 178 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function __construct() { |
| 55 | 55 | |
| 56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 56 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 57 | 57 | |
| 58 | 58 | // Set the singleton instance. |
| 59 | 59 | self::$instance = $this; |
@@ -80,17 +80,17 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
| 82 | 82 | $labels = array( |
| 83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
| 84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
| 85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
| 86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
| 87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
| 88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
| 89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
| 90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
| 91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
| 92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
| 93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
| 83 | + 'name' => _x('Topics', 'taxonomy general name', 'wordlift'), |
|
| 84 | + 'singular_name' => _x('Topic', 'taxonomy singular name', 'wordlift'), |
|
| 85 | + 'search_items' => __('Search Topics', 'wordlift'), |
|
| 86 | + 'all_items' => __('All Topics', 'wordlift'), |
|
| 87 | + 'parent_item' => __('Parent Topic', 'wordlift'), |
|
| 88 | + 'parent_item_colon' => __('Parent Topic:', 'wordlift'), |
|
| 89 | + 'edit_item' => __('Edit Topic', 'wordlift'), |
|
| 90 | + 'update_item' => __('Update Topic', 'wordlift'), |
|
| 91 | + 'add_new_item' => __('Add New Topic', 'wordlift'), |
|
| 92 | + 'new_item_name' => __('New Topic', 'wordlift'), |
|
| 93 | + 'menu_name' => __('Topics', 'wordlift'), |
|
| 94 | 94 | ); |
| 95 | 95 | |
| 96 | 96 | $capabilities = array( |
@@ -125,13 +125,13 @@ discard block |
||
| 125 | 125 | * |
| 126 | 126 | * @since 3.5.0 |
| 127 | 127 | */ |
| 128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
| 128 | + public function get_or_create_term_from_topic_entity($topic) { |
|
| 129 | 129 | |
| 130 | 130 | // Define taxonomy term slug |
| 131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
| 131 | + $term_slug = sanitize_title($topic->post_title); |
|
| 132 | 132 | // Look for an existing taxonomy term with a given slug |
| 133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
| 134 | - if ( $term ) { |
|
| 133 | + $term = get_term_by('slug', $term_slug, self::TAXONOMY_NAME); |
|
| 134 | + if ($term) { |
|
| 135 | 135 | return (int) $term->term_id; |
| 136 | 136 | } |
| 137 | 137 | // Otherwise create a new term and return it |
@@ -152,17 +152,17 @@ discard block |
||
| 152 | 152 | * |
| 153 | 153 | * @since 3.5.0 |
| 154 | 154 | */ |
| 155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
| 155 | + public function set_topic_for($post_id, $topic_id) { |
|
| 156 | 156 | // Retrieve the topic entity post |
| 157 | - $topic_entity_post = get_post( $topic_id ); |
|
| 157 | + $topic_entity_post = get_post($topic_id); |
|
| 158 | 158 | // If current topic does not exist in db false is returned |
| 159 | - if ( null === $topic_entity_post ) { |
|
| 159 | + if (null === $topic_entity_post) { |
|
| 160 | 160 | return false; |
| 161 | 161 | } |
| 162 | 162 | // Create the proper taxonomy term if needed |
| 163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
| 163 | + $term_id = $this->get_or_create_term_from_topic_entity($topic_entity_post); |
|
| 164 | 164 | // Link the term to the current post |
| 165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
| 165 | + wp_set_post_terms($post_id, $term_id, self::TAXONOMY_NAME, false); |
|
| 166 | 166 | return true; |
| 167 | 167 | } |
| 168 | 168 | |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @since 3.5.0 |
| 173 | 173 | */ |
| 174 | - public function unlink_topic_for( $post_id ) { |
|
| 175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
| 174 | + public function unlink_topic_for($post_id) { |
|
| 175 | + wp_delete_object_term_relationships($post_id, self::TAXONOMY_NAME); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | } |
@@ -14,369 +14,369 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Rating_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Entity rating max. |
|
| 19 | - * |
|
| 20 | - * @since 3.3.0 |
|
| 21 | - */ |
|
| 22 | - const RATING_MAX = 7; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Entity rating score meta key. |
|
| 26 | - * |
|
| 27 | - * @since 3.3.0 |
|
| 28 | - */ |
|
| 29 | - const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Entity rating warnings meta key. |
|
| 33 | - * |
|
| 34 | - * @since 3.3.0 |
|
| 35 | - */ |
|
| 36 | - const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Entity warning has related post identifier. |
|
| 40 | - * |
|
| 41 | - * @since 3.3.0 |
|
| 42 | - */ |
|
| 43 | - const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Entity warning has content post identifier. |
|
| 47 | - * |
|
| 48 | - * @since 3.3.0 |
|
| 49 | - */ |
|
| 50 | - const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Entity warning has related entities identifier. |
|
| 54 | - * |
|
| 55 | - * @since 3.3.0 |
|
| 56 | - */ |
|
| 57 | - const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Entity warning is published identifier. |
|
| 61 | - * |
|
| 62 | - * @since 3.3.0 |
|
| 63 | - */ |
|
| 64 | - const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Entity warning has thumbnail identifier. |
|
| 68 | - * |
|
| 69 | - * @since 3.3.0 |
|
| 70 | - */ |
|
| 71 | - const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Entity warning has same as identifier. |
|
| 75 | - * |
|
| 76 | - * @since 3.3.0 |
|
| 77 | - */ |
|
| 78 | - const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Entity warning has completed metadata identifier. |
|
| 82 | - * |
|
| 83 | - * @since 3.3.0 |
|
| 84 | - */ |
|
| 85 | - const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 89 | - * |
|
| 90 | - * @since 3.10.0 |
|
| 91 | - * @access private |
|
| 92 | - * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 93 | - */ |
|
| 94 | - private $entity_type_service; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * The Notice service. |
|
| 98 | - * |
|
| 99 | - * @since 3.3.0 |
|
| 100 | - * @access private |
|
| 101 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 102 | - */ |
|
| 103 | - private $notice_service; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * A {@link Wordlift_Log_Service} instance. |
|
| 107 | - * |
|
| 108 | - * @since 3.10.0 |
|
| 109 | - * @access private |
|
| 110 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 111 | - */ |
|
| 112 | - private $log; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Create a {@link Wordlift_Rating_Service} instance. |
|
| 116 | - * |
|
| 117 | - * @since 3.10.0 |
|
| 118 | - */ |
|
| 119 | - protected function __construct() { |
|
| 120 | - |
|
| 121 | - $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 122 | - $this->notice_service = Wordlift_Notice_Service::get_instance(); |
|
| 123 | - |
|
| 124 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private static $instance; |
|
| 129 | - |
|
| 130 | - public static function get_instance() { |
|
| 131 | - if ( ! isset( self::$instance ) ) { |
|
| 132 | - self::$instance = new self(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return self::$instance; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Set rating for a given entity. |
|
| 140 | - * |
|
| 141 | - * @param int $post_id The entity post id. |
|
| 142 | - * |
|
| 143 | - * @return array An array representing the rating obj. |
|
| 144 | - * @since 3.3.0 |
|
| 145 | - */ |
|
| 146 | - public function set_rating_for( $post_id ) { |
|
| 147 | - |
|
| 148 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 149 | - if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
| 150 | - return; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Calculate rating for the given post. |
|
| 158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 159 | - |
|
| 160 | - // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 161 | - // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 162 | - // |
|
| 163 | - // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 166 | - |
|
| 167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 168 | - |
|
| 169 | - // Finally returns the rating |
|
| 170 | - return $rating; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Get or calculate rating for a given entity |
|
| 175 | - * |
|
| 176 | - * @param int $post_id The entity post id. |
|
| 177 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 178 | - * |
|
| 179 | - * @return array An array representing the rating obj. |
|
| 180 | - * @since 3.3.0 |
|
| 181 | - */ |
|
| 182 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 183 | - |
|
| 184 | - // If forced reload is required or rating is missing. |
|
| 185 | - if ( $force_reload ) { |
|
| 186 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 187 | - |
|
| 188 | - return $this->set_rating_for( $post_id ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 192 | - |
|
| 193 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 194 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 195 | - |
|
| 196 | - return $this->set_rating_for( $post_id ); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 200 | - |
|
| 201 | - // Finally return score and warnings |
|
| 202 | - return array( |
|
| 203 | - 'raw_score' => $current_raw_score, |
|
| 204 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 205 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 206 | - 'warnings' => $current_warnings, |
|
| 207 | - ); |
|
| 208 | - |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Calculate rating for a given entity. |
|
| 213 | - * |
|
| 214 | - * Rating depends from following criteria: |
|
| 215 | - * |
|
| 216 | - * 1. Is the current entity related to at least 1 post? |
|
| 217 | - * 2. Is the current entity content post not empty? |
|
| 218 | - * 3. Is the current entity related to at least 1 entity? |
|
| 219 | - * 4. Is the entity published? |
|
| 220 | - * 5. There is a a thumbnail associated to the entity? |
|
| 221 | - * 6. Has the entity a sameas defined? |
|
| 222 | - * 7. Are all schema.org required metadata compiled? |
|
| 223 | - * |
|
| 224 | - * Each positive check means +1 in terms of rating score. |
|
| 225 | - * |
|
| 226 | - * @param int $post_id The entity post id. |
|
| 227 | - * |
|
| 228 | - * @return array An array representing the rating obj. |
|
| 229 | - * @since 3.3.0 |
|
| 230 | - */ |
|
| 231 | - private function calculate_rating_for( $post_id ) { |
|
| 232 | - |
|
| 233 | - // If it's not an entity, return. |
|
| 234 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 235 | - return array(); |
|
| 236 | - } |
|
| 237 | - // Retrieve the post object. |
|
| 238 | - $post = get_post( $post_id ); |
|
| 239 | - |
|
| 240 | - // Rating value. |
|
| 241 | - $score = 0; |
|
| 242 | - |
|
| 243 | - // Store warning messages. |
|
| 244 | - $warnings = array(); |
|
| 245 | - |
|
| 246 | - // Is the current entity related to at least 1 post? |
|
| 247 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 248 | - $score ++ : |
|
| 249 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 250 | - |
|
| 251 | - // Is the post content not empty? |
|
| 252 | - ( ! empty( $post->post_content ) ) ? |
|
| 253 | - $score ++ : |
|
| 254 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 255 | - |
|
| 256 | - // Is the current entity related to at least 1 entity? |
|
| 257 | - // Was the current entity already disambiguated? |
|
| 258 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 259 | - $score ++ : |
|
| 260 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 261 | - |
|
| 262 | - // Is the entity published? |
|
| 263 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 264 | - $score ++ : |
|
| 265 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 266 | - |
|
| 267 | - // Has a thumbnail? |
|
| 268 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 269 | - $score ++ : |
|
| 270 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 271 | - |
|
| 272 | - // Get all post meta keys for the current post |
|
| 273 | - global $wpdb; |
|
| 274 | - |
|
| 275 | - // Check intersection between available meta keys and expected ones |
|
| 276 | - // arrays to detect missing values. |
|
| 277 | - $available_meta_keys = $wpdb->get_col( |
|
| 278 | - $wpdb->prepare( |
|
| 279 | - "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", |
|
| 280 | - $post->ID |
|
| 281 | - ) |
|
| 282 | - ); |
|
| 283 | - |
|
| 284 | - // If each expected key is contained in available keys array ... |
|
| 285 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
| 286 | - $score ++ : |
|
| 287 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 288 | - |
|
| 289 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 290 | - |
|
| 291 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 292 | - array() : |
|
| 293 | - array_keys( $schema['custom_fields'] ); |
|
| 294 | - |
|
| 295 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 296 | - // If each expected key is contained in available keys array ... |
|
| 297 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 298 | - $score ++ : |
|
| 299 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 300 | - |
|
| 301 | - // Finally return score and warnings |
|
| 302 | - return array( |
|
| 303 | - 'raw_score' => $score, |
|
| 304 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 305 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 306 | - 'warnings' => $warnings, |
|
| 307 | - ); |
|
| 308 | - |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Get as rating as input and convert in a traffic-light rating |
|
| 313 | - * |
|
| 314 | - * @param int $score The rating score for a given entity. |
|
| 315 | - * |
|
| 316 | - * @return string The input HTML code. |
|
| 317 | - * @since 3.3.0 |
|
| 318 | - */ |
|
| 319 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 320 | - // RATING_MAX : $score = 3 : x |
|
| 321 | - // See http://php.net/manual/en/function.round.php |
|
| 322 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 323 | - |
|
| 324 | - // If rating is 0, return 1, otherwise return rating |
|
| 325 | - return ( 0 === $rating ) ? 1 : $rating; |
|
| 326 | - |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get as rating as input and convert in a traffic-light rating |
|
| 331 | - * |
|
| 332 | - * @param int $score The rating score for a given entity. |
|
| 333 | - * |
|
| 334 | - * @return string The input HTML code. |
|
| 335 | - * @since 3.3.0 |
|
| 336 | - */ |
|
| 337 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 338 | - |
|
| 339 | - // RATING_MAX : $score = 100 : x |
|
| 340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Add admin notices for the current entity depending on the current rating. |
|
| 345 | - * |
|
| 346 | - * @since 3.3.0 |
|
| 347 | - */ |
|
| 348 | - public function in_admin_header() { |
|
| 349 | - |
|
| 350 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 351 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 352 | - return; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - $screen = get_current_screen(); |
|
| 356 | - // If there is any valid screen nothing to do |
|
| 357 | - if ( null === $screen ) { |
|
| 358 | - return; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // If you're not in the entity post edit page, return. |
|
| 362 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 363 | - return; |
|
| 364 | - } |
|
| 365 | - // Retrieve the current global post |
|
| 366 | - global $post; |
|
| 367 | - // If it's not an entity, return. |
|
| 368 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
| 369 | - return; |
|
| 370 | - } |
|
| 371 | - // Retrieve an updated rating for the current entity |
|
| 372 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 373 | - |
|
| 374 | - // If there is at least 1 warning |
|
| 375 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 376 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 377 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - } |
|
| 17 | + /** |
|
| 18 | + * Entity rating max. |
|
| 19 | + * |
|
| 20 | + * @since 3.3.0 |
|
| 21 | + */ |
|
| 22 | + const RATING_MAX = 7; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Entity rating score meta key. |
|
| 26 | + * |
|
| 27 | + * @since 3.3.0 |
|
| 28 | + */ |
|
| 29 | + const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Entity rating warnings meta key. |
|
| 33 | + * |
|
| 34 | + * @since 3.3.0 |
|
| 35 | + */ |
|
| 36 | + const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Entity warning has related post identifier. |
|
| 40 | + * |
|
| 41 | + * @since 3.3.0 |
|
| 42 | + */ |
|
| 43 | + const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Entity warning has content post identifier. |
|
| 47 | + * |
|
| 48 | + * @since 3.3.0 |
|
| 49 | + */ |
|
| 50 | + const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Entity warning has related entities identifier. |
|
| 54 | + * |
|
| 55 | + * @since 3.3.0 |
|
| 56 | + */ |
|
| 57 | + const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Entity warning is published identifier. |
|
| 61 | + * |
|
| 62 | + * @since 3.3.0 |
|
| 63 | + */ |
|
| 64 | + const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Entity warning has thumbnail identifier. |
|
| 68 | + * |
|
| 69 | + * @since 3.3.0 |
|
| 70 | + */ |
|
| 71 | + const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Entity warning has same as identifier. |
|
| 75 | + * |
|
| 76 | + * @since 3.3.0 |
|
| 77 | + */ |
|
| 78 | + const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Entity warning has completed metadata identifier. |
|
| 82 | + * |
|
| 83 | + * @since 3.3.0 |
|
| 84 | + */ |
|
| 85 | + const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 89 | + * |
|
| 90 | + * @since 3.10.0 |
|
| 91 | + * @access private |
|
| 92 | + * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 93 | + */ |
|
| 94 | + private $entity_type_service; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * The Notice service. |
|
| 98 | + * |
|
| 99 | + * @since 3.3.0 |
|
| 100 | + * @access private |
|
| 101 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 102 | + */ |
|
| 103 | + private $notice_service; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * A {@link Wordlift_Log_Service} instance. |
|
| 107 | + * |
|
| 108 | + * @since 3.10.0 |
|
| 109 | + * @access private |
|
| 110 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 111 | + */ |
|
| 112 | + private $log; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Create a {@link Wordlift_Rating_Service} instance. |
|
| 116 | + * |
|
| 117 | + * @since 3.10.0 |
|
| 118 | + */ |
|
| 119 | + protected function __construct() { |
|
| 120 | + |
|
| 121 | + $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 122 | + $this->notice_service = Wordlift_Notice_Service::get_instance(); |
|
| 123 | + |
|
| 124 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private static $instance; |
|
| 129 | + |
|
| 130 | + public static function get_instance() { |
|
| 131 | + if ( ! isset( self::$instance ) ) { |
|
| 132 | + self::$instance = new self(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return self::$instance; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Set rating for a given entity. |
|
| 140 | + * |
|
| 141 | + * @param int $post_id The entity post id. |
|
| 142 | + * |
|
| 143 | + * @return array An array representing the rating obj. |
|
| 144 | + * @since 3.3.0 |
|
| 145 | + */ |
|
| 146 | + public function set_rating_for( $post_id ) { |
|
| 147 | + |
|
| 148 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 149 | + if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
| 150 | + return; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Calculate rating for the given post. |
|
| 158 | + $rating = $this->calculate_rating_for( $post_id ); |
|
| 159 | + |
|
| 160 | + // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 161 | + // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 162 | + // |
|
| 163 | + // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 164 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 166 | + |
|
| 167 | + $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 168 | + |
|
| 169 | + // Finally returns the rating |
|
| 170 | + return $rating; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Get or calculate rating for a given entity |
|
| 175 | + * |
|
| 176 | + * @param int $post_id The entity post id. |
|
| 177 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 178 | + * |
|
| 179 | + * @return array An array representing the rating obj. |
|
| 180 | + * @since 3.3.0 |
|
| 181 | + */ |
|
| 182 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 183 | + |
|
| 184 | + // If forced reload is required or rating is missing. |
|
| 185 | + if ( $force_reload ) { |
|
| 186 | + $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 187 | + |
|
| 188 | + return $this->set_rating_for( $post_id ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 192 | + |
|
| 193 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
| 194 | + $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 195 | + |
|
| 196 | + return $this->set_rating_for( $post_id ); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 200 | + |
|
| 201 | + // Finally return score and warnings |
|
| 202 | + return array( |
|
| 203 | + 'raw_score' => $current_raw_score, |
|
| 204 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 205 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 206 | + 'warnings' => $current_warnings, |
|
| 207 | + ); |
|
| 208 | + |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Calculate rating for a given entity. |
|
| 213 | + * |
|
| 214 | + * Rating depends from following criteria: |
|
| 215 | + * |
|
| 216 | + * 1. Is the current entity related to at least 1 post? |
|
| 217 | + * 2. Is the current entity content post not empty? |
|
| 218 | + * 3. Is the current entity related to at least 1 entity? |
|
| 219 | + * 4. Is the entity published? |
|
| 220 | + * 5. There is a a thumbnail associated to the entity? |
|
| 221 | + * 6. Has the entity a sameas defined? |
|
| 222 | + * 7. Are all schema.org required metadata compiled? |
|
| 223 | + * |
|
| 224 | + * Each positive check means +1 in terms of rating score. |
|
| 225 | + * |
|
| 226 | + * @param int $post_id The entity post id. |
|
| 227 | + * |
|
| 228 | + * @return array An array representing the rating obj. |
|
| 229 | + * @since 3.3.0 |
|
| 230 | + */ |
|
| 231 | + private function calculate_rating_for( $post_id ) { |
|
| 232 | + |
|
| 233 | + // If it's not an entity, return. |
|
| 234 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 235 | + return array(); |
|
| 236 | + } |
|
| 237 | + // Retrieve the post object. |
|
| 238 | + $post = get_post( $post_id ); |
|
| 239 | + |
|
| 240 | + // Rating value. |
|
| 241 | + $score = 0; |
|
| 242 | + |
|
| 243 | + // Store warning messages. |
|
| 244 | + $warnings = array(); |
|
| 245 | + |
|
| 246 | + // Is the current entity related to at least 1 post? |
|
| 247 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 248 | + $score ++ : |
|
| 249 | + array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 250 | + |
|
| 251 | + // Is the post content not empty? |
|
| 252 | + ( ! empty( $post->post_content ) ) ? |
|
| 253 | + $score ++ : |
|
| 254 | + array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 255 | + |
|
| 256 | + // Is the current entity related to at least 1 entity? |
|
| 257 | + // Was the current entity already disambiguated? |
|
| 258 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 259 | + $score ++ : |
|
| 260 | + array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 261 | + |
|
| 262 | + // Is the entity published? |
|
| 263 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 264 | + $score ++ : |
|
| 265 | + array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 266 | + |
|
| 267 | + // Has a thumbnail? |
|
| 268 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
| 269 | + $score ++ : |
|
| 270 | + array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 271 | + |
|
| 272 | + // Get all post meta keys for the current post |
|
| 273 | + global $wpdb; |
|
| 274 | + |
|
| 275 | + // Check intersection between available meta keys and expected ones |
|
| 276 | + // arrays to detect missing values. |
|
| 277 | + $available_meta_keys = $wpdb->get_col( |
|
| 278 | + $wpdb->prepare( |
|
| 279 | + "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", |
|
| 280 | + $post->ID |
|
| 281 | + ) |
|
| 282 | + ); |
|
| 283 | + |
|
| 284 | + // If each expected key is contained in available keys array ... |
|
| 285 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
| 286 | + $score ++ : |
|
| 287 | + array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 288 | + |
|
| 289 | + $schema = $this->entity_type_service->get( $post_id ); |
|
| 290 | + |
|
| 291 | + $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 292 | + array() : |
|
| 293 | + array_keys( $schema['custom_fields'] ); |
|
| 294 | + |
|
| 295 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 296 | + // If each expected key is contained in available keys array ... |
|
| 297 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 298 | + $score ++ : |
|
| 299 | + array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 300 | + |
|
| 301 | + // Finally return score and warnings |
|
| 302 | + return array( |
|
| 303 | + 'raw_score' => $score, |
|
| 304 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 305 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 306 | + 'warnings' => $warnings, |
|
| 307 | + ); |
|
| 308 | + |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Get as rating as input and convert in a traffic-light rating |
|
| 313 | + * |
|
| 314 | + * @param int $score The rating score for a given entity. |
|
| 315 | + * |
|
| 316 | + * @return string The input HTML code. |
|
| 317 | + * @since 3.3.0 |
|
| 318 | + */ |
|
| 319 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
| 320 | + // RATING_MAX : $score = 3 : x |
|
| 321 | + // See http://php.net/manual/en/function.round.php |
|
| 322 | + $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 323 | + |
|
| 324 | + // If rating is 0, return 1, otherwise return rating |
|
| 325 | + return ( 0 === $rating ) ? 1 : $rating; |
|
| 326 | + |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get as rating as input and convert in a traffic-light rating |
|
| 331 | + * |
|
| 332 | + * @param int $score The rating score for a given entity. |
|
| 333 | + * |
|
| 334 | + * @return string The input HTML code. |
|
| 335 | + * @since 3.3.0 |
|
| 336 | + */ |
|
| 337 | + public function convert_raw_score_to_percentage( $score ) { |
|
| 338 | + |
|
| 339 | + // RATING_MAX : $score = 100 : x |
|
| 340 | + return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Add admin notices for the current entity depending on the current rating. |
|
| 345 | + * |
|
| 346 | + * @since 3.3.0 |
|
| 347 | + */ |
|
| 348 | + public function in_admin_header() { |
|
| 349 | + |
|
| 350 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 351 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 352 | + return; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + $screen = get_current_screen(); |
|
| 356 | + // If there is any valid screen nothing to do |
|
| 357 | + if ( null === $screen ) { |
|
| 358 | + return; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // If you're not in the entity post edit page, return. |
|
| 362 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 363 | + return; |
|
| 364 | + } |
|
| 365 | + // Retrieve the current global post |
|
| 366 | + global $post; |
|
| 367 | + // If it's not an entity, return. |
|
| 368 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
| 369 | + return; |
|
| 370 | + } |
|
| 371 | + // Retrieve an updated rating for the current entity |
|
| 372 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
| 373 | + |
|
| 374 | + // If there is at least 1 warning |
|
| 375 | + if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 376 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 377 | + $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | 382 | } |
@@ -121,14 +121,14 @@ discard block |
||
| 121 | 121 | $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
| 122 | 122 | $this->notice_service = Wordlift_Notice_Service::get_instance(); |
| 123 | 123 | |
| 124 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 124 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rating_Service'); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | private static $instance; |
| 129 | 129 | |
| 130 | 130 | public static function get_instance() { |
| 131 | - if ( ! isset( self::$instance ) ) { |
|
| 131 | + if ( ! isset(self::$instance)) { |
|
| 132 | 132 | self::$instance = new self(); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -143,28 +143,28 @@ discard block |
||
| 143 | 143 | * @return array An array representing the rating obj. |
| 144 | 144 | * @since 3.3.0 |
| 145 | 145 | */ |
| 146 | - public function set_rating_for( $post_id ) { |
|
| 146 | + public function set_rating_for($post_id) { |
|
| 147 | 147 | |
| 148 | 148 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 149 | - if ( ! apply_filters( 'wl_feature__enable__entity-rating', true ) ) { |
|
| 149 | + if ( ! apply_filters('wl_feature__enable__entity-rating', true)) { |
|
| 150 | 150 | return; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 153 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Calculate rating for the given post. |
| 158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 158 | + $rating = $this->calculate_rating_for($post_id); |
|
| 159 | 159 | |
| 160 | 160 | // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
| 161 | 161 | // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
| 162 | 162 | // |
| 163 | 163 | // See https://codex.wordpress.org/Function_Reference/update_post_meta |
| 164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 164 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
| 165 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
| 166 | 166 | |
| 167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 167 | + $this->log->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
| 168 | 168 | |
| 169 | 169 | // Finally returns the rating |
| 170 | 170 | return $rating; |
@@ -179,30 +179,30 @@ discard block |
||
| 179 | 179 | * @return array An array representing the rating obj. |
| 180 | 180 | * @since 3.3.0 |
| 181 | 181 | */ |
| 182 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 182 | + public function get_rating_for($post_id, $force_reload = false) { |
|
| 183 | 183 | |
| 184 | 184 | // If forced reload is required or rating is missing. |
| 185 | - if ( $force_reload ) { |
|
| 186 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 185 | + if ($force_reload) { |
|
| 186 | + $this->log->trace("Force rating reload [ post_id :: $post_id ]"); |
|
| 187 | 187 | |
| 188 | - return $this->set_rating_for( $post_id ); |
|
| 188 | + return $this->set_rating_for($post_id); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 191 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
| 192 | 192 | |
| 193 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 194 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 193 | + if ( ! is_numeric($current_raw_score)) { |
|
| 194 | + $this->log->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
| 195 | 195 | |
| 196 | - return $this->set_rating_for( $post_id ); |
|
| 196 | + return $this->set_rating_for($post_id); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 199 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
| 200 | 200 | |
| 201 | 201 | // Finally return score and warnings |
| 202 | 202 | return array( |
| 203 | 203 | 'raw_score' => $current_raw_score, |
| 204 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 205 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 204 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
| 205 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
| 206 | 206 | 'warnings' => $current_warnings, |
| 207 | 207 | ); |
| 208 | 208 | |
@@ -228,14 +228,14 @@ discard block |
||
| 228 | 228 | * @return array An array representing the rating obj. |
| 229 | 229 | * @since 3.3.0 |
| 230 | 230 | */ |
| 231 | - private function calculate_rating_for( $post_id ) { |
|
| 231 | + private function calculate_rating_for($post_id) { |
|
| 232 | 232 | |
| 233 | 233 | // If it's not an entity, return. |
| 234 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 234 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
| 235 | 235 | return array(); |
| 236 | 236 | } |
| 237 | 237 | // Retrieve the post object. |
| 238 | - $post = get_post( $post_id ); |
|
| 238 | + $post = get_post($post_id); |
|
| 239 | 239 | |
| 240 | 240 | // Rating value. |
| 241 | 241 | $score = 0; |
@@ -244,30 +244,25 @@ discard block |
||
| 244 | 244 | $warnings = array(); |
| 245 | 245 | |
| 246 | 246 | // Is the current entity related to at least 1 post? |
| 247 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 248 | - $score ++ : |
|
| 249 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 247 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
| 248 | + $score++ : array_push($warnings, __('There are no related posts for the current entity.', 'wordlift')); |
|
| 250 | 249 | |
| 251 | 250 | // Is the post content not empty? |
| 252 | - ( ! empty( $post->post_content ) ) ? |
|
| 253 | - $score ++ : |
|
| 254 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 251 | + ( ! empty($post->post_content)) ? |
|
| 252 | + $score++ : array_push($warnings, __('This entity has not description.', 'wordlift')); |
|
| 255 | 253 | |
| 256 | 254 | // Is the current entity related to at least 1 entity? |
| 257 | 255 | // Was the current entity already disambiguated? |
| 258 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 259 | - $score ++ : |
|
| 260 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 256 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
| 257 | + $score++ : array_push($warnings, __('There are no related entities for the current entity.', 'wordlift')); |
|
| 261 | 258 | |
| 262 | 259 | // Is the entity published? |
| 263 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 264 | - $score ++ : |
|
| 265 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 260 | + ('publish' === get_post_status($post->ID)) ? |
|
| 261 | + $score++ : array_push($warnings, __('This entity is not published. It will not appear within analysis results.', 'wordlift')); |
|
| 266 | 262 | |
| 267 | 263 | // Has a thumbnail? |
| 268 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 269 | - $score ++ : |
|
| 270 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 264 | + (has_post_thumbnail($post->ID)) ? |
|
| 265 | + $score++ : array_push($warnings, __('This entity has no featured image yet.', 'wordlift')); |
|
| 271 | 266 | |
| 272 | 267 | // Get all post meta keys for the current post |
| 273 | 268 | global $wpdb; |
@@ -282,27 +277,24 @@ discard block |
||
| 282 | 277 | ); |
| 283 | 278 | |
| 284 | 279 | // If each expected key is contained in available keys array ... |
| 285 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true ) ) ? |
|
| 286 | - $score ++ : |
|
| 287 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 280 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys, true)) ? |
|
| 281 | + $score++ : array_push($warnings, __('There are no sameAs configured for this entity.', 'wordlift')); |
|
| 288 | 282 | |
| 289 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 283 | + $schema = $this->entity_type_service->get($post_id); |
|
| 290 | 284 | |
| 291 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 292 | - array() : |
|
| 293 | - array_keys( $schema['custom_fields'] ); |
|
| 285 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
| 286 | + array() : array_keys($schema['custom_fields']); |
|
| 294 | 287 | |
| 295 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 288 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
| 296 | 289 | // If each expected key is contained in available keys array ... |
| 297 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 298 | - $score ++ : |
|
| 299 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 290 | + (count($intersection) === count($expected_meta_keys)) ? |
|
| 291 | + $score++ : array_push($warnings, __('Schema.org metadata for this entity are not completed.', 'wordlift')); |
|
| 300 | 292 | |
| 301 | 293 | // Finally return score and warnings |
| 302 | 294 | return array( |
| 303 | 295 | 'raw_score' => $score, |
| 304 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 305 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 296 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
| 297 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
| 306 | 298 | 'warnings' => $warnings, |
| 307 | 299 | ); |
| 308 | 300 | |
@@ -316,13 +308,13 @@ discard block |
||
| 316 | 308 | * @return string The input HTML code. |
| 317 | 309 | * @since 3.3.0 |
| 318 | 310 | */ |
| 319 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 311 | + private function convert_raw_score_to_traffic_light($score) { |
|
| 320 | 312 | // RATING_MAX : $score = 3 : x |
| 321 | 313 | // See http://php.net/manual/en/function.round.php |
| 322 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 314 | + $rating = round(($score * 3) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 323 | 315 | |
| 324 | 316 | // If rating is 0, return 1, otherwise return rating |
| 325 | - return ( 0 === $rating ) ? 1 : $rating; |
|
| 317 | + return (0 === $rating) ? 1 : $rating; |
|
| 326 | 318 | |
| 327 | 319 | } |
| 328 | 320 | |
@@ -334,10 +326,10 @@ discard block |
||
| 334 | 326 | * @return string The input HTML code. |
| 335 | 327 | * @since 3.3.0 |
| 336 | 328 | */ |
| 337 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 329 | + public function convert_raw_score_to_percentage($score) { |
|
| 338 | 330 | |
| 339 | 331 | // RATING_MAX : $score = 100 : x |
| 340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 332 | + return round(($score * 100) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 341 | 333 | } |
| 342 | 334 | |
| 343 | 335 | /** |
@@ -348,33 +340,33 @@ discard block |
||
| 348 | 340 | public function in_admin_header() { |
| 349 | 341 | |
| 350 | 342 | // Return safely if get_current_screen() is not defined (yet) |
| 351 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 343 | + if (false === function_exists('get_current_screen')) { |
|
| 352 | 344 | return; |
| 353 | 345 | } |
| 354 | 346 | |
| 355 | 347 | $screen = get_current_screen(); |
| 356 | 348 | // If there is any valid screen nothing to do |
| 357 | - if ( null === $screen ) { |
|
| 349 | + if (null === $screen) { |
|
| 358 | 350 | return; |
| 359 | 351 | } |
| 360 | 352 | |
| 361 | 353 | // If you're not in the entity post edit page, return. |
| 362 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 354 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->id) { |
|
| 363 | 355 | return; |
| 364 | 356 | } |
| 365 | 357 | // Retrieve the current global post |
| 366 | 358 | global $post; |
| 367 | 359 | // If it's not an entity, return. |
| 368 | - if ( ! Wordlift_Entity_Service::get_instance()->is_entity( $post->ID ) ) { |
|
| 360 | + if ( ! Wordlift_Entity_Service::get_instance()->is_entity($post->ID)) { |
|
| 369 | 361 | return; |
| 370 | 362 | } |
| 371 | 363 | // Retrieve an updated rating for the current entity |
| 372 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 364 | + $rating = $this->get_rating_for($post->ID, true); |
|
| 373 | 365 | |
| 374 | 366 | // If there is at least 1 warning |
| 375 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 367 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
| 376 | 368 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
| 377 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 369 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
| 378 | 370 | } |
| 379 | 371 | |
| 380 | 372 | } |
@@ -13,492 +13,492 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class Wordlift_Plugin_WP_Background_Process extends Wordlift_Plugin_WP_Async_Request { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Action |
|
| 18 | - * |
|
| 19 | - * (default value: 'background_process') |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - * @access protected |
|
| 23 | - */ |
|
| 24 | - protected $action = 'background_process'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Start time of current process. |
|
| 28 | - * |
|
| 29 | - * (default value: 0) |
|
| 30 | - * |
|
| 31 | - * @var int |
|
| 32 | - * @access protected |
|
| 33 | - */ |
|
| 34 | - protected $start_time = 0; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Cron_hook_identifier |
|
| 38 | - * |
|
| 39 | - * @var mixed |
|
| 40 | - * @access protected |
|
| 41 | - */ |
|
| 42 | - protected $cron_hook_identifier; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Cron_interval_identifier |
|
| 46 | - * |
|
| 47 | - * @var mixed |
|
| 48 | - * @access protected |
|
| 49 | - */ |
|
| 50 | - protected $cron_interval_identifier; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Initiate new background process |
|
| 54 | - */ |
|
| 55 | - public function __construct() { |
|
| 56 | - parent::__construct(); |
|
| 57 | - |
|
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | - |
|
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Dispatch |
|
| 67 | - * |
|
| 68 | - * @access public |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function dispatch() { |
|
| 72 | - // Schedule the cron healthcheck. |
|
| 73 | - $this->schedule_event(); |
|
| 74 | - |
|
| 75 | - // Perform remote post. |
|
| 76 | - return parent::dispatch(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Push to queue |
|
| 81 | - * |
|
| 82 | - * @param mixed $data Data. |
|
| 83 | - * |
|
| 84 | - * @return $this |
|
| 85 | - */ |
|
| 86 | - public function push_to_queue( $data ) { |
|
| 87 | - $this->data[] = $data; |
|
| 88 | - |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Save queue |
|
| 94 | - * |
|
| 95 | - * @return $this |
|
| 96 | - */ |
|
| 97 | - public function save() { |
|
| 98 | - $key = $this->generate_key(); |
|
| 99 | - |
|
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Update queue |
|
| 109 | - * |
|
| 110 | - * @param string $key Key. |
|
| 111 | - * @param array $data Data. |
|
| 112 | - * |
|
| 113 | - * @return $this |
|
| 114 | - */ |
|
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $this; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Delete queue |
|
| 125 | - * |
|
| 126 | - * @param string $key Key. |
|
| 127 | - * |
|
| 128 | - * @return $this |
|
| 129 | - */ |
|
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Generate key |
|
| 138 | - * |
|
| 139 | - * Generates a unique key based on microtime. Queue items are |
|
| 140 | - * given a unique key so that they can be merged upon save. |
|
| 141 | - * |
|
| 142 | - * @param int $length Length. |
|
| 143 | - * |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 149 | - |
|
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Maybe process queue |
|
| 155 | - * |
|
| 156 | - * Checks whether data exists within the queue and that |
|
| 157 | - * the process is not already running. |
|
| 158 | - */ |
|
| 159 | - public function maybe_handle() { |
|
| 160 | - // Don't lock up other requests while processing |
|
| 161 | - session_write_close(); |
|
| 162 | - |
|
| 163 | - if ( $this->is_process_running() ) { |
|
| 164 | - // Background process already running. |
|
| 165 | - wp_die(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 169 | - // No data to process. |
|
| 170 | - wp_die(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | - |
|
| 175 | - $this->handle(); |
|
| 176 | - |
|
| 177 | - wp_die(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Is queue empty |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - protected function is_queue_empty() { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - $table = $wpdb->options; |
|
| 189 | - $column = 'option_name'; |
|
| 190 | - |
|
| 191 | - if ( is_multisite() ) { |
|
| 192 | - $table = $wpdb->sitemeta; |
|
| 193 | - $column = 'meta_key'; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | - |
|
| 198 | - $count = $wpdb->get_var( |
|
| 199 | - $wpdb->prepare( |
|
| 200 | - "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 201 | - $key |
|
| 202 | - ) |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - return ( $count > 0 ) ? false : true; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Is process running |
|
| 210 | - * |
|
| 211 | - * Check whether the current process is already running |
|
| 212 | - * in a background process. |
|
| 213 | - */ |
|
| 214 | - protected function is_process_running() { |
|
| 215 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 216 | - // Process already running. |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Lock process |
|
| 225 | - * |
|
| 226 | - * Lock the process so that multiple instances can't run simultaneously. |
|
| 227 | - * Override if applicable, but the duration should be greater than that |
|
| 228 | - * defined in the time_exceeded() method. |
|
| 229 | - */ |
|
| 230 | - protected function lock_process() { |
|
| 231 | - $this->start_time = time(); // Set start time of current process. |
|
| 232 | - |
|
| 233 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 234 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 235 | - |
|
| 236 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Unlock process |
|
| 241 | - * |
|
| 242 | - * Unlock the process so that other instances can spawn. |
|
| 243 | - * |
|
| 244 | - * @return $this |
|
| 245 | - */ |
|
| 246 | - protected function unlock_process() { |
|
| 247 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 248 | - |
|
| 249 | - return $this; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get batch |
|
| 254 | - * |
|
| 255 | - * @return stdClass Return the first batch from the queue |
|
| 256 | - */ |
|
| 257 | - protected function get_batch() { |
|
| 258 | - global $wpdb; |
|
| 259 | - |
|
| 260 | - $table = $wpdb->options; |
|
| 261 | - $column = 'option_name'; |
|
| 262 | - $key_column = 'option_id'; |
|
| 263 | - $value_column = 'option_value'; |
|
| 264 | - |
|
| 265 | - if ( is_multisite() ) { |
|
| 266 | - $table = $wpdb->sitemeta; |
|
| 267 | - $column = 'meta_key'; |
|
| 268 | - $key_column = 'meta_id'; |
|
| 269 | - $value_column = 'meta_value'; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 273 | - |
|
| 274 | - $query = $wpdb->get_row( |
|
| 275 | - $wpdb->prepare( |
|
| 276 | - "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 277 | - $key |
|
| 278 | - ) |
|
| 279 | - ); |
|
| 280 | - |
|
| 281 | - $batch = new stdClass(); |
|
| 282 | - $batch->key = $query->$column; |
|
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | - |
|
| 285 | - return $batch; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Handle |
|
| 290 | - * |
|
| 291 | - * Pass each queue item to the task handler, while remaining |
|
| 292 | - * within server memory and time limit constraints. |
|
| 293 | - */ |
|
| 294 | - protected function handle() { |
|
| 295 | - $this->lock_process(); |
|
| 296 | - |
|
| 297 | - do { |
|
| 298 | - $batch = $this->get_batch(); |
|
| 299 | - |
|
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 302 | - |
|
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 305 | - } else { |
|
| 306 | - unset( $batch->data[ $key ] ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | - // Batch limits reached. |
|
| 311 | - break; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // Update or delete current batch. |
|
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 318 | - } else { |
|
| 319 | - $this->delete( $batch->key ); |
|
| 320 | - } |
|
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | - |
|
| 323 | - $this->unlock_process(); |
|
| 324 | - |
|
| 325 | - // Start next batch or complete process. |
|
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 327 | - $this->dispatch(); |
|
| 328 | - } else { |
|
| 329 | - $this->complete(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - wp_die(); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Memory exceeded |
|
| 337 | - * |
|
| 338 | - * Ensures the batch process never exceeds 90% |
|
| 339 | - * of the maximum WordPress memory. |
|
| 340 | - * |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - protected function memory_exceeded() { |
|
| 344 | - $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 346 | - $return = false; |
|
| 347 | - |
|
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 349 | - $return = true; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Get memory limit |
|
| 357 | - * |
|
| 358 | - * @return int |
|
| 359 | - */ |
|
| 360 | - protected function get_memory_limit() { |
|
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | - } else { |
|
| 364 | - // Sensible default. |
|
| 365 | - $memory_limit = '128M'; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | - // Unlimited, set to 32GB. |
|
| 370 | - $memory_limit = '32000M'; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Time exceeded. |
|
| 378 | - * |
|
| 379 | - * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | - * A timeout limit of 30s is common on shared hosting. |
|
| 381 | - * |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - protected function time_exceeded() { |
|
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | - $return = false; |
|
| 387 | - |
|
| 388 | - if ( time() >= $finish ) { |
|
| 389 | - $return = true; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Complete. |
|
| 397 | - * |
|
| 398 | - * Override if applicable, but ensure that the below actions are |
|
| 399 | - * performed, or, call parent::complete(). |
|
| 400 | - */ |
|
| 401 | - protected function complete() { |
|
| 402 | - // Unschedule the cron healthcheck. |
|
| 403 | - $this->clear_scheduled_event(); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Schedule cron healthcheck |
|
| 408 | - * |
|
| 409 | - * @access public |
|
| 410 | - * |
|
| 411 | - * @param mixed $schedules Schedules. |
|
| 412 | - * |
|
| 413 | - * @return mixed |
|
| 414 | - */ |
|
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | - |
|
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // Adds every 5 minutes to the existing schedules. |
|
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | - 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | - ); |
|
| 427 | - |
|
| 428 | - return $schedules; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Handle cron healthcheck |
|
| 433 | - * |
|
| 434 | - * Restart the background process if not already running |
|
| 435 | - * and data exists in the queue. |
|
| 436 | - */ |
|
| 437 | - public function handle_cron_healthcheck() { |
|
| 438 | - if ( $this->is_process_running() ) { |
|
| 439 | - // Background process already running. |
|
| 440 | - exit; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 444 | - // No data to process. |
|
| 445 | - $this->clear_scheduled_event(); |
|
| 446 | - exit; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $this->handle(); |
|
| 450 | - |
|
| 451 | - exit; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Schedule event |
|
| 456 | - */ |
|
| 457 | - protected function schedule_event() { |
|
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Clear scheduled event |
|
| 465 | - */ |
|
| 466 | - protected function clear_scheduled_event() { |
|
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | - |
|
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Cancel Process |
|
| 476 | - * |
|
| 477 | - * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | - */ |
|
| 479 | - public function cancel_process() { |
|
| 480 | - if ( ! $this->is_queue_empty() ) { |
|
| 481 | - $batch = $this->get_batch(); |
|
| 482 | - |
|
| 483 | - $this->delete( $batch->key ); |
|
| 484 | - |
|
| 485 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Task |
|
| 492 | - * |
|
| 493 | - * Override this method to perform any actions required on each |
|
| 494 | - * queue item. Return the modified item for further processing |
|
| 495 | - * in the next pass through. Or, return false to remove the |
|
| 496 | - * item from the queue. |
|
| 497 | - * |
|
| 498 | - * @param mixed $item Queue item to iterate over. |
|
| 499 | - * |
|
| 500 | - * @return mixed |
|
| 501 | - */ |
|
| 502 | - abstract protected function task( $item ); |
|
| 16 | + /** |
|
| 17 | + * Action |
|
| 18 | + * |
|
| 19 | + * (default value: 'background_process') |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + * @access protected |
|
| 23 | + */ |
|
| 24 | + protected $action = 'background_process'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Start time of current process. |
|
| 28 | + * |
|
| 29 | + * (default value: 0) |
|
| 30 | + * |
|
| 31 | + * @var int |
|
| 32 | + * @access protected |
|
| 33 | + */ |
|
| 34 | + protected $start_time = 0; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Cron_hook_identifier |
|
| 38 | + * |
|
| 39 | + * @var mixed |
|
| 40 | + * @access protected |
|
| 41 | + */ |
|
| 42 | + protected $cron_hook_identifier; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Cron_interval_identifier |
|
| 46 | + * |
|
| 47 | + * @var mixed |
|
| 48 | + * @access protected |
|
| 49 | + */ |
|
| 50 | + protected $cron_interval_identifier; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Initiate new background process |
|
| 54 | + */ |
|
| 55 | + public function __construct() { |
|
| 56 | + parent::__construct(); |
|
| 57 | + |
|
| 58 | + $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | + |
|
| 61 | + add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | + add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Dispatch |
|
| 67 | + * |
|
| 68 | + * @access public |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function dispatch() { |
|
| 72 | + // Schedule the cron healthcheck. |
|
| 73 | + $this->schedule_event(); |
|
| 74 | + |
|
| 75 | + // Perform remote post. |
|
| 76 | + return parent::dispatch(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Push to queue |
|
| 81 | + * |
|
| 82 | + * @param mixed $data Data. |
|
| 83 | + * |
|
| 84 | + * @return $this |
|
| 85 | + */ |
|
| 86 | + public function push_to_queue( $data ) { |
|
| 87 | + $this->data[] = $data; |
|
| 88 | + |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Save queue |
|
| 94 | + * |
|
| 95 | + * @return $this |
|
| 96 | + */ |
|
| 97 | + public function save() { |
|
| 98 | + $key = $this->generate_key(); |
|
| 99 | + |
|
| 100 | + if ( ! empty( $this->data ) ) { |
|
| 101 | + update_site_option( $key, $this->data ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Update queue |
|
| 109 | + * |
|
| 110 | + * @param string $key Key. |
|
| 111 | + * @param array $data Data. |
|
| 112 | + * |
|
| 113 | + * @return $this |
|
| 114 | + */ |
|
| 115 | + public function update( $key, $data ) { |
|
| 116 | + if ( ! empty( $data ) ) { |
|
| 117 | + update_site_option( $key, $data ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $this; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Delete queue |
|
| 125 | + * |
|
| 126 | + * @param string $key Key. |
|
| 127 | + * |
|
| 128 | + * @return $this |
|
| 129 | + */ |
|
| 130 | + public function delete( $key ) { |
|
| 131 | + delete_site_option( $key ); |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Generate key |
|
| 138 | + * |
|
| 139 | + * Generates a unique key based on microtime. Queue items are |
|
| 140 | + * given a unique key so that they can be merged upon save. |
|
| 141 | + * |
|
| 142 | + * @param int $length Length. |
|
| 143 | + * |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + protected function generate_key( $length = 64 ) { |
|
| 147 | + $unique = md5( microtime() . rand() ); |
|
| 148 | + $prepend = $this->identifier . '_batch_'; |
|
| 149 | + |
|
| 150 | + return substr( $prepend . $unique, 0, $length ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Maybe process queue |
|
| 155 | + * |
|
| 156 | + * Checks whether data exists within the queue and that |
|
| 157 | + * the process is not already running. |
|
| 158 | + */ |
|
| 159 | + public function maybe_handle() { |
|
| 160 | + // Don't lock up other requests while processing |
|
| 161 | + session_write_close(); |
|
| 162 | + |
|
| 163 | + if ( $this->is_process_running() ) { |
|
| 164 | + // Background process already running. |
|
| 165 | + wp_die(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + if ( $this->is_queue_empty() ) { |
|
| 169 | + // No data to process. |
|
| 170 | + wp_die(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | + |
|
| 175 | + $this->handle(); |
|
| 176 | + |
|
| 177 | + wp_die(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Is queue empty |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + protected function is_queue_empty() { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + $table = $wpdb->options; |
|
| 189 | + $column = 'option_name'; |
|
| 190 | + |
|
| 191 | + if ( is_multisite() ) { |
|
| 192 | + $table = $wpdb->sitemeta; |
|
| 193 | + $column = 'meta_key'; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | + |
|
| 198 | + $count = $wpdb->get_var( |
|
| 199 | + $wpdb->prepare( |
|
| 200 | + "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 201 | + $key |
|
| 202 | + ) |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + return ( $count > 0 ) ? false : true; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Is process running |
|
| 210 | + * |
|
| 211 | + * Check whether the current process is already running |
|
| 212 | + * in a background process. |
|
| 213 | + */ |
|
| 214 | + protected function is_process_running() { |
|
| 215 | + if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 216 | + // Process already running. |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Lock process |
|
| 225 | + * |
|
| 226 | + * Lock the process so that multiple instances can't run simultaneously. |
|
| 227 | + * Override if applicable, but the duration should be greater than that |
|
| 228 | + * defined in the time_exceeded() method. |
|
| 229 | + */ |
|
| 230 | + protected function lock_process() { |
|
| 231 | + $this->start_time = time(); // Set start time of current process. |
|
| 232 | + |
|
| 233 | + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 234 | + $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 235 | + |
|
| 236 | + set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Unlock process |
|
| 241 | + * |
|
| 242 | + * Unlock the process so that other instances can spawn. |
|
| 243 | + * |
|
| 244 | + * @return $this |
|
| 245 | + */ |
|
| 246 | + protected function unlock_process() { |
|
| 247 | + delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 248 | + |
|
| 249 | + return $this; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get batch |
|
| 254 | + * |
|
| 255 | + * @return stdClass Return the first batch from the queue |
|
| 256 | + */ |
|
| 257 | + protected function get_batch() { |
|
| 258 | + global $wpdb; |
|
| 259 | + |
|
| 260 | + $table = $wpdb->options; |
|
| 261 | + $column = 'option_name'; |
|
| 262 | + $key_column = 'option_id'; |
|
| 263 | + $value_column = 'option_value'; |
|
| 264 | + |
|
| 265 | + if ( is_multisite() ) { |
|
| 266 | + $table = $wpdb->sitemeta; |
|
| 267 | + $column = 'meta_key'; |
|
| 268 | + $key_column = 'meta_id'; |
|
| 269 | + $value_column = 'meta_value'; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 273 | + |
|
| 274 | + $query = $wpdb->get_row( |
|
| 275 | + $wpdb->prepare( |
|
| 276 | + "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 277 | + $key |
|
| 278 | + ) |
|
| 279 | + ); |
|
| 280 | + |
|
| 281 | + $batch = new stdClass(); |
|
| 282 | + $batch->key = $query->$column; |
|
| 283 | + $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | + |
|
| 285 | + return $batch; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Handle |
|
| 290 | + * |
|
| 291 | + * Pass each queue item to the task handler, while remaining |
|
| 292 | + * within server memory and time limit constraints. |
|
| 293 | + */ |
|
| 294 | + protected function handle() { |
|
| 295 | + $this->lock_process(); |
|
| 296 | + |
|
| 297 | + do { |
|
| 298 | + $batch = $this->get_batch(); |
|
| 299 | + |
|
| 300 | + foreach ( $batch->data as $key => $value ) { |
|
| 301 | + $task = $this->task( $value ); |
|
| 302 | + |
|
| 303 | + if ( false !== $task ) { |
|
| 304 | + $batch->data[ $key ] = $task; |
|
| 305 | + } else { |
|
| 306 | + unset( $batch->data[ $key ] ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | + // Batch limits reached. |
|
| 311 | + break; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // Update or delete current batch. |
|
| 316 | + if ( ! empty( $batch->data ) ) { |
|
| 317 | + $this->update( $batch->key, $batch->data ); |
|
| 318 | + } else { |
|
| 319 | + $this->delete( $batch->key ); |
|
| 320 | + } |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | + |
|
| 323 | + $this->unlock_process(); |
|
| 324 | + |
|
| 325 | + // Start next batch or complete process. |
|
| 326 | + if ( ! $this->is_queue_empty() ) { |
|
| 327 | + $this->dispatch(); |
|
| 328 | + } else { |
|
| 329 | + $this->complete(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + wp_die(); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Memory exceeded |
|
| 337 | + * |
|
| 338 | + * Ensures the batch process never exceeds 90% |
|
| 339 | + * of the maximum WordPress memory. |
|
| 340 | + * |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + protected function memory_exceeded() { |
|
| 344 | + $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | + $current_memory = memory_get_usage( true ); |
|
| 346 | + $return = false; |
|
| 347 | + |
|
| 348 | + if ( $current_memory >= $memory_limit ) { |
|
| 349 | + $return = true; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Get memory limit |
|
| 357 | + * |
|
| 358 | + * @return int |
|
| 359 | + */ |
|
| 360 | + protected function get_memory_limit() { |
|
| 361 | + if ( function_exists( 'ini_get' ) ) { |
|
| 362 | + $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | + } else { |
|
| 364 | + // Sensible default. |
|
| 365 | + $memory_limit = '128M'; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | + // Unlimited, set to 32GB. |
|
| 370 | + $memory_limit = '32000M'; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Time exceeded. |
|
| 378 | + * |
|
| 379 | + * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | + * A timeout limit of 30s is common on shared hosting. |
|
| 381 | + * |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + protected function time_exceeded() { |
|
| 385 | + $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | + $return = false; |
|
| 387 | + |
|
| 388 | + if ( time() >= $finish ) { |
|
| 389 | + $return = true; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Complete. |
|
| 397 | + * |
|
| 398 | + * Override if applicable, but ensure that the below actions are |
|
| 399 | + * performed, or, call parent::complete(). |
|
| 400 | + */ |
|
| 401 | + protected function complete() { |
|
| 402 | + // Unschedule the cron healthcheck. |
|
| 403 | + $this->clear_scheduled_event(); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Schedule cron healthcheck |
|
| 408 | + * |
|
| 409 | + * @access public |
|
| 410 | + * |
|
| 411 | + * @param mixed $schedules Schedules. |
|
| 412 | + * |
|
| 413 | + * @return mixed |
|
| 414 | + */ |
|
| 415 | + public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | + $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | + |
|
| 418 | + if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | + $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // Adds every 5 minutes to the existing schedules. |
|
| 423 | + $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | + 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | + 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | + ); |
|
| 427 | + |
|
| 428 | + return $schedules; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Handle cron healthcheck |
|
| 433 | + * |
|
| 434 | + * Restart the background process if not already running |
|
| 435 | + * and data exists in the queue. |
|
| 436 | + */ |
|
| 437 | + public function handle_cron_healthcheck() { |
|
| 438 | + if ( $this->is_process_running() ) { |
|
| 439 | + // Background process already running. |
|
| 440 | + exit; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + if ( $this->is_queue_empty() ) { |
|
| 444 | + // No data to process. |
|
| 445 | + $this->clear_scheduled_event(); |
|
| 446 | + exit; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $this->handle(); |
|
| 450 | + |
|
| 451 | + exit; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Schedule event |
|
| 456 | + */ |
|
| 457 | + protected function schedule_event() { |
|
| 458 | + if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | + wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Clear scheduled event |
|
| 465 | + */ |
|
| 466 | + protected function clear_scheduled_event() { |
|
| 467 | + $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | + |
|
| 469 | + if ( $timestamp ) { |
|
| 470 | + wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Cancel Process |
|
| 476 | + * |
|
| 477 | + * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | + */ |
|
| 479 | + public function cancel_process() { |
|
| 480 | + if ( ! $this->is_queue_empty() ) { |
|
| 481 | + $batch = $this->get_batch(); |
|
| 482 | + |
|
| 483 | + $this->delete( $batch->key ); |
|
| 484 | + |
|
| 485 | + wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Task |
|
| 492 | + * |
|
| 493 | + * Override this method to perform any actions required on each |
|
| 494 | + * queue item. Return the modified item for further processing |
|
| 495 | + * in the next pass through. Or, return false to remove the |
|
| 496 | + * item from the queue. |
|
| 497 | + * |
|
| 498 | + * @param mixed $item Queue item to iterate over. |
|
| 499 | + * |
|
| 500 | + * @return mixed |
|
| 501 | + */ |
|
| 502 | + abstract protected function task( $item ); |
|
| 503 | 503 | |
| 504 | 504 | } |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | public function __construct() { |
| 56 | 56 | parent::__construct(); |
| 57 | 57 | |
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 58 | + $this->cron_hook_identifier = $this->identifier.'_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier.'_cron_interval'; |
|
| 60 | 60 | |
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 61 | + add_action($this->cron_hook_identifier, array($this, 'handle_cron_healthcheck')); |
|
| 62 | + add_filter('cron_schedules', array($this, 'schedule_cron_healthcheck')); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return $this |
| 85 | 85 | */ |
| 86 | - public function push_to_queue( $data ) { |
|
| 86 | + public function push_to_queue($data) { |
|
| 87 | 87 | $this->data[] = $data; |
| 88 | 88 | |
| 89 | 89 | return $this; |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | public function save() { |
| 98 | 98 | $key = $this->generate_key(); |
| 99 | 99 | |
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 100 | + if ( ! empty($this->data)) { |
|
| 101 | + update_site_option($key, $this->data); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | return $this; |
@@ -112,9 +112,9 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return $this |
| 114 | 114 | */ |
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 115 | + public function update($key, $data) { |
|
| 116 | + if ( ! empty($data)) { |
|
| 117 | + update_site_option($key, $data); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | return $this; |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | * @return $this |
| 129 | 129 | */ |
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 130 | + public function delete($key) { |
|
| 131 | + delete_site_option($key); |
|
| 132 | 132 | |
| 133 | 133 | return $this; |
| 134 | 134 | } |
@@ -143,11 +143,11 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 146 | + protected function generate_key($length = 64) { |
|
| 147 | + $unique = md5(microtime().rand()); |
|
| 148 | + $prepend = $this->identifier.'_batch_'; |
|
| 149 | 149 | |
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 150 | + return substr($prepend.$unique, 0, $length); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -160,17 +160,17 @@ discard block |
||
| 160 | 160 | // Don't lock up other requests while processing |
| 161 | 161 | session_write_close(); |
| 162 | 162 | |
| 163 | - if ( $this->is_process_running() ) { |
|
| 163 | + if ($this->is_process_running()) { |
|
| 164 | 164 | // Background process already running. |
| 165 | 165 | wp_die(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 168 | + if ($this->is_queue_empty()) { |
|
| 169 | 169 | // No data to process. |
| 170 | 170 | wp_die(); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 173 | + check_ajax_referer($this->identifier, 'nonce'); |
|
| 174 | 174 | |
| 175 | 175 | $this->handle(); |
| 176 | 176 | |
@@ -188,12 +188,12 @@ discard block |
||
| 188 | 188 | $table = $wpdb->options; |
| 189 | 189 | $column = 'option_name'; |
| 190 | 190 | |
| 191 | - if ( is_multisite() ) { |
|
| 191 | + if (is_multisite()) { |
|
| 192 | 192 | $table = $wpdb->sitemeta; |
| 193 | 193 | $column = 'meta_key'; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 196 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 197 | 197 | |
| 198 | 198 | $count = $wpdb->get_var( |
| 199 | 199 | $wpdb->prepare( |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | ) |
| 203 | 203 | ); |
| 204 | 204 | |
| 205 | - return ( $count > 0 ) ? false : true; |
|
| 205 | + return ($count > 0) ? false : true; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * in a background process. |
| 213 | 213 | */ |
| 214 | 214 | protected function is_process_running() { |
| 215 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 215 | + if (get_site_transient($this->identifier.'_process_lock')) { |
|
| 216 | 216 | // Process already running. |
| 217 | 217 | return true; |
| 218 | 218 | } |
@@ -230,10 +230,10 @@ discard block |
||
| 230 | 230 | protected function lock_process() { |
| 231 | 231 | $this->start_time = time(); // Set start time of current process. |
| 232 | 232 | |
| 233 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 234 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 233 | + $lock_duration = (property_exists($this, 'queue_lock_time')) ? $this->queue_lock_time : 60; // 1 minute |
|
| 234 | + $lock_duration = apply_filters($this->identifier.'_queue_lock_time', $lock_duration); |
|
| 235 | 235 | |
| 236 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 236 | + set_site_transient($this->identifier.'_process_lock', microtime(), $lock_duration); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | * @return $this |
| 245 | 245 | */ |
| 246 | 246 | protected function unlock_process() { |
| 247 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 247 | + delete_site_transient($this->identifier.'_process_lock'); |
|
| 248 | 248 | |
| 249 | 249 | return $this; |
| 250 | 250 | } |
@@ -262,14 +262,14 @@ discard block |
||
| 262 | 262 | $key_column = 'option_id'; |
| 263 | 263 | $value_column = 'option_value'; |
| 264 | 264 | |
| 265 | - if ( is_multisite() ) { |
|
| 265 | + if (is_multisite()) { |
|
| 266 | 266 | $table = $wpdb->sitemeta; |
| 267 | 267 | $column = 'meta_key'; |
| 268 | 268 | $key_column = 'meta_id'; |
| 269 | 269 | $value_column = 'meta_value'; |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 272 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 273 | 273 | |
| 274 | 274 | $query = $wpdb->get_row( |
| 275 | 275 | $wpdb->prepare( |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | $batch = new stdClass(); |
| 282 | 282 | $batch->key = $query->$column; |
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 283 | + $batch->data = maybe_unserialize($query->$value_column); |
|
| 284 | 284 | |
| 285 | 285 | return $batch; |
| 286 | 286 | } |
@@ -297,33 +297,33 @@ discard block |
||
| 297 | 297 | do { |
| 298 | 298 | $batch = $this->get_batch(); |
| 299 | 299 | |
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 300 | + foreach ($batch->data as $key => $value) { |
|
| 301 | + $task = $this->task($value); |
|
| 302 | 302 | |
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 303 | + if (false !== $task) { |
|
| 304 | + $batch->data[$key] = $task; |
|
| 305 | 305 | } else { |
| 306 | - unset( $batch->data[ $key ] ); |
|
| 306 | + unset($batch->data[$key]); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 309 | + if ($this->time_exceeded() || $this->memory_exceeded()) { |
|
| 310 | 310 | // Batch limits reached. |
| 311 | 311 | break; |
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | // Update or delete current batch. |
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 316 | + if ( ! empty($batch->data)) { |
|
| 317 | + $this->update($batch->key, $batch->data); |
|
| 318 | 318 | } else { |
| 319 | - $this->delete( $batch->key ); |
|
| 319 | + $this->delete($batch->key); |
|
| 320 | 320 | } |
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty()); |
|
| 322 | 322 | |
| 323 | 323 | $this->unlock_process(); |
| 324 | 324 | |
| 325 | 325 | // Start next batch or complete process. |
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 326 | + if ( ! $this->is_queue_empty()) { |
|
| 327 | 327 | $this->dispatch(); |
| 328 | 328 | } else { |
| 329 | 329 | $this->complete(); |
@@ -342,14 +342,14 @@ discard block |
||
| 342 | 342 | */ |
| 343 | 343 | protected function memory_exceeded() { |
| 344 | 344 | $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 345 | + $current_memory = memory_get_usage(true); |
|
| 346 | 346 | $return = false; |
| 347 | 347 | |
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 348 | + if ($current_memory >= $memory_limit) { |
|
| 349 | 349 | $return = true; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 352 | + return apply_filters($this->identifier.'_memory_exceeded', $return); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -358,19 +358,19 @@ discard block |
||
| 358 | 358 | * @return int |
| 359 | 359 | */ |
| 360 | 360 | protected function get_memory_limit() { |
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 361 | + if (function_exists('ini_get')) { |
|
| 362 | + $memory_limit = ini_get('memory_limit'); |
|
| 363 | 363 | } else { |
| 364 | 364 | // Sensible default. |
| 365 | 365 | $memory_limit = '128M'; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 368 | + if ( ! $memory_limit || - 1 === intval($memory_limit)) { |
|
| 369 | 369 | // Unlimited, set to 32GB. |
| 370 | 370 | $memory_limit = '32000M'; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 373 | + return wp_convert_hr_to_bytes($memory_limit); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -382,14 +382,14 @@ discard block |
||
| 382 | 382 | * @return bool |
| 383 | 383 | */ |
| 384 | 384 | protected function time_exceeded() { |
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 385 | + $finish = $this->start_time + apply_filters($this->identifier.'_default_time_limit', 20); // 20 seconds |
|
| 386 | 386 | $return = false; |
| 387 | 387 | |
| 388 | - if ( time() >= $finish ) { |
|
| 388 | + if (time() >= $finish) { |
|
| 389 | 389 | $return = true; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 392 | + return apply_filters($this->identifier.'_time_exceeded', $return); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -412,17 +412,17 @@ discard block |
||
| 412 | 412 | * |
| 413 | 413 | * @return mixed |
| 414 | 414 | */ |
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 415 | + public function schedule_cron_healthcheck($schedules) { |
|
| 416 | + $interval = apply_filters($this->identifier.'_cron_interval', 5); |
|
| 417 | 417 | |
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 418 | + if (property_exists($this, 'cron_interval')) { |
|
| 419 | + $interval = apply_filters($this->identifier.'_cron_interval', $this->cron_interval); |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | // Adds every 5 minutes to the existing schedules. |
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 423 | + $schedules[$this->identifier.'_cron_interval'] = array( |
|
| 424 | 424 | 'interval' => MINUTE_IN_SECONDS * $interval, |
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 425 | + 'display' => sprintf(__('Every %d Minutes'), $interval), |
|
| 426 | 426 | ); |
| 427 | 427 | |
| 428 | 428 | return $schedules; |
@@ -435,12 +435,12 @@ discard block |
||
| 435 | 435 | * and data exists in the queue. |
| 436 | 436 | */ |
| 437 | 437 | public function handle_cron_healthcheck() { |
| 438 | - if ( $this->is_process_running() ) { |
|
| 438 | + if ($this->is_process_running()) { |
|
| 439 | 439 | // Background process already running. |
| 440 | 440 | exit; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 443 | + if ($this->is_queue_empty()) { |
|
| 444 | 444 | // No data to process. |
| 445 | 445 | $this->clear_scheduled_event(); |
| 446 | 446 | exit; |
@@ -455,8 +455,8 @@ discard block |
||
| 455 | 455 | * Schedule event |
| 456 | 456 | */ |
| 457 | 457 | protected function schedule_event() { |
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 458 | + if ( ! wp_next_scheduled($this->cron_hook_identifier)) { |
|
| 459 | + wp_schedule_event(time(), $this->cron_interval_identifier, $this->cron_hook_identifier); |
|
| 460 | 460 | } |
| 461 | 461 | } |
| 462 | 462 | |
@@ -464,10 +464,10 @@ discard block |
||
| 464 | 464 | * Clear scheduled event |
| 465 | 465 | */ |
| 466 | 466 | protected function clear_scheduled_event() { |
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 467 | + $timestamp = wp_next_scheduled($this->cron_hook_identifier); |
|
| 468 | 468 | |
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 469 | + if ($timestamp) { |
|
| 470 | + wp_unschedule_event($timestamp, $this->cron_hook_identifier); |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
@@ -477,12 +477,12 @@ discard block |
||
| 477 | 477 | * Stop processing queue items, clear cronjob and delete batch. |
| 478 | 478 | */ |
| 479 | 479 | public function cancel_process() { |
| 480 | - if ( ! $this->is_queue_empty() ) { |
|
| 480 | + if ( ! $this->is_queue_empty()) { |
|
| 481 | 481 | $batch = $this->get_batch(); |
| 482 | 482 | |
| 483 | - $this->delete( $batch->key ); |
|
| 483 | + $this->delete($batch->key); |
|
| 484 | 484 | |
| 485 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 485 | + wp_clear_scheduled_hook($this->cron_hook_identifier); |
|
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | } |
@@ -499,6 +499,6 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return mixed |
| 501 | 501 | */ |
| 502 | - abstract protected function task( $item ); |
|
| 502 | + abstract protected function task($item); |
|
| 503 | 503 | |
| 504 | 504 | } |
@@ -2,53 +2,53 @@ |
||
| 2 | 2 | |
| 3 | 3 | class Wordlift_Timeline_Widget extends WP_Widget { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Sets up the widgets name etc |
|
| 7 | - */ |
|
| 8 | - public function __construct() { |
|
| 9 | - // widget actual processes |
|
| 10 | - parent::__construct( |
|
| 11 | - 'wl_timeline_widget', // Base ID |
|
| 12 | - __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
| 13 | - array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
| 14 | - ); |
|
| 15 | - } |
|
| 5 | + /** |
|
| 6 | + * Sets up the widgets name etc |
|
| 7 | + */ |
|
| 8 | + public function __construct() { |
|
| 9 | + // widget actual processes |
|
| 10 | + parent::__construct( |
|
| 11 | + 'wl_timeline_widget', // Base ID |
|
| 12 | + __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
| 13 | + array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
| 14 | + ); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Outputs the content of the widget |
|
| 19 | - * |
|
| 20 | - * @param array $args |
|
| 21 | - * @param array $instance |
|
| 22 | - */ |
|
| 23 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 24 | - public function widget( $args, $instance ) { |
|
| 25 | - // outputs the content of the widget |
|
| 26 | - echo do_shortcode( '[wl_timeline global=true]' ); |
|
| 27 | - } |
|
| 17 | + /** |
|
| 18 | + * Outputs the content of the widget |
|
| 19 | + * |
|
| 20 | + * @param array $args |
|
| 21 | + * @param array $instance |
|
| 22 | + */ |
|
| 23 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 24 | + public function widget( $args, $instance ) { |
|
| 25 | + // outputs the content of the widget |
|
| 26 | + echo do_shortcode( '[wl_timeline global=true]' ); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Outputs the options form on admin |
|
| 31 | - * |
|
| 32 | - * @param array $instance The widget options |
|
| 33 | - */ |
|
| 34 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 35 | - public function form( $instance ) { |
|
| 36 | - // outputs the options form on admin |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Outputs the options form on admin |
|
| 31 | + * |
|
| 32 | + * @param array $instance The widget options |
|
| 33 | + */ |
|
| 34 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 35 | + public function form( $instance ) { |
|
| 36 | + // outputs the options form on admin |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Processing widget options on save |
|
| 41 | - * |
|
| 42 | - * @param array $new_instance The new options |
|
| 43 | - * @param array $old_instance The previous options |
|
| 44 | - */ |
|
| 45 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 46 | - public function update( $new_instance, $old_instance ) { |
|
| 47 | - // processes widget options to be saved |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * Processing widget options on save |
|
| 41 | + * |
|
| 42 | + * @param array $new_instance The new options |
|
| 43 | + * @param array $old_instance The previous options |
|
| 44 | + */ |
|
| 45 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 46 | + public function update( $new_instance, $old_instance ) { |
|
| 47 | + // processes widget options to be saved |
|
| 48 | + } |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | function wl_register_timeline_widget() { |
| 52 | 52 | |
| 53 | - register_widget( 'WordLift_Timeline_Widget' ); |
|
| 53 | + register_widget( 'WordLift_Timeline_Widget' ); |
|
| 54 | 54 | } |
@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | // widget actual processes |
| 10 | 10 | parent::__construct( |
| 11 | 11 | 'wl_timeline_widget', // Base ID |
| 12 | - __( 'WordLift Timeline Widget', 'wordlift' ), // Name |
|
| 13 | - array( 'description' => __( 'Displays entities of type event using an interactive timeline.', 'wordlift' ) ) // Args |
|
| 12 | + __('WordLift Timeline Widget', 'wordlift'), // Name |
|
| 13 | + array('description' => __('Displays entities of type event using an interactive timeline.', 'wordlift')) // Args |
|
| 14 | 14 | ); |
| 15 | 15 | } |
| 16 | 16 | |
@@ -21,9 +21,9 @@ discard block |
||
| 21 | 21 | * @param array $instance |
| 22 | 22 | */ |
| 23 | 23 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 24 | - public function widget( $args, $instance ) { |
|
| 24 | + public function widget($args, $instance) { |
|
| 25 | 25 | // outputs the content of the widget |
| 26 | - echo do_shortcode( '[wl_timeline global=true]' ); |
|
| 26 | + echo do_shortcode('[wl_timeline global=true]'); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * @param array $instance The widget options |
| 33 | 33 | */ |
| 34 | 34 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 35 | - public function form( $instance ) { |
|
| 35 | + public function form($instance) { |
|
| 36 | 36 | // outputs the options form on admin |
| 37 | 37 | } |
| 38 | 38 | |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | * @param array $old_instance The previous options |
| 44 | 44 | */ |
| 45 | 45 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 46 | - public function update( $new_instance, $old_instance ) { |
|
| 46 | + public function update($new_instance, $old_instance) { |
|
| 47 | 47 | // processes widget options to be saved |
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | function wl_register_timeline_widget() { |
| 52 | 52 | |
| 53 | - register_widget( 'WordLift_Timeline_Widget' ); |
|
| 53 | + register_widget('WordLift_Timeline_Widget'); |
|
| 54 | 54 | } |
@@ -10,56 +10,56 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | class Wordlift_Chord_Widget extends WP_Widget { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Sets up the widgets name etc |
|
| 15 | - */ |
|
| 16 | - public function __construct() { |
|
| 17 | - // widget actual processes. |
|
| 18 | - parent::__construct( |
|
| 19 | - 'wl_chord_widget', // Base ID. |
|
| 20 | - __( 'Chord Widget', 'wordlift' ), // Name. |
|
| 21 | - array( |
|
| 22 | - 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), |
|
| 23 | - ) // Args. |
|
| 24 | - ); |
|
| 25 | - } |
|
| 13 | + /** |
|
| 14 | + * Sets up the widgets name etc |
|
| 15 | + */ |
|
| 16 | + public function __construct() { |
|
| 17 | + // widget actual processes. |
|
| 18 | + parent::__construct( |
|
| 19 | + 'wl_chord_widget', // Base ID. |
|
| 20 | + __( 'Chord Widget', 'wordlift' ), // Name. |
|
| 21 | + array( |
|
| 22 | + 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), |
|
| 23 | + ) // Args. |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Outputs the content of the widget |
|
| 29 | - * |
|
| 30 | - * @param array $args widget args. |
|
| 31 | - * @param array $instance widget instance. |
|
| 32 | - */ |
|
| 33 | - // @codingStandardsIgnoreLine Generic.CodeAnalysis.UnusedFunctionParameter.Found |
|
| 34 | - public function widget( $args, $instance ) { |
|
| 35 | - // outputs the content of the widget. |
|
| 36 | - echo do_shortcode( '[wl_chord global=true]' ); |
|
| 37 | - } |
|
| 27 | + /** |
|
| 28 | + * Outputs the content of the widget |
|
| 29 | + * |
|
| 30 | + * @param array $args widget args. |
|
| 31 | + * @param array $instance widget instance. |
|
| 32 | + */ |
|
| 33 | + // @codingStandardsIgnoreLine Generic.CodeAnalysis.UnusedFunctionParameter.Found |
|
| 34 | + public function widget( $args, $instance ) { |
|
| 35 | + // outputs the content of the widget. |
|
| 36 | + echo do_shortcode( '[wl_chord global=true]' ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Outputs the options form on admin |
|
| 41 | - * |
|
| 42 | - * @param array $instance The widget options. |
|
| 43 | - * |
|
| 44 | - * @return string|void |
|
| 45 | - */ |
|
| 46 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 47 | - public function form( $instance ) { |
|
| 48 | - // outputs the options form on admin. |
|
| 49 | - } |
|
| 39 | + /** |
|
| 40 | + * Outputs the options form on admin |
|
| 41 | + * |
|
| 42 | + * @param array $instance The widget options. |
|
| 43 | + * |
|
| 44 | + * @return string|void |
|
| 45 | + */ |
|
| 46 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 47 | + public function form( $instance ) { |
|
| 48 | + // outputs the options form on admin. |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Processing widget options on save |
|
| 53 | - * |
|
| 54 | - * @param array $new_instance The new options. |
|
| 55 | - * @param array $old_instance The previous options. |
|
| 56 | - * |
|
| 57 | - * @return array|void |
|
| 58 | - */ |
|
| 59 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 60 | - public function update( $new_instance, $old_instance ) { |
|
| 61 | - // processes widget options to be saved. |
|
| 62 | - } |
|
| 51 | + /** |
|
| 52 | + * Processing widget options on save |
|
| 53 | + * |
|
| 54 | + * @param array $new_instance The new options. |
|
| 55 | + * @param array $old_instance The previous options. |
|
| 56 | + * |
|
| 57 | + * @return array|void |
|
| 58 | + */ |
|
| 59 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 60 | + public function update( $new_instance, $old_instance ) { |
|
| 61 | + // processes widget options to be saved. |
|
| 62 | + } |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -69,5 +69,5 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | function wl_register_chord_widget() { |
| 71 | 71 | |
| 72 | - register_widget( 'WordLift_Chord_Widget' ); |
|
| 72 | + register_widget( 'WordLift_Chord_Widget' ); |
|
| 73 | 73 | } |
@@ -17,9 +17,9 @@ discard block |
||
| 17 | 17 | // widget actual processes. |
| 18 | 18 | parent::__construct( |
| 19 | 19 | 'wl_chord_widget', // Base ID. |
| 20 | - __( 'Chord Widget', 'wordlift' ), // Name. |
|
| 20 | + __('Chord Widget', 'wordlift'), // Name. |
|
| 21 | 21 | array( |
| 22 | - 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), |
|
| 22 | + 'description' => __('The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift'), |
|
| 23 | 23 | ) // Args. |
| 24 | 24 | ); |
| 25 | 25 | } |
@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | * @param array $instance widget instance. |
| 32 | 32 | */ |
| 33 | 33 | // @codingStandardsIgnoreLine Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 34 | - public function widget( $args, $instance ) { |
|
| 34 | + public function widget($args, $instance) { |
|
| 35 | 35 | // outputs the content of the widget. |
| 36 | - echo do_shortcode( '[wl_chord global=true]' ); |
|
| 36 | + echo do_shortcode('[wl_chord global=true]'); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * @return string|void |
| 45 | 45 | */ |
| 46 | 46 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 47 | - public function form( $instance ) { |
|
| 47 | + public function form($instance) { |
|
| 48 | 48 | // outputs the options form on admin. |
| 49 | 49 | } |
| 50 | 50 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @return array|void |
| 58 | 58 | */ |
| 59 | 59 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 60 | - public function update( $new_instance, $old_instance ) { |
|
| 60 | + public function update($new_instance, $old_instance) { |
|
| 61 | 61 | // processes widget options to be saved. |
| 62 | 62 | } |
| 63 | 63 | } |
@@ -69,5 +69,5 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | function wl_register_chord_widget() { |
| 71 | 71 | |
| 72 | - register_widget( 'WordLift_Chord_Widget' ); |
|
| 72 | + register_widget('WordLift_Chord_Widget'); |
|
| 73 | 73 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | // Use the WordLift API URL set on the command line. |
| 15 | 15 | if ( ! defined( 'WORDLIFT_API_URL' ) && false !== getenv( 'WORDLIFT_API_URL' ) ) { |
| 16 | - define( 'WORDLIFT_API_URL', getenv( 'WORDLIFT_API_URL' ) ); |
|
| 16 | + define( 'WORDLIFT_API_URL', getenv( 'WORDLIFT_API_URL' ) ); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // 3.13.0, we use by default WLS 1.11 which provides us with the new, faster |
@@ -52,15 +52,15 @@ discard block |
||
| 52 | 52 | * @since 3.16.0 |
| 53 | 53 | */ |
| 54 | 54 | function wl_temp_dir() { |
| 55 | - $tempdir = get_temp_dir(); |
|
| 56 | - $unique = md5( site_url() . get_current_blog_id() ); |
|
| 57 | - $unique_temp_dir = $tempdir . 'wl_' . $unique; // $tempdir should have a trailing slash. |
|
| 55 | + $tempdir = get_temp_dir(); |
|
| 56 | + $unique = md5( site_url() . get_current_blog_id() ); |
|
| 57 | + $unique_temp_dir = $tempdir . 'wl_' . $unique; // $tempdir should have a trailing slash. |
|
| 58 | 58 | |
| 59 | - // If directory do not exist, create it. |
|
| 60 | - if ( ! file_exists( $unique_temp_dir ) ) { |
|
| 61 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 62 | - @mkdir( $unique_temp_dir ); |
|
| 63 | - } |
|
| 59 | + // If directory do not exist, create it. |
|
| 60 | + if ( ! file_exists( $unique_temp_dir ) ) { |
|
| 61 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 62 | + @mkdir( $unique_temp_dir ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return $unique_temp_dir . '/'; |
|
| 65 | + return $unique_temp_dir . '/'; |
|
| 66 | 66 | } |
@@ -6,40 +6,40 @@ discard block |
||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | // Set the temporary files folder. |
| 9 | -defined( 'WL_TEMP_DIR' ) || define( 'WL_TEMP_DIR', wl_temp_dir() ); |
|
| 9 | +defined('WL_TEMP_DIR') || define('WL_TEMP_DIR', wl_temp_dir()); |
|
| 10 | 10 | |
| 11 | 11 | // Define the meta name used to store the entity URL. |
| 12 | -define( 'WL_ENTITY_URL_META_NAME', 'entity_url' ); |
|
| 12 | +define('WL_ENTITY_URL_META_NAME', 'entity_url'); |
|
| 13 | 13 | |
| 14 | 14 | // Use the WordLift API URL set on the command line. |
| 15 | -if ( ! defined( 'WORDLIFT_API_URL' ) && false !== getenv( 'WORDLIFT_API_URL' ) ) { |
|
| 16 | - define( 'WORDLIFT_API_URL', getenv( 'WORDLIFT_API_URL' ) ); |
|
| 15 | +if ( ! defined('WORDLIFT_API_URL') && false !== getenv('WORDLIFT_API_URL')) { |
|
| 16 | + define('WORDLIFT_API_URL', getenv('WORDLIFT_API_URL')); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // 3.13.0, we use by default WLS 1.11 which provides us with the new, faster |
| 20 | 20 | // chunked analysis. |
| 21 | -define( 'WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE', defined( 'WORDLIFT_API_URL' ) ? WORDLIFT_API_URL . '/' : 'https://api.wordlift.io/' ); |
|
| 21 | +define('WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE', defined('WORDLIFT_API_URL') ? WORDLIFT_API_URL . '/' : 'https://api.wordlift.io/'); |
|
| 22 | 22 | |
| 23 | 23 | // @since 3.29.0 we do not use https://developers.google.com/structured-data/testing-tool/?url= |
| 24 | -define( 'WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL', 'https://search.google.com/test/rich-results?url=' ); |
|
| 24 | +define('WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL', 'https://search.google.com/test/rich-results?url='); |
|
| 25 | 25 | |
| 26 | 26 | // If is set to true, there will be additional button in 'Download Your Data' page |
| 27 | 27 | // that will allow users to download their data in JSON-LD format. |
| 28 | -defined( 'WL_CONFIG_DOWNLOAD_GA_CONTENT_DATA' ) || define( 'WL_CONFIG_DOWNLOAD_GA_CONTENT_DATA', false ); |
|
| 28 | +defined('WL_CONFIG_DOWNLOAD_GA_CONTENT_DATA') || define('WL_CONFIG_DOWNLOAD_GA_CONTENT_DATA', false); |
|
| 29 | 29 | |
| 30 | 30 | /* |
| 31 | 31 | * Define the default scope for autocomplete requests. |
| 32 | 32 | * |
| 33 | 33 | * @see https://github.com/insideout10/wordlift-plugin/issues/839 |
| 34 | 34 | */ |
| 35 | -defined( 'WL_AUTOCOMPLETE_SCOPE' ) || define( 'WL_AUTOCOMPLETE_SCOPE', 'cloud' ); |
|
| 35 | +defined('WL_AUTOCOMPLETE_SCOPE') || define('WL_AUTOCOMPLETE_SCOPE', 'cloud'); |
|
| 36 | 36 | |
| 37 | 37 | /* |
| 38 | 38 | * Enable/disable the `all entity types` feature. Initially we keep the feature disabled to enture proper Q/A. |
| 39 | 39 | * |
| 40 | 40 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 41 | 41 | */ |
| 42 | -defined( 'WL_ALL_ENTITY_TYPES' ) || define( 'WL_ALL_ENTITY_TYPES', false ); |
|
| 42 | +defined('WL_ALL_ENTITY_TYPES') || define('WL_ALL_ENTITY_TYPES', false); |
|
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Get a site unique directory under the system or WordPress temporary directory. |
@@ -53,14 +53,14 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | function wl_temp_dir() { |
| 55 | 55 | $tempdir = get_temp_dir(); |
| 56 | - $unique = md5( site_url() . get_current_blog_id() ); |
|
| 57 | - $unique_temp_dir = $tempdir . 'wl_' . $unique; // $tempdir should have a trailing slash. |
|
| 56 | + $unique = md5(site_url().get_current_blog_id()); |
|
| 57 | + $unique_temp_dir = $tempdir.'wl_'.$unique; // $tempdir should have a trailing slash. |
|
| 58 | 58 | |
| 59 | 59 | // If directory do not exist, create it. |
| 60 | - if ( ! file_exists( $unique_temp_dir ) ) { |
|
| 60 | + if ( ! file_exists($unique_temp_dir)) { |
|
| 61 | 61 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 62 | - @mkdir( $unique_temp_dir ); |
|
| 62 | + @mkdir($unique_temp_dir); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - return $unique_temp_dir . '/'; |
|
| 65 | + return $unique_temp_dir.'/'; |
|
| 66 | 66 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | function wl_write_log( $log ) { |
| 19 | 19 | |
| 20 | - Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 20 | + Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | |
@@ -26,21 +26,21 @@ discard block |
||
| 26 | 26 | * see http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/ |
| 27 | 27 | */ |
| 28 | 28 | function wordlift_allowed_post_tags() { |
| 29 | - global $allowedposttags; |
|
| 30 | - |
|
| 31 | - $tags = array( 'span' ); |
|
| 32 | - $new_attributes = array( |
|
| 33 | - 'itemscope' => array(), |
|
| 34 | - 'itemtype' => array(), |
|
| 35 | - 'itemprop' => array(), |
|
| 36 | - 'itemid' => array(), |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - foreach ( $tags as $tag ) { |
|
| 40 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 41 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 42 | - } |
|
| 43 | - } |
|
| 29 | + global $allowedposttags; |
|
| 30 | + |
|
| 31 | + $tags = array( 'span' ); |
|
| 32 | + $new_attributes = array( |
|
| 33 | + 'itemscope' => array(), |
|
| 34 | + 'itemtype' => array(), |
|
| 35 | + 'itemprop' => array(), |
|
| 36 | + 'itemid' => array(), |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + foreach ( $tags as $tag ) { |
|
| 40 | + if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 41 | + $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // add allowed post tags. |
@@ -51,18 +51,18 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | function wordlift_admin_enqueue_scripts() { |
| 53 | 53 | |
| 54 | - // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 55 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 56 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 54 | + // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 55 | + wp_enqueue_script( 'wpdialogs' ); |
|
| 56 | + wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 57 | 57 | |
| 58 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css', array(), WORDLIFT_VERSION ); |
|
| 58 | + wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css', array(), WORDLIFT_VERSION ); |
|
| 59 | 59 | |
| 60 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 60 | + wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 61 | 61 | |
| 62 | - // Disable auto-save for custom entity posts only |
|
| 63 | - if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 64 | - wp_dequeue_script( 'autosave' ); |
|
| 65 | - } |
|
| 62 | + // Disable auto-save for custom entity posts only |
|
| 63 | + if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 64 | + wp_dequeue_script( 'autosave' ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | 67 | } |
| 68 | 68 | |
@@ -78,21 +78,21 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | function wordlift_allowed_html( $allowedtags, $context ) { |
| 80 | 80 | |
| 81 | - if ( 'post' !== $context ) { |
|
| 82 | - return $allowedtags; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return array_merge_recursive( |
|
| 86 | - $allowedtags, |
|
| 87 | - array( |
|
| 88 | - 'span' => array( |
|
| 89 | - 'itemscope' => true, |
|
| 90 | - 'itemtype' => true, |
|
| 91 | - 'itemid' => true, |
|
| 92 | - 'itemprop' => true, |
|
| 93 | - ), |
|
| 94 | - ) |
|
| 95 | - ); |
|
| 81 | + if ( 'post' !== $context ) { |
|
| 82 | + return $allowedtags; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return array_merge_recursive( |
|
| 86 | + $allowedtags, |
|
| 87 | + array( |
|
| 88 | + 'span' => array( |
|
| 89 | + 'itemscope' => true, |
|
| 90 | + 'itemtype' => true, |
|
| 91 | + 'itemid' => true, |
|
| 92 | + 'itemprop' => true, |
|
| 93 | + ), |
|
| 94 | + ) |
|
| 95 | + ); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | */ |
| 108 | 108 | function wl_get_image_urls( $post_id ) { |
| 109 | 109 | |
| 110 | - return Wordlift_Storage_Factory::get_instance() |
|
| 111 | - ->post_images() |
|
| 112 | - ->get( $post_id ); |
|
| 110 | + return Wordlift_Storage_Factory::get_instance() |
|
| 111 | + ->post_images() |
|
| 112 | + ->get( $post_id ); |
|
| 113 | 113 | |
| 114 | 114 | } |
| 115 | 115 | |
@@ -123,26 +123,26 @@ discard block |
||
| 123 | 123 | */ |
| 124 | 124 | function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
| 125 | 125 | |
| 126 | - // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 127 | - |
|
| 128 | - $posts = get_posts( |
|
| 129 | - array( |
|
| 130 | - 'post_type' => 'attachment', |
|
| 131 | - 'posts_per_page' => 1, |
|
| 132 | - 'post_status' => 'any', |
|
| 133 | - 'post_parent' => $parent_post_id, |
|
| 134 | - 'meta_key' => 'wl_source_url', |
|
| 135 | - 'meta_value' => $source_url, |
|
| 136 | - ) |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - // Return the found post. |
|
| 140 | - if ( 1 === count( $posts ) ) { |
|
| 141 | - return $posts[0]; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Return null. |
|
| 145 | - return null; |
|
| 126 | + // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 127 | + |
|
| 128 | + $posts = get_posts( |
|
| 129 | + array( |
|
| 130 | + 'post_type' => 'attachment', |
|
| 131 | + 'posts_per_page' => 1, |
|
| 132 | + 'post_status' => 'any', |
|
| 133 | + 'post_parent' => $parent_post_id, |
|
| 134 | + 'meta_key' => 'wl_source_url', |
|
| 135 | + 'meta_value' => $source_url, |
|
| 136 | + ) |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + // Return the found post. |
|
| 140 | + if ( 1 === count( $posts ) ) { |
|
| 141 | + return $posts[0]; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Return null. |
|
| 145 | + return null; |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | function wl_set_source_url( $post_id, $source_url ) { |
| 155 | 155 | |
| 156 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 157 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 156 | + delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 157 | + add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | function wl_sanitize_uri_path( $path, $char = '_' ) { |
| 172 | 172 | |
| 173 | - return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 173 | + return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -182,47 +182,47 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | function wl_replace_item_id_with_uri( $content ) { |
| 184 | 184 | |
| 185 | - $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 186 | - $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 185 | + $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 186 | + $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 187 | 187 | |
| 188 | - // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 189 | - $content = stripslashes( $content ); |
|
| 188 | + // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 189 | + $content = stripslashes( $content ); |
|
| 190 | 190 | |
| 191 | - // If any match are found. |
|
| 192 | - $matches = array(); |
|
| 193 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 191 | + // If any match are found. |
|
| 192 | + $matches = array(); |
|
| 193 | + if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 194 | 194 | |
| 195 | - foreach ( $matches as $match ) { |
|
| 195 | + foreach ( $matches as $match ) { |
|
| 196 | 196 | |
| 197 | - // Get the item ID. |
|
| 198 | - $item_id = $match[1]; |
|
| 197 | + // Get the item ID. |
|
| 198 | + $item_id = $match[1]; |
|
| 199 | 199 | |
| 200 | - // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 201 | - $post = Wordlift_Entity_Service::get_instance() |
|
| 202 | - ->get_entity_post_by_uri( $item_id ); |
|
| 200 | + // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 201 | + $post = Wordlift_Entity_Service::get_instance() |
|
| 202 | + ->get_entity_post_by_uri( $item_id ); |
|
| 203 | 203 | |
| 204 | - // If no entity is found, continue to the next one. |
|
| 205 | - if ( null === $post ) { |
|
| 206 | - continue; |
|
| 207 | - } |
|
| 204 | + // If no entity is found, continue to the next one. |
|
| 205 | + if ( null === $post ) { |
|
| 206 | + continue; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - // Get the URI for that post. |
|
| 210 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 209 | + // Get the URI for that post. |
|
| 210 | + $uri = wl_get_entity_uri( $post->ID ); |
|
| 211 | 211 | |
| 212 | - // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 212 | + // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 213 | 213 | |
| 214 | - // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 215 | - if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 216 | - $uri_e = esc_html( $uri ); |
|
| 217 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - } |
|
| 214 | + // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 215 | + if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 216 | + $uri_e = esc_html( $uri ); |
|
| 217 | + $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - // Reapply slashes. |
|
| 223 | - $content = addslashes( $content ); |
|
| 222 | + // Reapply slashes. |
|
| 223 | + $content = addslashes( $content ); |
|
| 224 | 224 | |
| 225 | - return $content; |
|
| 225 | + return $content; |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
@@ -15,9 +15,9 @@ discard block |
||
| 15 | 15 | * |
| 16 | 16 | * @deprecated use Wordlift_Log_Service::get_instance()->info( $log ); |
| 17 | 17 | */ |
| 18 | -function wl_write_log( $log ) { |
|
| 18 | +function wl_write_log($log) { |
|
| 19 | 19 | |
| 20 | - Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 20 | + Wordlift_Log_Service::get_instance()->debug($log); |
|
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | function wordlift_allowed_post_tags() { |
| 29 | 29 | global $allowedposttags; |
| 30 | 30 | |
| 31 | - $tags = array( 'span' ); |
|
| 31 | + $tags = array('span'); |
|
| 32 | 32 | $new_attributes = array( |
| 33 | 33 | 'itemscope' => array(), |
| 34 | 34 | 'itemtype' => array(), |
@@ -36,15 +36,15 @@ discard block |
||
| 36 | 36 | 'itemid' => array(), |
| 37 | 37 | ); |
| 38 | 38 | |
| 39 | - foreach ( $tags as $tag ) { |
|
| 40 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 41 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 39 | + foreach ($tags as $tag) { |
|
| 40 | + if (isset($allowedposttags[$tag]) && is_array($allowedposttags[$tag])) { |
|
| 41 | + $allowedposttags[$tag] = array_merge($allowedposttags[$tag], $new_attributes); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 42 | 42 | } |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // add allowed post tags. |
| 47 | -add_action( 'init', 'wordlift_allowed_post_tags' ); |
|
| 47 | +add_action('init', 'wordlift_allowed_post_tags'); |
|
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * Register additional scripts for the admin UI. |
@@ -52,21 +52,21 @@ discard block |
||
| 52 | 52 | function wordlift_admin_enqueue_scripts() { |
| 53 | 53 | |
| 54 | 54 | // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
| 55 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 56 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 55 | + wp_enqueue_script('wpdialogs'); |
|
| 56 | + wp_enqueue_style('wp-jquery-ui-dialog'); |
|
| 57 | 57 | |
| 58 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css', array(), WORDLIFT_VERSION ); |
|
| 58 | + wp_enqueue_style('wordlift-reloaded', plugin_dir_url(__FILE__).'css/wordlift-reloaded.min.css', array(), WORDLIFT_VERSION); |
|
| 59 | 59 | |
| 60 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 60 | + wp_enqueue_script('jquery-ui-autocomplete'); |
|
| 61 | 61 | |
| 62 | 62 | // Disable auto-save for custom entity posts only |
| 63 | - if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 64 | - wp_dequeue_script( 'autosave' ); |
|
| 63 | + if (Wordlift_Entity_Service::TYPE_NAME === get_post_type()) { |
|
| 64 | + wp_dequeue_script('autosave'); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | -add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' ); |
|
| 69 | +add_action('admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts'); |
|
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | 72 | * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes. |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array An array which contains allowed microdata attributes. |
| 78 | 78 | */ |
| 79 | -function wordlift_allowed_html( $allowedtags, $context ) { |
|
| 79 | +function wordlift_allowed_html($allowedtags, $context) { |
|
| 80 | 80 | |
| 81 | - if ( 'post' !== $context ) { |
|
| 81 | + if ('post' !== $context) { |
|
| 82 | 82 | return $allowedtags; |
| 83 | 83 | } |
| 84 | 84 | |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | ); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | -add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
|
| 98 | +add_filter('wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2); |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * Get all the images bound to a post. |
@@ -105,11 +105,11 @@ discard block |
||
| 105 | 105 | * @return array An array of image URLs. |
| 106 | 106 | * @deprecated use Wordlift_Storage_Factory::get_instance()->post_images()->get( $post_id ) |
| 107 | 107 | */ |
| 108 | -function wl_get_image_urls( $post_id ) { |
|
| 108 | +function wl_get_image_urls($post_id) { |
|
| 109 | 109 | |
| 110 | 110 | return Wordlift_Storage_Factory::get_instance() |
| 111 | 111 | ->post_images() |
| 112 | - ->get( $post_id ); |
|
| 112 | + ->get($post_id); |
|
| 113 | 113 | |
| 114 | 114 | } |
| 115 | 115 | |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return WP_Post|null A post instance or null if not found. |
| 123 | 123 | */ |
| 124 | -function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
|
| 124 | +function wl_get_attachment_for_source_url($parent_post_id, $source_url) { |
|
| 125 | 125 | |
| 126 | 126 | // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
| 127 | 127 | |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | ); |
| 138 | 138 | |
| 139 | 139 | // Return the found post. |
| 140 | - if ( 1 === count( $posts ) ) { |
|
| 140 | + if (1 === count($posts)) { |
|
| 141 | 141 | return $posts[0]; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | * @param int $post_id The post ID. |
| 152 | 152 | * @param string $source_url The source URL. |
| 153 | 153 | */ |
| 154 | -function wl_set_source_url( $post_id, $source_url ) { |
|
| 154 | +function wl_set_source_url($post_id, $source_url) { |
|
| 155 | 155 | |
| 156 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 157 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 156 | + delete_post_meta($post_id, 'wl_source_url'); |
|
| 157 | + add_post_meta($post_id, 'wl_source_url', $source_url); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -168,9 +168,9 @@ discard block |
||
| 168 | 168 | * @deprecated use Wordlift_Uri_Service::get_instance()->sanitize_path(); |
| 169 | 169 | * @see https://codex.wordpress.org/Function_Reference/sanitize_title |
| 170 | 170 | */ |
| 171 | -function wl_sanitize_uri_path( $path, $char = '_' ) { |
|
| 171 | +function wl_sanitize_uri_path($path, $char = '_') { |
|
| 172 | 172 | |
| 173 | - return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 173 | + return Wordlift_Uri_Service::get_instance()->sanitize_path($path, $char); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -180,52 +180,52 @@ discard block |
||
| 180 | 180 | * |
| 181 | 181 | * @return string The updated post content. |
| 182 | 182 | */ |
| 183 | -function wl_replace_item_id_with_uri( $content ) { |
|
| 183 | +function wl_replace_item_id_with_uri($content) { |
|
| 184 | 184 | |
| 185 | - $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 186 | - $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 185 | + $log = Wordlift_Log_Service::get_logger('wl_replace_item_id_with_uri'); |
|
| 186 | + $log->trace('Replacing item IDs with URIs...'); |
|
| 187 | 187 | |
| 188 | 188 | // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
| 189 | - $content = stripslashes( $content ); |
|
| 189 | + $content = stripslashes($content); |
|
| 190 | 190 | |
| 191 | 191 | // If any match are found. |
| 192 | 192 | $matches = array(); |
| 193 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 193 | + if (0 < preg_match_all('/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER)) { |
|
| 194 | 194 | |
| 195 | - foreach ( $matches as $match ) { |
|
| 195 | + foreach ($matches as $match) { |
|
| 196 | 196 | |
| 197 | 197 | // Get the item ID. |
| 198 | 198 | $item_id = $match[1]; |
| 199 | 199 | |
| 200 | 200 | // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
| 201 | 201 | $post = Wordlift_Entity_Service::get_instance() |
| 202 | - ->get_entity_post_by_uri( $item_id ); |
|
| 202 | + ->get_entity_post_by_uri($item_id); |
|
| 203 | 203 | |
| 204 | 204 | // If no entity is found, continue to the next one. |
| 205 | - if ( null === $post ) { |
|
| 205 | + if (null === $post) { |
|
| 206 | 206 | continue; |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // Get the URI for that post. |
| 210 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 210 | + $uri = wl_get_entity_uri($post->ID); |
|
| 211 | 211 | |
| 212 | 212 | // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
| 213 | 213 | |
| 214 | 214 | // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
| 215 | - if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 216 | - $uri_e = esc_html( $uri ); |
|
| 217 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 215 | + if ( ! empty($uri) && $item_id !== $uri) { |
|
| 216 | + $uri_e = esc_html($uri); |
|
| 217 | + $content = str_replace(" itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // Reapply slashes. |
| 223 | - $content = addslashes( $content ); |
|
| 223 | + $content = addslashes($content); |
|
| 224 | 224 | |
| 225 | 225 | return $content; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | -add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
|
| 228 | +add_filter('content_save_pre', 'wl_replace_item_id_with_uri', 1, 1); |
|
| 229 | 229 | |
| 230 | 230 | require_once 'wordlift-entity-functions.php'; |
| 231 | 231 | |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | - 'version' => 'f3639839f6d33cdc7c6ca16c2178908a', |
|
| 2 | + 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 3 | + 'version' => 'f3639839f6d33cdc7c6ca16c2178908a', |
|
| 4 | 4 | ); |
@@ -1,4 +1,4 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | - 'dependencies' => array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 2 | + 'dependencies' => array('react', 'react-dom', 'wp-polyfill'), |
|
| 3 | 3 | 'version' => 'f3639839f6d33cdc7c6ca16c2178908a', |
| 4 | 4 | ); |
@@ -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' => '58771ef40b11db9353dc73af4117a5da', |
|
| 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' => '58771ef40b11db9353dc73af4117a5da', |
|
| 4 | 4 | ); |
@@ -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' ), |
|
| 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 | 3 | 'version' => '58771ef40b11db9353dc73af4117a5da', |
| 4 | 4 | ); |