@@ -18,85 +18,85 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Content_Filter_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The pattern to find entities in text. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - */ |
|
| 26 | - const PATTERN = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 21 | + /** |
|
| 22 | + * The pattern to find entities in text. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + */ |
|
| 26 | + const PATTERN = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.8.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.8.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Wordlift_Content_Filter_Service constructor. |
|
| 39 | - * |
|
| 40 | - * @since 3.8.0 |
|
| 41 | - * |
|
| 42 | - * @param $entity_service |
|
| 43 | - */ |
|
| 44 | - public function __construct( $entity_service ) { |
|
| 37 | + /** |
|
| 38 | + * Wordlift_Content_Filter_Service constructor. |
|
| 39 | + * |
|
| 40 | + * @since 3.8.0 |
|
| 41 | + * |
|
| 42 | + * @param $entity_service |
|
| 43 | + */ |
|
| 44 | + public function __construct( $entity_service ) { |
|
| 45 | 45 | |
| 46 | - $this->entity_service = $entity_service; |
|
| 46 | + $this->entity_service = $entity_service; |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 52 | - * hooked to the 'the_content' WP's filter. |
|
| 53 | - * |
|
| 54 | - * @since 3.8.0 |
|
| 55 | - * |
|
| 56 | - * @param string $content The content being filtered. |
|
| 57 | - * |
|
| 58 | - * @return string The filtered content. |
|
| 59 | - */ |
|
| 60 | - public function the_content( $content ) { |
|
| 50 | + /** |
|
| 51 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 52 | + * hooked to the 'the_content' WP's filter. |
|
| 53 | + * |
|
| 54 | + * @since 3.8.0 |
|
| 55 | + * |
|
| 56 | + * @param string $content The content being filtered. |
|
| 57 | + * |
|
| 58 | + * @return string The filtered content. |
|
| 59 | + */ |
|
| 60 | + public function the_content( $content ) { |
|
| 61 | 61 | |
| 62 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 63 | - // occurs fail silently returning the original content. |
|
| 64 | - return preg_replace_callback( self::PATTERN, array( |
|
| 65 | - $this, |
|
| 66 | - 'link', |
|
| 67 | - ), $content ) ?: $content; |
|
| 68 | - } |
|
| 62 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 63 | + // occurs fail silently returning the original content. |
|
| 64 | + return preg_replace_callback( self::PATTERN, array( |
|
| 65 | + $this, |
|
| 66 | + 'link', |
|
| 67 | + ), $content ) ?: $content; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Get the entity match and replace it with a page link. |
|
| 72 | - * |
|
| 73 | - * @since 3.8.0 |
|
| 74 | - * |
|
| 75 | - * @param array $matches An array of matches. |
|
| 76 | - * |
|
| 77 | - * @return string The replaced text with the link to the entity page. |
|
| 78 | - */ |
|
| 79 | - private function link( $matches ) { |
|
| 70 | + /** |
|
| 71 | + * Get the entity match and replace it with a page link. |
|
| 72 | + * |
|
| 73 | + * @since 3.8.0 |
|
| 74 | + * |
|
| 75 | + * @param array $matches An array of matches. |
|
| 76 | + * |
|
| 77 | + * @return string The replaced text with the link to the entity page. |
|
| 78 | + */ |
|
| 79 | + private function link( $matches ) { |
|
| 80 | 80 | |
| 81 | - // Get the entity itemid URI and label. |
|
| 82 | - $uri = $matches[2]; |
|
| 83 | - $label = $matches[3]; |
|
| 81 | + // Get the entity itemid URI and label. |
|
| 82 | + $uri = $matches[2]; |
|
| 83 | + $label = $matches[3]; |
|
| 84 | 84 | |
| 85 | - // Get the entity post by URI. |
|
| 86 | - if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) { |
|
| 85 | + // Get the entity post by URI. |
|
| 86 | + if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) { |
|
| 87 | 87 | |
| 88 | - // If the entity post is not found return the label w/o the markup |
|
| 89 | - // around it. |
|
| 90 | - // |
|
| 91 | - // See https://github.com/insideout10/wordlift-plugin/issues/461 |
|
| 92 | - return $label; |
|
| 93 | - } |
|
| 88 | + // If the entity post is not found return the label w/o the markup |
|
| 89 | + // around it. |
|
| 90 | + // |
|
| 91 | + // See https://github.com/insideout10/wordlift-plugin/issues/461 |
|
| 92 | + return $label; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - // Get the link. |
|
| 96 | - $link = get_permalink( $post ); |
|
| 95 | + // Get the link. |
|
| 96 | + $link = get_permalink( $post ); |
|
| 97 | 97 | |
| 98 | - // Return the link. |
|
| 99 | - return "<a class='wl-entity-page-link' href='$link'>$label</a>"; |
|
| 100 | - } |
|
| 98 | + // Return the link. |
|
| 99 | + return "<a class='wl-entity-page-link' href='$link'>$label</a>"; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @param $entity_service |
| 43 | 43 | */ |
| 44 | - public function __construct( $entity_service ) { |
|
| 44 | + public function __construct($entity_service) { |
|
| 45 | 45 | |
| 46 | 46 | $this->entity_service = $entity_service; |
| 47 | 47 | |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return string The filtered content. |
| 59 | 59 | */ |
| 60 | - public function the_content( $content ) { |
|
| 60 | + public function the_content($content) { |
|
| 61 | 61 | |
| 62 | 62 | // Replace each match of the entity tag with the entity link. If an error |
| 63 | 63 | // occurs fail silently returning the original content. |
| 64 | - return preg_replace_callback( self::PATTERN, array( |
|
| 64 | + return preg_replace_callback(self::PATTERN, array( |
|
| 65 | 65 | $this, |
| 66 | 66 | 'link', |
| 67 | - ), $content ) ?: $content; |
|
| 67 | + ), $content) ?: $content; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -76,14 +76,14 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return string The replaced text with the link to the entity page. |
| 78 | 78 | */ |
| 79 | - private function link( $matches ) { |
|
| 79 | + private function link($matches) { |
|
| 80 | 80 | |
| 81 | 81 | // Get the entity itemid URI and label. |
| 82 | 82 | $uri = $matches[2]; |
| 83 | 83 | $label = $matches[3]; |
| 84 | 84 | |
| 85 | 85 | // Get the entity post by URI. |
| 86 | - if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) { |
|
| 86 | + if (null === ($post = $this->entity_service->get_entity_post_by_uri($uri))) { |
|
| 87 | 87 | |
| 88 | 88 | // If the entity post is not found return the label w/o the markup |
| 89 | 89 | // around it. |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Get the link. |
| 96 | - $link = get_permalink( $post ); |
|
| 96 | + $link = get_permalink($post); |
|
| 97 | 97 | |
| 98 | 98 | // Return the link. |
| 99 | 99 | return "<a class='wl-entity-page-link' href='$link'>$label</a>"; |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | * |
| 207 | 207 | * @since 3.4.0 |
| 208 | 208 | * |
| 209 | - * @return int Average entities rating. |
|
| 209 | + * @return double Average entities rating. |
|
| 210 | 210 | */ |
| 211 | 211 | private function average_entities_rating() { |
| 212 | 212 | |
@@ -265,6 +265,9 @@ discard block |
||
| 265 | 265 | return (int) false; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | + /** |
|
| 269 | + * @param string $query |
|
| 270 | + */ |
|
| 268 | 271 | private function rl_sparql_select( $query ) { |
| 269 | 272 | |
| 270 | 273 | // Prepare the SPARQL statement by prepending the default namespaces. |
@@ -277,6 +280,9 @@ discard block |
||
| 277 | 280 | return wp_remote_get( $url, $args ); |
| 278 | 281 | } |
| 279 | 282 | |
| 283 | + /** |
|
| 284 | + * @param string $param |
|
| 285 | + */ |
|
| 280 | 286 | private function render_stat_param( $param ) { |
| 281 | 287 | |
| 282 | 288 | return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
@@ -14,81 +14,81 @@ discard block |
||
| 14 | 14 | class Wordlift_Dashboard_Service { |
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Transient Name |
|
| 19 | - * |
|
| 20 | - * @since 3.4.0 |
|
| 21 | - */ |
|
| 22 | - const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Transient Expiration (in seconds) |
|
| 26 | - * |
|
| 27 | - * @since 3.4.0 |
|
| 28 | - */ |
|
| 29 | - const TRANSIENT_EXPIRATION = 86400; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Rating_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.10.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $rating_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Create a Wordlift_Entity_List_Service. |
|
| 42 | - * |
|
| 43 | - * @since 3.4.0 |
|
| 44 | - * |
|
| 45 | - * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $rating_service ) { |
|
| 48 | - |
|
| 49 | - $this->rating_service = $rating_service; |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Return stats layout |
|
| 55 | - * |
|
| 56 | - * @since 3.4.0 |
|
| 57 | - * |
|
| 58 | - * @return string Dashboard widget html markup |
|
| 59 | - */ |
|
| 60 | - public function dashboard_widget_callback( $post ) { |
|
| 61 | - |
|
| 62 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | - $this->render_stat_param( 'posts' ) |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | - $rating_caption = sprintf( wp_kses( |
|
| 74 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | - $this->render_stat_param( 'entities' ), |
|
| 77 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | - $this->render_stat_param( 'rating' ) |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | - $graph_caption = sprintf( wp_kses( |
|
| 83 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | - $this->render_stat_param( 'wikidata' ), |
|
| 86 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 90 | - |
|
| 91 | - echo <<<EOF |
|
| 17 | + /** |
|
| 18 | + * Transient Name |
|
| 19 | + * |
|
| 20 | + * @since 3.4.0 |
|
| 21 | + */ |
|
| 22 | + const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Transient Expiration (in seconds) |
|
| 26 | + * |
|
| 27 | + * @since 3.4.0 |
|
| 28 | + */ |
|
| 29 | + const TRANSIENT_EXPIRATION = 86400; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Rating_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.10.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $rating_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Create a Wordlift_Entity_List_Service. |
|
| 42 | + * |
|
| 43 | + * @since 3.4.0 |
|
| 44 | + * |
|
| 45 | + * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $rating_service ) { |
|
| 48 | + |
|
| 49 | + $this->rating_service = $rating_service; |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Return stats layout |
|
| 55 | + * |
|
| 56 | + * @since 3.4.0 |
|
| 57 | + * |
|
| 58 | + * @return string Dashboard widget html markup |
|
| 59 | + */ |
|
| 60 | + public function dashboard_widget_callback( $post ) { |
|
| 61 | + |
|
| 62 | + $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | + $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | + __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | + $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | + esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | + $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | + $this->render_stat_param( 'posts' ) |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | + $rating_caption = sprintf( wp_kses( |
|
| 74 | + __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | + $this->render_stat_param( 'entities' ), |
|
| 77 | + esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | + $this->render_stat_param( 'rating' ) |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | + $graph_caption = sprintf( wp_kses( |
|
| 83 | + __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | + $this->render_stat_param( 'wikidata' ), |
|
| 86 | + esc_url( 'https://www.wikidata.org/' ) |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + $triples_label = __( 'triples', 'wordlift' ); |
|
| 90 | + |
|
| 91 | + echo <<<EOF |
|
| 92 | 92 | <div id="wl-dashboard-widget-inner-wrapper"> |
| 93 | 93 | <div class="wl-stat-card"> |
| 94 | 94 | <div class="wl-stat-graph-wrapper"> |
@@ -117,169 +117,169 @@ discard block |
||
| 117 | 117 | </div> |
| 118 | 118 | EOF; |
| 119 | 119 | |
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Return stats |
|
| 124 | - * |
|
| 125 | - * @since 3.4.0 |
|
| 126 | - * |
|
| 127 | - * @return string markup |
|
| 128 | - */ |
|
| 129 | - public function add_dashboard_widgets() { |
|
| 130 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | - $this, |
|
| 132 | - 'dashboard_widget_callback', |
|
| 133 | - ) ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Return stats |
|
| 138 | - * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 139 | - * |
|
| 140 | - * @since 3.4.0 |
|
| 141 | - * |
|
| 142 | - * @return string JSON obj with all available stats. |
|
| 143 | - */ |
|
| 144 | - public function ajax_get_stats() { |
|
| 145 | - |
|
| 146 | - // If needed, the transient is force to reloaed |
|
| 147 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Try to retrieve the transient |
|
| 152 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 153 | - |
|
| 154 | - if ( ! $stats ) { |
|
| 155 | - // Calculate stats |
|
| 156 | - $stats = array( |
|
| 157 | - 'entities' => $this->count_entities(), |
|
| 158 | - 'posts' => $this->count_posts(), |
|
| 159 | - 'annotated_posts' => $this->count_annotated_posts(), |
|
| 160 | - 'triples' => $this->count_triples() ?: '-', |
|
| 161 | - 'rating' => $this->average_entities_rating(), |
|
| 162 | - ); |
|
| 163 | - // Cache stats results trough transient |
|
| 164 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 165 | - } |
|
| 166 | - // Return stats as json object |
|
| 167 | - wl_core_send_json( $stats ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Calculate total number of published posts |
|
| 172 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 173 | - * @since 3.4.0 |
|
| 174 | - * |
|
| 175 | - * @return int Total number of published posts. |
|
| 176 | - */ |
|
| 177 | - public function count_posts() { |
|
| 178 | - |
|
| 179 | - return (int) wp_count_posts()->publish; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Calculate total number of annotated published posts |
|
| 184 | - * @since 3.4.0 |
|
| 185 | - * |
|
| 186 | - * @return int Total number of annotated published posts. |
|
| 187 | - */ |
|
| 188 | - public function count_annotated_posts() { |
|
| 189 | - |
|
| 190 | - // Prepare interaction with db |
|
| 191 | - global $wpdb; |
|
| 192 | - // Retrieve Wordlift relation instances table name |
|
| 193 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 194 | - // Calculate sql statement |
|
| 195 | - $sql_statement = <<<EOF |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Return stats |
|
| 124 | + * |
|
| 125 | + * @since 3.4.0 |
|
| 126 | + * |
|
| 127 | + * @return string markup |
|
| 128 | + */ |
|
| 129 | + public function add_dashboard_widgets() { |
|
| 130 | + wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | + $this, |
|
| 132 | + 'dashboard_widget_callback', |
|
| 133 | + ) ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Return stats |
|
| 138 | + * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 139 | + * |
|
| 140 | + * @since 3.4.0 |
|
| 141 | + * |
|
| 142 | + * @return string JSON obj with all available stats. |
|
| 143 | + */ |
|
| 144 | + public function ajax_get_stats() { |
|
| 145 | + |
|
| 146 | + // If needed, the transient is force to reloaed |
|
| 147 | + if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | + delete_transient( self::TRANSIENT_NAME ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Try to retrieve the transient |
|
| 152 | + $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 153 | + |
|
| 154 | + if ( ! $stats ) { |
|
| 155 | + // Calculate stats |
|
| 156 | + $stats = array( |
|
| 157 | + 'entities' => $this->count_entities(), |
|
| 158 | + 'posts' => $this->count_posts(), |
|
| 159 | + 'annotated_posts' => $this->count_annotated_posts(), |
|
| 160 | + 'triples' => $this->count_triples() ?: '-', |
|
| 161 | + 'rating' => $this->average_entities_rating(), |
|
| 162 | + ); |
|
| 163 | + // Cache stats results trough transient |
|
| 164 | + set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 165 | + } |
|
| 166 | + // Return stats as json object |
|
| 167 | + wl_core_send_json( $stats ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Calculate total number of published posts |
|
| 172 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 173 | + * @since 3.4.0 |
|
| 174 | + * |
|
| 175 | + * @return int Total number of published posts. |
|
| 176 | + */ |
|
| 177 | + public function count_posts() { |
|
| 178 | + |
|
| 179 | + return (int) wp_count_posts()->publish; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Calculate total number of annotated published posts |
|
| 184 | + * @since 3.4.0 |
|
| 185 | + * |
|
| 186 | + * @return int Total number of annotated published posts. |
|
| 187 | + */ |
|
| 188 | + public function count_annotated_posts() { |
|
| 189 | + |
|
| 190 | + // Prepare interaction with db |
|
| 191 | + global $wpdb; |
|
| 192 | + // Retrieve Wordlift relation instances table name |
|
| 193 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 194 | + // Calculate sql statement |
|
| 195 | + $sql_statement = <<<EOF |
|
| 196 | 196 | SELECT COUNT(distinct(p.id)) FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.subject_id AND p.post_type = 'post' AND p.post_status = 'publish'; |
| 197 | 197 | EOF; |
| 198 | 198 | |
| 199 | - // Perform the query |
|
| 200 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 201 | - |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Calculate the average entities rating. |
|
| 206 | - * |
|
| 207 | - * @since 3.4.0 |
|
| 208 | - * |
|
| 209 | - * @return int Average entities rating. |
|
| 210 | - */ |
|
| 211 | - private function average_entities_rating() { |
|
| 212 | - |
|
| 213 | - // Prepare interaction with db |
|
| 214 | - global $wpdb; |
|
| 215 | - $query = $wpdb->prepare( |
|
| 216 | - "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 217 | - Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - // Perform the query. |
|
| 221 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Calculate total number of published entities. |
|
| 226 | - * |
|
| 227 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 228 | - * |
|
| 229 | - * @since 3.4.0 |
|
| 230 | - * |
|
| 231 | - * @return int Total number of posts. |
|
| 232 | - */ |
|
| 233 | - private function count_entities() { |
|
| 234 | - |
|
| 235 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Calculate total number of published rdf triples |
|
| 240 | - * @since 3.4.0 |
|
| 241 | - * |
|
| 242 | - * @return int Total number of triples. |
|
| 243 | - */ |
|
| 244 | - public function count_triples() { |
|
| 245 | - |
|
| 246 | - // Set the SPARQL query. |
|
| 247 | - $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 248 | - // Send the request. |
|
| 249 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 250 | - |
|
| 251 | - // Return the error in case of failure. |
|
| 252 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 253 | - return (int) false; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Get the body. |
|
| 257 | - $body = $response['body']; |
|
| 258 | - // Get the values. |
|
| 259 | - $matches = array(); |
|
| 260 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 261 | - // Return the counts. |
|
| 262 | - return (int) $matches[1]; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return (int) false; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - private function rl_sparql_select( $query ) { |
|
| 269 | - |
|
| 270 | - // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 271 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 272 | - // Get the SPARQL SELECT URL. |
|
| 273 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 274 | - // Prepare the request. |
|
| 275 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 276 | - |
|
| 277 | - return wp_remote_get( $url, $args ); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - private function render_stat_param( $param ) { |
|
| 281 | - |
|
| 282 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 283 | - } |
|
| 199 | + // Perform the query |
|
| 200 | + return (int) $wpdb->get_var( $sql_statement ); |
|
| 201 | + |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Calculate the average entities rating. |
|
| 206 | + * |
|
| 207 | + * @since 3.4.0 |
|
| 208 | + * |
|
| 209 | + * @return int Average entities rating. |
|
| 210 | + */ |
|
| 211 | + private function average_entities_rating() { |
|
| 212 | + |
|
| 213 | + // Prepare interaction with db |
|
| 214 | + global $wpdb; |
|
| 215 | + $query = $wpdb->prepare( |
|
| 216 | + "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 217 | + Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + // Perform the query. |
|
| 221 | + return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Calculate total number of published entities. |
|
| 226 | + * |
|
| 227 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 228 | + * |
|
| 229 | + * @since 3.4.0 |
|
| 230 | + * |
|
| 231 | + * @return int Total number of posts. |
|
| 232 | + */ |
|
| 233 | + private function count_entities() { |
|
| 234 | + |
|
| 235 | + return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Calculate total number of published rdf triples |
|
| 240 | + * @since 3.4.0 |
|
| 241 | + * |
|
| 242 | + * @return int Total number of triples. |
|
| 243 | + */ |
|
| 244 | + public function count_triples() { |
|
| 245 | + |
|
| 246 | + // Set the SPARQL query. |
|
| 247 | + $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 248 | + // Send the request. |
|
| 249 | + $response = $this->rl_sparql_select( $sparql ); |
|
| 250 | + |
|
| 251 | + // Return the error in case of failure. |
|
| 252 | + if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 253 | + return (int) false; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Get the body. |
|
| 257 | + $body = $response['body']; |
|
| 258 | + // Get the values. |
|
| 259 | + $matches = array(); |
|
| 260 | + if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 261 | + // Return the counts. |
|
| 262 | + return (int) $matches[1]; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return (int) false; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + private function rl_sparql_select( $query ) { |
|
| 269 | + |
|
| 270 | + // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 271 | + $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 272 | + // Get the SPARQL SELECT URL. |
|
| 273 | + $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 274 | + // Prepare the request. |
|
| 275 | + $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 276 | + |
|
| 277 | + return wp_remote_get( $url, $args ); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + private function render_stat_param( $param ) { |
|
| 281 | + |
|
| 282 | + return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | 285 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
| 46 | 46 | */ |
| 47 | - public function __construct( $rating_service ) { |
|
| 47 | + public function __construct($rating_service) { |
|
| 48 | 48 | |
| 49 | 49 | $this->rating_service = $rating_service; |
| 50 | 50 | |
@@ -57,36 +57,36 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return string Dashboard widget html markup |
| 59 | 59 | */ |
| 60 | - public function dashboard_widget_callback( $post ) { |
|
| 61 | - |
|
| 62 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | - $this->render_stat_param( 'posts' ) |
|
| 60 | + public function dashboard_widget_callback($post) { |
|
| 61 | + |
|
| 62 | + $enriched_posts_title = __('enriched posts', 'wordlift'); |
|
| 63 | + $enriched_posts_caption = sprintf(wp_kses( |
|
| 64 | + __('%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift'), |
|
| 65 | + array('a' => array('href' => array())) ), |
|
| 66 | + $this->render_stat_param('annotated_posts_percentage'), |
|
| 67 | + esc_url(admin_url('edit.php')), |
|
| 68 | + $this->render_stat_param('annotated_posts'), |
|
| 69 | + $this->render_stat_param('posts') |
|
| 70 | 70 | ); |
| 71 | 71 | |
| 72 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | - $rating_caption = sprintf( wp_kses( |
|
| 74 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | - $this->render_stat_param( 'entities' ), |
|
| 77 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | - $this->render_stat_param( 'rating' ) |
|
| 72 | + $rating_title = __('average entity rating', 'wordlift'); |
|
| 73 | + $rating_caption = sprintf(wp_kses( |
|
| 74 | + __('You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift'), |
|
| 75 | + array('a' => array('href' => array())) ), |
|
| 76 | + $this->render_stat_param('entities'), |
|
| 77 | + esc_url(admin_url('edit.php?post_type=entity')), |
|
| 78 | + $this->render_stat_param('rating') |
|
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | - $graph_caption = sprintf( wp_kses( |
|
| 83 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | - $this->render_stat_param( 'wikidata' ), |
|
| 86 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 81 | + $graph_title = __('triples in your graph', 'wordlift'); |
|
| 82 | + $graph_caption = sprintf(wp_kses( |
|
| 83 | + __('Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift'), |
|
| 84 | + array('a' => array('href' => array())) ), |
|
| 85 | + $this->render_stat_param('wikidata'), |
|
| 86 | + esc_url('https://www.wikidata.org/') |
|
| 87 | 87 | ); |
| 88 | 88 | |
| 89 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 89 | + $triples_label = __('triples', 'wordlift'); |
|
| 90 | 90 | |
| 91 | 91 | echo <<<EOF |
| 92 | 92 | <div id="wl-dashboard-widget-inner-wrapper"> |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | * @return string markup |
| 128 | 128 | */ |
| 129 | 129 | public function add_dashboard_widgets() { |
| 130 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 130 | + wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | 131 | $this, |
| 132 | 132 | 'dashboard_widget_callback', |
| 133 | - ) ); |
|
| 133 | + )); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -144,14 +144,14 @@ discard block |
||
| 144 | 144 | public function ajax_get_stats() { |
| 145 | 145 | |
| 146 | 146 | // If needed, the transient is force to reloaed |
| 147 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 147 | + if (isset($_GET['force_reload'])) { |
|
| 148 | + delete_transient(self::TRANSIENT_NAME); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Try to retrieve the transient |
| 152 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 152 | + $stats = get_transient(self::TRANSIENT_NAME); |
|
| 153 | 153 | |
| 154 | - if ( ! $stats ) { |
|
| 154 | + if ( ! $stats) { |
|
| 155 | 155 | // Calculate stats |
| 156 | 156 | $stats = array( |
| 157 | 157 | 'entities' => $this->count_entities(), |
@@ -161,10 +161,10 @@ discard block |
||
| 161 | 161 | 'rating' => $this->average_entities_rating(), |
| 162 | 162 | ); |
| 163 | 163 | // Cache stats results trough transient |
| 164 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 164 | + set_transient(self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION); |
|
| 165 | 165 | } |
| 166 | 166 | // Return stats as json object |
| 167 | - wl_core_send_json( $stats ); |
|
| 167 | + wl_core_send_json($stats); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | EOF; |
| 198 | 198 | |
| 199 | 199 | // Perform the query |
| 200 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 200 | + return (int) $wpdb->get_var($sql_statement); |
|
| 201 | 201 | |
| 202 | 202 | } |
| 203 | 203 | |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | // Perform the query. |
| 221 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 221 | + return $this->rating_service->convert_raw_score_to_percentage($wpdb->get_var($query)); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | */ |
| 233 | 233 | private function count_entities() { |
| 234 | 234 | |
| 235 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 235 | + return (int) wp_count_posts(Wordlift_Entity_Service::TYPE_NAME)->publish; |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -246,10 +246,10 @@ discard block |
||
| 246 | 246 | // Set the SPARQL query. |
| 247 | 247 | $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
| 248 | 248 | // Send the request. |
| 249 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 249 | + $response = $this->rl_sparql_select($sparql); |
|
| 250 | 250 | |
| 251 | 251 | // Return the error in case of failure. |
| 252 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 252 | + if (is_wp_error($response) || 200 !== (int) $response['response']['code']) { |
|
| 253 | 253 | return (int) false; |
| 254 | 254 | } |
| 255 | 255 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $body = $response['body']; |
| 258 | 258 | // Get the values. |
| 259 | 259 | $matches = array(); |
| 260 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 260 | + if (1 === preg_match('/(\d+)/im', $body, $matches) && 2 === count($matches)) { |
|
| 261 | 261 | // Return the counts. |
| 262 | 262 | return (int) $matches[1]; |
| 263 | 263 | } |
@@ -265,21 +265,21 @@ discard block |
||
| 265 | 265 | return (int) false; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - private function rl_sparql_select( $query ) { |
|
| 268 | + private function rl_sparql_select($query) { |
|
| 269 | 269 | |
| 270 | 270 | // Prepare the SPARQL statement by prepending the default namespaces. |
| 271 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 271 | + $sparql = rl_sparql_prefixes()."\n".$query; |
|
| 272 | 272 | // Get the SPARQL SELECT URL. |
| 273 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 273 | + $url = wl_configuration_get_query_select_url('csv').urlencode($sparql); |
|
| 274 | 274 | // Prepare the request. |
| 275 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 275 | + $args = unserialize(WL_REDLINK_API_HTTP_OPTIONS); |
|
| 276 | 276 | |
| 277 | - return wp_remote_get( $url, $args ); |
|
| 277 | + return wp_remote_get($url, $args); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - private function render_stat_param( $param ) { |
|
| 280 | + private function render_stat_param($param) { |
|
| 281 | 281 | |
| 282 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 282 | + return '<span id="wl-dashboard-widget-'.$param.'" class="wl-stat-value"></span>'; |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | } |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | * |
| 186 | 186 | * @param int $uri An uri. |
| 187 | 187 | * |
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 188 | + * @return boolean if the uri internal to the current dataset otherwise false. |
|
| 189 | 189 | */ |
| 190 | 190 | public function is_internal_uri( $uri ) { |
| 191 | 191 | |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | * @param null $logo The entity logo id (or NULL if none). |
| 425 | 425 | * @param string $status The post status, by default 'publish'. |
| 426 | 426 | * |
| 427 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 427 | + * @return integer The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | 428 | */ |
| 429 | 429 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
| 430 | 430 | |
@@ -7,465 +7,465 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity post type name. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - */ |
|
| 33 | - const TYPE_NAME = 'entity'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The alternative label meta key. |
|
| 37 | - * |
|
| 38 | - * @since 3.2.0 |
|
| 39 | - */ |
|
| 40 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The alternative label input template. |
|
| 44 | - * |
|
| 45 | - * @since 3.2.0 |
|
| 46 | - */ |
|
| 47 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity post type name. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + */ |
|
| 33 | + const TYPE_NAME = 'entity'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The alternative label meta key. |
|
| 37 | + * |
|
| 38 | + * @since 3.2.0 |
|
| 39 | + */ |
|
| 40 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The alternative label input template. |
|
| 44 | + * |
|
| 45 | + * @since 3.2.0 |
|
| 46 | + */ |
|
| 47 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 49 | 49 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 50 | 50 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 51 | 51 | <button class="button wl-delete-button">%s</button> |
| 52 | 52 | </div>'; |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * A singleton instance of the Entity service. |
|
| 56 | - * |
|
| 57 | - * @since 3.2.0 |
|
| 58 | - * @access private |
|
| 59 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | - */ |
|
| 61 | - private static $instance; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Create a Wordlift_Entity_Service instance. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | - */ |
|
| 70 | - public function __construct( $ui_service ) { |
|
| 71 | - |
|
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | - |
|
| 74 | - // Set the UI service. |
|
| 75 | - $this->ui_service = $ui_service; |
|
| 76 | - |
|
| 77 | - // Set the singleton instance. |
|
| 78 | - self::$instance = $this; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the singleton instance of the Entity service. |
|
| 84 | - * |
|
| 85 | - * @since 3.2.0 |
|
| 86 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - public static function get_instance() { |
|
| 89 | - |
|
| 90 | - return self::$instance; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Determines whether a post is an entity or not. |
|
| 95 | - * |
|
| 96 | - * @since 3.1.0 |
|
| 97 | - * |
|
| 98 | - * @param int $post_id A post id. |
|
| 99 | - * |
|
| 100 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | - */ |
|
| 102 | - public function is_entity( $post_id ) { |
|
| 103 | - |
|
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the proper classification scope for a given entity post |
|
| 109 | - * |
|
| 110 | - * @since 3.5.0 |
|
| 111 | - * |
|
| 112 | - * @param integer $post_id An entity post id. |
|
| 113 | - * |
|
| 114 | - * @return string Returns an uri. |
|
| 115 | - */ |
|
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 117 | - |
|
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - // Retrieve the entity type |
|
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | - // Retrieve classification boxes configuration |
|
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | - return $cb['id']; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // or null |
|
| 133 | - return null; |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - public function is_used( $post_id ) { |
|
| 139 | - |
|
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - // Retrieve the post |
|
| 144 | - $entity = get_post( $post_id ); |
|
| 145 | - |
|
| 146 | - global $wpdb; |
|
| 147 | - // Retrieve Wordlift relation instances table name |
|
| 148 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | - |
|
| 150 | - // Check is it's referenced / related to another post / entity |
|
| 151 | - $stmt = $wpdb->prepare( |
|
| 152 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | - $entity->ID |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - // Perform the query |
|
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | - if ( 0 < $relation_instances ) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Check if the entity uri is used as meta_value |
|
| 164 | - $stmt = $wpdb->prepare( |
|
| 165 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | - $entity->ID, |
|
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 168 | - ); |
|
| 169 | - // Perform the query |
|
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | - |
|
| 172 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | - if ( 0 < $meta_instances ) { |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // If we are here, it means the current entity is not used at the moment |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Determines whether a given uri is an internal uri or not. |
|
| 183 | - * |
|
| 184 | - * @since 3.3.2 |
|
| 185 | - * |
|
| 186 | - * @param int $uri An uri. |
|
| 187 | - * |
|
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | - */ |
|
| 190 | - public function is_internal_uri( $uri ) { |
|
| 191 | - |
|
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | - * |
|
| 198 | - * @since 3.2.0 |
|
| 199 | - * |
|
| 200 | - * @param string $uri The entity URI. |
|
| 201 | - * |
|
| 202 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | - */ |
|
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 205 | - |
|
| 206 | - // Check if we've been provided with a value otherwise return null. |
|
| 207 | - if ( empty( $uri ) ) { |
|
| 208 | - return null; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $query_args = array( |
|
| 212 | - 'posts_per_page' => 1, |
|
| 213 | - 'post_status' => 'any', |
|
| 214 | - 'post_type' => self::TYPE_NAME, |
|
| 215 | - 'meta_query' => array( |
|
| 216 | - array( |
|
| 217 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | - 'value' => $uri, |
|
| 219 | - 'compare' => '=', |
|
| 220 | - ), |
|
| 221 | - ), |
|
| 222 | - ); |
|
| 223 | - |
|
| 224 | - // Only if the current uri is not an internal uri |
|
| 225 | - // entity search is performed also looking at sameAs values |
|
| 226 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 227 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | - |
|
| 229 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 230 | - $query_args['meta_query'][] = array( |
|
| 231 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 232 | - 'value' => $uri, |
|
| 233 | - 'compare' => '=', |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $query = new WP_Query( $query_args ); |
|
| 238 | - |
|
| 239 | - // Get the matching entity posts. |
|
| 240 | - $posts = $query->get_posts(); |
|
| 241 | - |
|
| 242 | - // Return null if no post is found. |
|
| 243 | - if ( 0 === count( $posts ) ) { |
|
| 244 | - return null; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // Return the found post. |
|
| 248 | - return $posts[0]; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 253 | - * we check that the post we're saving is the current post. |
|
| 254 | - * |
|
| 255 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 256 | - * |
|
| 257 | - * @since 3.2.0 |
|
| 258 | - * |
|
| 259 | - * @param int $post_id Post ID. |
|
| 260 | - * @param WP_Post $post Post object. |
|
| 261 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 262 | - */ |
|
| 263 | - public function save_post( $post_id, $post, $update ) { |
|
| 264 | - |
|
| 265 | - // We're setting the alternative label that have been provided via the UI |
|
| 266 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 267 | - // programmatically by some other function: we need to check therefore if |
|
| 268 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 269 | - // |
|
| 270 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 271 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 272 | - return; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // Get the alt labels from the request (or empty array). |
|
| 276 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | - |
|
| 278 | - // Set the alternative labels. |
|
| 279 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 280 | - |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Set the alternative labels. |
|
| 285 | - * |
|
| 286 | - * @since 3.2.0 |
|
| 287 | - * |
|
| 288 | - * @param int $post_id The post id. |
|
| 289 | - * @param array $alt_labels An array of labels. |
|
| 290 | - */ |
|
| 291 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 292 | - |
|
| 293 | - // Force $alt_labels to be an array |
|
| 294 | - if ( ! is_array( $alt_labels ) ) { |
|
| 295 | - $alt_labels = array( $alt_labels ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 299 | - |
|
| 300 | - // Delete all the existing alternate labels. |
|
| 301 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 302 | - |
|
| 303 | - // Set the alternative labels. |
|
| 304 | - foreach ( $alt_labels as $alt_label ) { |
|
| 305 | - if ( ! empty( $alt_label ) ) { |
|
| 306 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Retrieve the alternate labels. |
|
| 314 | - * |
|
| 315 | - * @since 3.2.0 |
|
| 316 | - * |
|
| 317 | - * @param int $post_id Post id. |
|
| 318 | - * |
|
| 319 | - * @return mixed An array of alternative labels. |
|
| 320 | - */ |
|
| 321 | - public function get_alternative_labels( $post_id ) { |
|
| 322 | - |
|
| 323 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 328 | - * |
|
| 329 | - * @since 3.2.0 |
|
| 330 | - * |
|
| 331 | - * @param WP_Post $post Post object. |
|
| 332 | - */ |
|
| 333 | - public function edit_form_before_permalink( $post ) { |
|
| 334 | - |
|
| 335 | - // If it's not an entity, return. |
|
| 336 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 337 | - return; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // Print the input template. |
|
| 341 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 342 | - |
|
| 343 | - // Print all the currently set alternative labels. |
|
| 344 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 345 | - |
|
| 346 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 347 | - |
|
| 348 | - }; |
|
| 349 | - |
|
| 350 | - // Print the button. |
|
| 351 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 352 | - |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Get the URI for the entity with the specified post id. |
|
| 357 | - * |
|
| 358 | - * @since 3.6.0 |
|
| 359 | - * |
|
| 360 | - * @param int $post_id The entity post id. |
|
| 361 | - * |
|
| 362 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 363 | - */ |
|
| 364 | - public function get_uri( $post_id ) { |
|
| 365 | - |
|
| 366 | - // If a null is given, nothing to do |
|
| 367 | - if ( null == $post_id ) { |
|
| 368 | - return null; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 372 | - |
|
| 373 | - // If the dataset uri is not properly configured, null is returned |
|
| 374 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 375 | - return null; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Set the URI if it isn't set yet. |
|
| 379 | - $post_status = get_post_status( $post_id ); |
|
| 380 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - return $uri; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the alternative label input HTML code. |
|
| 391 | - * |
|
| 392 | - * @since 3.2.0 |
|
| 393 | - * |
|
| 394 | - * @param string $value The input value. |
|
| 395 | - * |
|
| 396 | - * @return string The input HTML code. |
|
| 397 | - */ |
|
| 398 | - private function get_alternative_label_input( $value = '' ) { |
|
| 399 | - |
|
| 400 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get the number of entity posts published in this blog. |
|
| 405 | - * |
|
| 406 | - * @since 3.6.0 |
|
| 407 | - * |
|
| 408 | - * @return int The number of published entity posts. |
|
| 409 | - */ |
|
| 410 | - public function count() { |
|
| 411 | - |
|
| 412 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 413 | - |
|
| 414 | - return $count->publish; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Create a new entity. |
|
| 419 | - * |
|
| 420 | - * @since 3.9.0 |
|
| 421 | - * |
|
| 422 | - * @param string $name The entity name. |
|
| 423 | - * @param string $type_uri The entity's type URI. |
|
| 424 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 425 | - * @param string $status The post status, by default 'publish'. |
|
| 426 | - * |
|
| 427 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | - */ |
|
| 429 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 430 | - |
|
| 431 | - // Create an entity for the publisher. |
|
| 432 | - $post_id = wp_insert_post( array( |
|
| 433 | - 'post_type' => self::TYPE_NAME, |
|
| 434 | - 'post_title' => $name, |
|
| 435 | - 'post_status' => $status, |
|
| 436 | - 'post_content' => '', |
|
| 437 | - ) ); |
|
| 438 | - |
|
| 439 | - // Return the error if any. |
|
| 440 | - if ( is_wp_error( $post_id ) ) { |
|
| 441 | - return $post_id; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // Set the entity logo. |
|
| 445 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | - set_post_thumbnail( $post_id, $logo ); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - // Set the entity type. |
|
| 450 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 451 | - |
|
| 452 | - return $post_id; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get the entities related to the one with the specified id. By default only |
|
| 457 | - * published entities will be returned. |
|
| 458 | - * |
|
| 459 | - * @since 3.10.0 |
|
| 460 | - * |
|
| 461 | - * @param int $id The post id. |
|
| 462 | - * @param string $post_status The target post status (default = publish). |
|
| 463 | - * |
|
| 464 | - * @return array An array of post ids. |
|
| 465 | - */ |
|
| 466 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 467 | - |
|
| 468 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 469 | - } |
|
| 54 | + /** |
|
| 55 | + * A singleton instance of the Entity service. |
|
| 56 | + * |
|
| 57 | + * @since 3.2.0 |
|
| 58 | + * @access private |
|
| 59 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | + */ |
|
| 61 | + private static $instance; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Create a Wordlift_Entity_Service instance. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | + */ |
|
| 70 | + public function __construct( $ui_service ) { |
|
| 71 | + |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | + |
|
| 74 | + // Set the UI service. |
|
| 75 | + $this->ui_service = $ui_service; |
|
| 76 | + |
|
| 77 | + // Set the singleton instance. |
|
| 78 | + self::$instance = $this; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the singleton instance of the Entity service. |
|
| 84 | + * |
|
| 85 | + * @since 3.2.0 |
|
| 86 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + public static function get_instance() { |
|
| 89 | + |
|
| 90 | + return self::$instance; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Determines whether a post is an entity or not. |
|
| 95 | + * |
|
| 96 | + * @since 3.1.0 |
|
| 97 | + * |
|
| 98 | + * @param int $post_id A post id. |
|
| 99 | + * |
|
| 100 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | + */ |
|
| 102 | + public function is_entity( $post_id ) { |
|
| 103 | + |
|
| 104 | + return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the proper classification scope for a given entity post |
|
| 109 | + * |
|
| 110 | + * @since 3.5.0 |
|
| 111 | + * |
|
| 112 | + * @param integer $post_id An entity post id. |
|
| 113 | + * |
|
| 114 | + * @return string Returns an uri. |
|
| 115 | + */ |
|
| 116 | + public function get_classification_scope_for( $post_id ) { |
|
| 117 | + |
|
| 118 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + // Retrieve the entity type |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | + // Retrieve classification boxes configuration |
|
| 125 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | + foreach ( $classification_boxes as $cb ) { |
|
| 127 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | + return $cb['id']; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // or null |
|
| 133 | + return null; |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + public function is_used( $post_id ) { |
|
| 139 | + |
|
| 140 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + // Retrieve the post |
|
| 144 | + $entity = get_post( $post_id ); |
|
| 145 | + |
|
| 146 | + global $wpdb; |
|
| 147 | + // Retrieve Wordlift relation instances table name |
|
| 148 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | + |
|
| 150 | + // Check is it's referenced / related to another post / entity |
|
| 151 | + $stmt = $wpdb->prepare( |
|
| 152 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | + $entity->ID |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + // Perform the query |
|
| 157 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | + if ( 0 < $relation_instances ) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Check if the entity uri is used as meta_value |
|
| 164 | + $stmt = $wpdb->prepare( |
|
| 165 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | + $entity->ID, |
|
| 167 | + wl_get_entity_uri( $entity->ID ) |
|
| 168 | + ); |
|
| 169 | + // Perform the query |
|
| 170 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | + |
|
| 172 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | + if ( 0 < $meta_instances ) { |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // If we are here, it means the current entity is not used at the moment |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Determines whether a given uri is an internal uri or not. |
|
| 183 | + * |
|
| 184 | + * @since 3.3.2 |
|
| 185 | + * |
|
| 186 | + * @param int $uri An uri. |
|
| 187 | + * |
|
| 188 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | + */ |
|
| 190 | + public function is_internal_uri( $uri ) { |
|
| 191 | + |
|
| 192 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | + * |
|
| 198 | + * @since 3.2.0 |
|
| 199 | + * |
|
| 200 | + * @param string $uri The entity URI. |
|
| 201 | + * |
|
| 202 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | + */ |
|
| 204 | + public function get_entity_post_by_uri( $uri ) { |
|
| 205 | + |
|
| 206 | + // Check if we've been provided with a value otherwise return null. |
|
| 207 | + if ( empty( $uri ) ) { |
|
| 208 | + return null; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $query_args = array( |
|
| 212 | + 'posts_per_page' => 1, |
|
| 213 | + 'post_status' => 'any', |
|
| 214 | + 'post_type' => self::TYPE_NAME, |
|
| 215 | + 'meta_query' => array( |
|
| 216 | + array( |
|
| 217 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | + 'value' => $uri, |
|
| 219 | + 'compare' => '=', |
|
| 220 | + ), |
|
| 221 | + ), |
|
| 222 | + ); |
|
| 223 | + |
|
| 224 | + // Only if the current uri is not an internal uri |
|
| 225 | + // entity search is performed also looking at sameAs values |
|
| 226 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 227 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | + |
|
| 229 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 230 | + $query_args['meta_query'][] = array( |
|
| 231 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 232 | + 'value' => $uri, |
|
| 233 | + 'compare' => '=', |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $query = new WP_Query( $query_args ); |
|
| 238 | + |
|
| 239 | + // Get the matching entity posts. |
|
| 240 | + $posts = $query->get_posts(); |
|
| 241 | + |
|
| 242 | + // Return null if no post is found. |
|
| 243 | + if ( 0 === count( $posts ) ) { |
|
| 244 | + return null; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // Return the found post. |
|
| 248 | + return $posts[0]; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 253 | + * we check that the post we're saving is the current post. |
|
| 254 | + * |
|
| 255 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 256 | + * |
|
| 257 | + * @since 3.2.0 |
|
| 258 | + * |
|
| 259 | + * @param int $post_id Post ID. |
|
| 260 | + * @param WP_Post $post Post object. |
|
| 261 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 262 | + */ |
|
| 263 | + public function save_post( $post_id, $post, $update ) { |
|
| 264 | + |
|
| 265 | + // We're setting the alternative label that have been provided via the UI |
|
| 266 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 267 | + // programmatically by some other function: we need to check therefore if |
|
| 268 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 269 | + // |
|
| 270 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 271 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 272 | + return; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // Get the alt labels from the request (or empty array). |
|
| 276 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | + |
|
| 278 | + // Set the alternative labels. |
|
| 279 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 280 | + |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Set the alternative labels. |
|
| 285 | + * |
|
| 286 | + * @since 3.2.0 |
|
| 287 | + * |
|
| 288 | + * @param int $post_id The post id. |
|
| 289 | + * @param array $alt_labels An array of labels. |
|
| 290 | + */ |
|
| 291 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 292 | + |
|
| 293 | + // Force $alt_labels to be an array |
|
| 294 | + if ( ! is_array( $alt_labels ) ) { |
|
| 295 | + $alt_labels = array( $alt_labels ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 299 | + |
|
| 300 | + // Delete all the existing alternate labels. |
|
| 301 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 302 | + |
|
| 303 | + // Set the alternative labels. |
|
| 304 | + foreach ( $alt_labels as $alt_label ) { |
|
| 305 | + if ( ! empty( $alt_label ) ) { |
|
| 306 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Retrieve the alternate labels. |
|
| 314 | + * |
|
| 315 | + * @since 3.2.0 |
|
| 316 | + * |
|
| 317 | + * @param int $post_id Post id. |
|
| 318 | + * |
|
| 319 | + * @return mixed An array of alternative labels. |
|
| 320 | + */ |
|
| 321 | + public function get_alternative_labels( $post_id ) { |
|
| 322 | + |
|
| 323 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 328 | + * |
|
| 329 | + * @since 3.2.0 |
|
| 330 | + * |
|
| 331 | + * @param WP_Post $post Post object. |
|
| 332 | + */ |
|
| 333 | + public function edit_form_before_permalink( $post ) { |
|
| 334 | + |
|
| 335 | + // If it's not an entity, return. |
|
| 336 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 337 | + return; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // Print the input template. |
|
| 341 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 342 | + |
|
| 343 | + // Print all the currently set alternative labels. |
|
| 344 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 345 | + |
|
| 346 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 347 | + |
|
| 348 | + }; |
|
| 349 | + |
|
| 350 | + // Print the button. |
|
| 351 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 352 | + |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Get the URI for the entity with the specified post id. |
|
| 357 | + * |
|
| 358 | + * @since 3.6.0 |
|
| 359 | + * |
|
| 360 | + * @param int $post_id The entity post id. |
|
| 361 | + * |
|
| 362 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 363 | + */ |
|
| 364 | + public function get_uri( $post_id ) { |
|
| 365 | + |
|
| 366 | + // If a null is given, nothing to do |
|
| 367 | + if ( null == $post_id ) { |
|
| 368 | + return null; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 372 | + |
|
| 373 | + // If the dataset uri is not properly configured, null is returned |
|
| 374 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 375 | + return null; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Set the URI if it isn't set yet. |
|
| 379 | + $post_status = get_post_status( $post_id ); |
|
| 380 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + return $uri; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the alternative label input HTML code. |
|
| 391 | + * |
|
| 392 | + * @since 3.2.0 |
|
| 393 | + * |
|
| 394 | + * @param string $value The input value. |
|
| 395 | + * |
|
| 396 | + * @return string The input HTML code. |
|
| 397 | + */ |
|
| 398 | + private function get_alternative_label_input( $value = '' ) { |
|
| 399 | + |
|
| 400 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get the number of entity posts published in this blog. |
|
| 405 | + * |
|
| 406 | + * @since 3.6.0 |
|
| 407 | + * |
|
| 408 | + * @return int The number of published entity posts. |
|
| 409 | + */ |
|
| 410 | + public function count() { |
|
| 411 | + |
|
| 412 | + $count = wp_count_posts( self::TYPE_NAME ); |
|
| 413 | + |
|
| 414 | + return $count->publish; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Create a new entity. |
|
| 419 | + * |
|
| 420 | + * @since 3.9.0 |
|
| 421 | + * |
|
| 422 | + * @param string $name The entity name. |
|
| 423 | + * @param string $type_uri The entity's type URI. |
|
| 424 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 425 | + * @param string $status The post status, by default 'publish'. |
|
| 426 | + * |
|
| 427 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | + */ |
|
| 429 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 430 | + |
|
| 431 | + // Create an entity for the publisher. |
|
| 432 | + $post_id = wp_insert_post( array( |
|
| 433 | + 'post_type' => self::TYPE_NAME, |
|
| 434 | + 'post_title' => $name, |
|
| 435 | + 'post_status' => $status, |
|
| 436 | + 'post_content' => '', |
|
| 437 | + ) ); |
|
| 438 | + |
|
| 439 | + // Return the error if any. |
|
| 440 | + if ( is_wp_error( $post_id ) ) { |
|
| 441 | + return $post_id; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // Set the entity logo. |
|
| 445 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | + set_post_thumbnail( $post_id, $logo ); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + // Set the entity type. |
|
| 450 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 451 | + |
|
| 452 | + return $post_id; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get the entities related to the one with the specified id. By default only |
|
| 457 | + * published entities will be returned. |
|
| 458 | + * |
|
| 459 | + * @since 3.10.0 |
|
| 460 | + * |
|
| 461 | + * @param int $id The post id. |
|
| 462 | + * @param string $post_status The target post status (default = publish). |
|
| 463 | + * |
|
| 464 | + * @return array An array of post ids. |
|
| 465 | + */ |
|
| 466 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 467 | + |
|
| 468 | + return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | 471 | } |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 69 | 69 | */ |
| 70 | - public function __construct( $ui_service ) { |
|
| 70 | + public function __construct($ui_service) { |
|
| 71 | 71 | |
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 73 | 73 | |
| 74 | 74 | // Set the UI service. |
| 75 | 75 | $this->ui_service = $ui_service; |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return bool Return true if the post is an entity otherwise false. |
| 101 | 101 | */ |
| 102 | - public function is_entity( $post_id ) { |
|
| 102 | + public function is_entity($post_id) { |
|
| 103 | 103 | |
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 104 | + return (self::TYPE_NAME === get_post_type($post_id)); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return string Returns an uri. |
| 115 | 115 | */ |
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 116 | + public function get_classification_scope_for($post_id) { |
|
| 117 | 117 | |
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 118 | + if (false === $this->is_entity($post_id)) { |
|
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | // Retrieve the entity type |
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type($post_id); |
|
| 123 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 124 | 124 | // Retrieve classification boxes configuration |
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 125 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 126 | + foreach ($classification_boxes as $cb) { |
|
| 127 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 128 | 128 | return $cb['id']; |
| 129 | 129 | } |
| 130 | 130 | } |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
| 138 | - public function is_used( $post_id ) { |
|
| 138 | + public function is_used($post_id) { |
|
| 139 | 139 | |
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 140 | + if (false === $this->is_entity($post_id)) { |
|
| 141 | 141 | return null; |
| 142 | 142 | } |
| 143 | 143 | // Retrieve the post |
| 144 | - $entity = get_post( $post_id ); |
|
| 144 | + $entity = get_post($post_id); |
|
| 145 | 145 | |
| 146 | 146 | global $wpdb; |
| 147 | 147 | // Retrieve Wordlift relation instances table name |
@@ -154,9 +154,9 @@ discard block |
||
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | 156 | // Perform the query |
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 157 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 158 | 158 | // If there is at least one relation instance for the current entity, then it's used |
| 159 | - if ( 0 < $relation_instances ) { |
|
| 159 | + if (0 < $relation_instances) { |
|
| 160 | 160 | return true; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | $stmt = $wpdb->prepare( |
| 165 | 165 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 166 | 166 | $entity->ID, |
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 167 | + wl_get_entity_uri($entity->ID) |
|
| 168 | 168 | ); |
| 169 | 169 | // Perform the query |
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 170 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 171 | 171 | |
| 172 | 172 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 173 | - if ( 0 < $meta_instances ) { |
|
| 173 | + if (0 < $meta_instances) { |
|
| 174 | 174 | return true; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return true if the uri internal to the current dataset otherwise false. |
| 189 | 189 | */ |
| 190 | - public function is_internal_uri( $uri ) { |
|
| 190 | + public function is_internal_uri($uri) { |
|
| 191 | 191 | |
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 192 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -201,10 +201,10 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 203 | 203 | */ |
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 204 | + public function get_entity_post_by_uri($uri) { |
|
| 205 | 205 | |
| 206 | 206 | // Check if we've been provided with a value otherwise return null. |
| 207 | - if ( empty( $uri ) ) { |
|
| 207 | + if (empty($uri)) { |
|
| 208 | 208 | return null; |
| 209 | 209 | } |
| 210 | 210 | |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | // Only if the current uri is not an internal uri |
| 225 | 225 | // entity search is performed also looking at sameAs values |
| 226 | 226 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 227 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 227 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 228 | 228 | |
| 229 | 229 | $query_args['meta_query']['relation'] = 'OR'; |
| 230 | 230 | $query_args['meta_query'][] = array( |
@@ -234,13 +234,13 @@ discard block |
||
| 234 | 234 | ); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - $query = new WP_Query( $query_args ); |
|
| 237 | + $query = new WP_Query($query_args); |
|
| 238 | 238 | |
| 239 | 239 | // Get the matching entity posts. |
| 240 | 240 | $posts = $query->get_posts(); |
| 241 | 241 | |
| 242 | 242 | // Return null if no post is found. |
| 243 | - if ( 0 === count( $posts ) ) { |
|
| 243 | + if (0 === count($posts)) { |
|
| 244 | 244 | return null; |
| 245 | 245 | } |
| 246 | 246 | |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | * @param WP_Post $post Post object. |
| 261 | 261 | * @param bool $update Whether this is an existing post being updated or not. |
| 262 | 262 | */ |
| 263 | - public function save_post( $post_id, $post, $update ) { |
|
| 263 | + public function save_post($post_id, $post, $update) { |
|
| 264 | 264 | |
| 265 | 265 | // We're setting the alternative label that have been provided via the UI |
| 266 | 266 | // (in fact we're using $_REQUEST), while save_post may be also called |
@@ -268,15 +268,15 @@ discard block |
||
| 268 | 268 | // the $post_id in the save_post call matches the post id set in the request. |
| 269 | 269 | // |
| 270 | 270 | // If this is not the current post being saved or if it's not an entity, return. |
| 271 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 271 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 272 | 272 | return; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | // Get the alt labels from the request (or empty array). |
| 276 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 276 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | 277 | |
| 278 | 278 | // Set the alternative labels. |
| 279 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 279 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 280 | 280 | |
| 281 | 281 | } |
| 282 | 282 | |
@@ -288,22 +288,22 @@ discard block |
||
| 288 | 288 | * @param int $post_id The post id. |
| 289 | 289 | * @param array $alt_labels An array of labels. |
| 290 | 290 | */ |
| 291 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 291 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 292 | 292 | |
| 293 | 293 | // Force $alt_labels to be an array |
| 294 | - if ( ! is_array( $alt_labels ) ) { |
|
| 295 | - $alt_labels = array( $alt_labels ); |
|
| 294 | + if ( ! is_array($alt_labels)) { |
|
| 295 | + $alt_labels = array($alt_labels); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 298 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 299 | 299 | |
| 300 | 300 | // Delete all the existing alternate labels. |
| 301 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 301 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 302 | 302 | |
| 303 | 303 | // Set the alternative labels. |
| 304 | - foreach ( $alt_labels as $alt_label ) { |
|
| 305 | - if ( ! empty( $alt_label ) ) { |
|
| 306 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 304 | + foreach ($alt_labels as $alt_label) { |
|
| 305 | + if ( ! empty($alt_label)) { |
|
| 306 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
@@ -318,9 +318,9 @@ discard block |
||
| 318 | 318 | * |
| 319 | 319 | * @return mixed An array of alternative labels. |
| 320 | 320 | */ |
| 321 | - public function get_alternative_labels( $post_id ) { |
|
| 321 | + public function get_alternative_labels($post_id) { |
|
| 322 | 322 | |
| 323 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 323 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** |
@@ -330,25 +330,25 @@ discard block |
||
| 330 | 330 | * |
| 331 | 331 | * @param WP_Post $post Post object. |
| 332 | 332 | */ |
| 333 | - public function edit_form_before_permalink( $post ) { |
|
| 333 | + public function edit_form_before_permalink($post) { |
|
| 334 | 334 | |
| 335 | 335 | // If it's not an entity, return. |
| 336 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 336 | + if ( ! $this->is_entity($post->ID)) { |
|
| 337 | 337 | return; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | // Print the input template. |
| 341 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 341 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 342 | 342 | |
| 343 | 343 | // Print all the currently set alternative labels. |
| 344 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 344 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 345 | 345 | |
| 346 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 346 | + echo $this->get_alternative_label_input($alt_label); |
|
| 347 | 347 | |
| 348 | 348 | }; |
| 349 | 349 | |
| 350 | 350 | // Print the button. |
| 351 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 351 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
@@ -361,25 +361,25 @@ discard block |
||
| 361 | 361 | * |
| 362 | 362 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 363 | 363 | */ |
| 364 | - public function get_uri( $post_id ) { |
|
| 364 | + public function get_uri($post_id) { |
|
| 365 | 365 | |
| 366 | 366 | // If a null is given, nothing to do |
| 367 | - if ( null == $post_id ) { |
|
| 367 | + if (null == $post_id) { |
|
| 368 | 368 | return null; |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 371 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 372 | 372 | |
| 373 | 373 | // If the dataset uri is not properly configured, null is returned |
| 374 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 374 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 375 | 375 | return null; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | // Set the URI if it isn't set yet. |
| 379 | - $post_status = get_post_status( $post_id ); |
|
| 380 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 379 | + $post_status = get_post_status($post_id); |
|
| 380 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 381 | + $uri = wl_build_entity_uri($post_id); |
|
| 382 | + wl_set_entity_uri($post_id, $uri); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | return $uri; |
@@ -395,9 +395,9 @@ discard block |
||
| 395 | 395 | * |
| 396 | 396 | * @return string The input HTML code. |
| 397 | 397 | */ |
| 398 | - private function get_alternative_label_input( $value = '' ) { |
|
| 398 | + private function get_alternative_label_input($value = '') { |
|
| 399 | 399 | |
| 400 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 400 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | /** |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | */ |
| 410 | 410 | public function count() { |
| 411 | 411 | |
| 412 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 412 | + $count = wp_count_posts(self::TYPE_NAME); |
|
| 413 | 413 | |
| 414 | 414 | return $count->publish; |
| 415 | 415 | } |
@@ -426,28 +426,28 @@ discard block |
||
| 426 | 426 | * |
| 427 | 427 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 428 | 428 | */ |
| 429 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 429 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 430 | 430 | |
| 431 | 431 | // Create an entity for the publisher. |
| 432 | - $post_id = wp_insert_post( array( |
|
| 432 | + $post_id = wp_insert_post(array( |
|
| 433 | 433 | 'post_type' => self::TYPE_NAME, |
| 434 | 434 | 'post_title' => $name, |
| 435 | 435 | 'post_status' => $status, |
| 436 | 436 | 'post_content' => '', |
| 437 | - ) ); |
|
| 437 | + )); |
|
| 438 | 438 | |
| 439 | 439 | // Return the error if any. |
| 440 | - if ( is_wp_error( $post_id ) ) { |
|
| 440 | + if (is_wp_error($post_id)) { |
|
| 441 | 441 | return $post_id; |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | // Set the entity logo. |
| 445 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | - set_post_thumbnail( $post_id, $logo ); |
|
| 445 | + if ($logo && is_numeric($logo)) { |
|
| 446 | + set_post_thumbnail($post_id, $logo); |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | 449 | // Set the entity type. |
| 450 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 450 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 451 | 451 | |
| 452 | 452 | return $post_id; |
| 453 | 453 | } |
@@ -463,9 +463,9 @@ discard block |
||
| 463 | 463 | * |
| 464 | 464 | * @return array An array of post ids. |
| 465 | 465 | */ |
| 466 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 466 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 467 | 467 | |
| 468 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 468 | + return wl_core_inner_get_related_entities('post_ids', $id, null, $post_status); |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | } |
@@ -173,7 +173,6 @@ discard block |
||
| 173 | 173 | * @since 3.3.0 |
| 174 | 174 | * |
| 175 | 175 | * @param int $post_id The entity post id. |
| 176 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | 176 | * |
| 178 | 177 | * @return array An array representing the rating obj. |
| 179 | 178 | */ |
@@ -332,7 +331,7 @@ discard block |
||
| 332 | 331 | * |
| 333 | 332 | * @param int $score The rating score for a given entity. |
| 334 | 333 | * |
| 335 | - * @return string The input HTML code. |
|
| 334 | + * @return double The input HTML code. |
|
| 336 | 335 | */ |
| 337 | 336 | public function convert_raw_score_to_percentage( $score ) { |
| 338 | 337 | |
@@ -14,370 +14,370 @@ |
||
| 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_Service} instance. |
|
| 89 | - * |
|
| 90 | - * @since 3.10.0 |
|
| 91 | - * @access private |
|
| 92 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | - */ |
|
| 94 | - private $entity_service; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | - * |
|
| 99 | - * @since 3.10.0 |
|
| 100 | - * @access private |
|
| 101 | - * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | - */ |
|
| 103 | - private $entity_type_service; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The Notice service. |
|
| 107 | - * |
|
| 108 | - * @since 3.3.0 |
|
| 109 | - * @access private |
|
| 110 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | - */ |
|
| 112 | - private $notice_service; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * A {@link Wordlift_Log_Service} instance. |
|
| 116 | - * |
|
| 117 | - * @since 3.10.0 |
|
| 118 | - * @access private |
|
| 119 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | - */ |
|
| 121 | - private $log; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | - * |
|
| 126 | - * @since 3.10.0 |
|
| 127 | - * |
|
| 128 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | - * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | - */ |
|
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | - |
|
| 134 | - $this->entity_service = $entity_service; |
|
| 135 | - $this->entity_type_service = $entity_type_service; |
|
| 136 | - $this->notice_service = $notice_service; |
|
| 137 | - |
|
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Set rating for a given entity. |
|
| 144 | - * |
|
| 145 | - * @since 3.3.0 |
|
| 146 | - * |
|
| 147 | - * @param int $post_id The entity post id. |
|
| 148 | - * |
|
| 149 | - * @return array An array representing the rating obj. |
|
| 150 | - */ |
|
| 151 | - public function set_rating_for( $post_id ) { |
|
| 152 | - |
|
| 153 | - // Calculate rating for the given post. |
|
| 154 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 155 | - |
|
| 156 | - // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 157 | - // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 158 | - // |
|
| 159 | - // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 160 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 162 | - |
|
| 163 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 164 | - |
|
| 165 | - // Finally returns the rating |
|
| 166 | - return $rating; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Get or calculate rating for a given entity |
|
| 172 | - * |
|
| 173 | - * @since 3.3.0 |
|
| 174 | - * |
|
| 175 | - * @param int $post_id The entity post id. |
|
| 176 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | - * |
|
| 178 | - * @return array An array representing the rating obj. |
|
| 179 | - */ |
|
| 180 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 181 | - |
|
| 182 | - // If forced reload is required or rating is missing. |
|
| 183 | - if ( $force_reload ) { |
|
| 184 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 185 | - |
|
| 186 | - return $this->set_rating_for( $post_id ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 190 | - |
|
| 191 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 193 | - |
|
| 194 | - return $this->set_rating_for( $post_id ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 198 | - |
|
| 199 | - // Finally return score and warnings |
|
| 200 | - return array( |
|
| 201 | - 'raw_score' => $current_raw_score, |
|
| 202 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 204 | - 'warnings' => $current_warnings, |
|
| 205 | - ); |
|
| 206 | - |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Calculate rating for a given entity. |
|
| 211 | - * |
|
| 212 | - * Rating depends from following criteria: |
|
| 213 | - * |
|
| 214 | - * 1. Is the current entity related to at least 1 post? |
|
| 215 | - * 2. Is the current entity content post not empty? |
|
| 216 | - * 3. Is the current entity related to at least 1 entity? |
|
| 217 | - * 4. Is the entity published? |
|
| 218 | - * 5. There is a a thumbnail associated to the entity? |
|
| 219 | - * 6. Has the entity a sameas defined? |
|
| 220 | - * 7. Are all schema.org required metadata compiled? |
|
| 221 | - * |
|
| 222 | - * Each positive check means +1 in terms of rating score. |
|
| 223 | - * |
|
| 224 | - * @since 3.3.0 |
|
| 225 | - * |
|
| 226 | - * @param int $post_id The entity post id. |
|
| 227 | - * |
|
| 228 | - * @return array An array representing the rating obj. |
|
| 229 | - */ |
|
| 230 | - private function calculate_rating_for( $post_id ) { |
|
| 231 | - |
|
| 232 | - // If it's not an entity, return. |
|
| 233 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 234 | - return array(); |
|
| 235 | - } |
|
| 236 | - // Retrieve the post object. |
|
| 237 | - $post = get_post( $post_id ); |
|
| 238 | - |
|
| 239 | - // Rating value. |
|
| 240 | - $score = 0; |
|
| 241 | - |
|
| 242 | - // Store warning messages. |
|
| 243 | - $warnings = array(); |
|
| 244 | - |
|
| 245 | - // Is the current entity related to at least 1 post? |
|
| 246 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | - $score ++ : |
|
| 248 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 249 | - |
|
| 250 | - // Is the post content not empty? |
|
| 251 | - ( ! empty( $post->post_content ) ) ? |
|
| 252 | - $score ++ : |
|
| 253 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 254 | - |
|
| 255 | - // Is the current entity related to at least 1 entity? |
|
| 256 | - // Was the current entity already disambiguated? |
|
| 257 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | - $score ++ : |
|
| 259 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 260 | - |
|
| 261 | - // Is the entity published? |
|
| 262 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | - $score ++ : |
|
| 264 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 265 | - |
|
| 266 | - // Has a thumbnail? |
|
| 267 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | - $score ++ : |
|
| 269 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 270 | - |
|
| 271 | - // Get all post meta keys for the current post |
|
| 272 | - global $wpdb; |
|
| 273 | - |
|
| 274 | - $query = $wpdb->prepare( |
|
| 275 | - "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - // Check intersection between available meta keys and expected ones |
|
| 279 | - // arrays to detect missing values. |
|
| 280 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 281 | - |
|
| 282 | - // If each expected key is contained in available keys array ... |
|
| 283 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | - $score ++ : |
|
| 285 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 286 | - |
|
| 287 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 288 | - |
|
| 289 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | - array() : |
|
| 291 | - array_keys( $schema['custom_fields'] ); |
|
| 292 | - |
|
| 293 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 294 | - // If each expected key is contained in available keys array ... |
|
| 295 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | - $score ++ : |
|
| 297 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 298 | - |
|
| 299 | - // Finally return score and warnings |
|
| 300 | - return array( |
|
| 301 | - 'raw_score' => $score, |
|
| 302 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 304 | - 'warnings' => $warnings, |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get as rating as input and convert in a traffic-light rating |
|
| 311 | - * |
|
| 312 | - * @since 3.3.0 |
|
| 313 | - * |
|
| 314 | - * @param int $score The rating score for a given entity. |
|
| 315 | - * |
|
| 316 | - * @return string The input HTML code. |
|
| 317 | - */ |
|
| 318 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 319 | - // RATING_MAX : $score = 3 : x |
|
| 320 | - // See http://php.net/manual/en/function.round.php |
|
| 321 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 322 | - |
|
| 323 | - // If rating is 0, return 1, otherwise return rating |
|
| 324 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 325 | - |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Get as rating as input and convert in a traffic-light rating |
|
| 330 | - * |
|
| 331 | - * @since 3.3.0 |
|
| 332 | - * |
|
| 333 | - * @param int $score The rating score for a given entity. |
|
| 334 | - * |
|
| 335 | - * @return string The input HTML code. |
|
| 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 | - /** |
|
| 345 | - * Add admin notices for the current entity depending on the current rating. |
|
| 346 | - * |
|
| 347 | - * @since 3.3.0 |
|
| 348 | - */ |
|
| 349 | - public function in_admin_header() { |
|
| 350 | - |
|
| 351 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 352 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 353 | - return; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $screen = get_current_screen(); |
|
| 357 | - // If there is any valid screen nothing to do |
|
| 358 | - if ( null === $screen ) { |
|
| 359 | - return; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - // If you're not in the entity post edit page, return. |
|
| 363 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 364 | - return; |
|
| 365 | - } |
|
| 366 | - // Retrieve the current global post |
|
| 367 | - global $post; |
|
| 368 | - // If it's not an entity, return. |
|
| 369 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 370 | - return; |
|
| 371 | - } |
|
| 372 | - // Retrieve an updated rating for the current entity |
|
| 373 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 374 | - |
|
| 375 | - // If there is at least 1 warning |
|
| 376 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 377 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 378 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - } |
|
| 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_Service} instance. |
|
| 89 | + * |
|
| 90 | + * @since 3.10.0 |
|
| 91 | + * @access private |
|
| 92 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | + */ |
|
| 94 | + private $entity_service; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | + * |
|
| 99 | + * @since 3.10.0 |
|
| 100 | + * @access private |
|
| 101 | + * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | + */ |
|
| 103 | + private $entity_type_service; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The Notice service. |
|
| 107 | + * |
|
| 108 | + * @since 3.3.0 |
|
| 109 | + * @access private |
|
| 110 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | + */ |
|
| 112 | + private $notice_service; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * A {@link Wordlift_Log_Service} instance. |
|
| 116 | + * |
|
| 117 | + * @since 3.10.0 |
|
| 118 | + * @access private |
|
| 119 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | + */ |
|
| 121 | + private $log; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | + * |
|
| 126 | + * @since 3.10.0 |
|
| 127 | + * |
|
| 128 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | + * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | + */ |
|
| 132 | + public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | + |
|
| 134 | + $this->entity_service = $entity_service; |
|
| 135 | + $this->entity_type_service = $entity_type_service; |
|
| 136 | + $this->notice_service = $notice_service; |
|
| 137 | + |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Set rating for a given entity. |
|
| 144 | + * |
|
| 145 | + * @since 3.3.0 |
|
| 146 | + * |
|
| 147 | + * @param int $post_id The entity post id. |
|
| 148 | + * |
|
| 149 | + * @return array An array representing the rating obj. |
|
| 150 | + */ |
|
| 151 | + public function set_rating_for( $post_id ) { |
|
| 152 | + |
|
| 153 | + // Calculate rating for the given post. |
|
| 154 | + $rating = $this->calculate_rating_for( $post_id ); |
|
| 155 | + |
|
| 156 | + // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 157 | + // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 158 | + // |
|
| 159 | + // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 160 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 162 | + |
|
| 163 | + $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 164 | + |
|
| 165 | + // Finally returns the rating |
|
| 166 | + return $rating; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Get or calculate rating for a given entity |
|
| 172 | + * |
|
| 173 | + * @since 3.3.0 |
|
| 174 | + * |
|
| 175 | + * @param int $post_id The entity post id. |
|
| 176 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | + * |
|
| 178 | + * @return array An array representing the rating obj. |
|
| 179 | + */ |
|
| 180 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 181 | + |
|
| 182 | + // If forced reload is required or rating is missing. |
|
| 183 | + if ( $force_reload ) { |
|
| 184 | + $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 185 | + |
|
| 186 | + return $this->set_rating_for( $post_id ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 190 | + |
|
| 191 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | + $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 193 | + |
|
| 194 | + return $this->set_rating_for( $post_id ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 198 | + |
|
| 199 | + // Finally return score and warnings |
|
| 200 | + return array( |
|
| 201 | + 'raw_score' => $current_raw_score, |
|
| 202 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 204 | + 'warnings' => $current_warnings, |
|
| 205 | + ); |
|
| 206 | + |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Calculate rating for a given entity. |
|
| 211 | + * |
|
| 212 | + * Rating depends from following criteria: |
|
| 213 | + * |
|
| 214 | + * 1. Is the current entity related to at least 1 post? |
|
| 215 | + * 2. Is the current entity content post not empty? |
|
| 216 | + * 3. Is the current entity related to at least 1 entity? |
|
| 217 | + * 4. Is the entity published? |
|
| 218 | + * 5. There is a a thumbnail associated to the entity? |
|
| 219 | + * 6. Has the entity a sameas defined? |
|
| 220 | + * 7. Are all schema.org required metadata compiled? |
|
| 221 | + * |
|
| 222 | + * Each positive check means +1 in terms of rating score. |
|
| 223 | + * |
|
| 224 | + * @since 3.3.0 |
|
| 225 | + * |
|
| 226 | + * @param int $post_id The entity post id. |
|
| 227 | + * |
|
| 228 | + * @return array An array representing the rating obj. |
|
| 229 | + */ |
|
| 230 | + private function calculate_rating_for( $post_id ) { |
|
| 231 | + |
|
| 232 | + // If it's not an entity, return. |
|
| 233 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 234 | + return array(); |
|
| 235 | + } |
|
| 236 | + // Retrieve the post object. |
|
| 237 | + $post = get_post( $post_id ); |
|
| 238 | + |
|
| 239 | + // Rating value. |
|
| 240 | + $score = 0; |
|
| 241 | + |
|
| 242 | + // Store warning messages. |
|
| 243 | + $warnings = array(); |
|
| 244 | + |
|
| 245 | + // Is the current entity related to at least 1 post? |
|
| 246 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | + $score ++ : |
|
| 248 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 249 | + |
|
| 250 | + // Is the post content not empty? |
|
| 251 | + ( ! empty( $post->post_content ) ) ? |
|
| 252 | + $score ++ : |
|
| 253 | + array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 254 | + |
|
| 255 | + // Is the current entity related to at least 1 entity? |
|
| 256 | + // Was the current entity already disambiguated? |
|
| 257 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | + $score ++ : |
|
| 259 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 260 | + |
|
| 261 | + // Is the entity published? |
|
| 262 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | + $score ++ : |
|
| 264 | + array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 265 | + |
|
| 266 | + // Has a thumbnail? |
|
| 267 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | + $score ++ : |
|
| 269 | + array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 270 | + |
|
| 271 | + // Get all post meta keys for the current post |
|
| 272 | + global $wpdb; |
|
| 273 | + |
|
| 274 | + $query = $wpdb->prepare( |
|
| 275 | + "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + // Check intersection between available meta keys and expected ones |
|
| 279 | + // arrays to detect missing values. |
|
| 280 | + $available_meta_keys = $wpdb->get_col( $query ); |
|
| 281 | + |
|
| 282 | + // If each expected key is contained in available keys array ... |
|
| 283 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | + $score ++ : |
|
| 285 | + array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 286 | + |
|
| 287 | + $schema = $this->entity_type_service->get( $post_id ); |
|
| 288 | + |
|
| 289 | + $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | + array() : |
|
| 291 | + array_keys( $schema['custom_fields'] ); |
|
| 292 | + |
|
| 293 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 294 | + // If each expected key is contained in available keys array ... |
|
| 295 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | + $score ++ : |
|
| 297 | + array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 298 | + |
|
| 299 | + // Finally return score and warnings |
|
| 300 | + return array( |
|
| 301 | + 'raw_score' => $score, |
|
| 302 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 304 | + 'warnings' => $warnings, |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get as rating as input and convert in a traffic-light rating |
|
| 311 | + * |
|
| 312 | + * @since 3.3.0 |
|
| 313 | + * |
|
| 314 | + * @param int $score The rating score for a given entity. |
|
| 315 | + * |
|
| 316 | + * @return string The input HTML code. |
|
| 317 | + */ |
|
| 318 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
| 319 | + // RATING_MAX : $score = 3 : x |
|
| 320 | + // See http://php.net/manual/en/function.round.php |
|
| 321 | + $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 322 | + |
|
| 323 | + // If rating is 0, return 1, otherwise return rating |
|
| 324 | + return ( 0 == $rating ) ? 1 : $rating; |
|
| 325 | + |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Get as rating as input and convert in a traffic-light rating |
|
| 330 | + * |
|
| 331 | + * @since 3.3.0 |
|
| 332 | + * |
|
| 333 | + * @param int $score The rating score for a given entity. |
|
| 334 | + * |
|
| 335 | + * @return string The input HTML code. |
|
| 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 | + /** |
|
| 345 | + * Add admin notices for the current entity depending on the current rating. |
|
| 346 | + * |
|
| 347 | + * @since 3.3.0 |
|
| 348 | + */ |
|
| 349 | + public function in_admin_header() { |
|
| 350 | + |
|
| 351 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 352 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 353 | + return; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $screen = get_current_screen(); |
|
| 357 | + // If there is any valid screen nothing to do |
|
| 358 | + if ( null === $screen ) { |
|
| 359 | + return; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + // If you're not in the entity post edit page, return. |
|
| 363 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 364 | + return; |
|
| 365 | + } |
|
| 366 | + // Retrieve the current global post |
|
| 367 | + global $post; |
|
| 368 | + // If it's not an entity, return. |
|
| 369 | + if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 370 | + return; |
|
| 371 | + } |
|
| 372 | + // Retrieve an updated rating for the current entity |
|
| 373 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
| 374 | + |
|
| 375 | + // If there is at least 1 warning |
|
| 376 | + if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 377 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 378 | + $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | 383 | } |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
| 130 | 130 | * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
| 131 | 131 | */ |
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 132 | + public function __construct($entity_service, $entity_type_service, $notice_service) { |
|
| 133 | 133 | |
| 134 | 134 | $this->entity_service = $entity_service; |
| 135 | 135 | $this->entity_type_service = $entity_type_service; |
| 136 | 136 | $this->notice_service = $notice_service; |
| 137 | 137 | |
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rating_Service'); |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -148,19 +148,19 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return array An array representing the rating obj. |
| 150 | 150 | */ |
| 151 | - public function set_rating_for( $post_id ) { |
|
| 151 | + public function set_rating_for($post_id) { |
|
| 152 | 152 | |
| 153 | 153 | // Calculate rating for the given post. |
| 154 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 154 | + $rating = $this->calculate_rating_for($post_id); |
|
| 155 | 155 | |
| 156 | 156 | // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
| 157 | 157 | // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
| 158 | 158 | // |
| 159 | 159 | // See https://codex.wordpress.org/Function_Reference/update_post_meta |
| 160 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 160 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
| 161 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
| 162 | 162 | |
| 163 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 163 | + $this->log->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
| 164 | 164 | |
| 165 | 165 | // Finally returns the rating |
| 166 | 166 | return $rating; |
@@ -177,30 +177,30 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @return array An array representing the rating obj. |
| 179 | 179 | */ |
| 180 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 180 | + public function get_rating_for($post_id, $force_reload = false) { |
|
| 181 | 181 | |
| 182 | 182 | // If forced reload is required or rating is missing. |
| 183 | - if ( $force_reload ) { |
|
| 184 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 183 | + if ($force_reload) { |
|
| 184 | + $this->log->trace("Force rating reload [ post_id :: $post_id ]"); |
|
| 185 | 185 | |
| 186 | - return $this->set_rating_for( $post_id ); |
|
| 186 | + return $this->set_rating_for($post_id); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 189 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
| 190 | 190 | |
| 191 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 191 | + if ( ! is_numeric($current_raw_score)) { |
|
| 192 | + $this->log->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
| 193 | 193 | |
| 194 | - return $this->set_rating_for( $post_id ); |
|
| 194 | + return $this->set_rating_for($post_id); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 197 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
| 198 | 198 | |
| 199 | 199 | // Finally return score and warnings |
| 200 | 200 | return array( |
| 201 | 201 | 'raw_score' => $current_raw_score, |
| 202 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 202 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
| 203 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
| 204 | 204 | 'warnings' => $current_warnings, |
| 205 | 205 | ); |
| 206 | 206 | |
@@ -227,14 +227,14 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return array An array representing the rating obj. |
| 229 | 229 | */ |
| 230 | - private function calculate_rating_for( $post_id ) { |
|
| 230 | + private function calculate_rating_for($post_id) { |
|
| 231 | 231 | |
| 232 | 232 | // If it's not an entity, return. |
| 233 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 233 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 234 | 234 | return array(); |
| 235 | 235 | } |
| 236 | 236 | // Retrieve the post object. |
| 237 | - $post = get_post( $post_id ); |
|
| 237 | + $post = get_post($post_id); |
|
| 238 | 238 | |
| 239 | 239 | // Rating value. |
| 240 | 240 | $score = 0; |
@@ -243,30 +243,25 @@ discard block |
||
| 243 | 243 | $warnings = array(); |
| 244 | 244 | |
| 245 | 245 | // Is the current entity related to at least 1 post? |
| 246 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | - $score ++ : |
|
| 248 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 246 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
| 247 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift')); |
|
| 249 | 248 | |
| 250 | 249 | // Is the post content not empty? |
| 251 | - ( ! empty( $post->post_content ) ) ? |
|
| 252 | - $score ++ : |
|
| 253 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 250 | + ( ! empty($post->post_content)) ? |
|
| 251 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift')); |
|
| 254 | 252 | |
| 255 | 253 | // Is the current entity related to at least 1 entity? |
| 256 | 254 | // Was the current entity already disambiguated? |
| 257 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | - $score ++ : |
|
| 259 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 255 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
| 256 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift')); |
|
| 260 | 257 | |
| 261 | 258 | // Is the entity published? |
| 262 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | - $score ++ : |
|
| 264 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 259 | + ('publish' === get_post_status($post->ID)) ? |
|
| 260 | + $score++ : array_push($warnings, __(self::RATING_WARNING_IS_PUBLISHED, 'wordlift')); |
|
| 265 | 261 | |
| 266 | 262 | // Has a thumbnail? |
| 267 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | - $score ++ : |
|
| 269 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 263 | + (has_post_thumbnail($post->ID)) ? |
|
| 264 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift')); |
|
| 270 | 265 | |
| 271 | 266 | // Get all post meta keys for the current post |
| 272 | 267 | global $wpdb; |
@@ -277,30 +272,27 @@ discard block |
||
| 277 | 272 | |
| 278 | 273 | // Check intersection between available meta keys and expected ones |
| 279 | 274 | // arrays to detect missing values. |
| 280 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 275 | + $available_meta_keys = $wpdb->get_col($query); |
|
| 281 | 276 | |
| 282 | 277 | // If each expected key is contained in available keys array ... |
| 283 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | - $score ++ : |
|
| 285 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 278 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys)) ? |
|
| 279 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_SAME_AS, 'wordlift')); |
|
| 286 | 280 | |
| 287 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 281 | + $schema = $this->entity_type_service->get($post_id); |
|
| 288 | 282 | |
| 289 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | - array() : |
|
| 291 | - array_keys( $schema['custom_fields'] ); |
|
| 283 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
| 284 | + array() : array_keys($schema['custom_fields']); |
|
| 292 | 285 | |
| 293 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 286 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
| 294 | 287 | // If each expected key is contained in available keys array ... |
| 295 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | - $score ++ : |
|
| 297 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 288 | + (count($intersection) === count($expected_meta_keys)) ? |
|
| 289 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift')); |
|
| 298 | 290 | |
| 299 | 291 | // Finally return score and warnings |
| 300 | 292 | return array( |
| 301 | 293 | 'raw_score' => $score, |
| 302 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 294 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
| 295 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
| 304 | 296 | 'warnings' => $warnings, |
| 305 | 297 | ); |
| 306 | 298 | |
@@ -315,13 +307,13 @@ discard block |
||
| 315 | 307 | * |
| 316 | 308 | * @return string The input HTML code. |
| 317 | 309 | */ |
| 318 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 310 | + private function convert_raw_score_to_traffic_light($score) { |
|
| 319 | 311 | // RATING_MAX : $score = 3 : x |
| 320 | 312 | // See http://php.net/manual/en/function.round.php |
| 321 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 313 | + $rating = round(($score * 3) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 322 | 314 | |
| 323 | 315 | // If rating is 0, return 1, otherwise return rating |
| 324 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 316 | + return (0 == $rating) ? 1 : $rating; |
|
| 325 | 317 | |
| 326 | 318 | } |
| 327 | 319 | |
@@ -334,10 +326,10 @@ discard block |
||
| 334 | 326 | * |
| 335 | 327 | * @return string The input HTML code. |
| 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 | |
@@ -349,33 +341,33 @@ discard block |
||
| 349 | 341 | public function in_admin_header() { |
| 350 | 342 | |
| 351 | 343 | // Return safely if get_current_screen() is not defined (yet) |
| 352 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 344 | + if (false === function_exists('get_current_screen')) { |
|
| 353 | 345 | return; |
| 354 | 346 | } |
| 355 | 347 | |
| 356 | 348 | $screen = get_current_screen(); |
| 357 | 349 | // If there is any valid screen nothing to do |
| 358 | - if ( null === $screen ) { |
|
| 350 | + if (null === $screen) { |
|
| 359 | 351 | return; |
| 360 | 352 | } |
| 361 | 353 | |
| 362 | 354 | // If you're not in the entity post edit page, return. |
| 363 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 355 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->id) { |
|
| 364 | 356 | return; |
| 365 | 357 | } |
| 366 | 358 | // Retrieve the current global post |
| 367 | 359 | global $post; |
| 368 | 360 | // If it's not an entity, return. |
| 369 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 361 | + if ( ! $this->entity_service->is_entity($post->ID)) { |
|
| 370 | 362 | return; |
| 371 | 363 | } |
| 372 | 364 | // Retrieve an updated rating for the current entity |
| 373 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 365 | + $rating = $this->get_rating_for($post->ID, true); |
|
| 374 | 366 | |
| 375 | 367 | // If there is at least 1 warning |
| 376 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 368 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
| 377 | 369 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
| 378 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 370 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
| 379 | 371 | } |
| 380 | 372 | |
| 381 | 373 | } |
@@ -269,8 +269,8 @@ discard block |
||
| 269 | 269 | * If $predicate is omitted, $predicate filter is not applied |
| 270 | 270 | * Do not use it directly. Use wl_core_get_related_entities or wl_core_get_related_entity_ids instead. |
| 271 | 271 | * |
| 272 | - * @param $get |
|
| 273 | - * @param $item_id |
|
| 272 | + * @param string $get |
|
| 273 | + * @param integer $item_id |
|
| 274 | 274 | * @param string $predicate Name of the relation: null | 'what' | 'where' | 'when' | 'who' |
| 275 | 275 | * @param string $post_status Filter by post status null | 'publish' | 'draft' | 'pending' | 'trash'. null means *any* post status |
| 276 | 276 | * |
@@ -345,9 +345,10 @@ discard block |
||
| 345 | 345 | * If $predicate is omitted, $predicate filter is not applied |
| 346 | 346 | * Not use it directly. Use wl_core_get_related_posts or wl_core_get_related_posts_ids instead. |
| 347 | 347 | * |
| 348 | - * @param int $object_id The entity ID or the post ID. |
|
| 349 | 348 | * @param string $predicate Name of the relation: null | 'what' | 'where' | 'when' | 'who' |
| 350 | 349 | * @param string $post_status Filter by post status null | 'publish' | 'draft' | 'pending' | 'trash'. null means *any* post status |
| 350 | + * @param string $get |
|
| 351 | + * @param integer $item_id |
|
| 351 | 352 | * |
| 352 | 353 | * @return array Array of objects. |
| 353 | 354 | */ |
@@ -9,12 +9,12 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | function wl_core_check_relation_predicate_is_supported( $predicate ) { |
| 11 | 11 | |
| 12 | - return in_array( $predicate, array( |
|
| 13 | - WL_WHAT_RELATION, |
|
| 14 | - WL_WHEN_RELATION, |
|
| 15 | - WL_WHERE_RELATION, |
|
| 16 | - WL_WHO_RELATION, |
|
| 17 | - ) ); |
|
| 12 | + return in_array( $predicate, array( |
|
| 13 | + WL_WHAT_RELATION, |
|
| 14 | + WL_WHEN_RELATION, |
|
| 15 | + WL_WHERE_RELATION, |
|
| 16 | + WL_WHO_RELATION, |
|
| 17 | + ) ); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | function wl_core_get_validation_rules() { |
| 26 | 26 | |
| 27 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 27 | + return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -34,10 +34,10 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | function wl_core_get_relation_instances_table_name() { |
| 36 | 36 | |
| 37 | - global $wpdb; |
|
| 38 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 37 | + global $wpdb; |
|
| 38 | + $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 39 | 39 | |
| 40 | - return $table_name; |
|
| 40 | + return $table_name; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -52,33 +52,33 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | function wl_core_add_relation_instance( $subject_id, $predicate, $object_id ) { |
| 54 | 54 | |
| 55 | - // Checks on subject and object |
|
| 56 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Checks on the given relation |
|
| 61 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 62 | - return false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Prepare interaction with db |
|
| 66 | - global $wpdb; |
|
| 67 | - |
|
| 68 | - // Checks passed. Add relation if not exists: |
|
| 69 | - // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
| 70 | - $wpdb->replace( |
|
| 71 | - wl_core_get_relation_instances_table_name(), |
|
| 72 | - array( |
|
| 73 | - 'subject_id' => $subject_id, |
|
| 74 | - 'predicate' => $predicate, |
|
| 75 | - 'object_id' => $object_id, |
|
| 76 | - ), |
|
| 77 | - array( '%d', '%s', '%d' ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - // Return record id |
|
| 81 | - return $wpdb->insert_id; |
|
| 55 | + // Checks on subject and object |
|
| 56 | + if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Checks on the given relation |
|
| 61 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 62 | + return false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Prepare interaction with db |
|
| 66 | + global $wpdb; |
|
| 67 | + |
|
| 68 | + // Checks passed. Add relation if not exists: |
|
| 69 | + // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
| 70 | + $wpdb->replace( |
|
| 71 | + wl_core_get_relation_instances_table_name(), |
|
| 72 | + array( |
|
| 73 | + 'subject_id' => $subject_id, |
|
| 74 | + 'predicate' => $predicate, |
|
| 75 | + 'object_id' => $object_id, |
|
| 76 | + ), |
|
| 77 | + array( '%d', '%s', '%d' ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + // Return record id |
|
| 81 | + return $wpdb->insert_id; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -93,33 +93,33 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | function wl_core_delete_relation_instance( $subject_id, $predicate, $object_id ) { |
| 95 | 95 | |
| 96 | - // Checks on subject and object |
|
| 97 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Checks on the given relation |
|
| 102 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Prepare interaction with db |
|
| 107 | - global $wpdb; |
|
| 108 | - |
|
| 109 | - wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 110 | - |
|
| 111 | - // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 112 | - $wpdb->delete( |
|
| 113 | - wl_core_get_relation_instances_table_name(), |
|
| 114 | - array( |
|
| 115 | - 'subject_id' => $subject_id, |
|
| 116 | - 'predicate' => $predicate, |
|
| 117 | - 'object_id' => $object_id, |
|
| 118 | - ), |
|
| 119 | - array( '%d', '%s', '%d' ) |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - return true; |
|
| 96 | + // Checks on subject and object |
|
| 97 | + if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Checks on the given relation |
|
| 102 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Prepare interaction with db |
|
| 107 | + global $wpdb; |
|
| 108 | + |
|
| 109 | + wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 110 | + |
|
| 111 | + // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 112 | + $wpdb->delete( |
|
| 113 | + wl_core_get_relation_instances_table_name(), |
|
| 114 | + array( |
|
| 115 | + 'subject_id' => $subject_id, |
|
| 116 | + 'predicate' => $predicate, |
|
| 117 | + 'object_id' => $object_id, |
|
| 118 | + ), |
|
| 119 | + array( '%d', '%s', '%d' ) |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + return true; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -134,29 +134,29 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | function wl_core_add_relation_instances( $subject_id, $predicate, $object_ids ) { |
| 136 | 136 | |
| 137 | - // Checks on subject and object |
|
| 138 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Checks on the given relation |
|
| 143 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Check $object_ids is an array |
|
| 148 | - if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Call method to check and add each single relation |
|
| 153 | - $inserted_records_ids = array(); |
|
| 154 | - foreach ( $object_ids as $object_id ) { |
|
| 155 | - $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 156 | - $inserted_records_ids[] = $new_record_id; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $inserted_records_ids; |
|
| 137 | + // Checks on subject and object |
|
| 138 | + if ( ! is_numeric( $subject_id ) ) { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Checks on the given relation |
|
| 143 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Check $object_ids is an array |
|
| 148 | + if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 149 | + return false; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Call method to check and add each single relation |
|
| 153 | + $inserted_records_ids = array(); |
|
| 154 | + foreach ( $object_ids as $object_id ) { |
|
| 155 | + $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 156 | + $inserted_records_ids[] = $new_record_id; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $inserted_records_ids; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
@@ -169,26 +169,26 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | function wl_core_delete_relation_instances( $subject_id ) { |
| 171 | 171 | |
| 172 | - // Checks on subject and object |
|
| 173 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 172 | + // Checks on subject and object |
|
| 173 | + if ( ! is_numeric( $subject_id ) ) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - // Prepare interaction with db |
|
| 178 | - global $wpdb; |
|
| 177 | + // Prepare interaction with db |
|
| 178 | + global $wpdb; |
|
| 179 | 179 | |
| 180 | - // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
| 180 | + // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
| 181 | 181 | |
| 182 | - // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 183 | - $wpdb->delete( |
|
| 184 | - wl_core_get_relation_instances_table_name(), |
|
| 185 | - array( |
|
| 186 | - 'subject_id' => $subject_id, |
|
| 187 | - ), |
|
| 188 | - array( '%d' ) |
|
| 189 | - ); |
|
| 182 | + // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 183 | + $wpdb->delete( |
|
| 184 | + wl_core_get_relation_instances_table_name(), |
|
| 185 | + array( |
|
| 186 | + 'subject_id' => $subject_id, |
|
| 187 | + ), |
|
| 188 | + array( '%d' ) |
|
| 189 | + ); |
|
| 190 | 190 | |
| 191 | - return true; |
|
| 191 | + return true; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -204,18 +204,18 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | function wl_core_validate_filters_for_related( $filters ) { |
| 206 | 206 | |
| 207 | - if ( ! is_array( $filters ) ) { |
|
| 208 | - $filters = array(); |
|
| 209 | - } |
|
| 207 | + if ( ! is_array( $filters ) ) { |
|
| 208 | + $filters = array(); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - if ( ! isset( $filters['predicate'] ) ) { |
|
| 212 | - $filters['predicate'] = null; |
|
| 213 | - } |
|
| 214 | - if ( ! isset( $filters['status'] ) ) { |
|
| 215 | - $filters['status'] = null; |
|
| 216 | - } |
|
| 211 | + if ( ! isset( $filters['predicate'] ) ) { |
|
| 212 | + $filters['predicate'] = null; |
|
| 213 | + } |
|
| 214 | + if ( ! isset( $filters['status'] ) ) { |
|
| 215 | + $filters['status'] = null; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - return $filters; |
|
| 218 | + return $filters; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
| 238 | 238 | |
| 239 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 239 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 240 | 240 | |
| 241 | - return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 241 | + return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -259,9 +259,9 @@ discard block |
||
| 259 | 259 | */ |
| 260 | 260 | function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
| 261 | 261 | |
| 262 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 262 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 263 | 263 | |
| 264 | - return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 264 | + return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -278,20 +278,20 @@ discard block |
||
| 278 | 278 | */ |
| 279 | 279 | function wl_core_inner_get_related_entities( $get, $item_id, $predicate = null, $post_status = null ) { |
| 280 | 280 | |
| 281 | - if ( $results = wl_core_get_posts( array( |
|
| 282 | - 'get' => $get, |
|
| 283 | - 'post_type' => 'entity', |
|
| 284 | - 'post_status' => $post_status, |
|
| 285 | - 'related_to' => $item_id, |
|
| 286 | - 'as' => 'object', |
|
| 287 | - 'with_predicate' => $predicate, |
|
| 288 | - ) ) |
|
| 289 | - ) { |
|
| 290 | - return $results; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // If wl_core_get_posts return false then an empty array is returned |
|
| 294 | - return array(); |
|
| 281 | + if ( $results = wl_core_get_posts( array( |
|
| 282 | + 'get' => $get, |
|
| 283 | + 'post_type' => 'entity', |
|
| 284 | + 'post_status' => $post_status, |
|
| 285 | + 'related_to' => $item_id, |
|
| 286 | + 'as' => 'object', |
|
| 287 | + 'with_predicate' => $predicate, |
|
| 288 | + ) ) |
|
| 289 | + ) { |
|
| 290 | + return $results; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // If wl_core_get_posts return false then an empty array is returned |
|
| 294 | + return array(); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | */ |
| 313 | 313 | function wl_core_get_related_posts( $object_id, $filters = array() ) { |
| 314 | 314 | |
| 315 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 315 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 316 | 316 | |
| 317 | - return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 317 | + return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -335,9 +335,9 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
| 337 | 337 | |
| 338 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 338 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 339 | 339 | |
| 340 | - return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 340 | + return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | /** |
@@ -353,41 +353,41 @@ discard block |
||
| 353 | 353 | */ |
| 354 | 354 | function wl_core_inner_get_related_posts( $get, $item_id, $predicate = null, $post_status = null ) { |
| 355 | 355 | |
| 356 | - // Retrieve the post object |
|
| 357 | - $post = get_post( $item_id ); |
|
| 358 | - if ( null === $post ) { |
|
| 359 | - return array(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if ( "entity" === $post->post_type ) { |
|
| 363 | - if ( $results = wl_core_get_posts( array( |
|
| 364 | - 'get' => $get, |
|
| 365 | - 'post_type' => 'post', |
|
| 366 | - 'post_status' => $post_status, |
|
| 367 | - 'related_to' => $item_id, |
|
| 368 | - 'as' => 'subject', |
|
| 369 | - 'with_predicate' => $predicate, |
|
| 370 | - ) ) |
|
| 371 | - ) { |
|
| 372 | - return $results; |
|
| 373 | - } |
|
| 374 | - } else { |
|
| 375 | - if ( $results = wl_core_get_posts( array( |
|
| 376 | - 'get' => $get, |
|
| 377 | - 'post_type' => 'post', |
|
| 378 | - 'post_status' => $post_status, |
|
| 379 | - 'post__not_in' => array( $item_id ), |
|
| 380 | - 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 381 | - 'as' => 'subject', |
|
| 382 | - 'with_predicate' => $predicate, |
|
| 383 | - ) ) |
|
| 384 | - ) { |
|
| 385 | - return $results; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - // If wl_core_get_posts return false then an empty array is returned |
|
| 390 | - return array(); |
|
| 356 | + // Retrieve the post object |
|
| 357 | + $post = get_post( $item_id ); |
|
| 358 | + if ( null === $post ) { |
|
| 359 | + return array(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if ( "entity" === $post->post_type ) { |
|
| 363 | + if ( $results = wl_core_get_posts( array( |
|
| 364 | + 'get' => $get, |
|
| 365 | + 'post_type' => 'post', |
|
| 366 | + 'post_status' => $post_status, |
|
| 367 | + 'related_to' => $item_id, |
|
| 368 | + 'as' => 'subject', |
|
| 369 | + 'with_predicate' => $predicate, |
|
| 370 | + ) ) |
|
| 371 | + ) { |
|
| 372 | + return $results; |
|
| 373 | + } |
|
| 374 | + } else { |
|
| 375 | + if ( $results = wl_core_get_posts( array( |
|
| 376 | + 'get' => $get, |
|
| 377 | + 'post_type' => 'post', |
|
| 378 | + 'post_status' => $post_status, |
|
| 379 | + 'post__not_in' => array( $item_id ), |
|
| 380 | + 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 381 | + 'as' => 'subject', |
|
| 382 | + 'with_predicate' => $predicate, |
|
| 383 | + ) ) |
|
| 384 | + ) { |
|
| 385 | + return $results; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + // If wl_core_get_posts return false then an empty array is returned |
|
| 390 | + return array(); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -417,75 +417,75 @@ discard block |
||
| 417 | 417 | */ |
| 418 | 418 | function wl_core_sql_query_builder( $args ) { |
| 419 | 419 | |
| 420 | - // Prepare interaction with db |
|
| 421 | - global $wpdb; |
|
| 422 | - // Retrieve Wordlift relation instances table name |
|
| 423 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 424 | - |
|
| 425 | - // Sql Action |
|
| 426 | - $sql = "SELECT "; |
|
| 427 | - // Determine what has to be returned depending on 'get' argument value |
|
| 428 | - switch ( $args['get'] ) { |
|
| 429 | - case 'posts': |
|
| 430 | - $sql .= "p.*"; |
|
| 431 | - break; |
|
| 432 | - case 'post_ids': |
|
| 433 | - $sql .= "p.id"; |
|
| 434 | - break; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
| 438 | - $join_column = $args['as'] . "_id"; |
|
| 439 | - |
|
| 440 | - $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
| 441 | - |
|
| 442 | - // Sql add post type filter |
|
| 443 | - $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 444 | - |
|
| 445 | - // Sql add post status filter |
|
| 446 | - if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | - $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - // Add filtering conditions |
|
| 451 | - // If we look for posts related as objects this means that |
|
| 452 | - // related_to is a reference for a subject: subject_id is the filtering column |
|
| 453 | - // If we look for posts related as subject this means that |
|
| 454 | - // related_to is reference for an object: object_id is the filtering column |
|
| 455 | - |
|
| 456 | - $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 457 | - |
|
| 458 | - if ( isset( $args['related_to'] ) ) { |
|
| 459 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 460 | - } |
|
| 461 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 462 | - $sql .= " AND"; |
|
| 463 | - } |
|
| 464 | - if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | - $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 466 | - } |
|
| 467 | - if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | - $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 469 | - } |
|
| 470 | - if ( isset( $args['post__in'] ) ) { |
|
| 471 | - $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 472 | - } |
|
| 473 | - // Add predicate filter if required |
|
| 474 | - if ( isset( $args['with_predicate'] ) ) { |
|
| 475 | - // Sql Inner Join clausole |
|
| 476 | - $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 477 | - } |
|
| 478 | - // Add a group by clousole to avoid duplicated rows |
|
| 479 | - $sql .= " GROUP BY p.id"; |
|
| 480 | - |
|
| 481 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 482 | - // Sql Inner Join clausole |
|
| 483 | - $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 484 | - } |
|
| 485 | - // Close sql statement |
|
| 486 | - $sql .= ";"; |
|
| 487 | - |
|
| 488 | - return $sql; |
|
| 420 | + // Prepare interaction with db |
|
| 421 | + global $wpdb; |
|
| 422 | + // Retrieve Wordlift relation instances table name |
|
| 423 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 424 | + |
|
| 425 | + // Sql Action |
|
| 426 | + $sql = "SELECT "; |
|
| 427 | + // Determine what has to be returned depending on 'get' argument value |
|
| 428 | + switch ( $args['get'] ) { |
|
| 429 | + case 'posts': |
|
| 430 | + $sql .= "p.*"; |
|
| 431 | + break; |
|
| 432 | + case 'post_ids': |
|
| 433 | + $sql .= "p.id"; |
|
| 434 | + break; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
| 438 | + $join_column = $args['as'] . "_id"; |
|
| 439 | + |
|
| 440 | + $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
| 441 | + |
|
| 442 | + // Sql add post type filter |
|
| 443 | + $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 444 | + |
|
| 445 | + // Sql add post status filter |
|
| 446 | + if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | + $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + // Add filtering conditions |
|
| 451 | + // If we look for posts related as objects this means that |
|
| 452 | + // related_to is a reference for a subject: subject_id is the filtering column |
|
| 453 | + // If we look for posts related as subject this means that |
|
| 454 | + // related_to is reference for an object: object_id is the filtering column |
|
| 455 | + |
|
| 456 | + $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 457 | + |
|
| 458 | + if ( isset( $args['related_to'] ) ) { |
|
| 459 | + $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 460 | + } |
|
| 461 | + if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 462 | + $sql .= " AND"; |
|
| 463 | + } |
|
| 464 | + if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | + $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 466 | + } |
|
| 467 | + if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | + $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 469 | + } |
|
| 470 | + if ( isset( $args['post__in'] ) ) { |
|
| 471 | + $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 472 | + } |
|
| 473 | + // Add predicate filter if required |
|
| 474 | + if ( isset( $args['with_predicate'] ) ) { |
|
| 475 | + // Sql Inner Join clausole |
|
| 476 | + $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 477 | + } |
|
| 478 | + // Add a group by clousole to avoid duplicated rows |
|
| 479 | + $sql .= " GROUP BY p.id"; |
|
| 480 | + |
|
| 481 | + if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 482 | + // Sql Inner Join clausole |
|
| 483 | + $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 484 | + } |
|
| 485 | + // Close sql statement |
|
| 486 | + $sql .= ";"; |
|
| 487 | + |
|
| 488 | + return $sql; |
|
| 489 | 489 | |
| 490 | 490 | } |
| 491 | 491 | |
@@ -501,72 +501,72 @@ discard block |
||
| 501 | 501 | */ |
| 502 | 502 | function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
| 503 | 503 | |
| 504 | - // Merge given args with defaults args value |
|
| 505 | - $args = array_merge( array( |
|
| 506 | - 'with_predicate' => null, |
|
| 507 | - 'as' => 'subject', |
|
| 508 | - 'post_type' => 'post', |
|
| 509 | - 'get' => 'posts', |
|
| 510 | - 'post_status' => null, |
|
| 511 | - ), $args ); |
|
| 512 | - |
|
| 513 | - // Arguments validation rules |
|
| 514 | - // At least one between related_to and related_to__in has to be set |
|
| 515 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 518 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 519 | - return false; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - // The same check is applied to post_in, post__not_in and related_to__in options |
|
| 523 | - // Only arrays with at least one numeric value are considerad valid |
|
| 524 | - // The argument value is further sanitized in order to clean up not numeric values |
|
| 525 | - foreach ( |
|
| 526 | - array( |
|
| 527 | - 'post__in', |
|
| 528 | - 'post__not_in', |
|
| 529 | - 'related_to__in', |
|
| 530 | - ) as $option_name |
|
| 531 | - ) { |
|
| 532 | - if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | - if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 534 | - return false; |
|
| 535 | - } |
|
| 536 | - // Sanitize value removing non numeric values from the array |
|
| 537 | - $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - // Performing validation rules |
|
| 541 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | - if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | - if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 544 | - return false; |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - // Prepare interaction with db |
|
| 550 | - global $wpdb; |
|
| 551 | - // Build sql statement with given arguments |
|
| 552 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 553 | - |
|
| 554 | - // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
|
| 555 | - // $results = array(); |
|
| 556 | - |
|
| 557 | - // If ids are required, returns a one-dimensional array containing ids. |
|
| 558 | - // Otherwise an array of associative arrays representing the post | relation object |
|
| 559 | - if ( 'post_ids' == $args['get'] ) { |
|
| 560 | - # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
| 561 | - $results = $wpdb->get_col( $sql_statement ); |
|
| 562 | - } else { |
|
| 563 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 564 | - } |
|
| 565 | - // If there were an error performing the query then false is returned |
|
| 566 | - if ( ! empty( $wpdb->last_error ) ) { |
|
| 567 | - return false; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - // Finally |
|
| 571 | - return $results; |
|
| 504 | + // Merge given args with defaults args value |
|
| 505 | + $args = array_merge( array( |
|
| 506 | + 'with_predicate' => null, |
|
| 507 | + 'as' => 'subject', |
|
| 508 | + 'post_type' => 'post', |
|
| 509 | + 'get' => 'posts', |
|
| 510 | + 'post_status' => null, |
|
| 511 | + ), $args ); |
|
| 512 | + |
|
| 513 | + // Arguments validation rules |
|
| 514 | + // At least one between related_to and related_to__in has to be set |
|
| 515 | + if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | + if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 519 | + return false; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + // The same check is applied to post_in, post__not_in and related_to__in options |
|
| 523 | + // Only arrays with at least one numeric value are considerad valid |
|
| 524 | + // The argument value is further sanitized in order to clean up not numeric values |
|
| 525 | + foreach ( |
|
| 526 | + array( |
|
| 527 | + 'post__in', |
|
| 528 | + 'post__not_in', |
|
| 529 | + 'related_to__in', |
|
| 530 | + ) as $option_name |
|
| 531 | + ) { |
|
| 532 | + if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | + if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 534 | + return false; |
|
| 535 | + } |
|
| 536 | + // Sanitize value removing non numeric values from the array |
|
| 537 | + $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + // Performing validation rules |
|
| 541 | + foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | + if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | + if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 544 | + return false; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + // Prepare interaction with db |
|
| 550 | + global $wpdb; |
|
| 551 | + // Build sql statement with given arguments |
|
| 552 | + $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 553 | + |
|
| 554 | + // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
|
| 555 | + // $results = array(); |
|
| 556 | + |
|
| 557 | + // If ids are required, returns a one-dimensional array containing ids. |
|
| 558 | + // Otherwise an array of associative arrays representing the post | relation object |
|
| 559 | + if ( 'post_ids' == $args['get'] ) { |
|
| 560 | + # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
| 561 | + $results = $wpdb->get_col( $sql_statement ); |
|
| 562 | + } else { |
|
| 563 | + $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 564 | + } |
|
| 565 | + // If there were an error performing the query then false is returned |
|
| 566 | + if ( ! empty( $wpdb->last_error ) ) { |
|
| 567 | + return false; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + // Finally |
|
| 571 | + return $results; |
|
| 572 | 572 | } |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @return boolean Return true if supported, false otherwise |
| 9 | 9 | */ |
| 10 | -function wl_core_check_relation_predicate_is_supported( $predicate ) { |
|
| 10 | +function wl_core_check_relation_predicate_is_supported($predicate) { |
|
| 11 | 11 | |
| 12 | - return in_array( $predicate, array( |
|
| 12 | + return in_array($predicate, array( |
|
| 13 | 13 | WL_WHAT_RELATION, |
| 14 | 14 | WL_WHEN_RELATION, |
| 15 | 15 | WL_WHERE_RELATION, |
| 16 | 16 | WL_WHO_RELATION, |
| 17 | - ) ); |
|
| 17 | + )); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | function wl_core_get_validation_rules() { |
| 26 | 26 | |
| 27 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 27 | + return unserialize(WL_CORE_GET_POSTS_VALIDATION_RULES); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | function wl_core_get_relation_instances_table_name() { |
| 36 | 36 | |
| 37 | 37 | global $wpdb; |
| 38 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 38 | + $table_name = $wpdb->prefix.WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 39 | 39 | |
| 40 | 40 | return $table_name; |
| 41 | 41 | } |
@@ -50,15 +50,15 @@ discard block |
||
| 50 | 50 | * @uses $wpdb->replace() to perform the query |
| 51 | 51 | * @return (integer|boolean) Return then relation instance ID or false |
| 52 | 52 | */ |
| 53 | -function wl_core_add_relation_instance( $subject_id, $predicate, $object_id ) { |
|
| 53 | +function wl_core_add_relation_instance($subject_id, $predicate, $object_id) { |
|
| 54 | 54 | |
| 55 | 55 | // Checks on subject and object |
| 56 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 56 | + if ( ! is_numeric($subject_id) || ! is_numeric($object_id)) { |
|
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Checks on the given relation |
| 61 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 61 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 62 | 62 | return false; |
| 63 | 63 | } |
| 64 | 64 | |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | 'predicate' => $predicate, |
| 75 | 75 | 'object_id' => $object_id, |
| 76 | 76 | ), |
| 77 | - array( '%d', '%s', '%d' ) |
|
| 77 | + array('%d', '%s', '%d') |
|
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | // Return record id |
@@ -91,22 +91,22 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return (boolean) False for failure. True for success. |
| 93 | 93 | */ |
| 94 | -function wl_core_delete_relation_instance( $subject_id, $predicate, $object_id ) { |
|
| 94 | +function wl_core_delete_relation_instance($subject_id, $predicate, $object_id) { |
|
| 95 | 95 | |
| 96 | 96 | // Checks on subject and object |
| 97 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 97 | + if ( ! is_numeric($subject_id) || ! is_numeric($object_id)) { |
|
| 98 | 98 | return false; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Checks on the given relation |
| 102 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 102 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Prepare interaction with db |
| 107 | 107 | global $wpdb; |
| 108 | 108 | |
| 109 | - wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 109 | + wl_write_log("Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]"); |
|
| 110 | 110 | |
| 111 | 111 | // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
| 112 | 112 | $wpdb->delete( |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | 'predicate' => $predicate, |
| 117 | 117 | 'object_id' => $object_id, |
| 118 | 118 | ), |
| 119 | - array( '%d', '%s', '%d' ) |
|
| 119 | + array('%d', '%s', '%d') |
|
| 120 | 120 | ); |
| 121 | 121 | |
| 122 | 122 | return true; |
@@ -132,27 +132,27 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * @return (integer|boolean) Return the relation instances IDs or false |
| 134 | 134 | */ |
| 135 | -function wl_core_add_relation_instances( $subject_id, $predicate, $object_ids ) { |
|
| 135 | +function wl_core_add_relation_instances($subject_id, $predicate, $object_ids) { |
|
| 136 | 136 | |
| 137 | 137 | // Checks on subject and object |
| 138 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 138 | + if ( ! is_numeric($subject_id)) { |
|
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | // Checks on the given relation |
| 143 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 143 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 144 | 144 | return false; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | // Check $object_ids is an array |
| 148 | - if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 148 | + if ( ! is_array($object_ids) || empty($object_ids)) { |
|
| 149 | 149 | return false; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Call method to check and add each single relation |
| 153 | 153 | $inserted_records_ids = array(); |
| 154 | - foreach ( $object_ids as $object_id ) { |
|
| 155 | - $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 154 | + foreach ($object_ids as $object_id) { |
|
| 155 | + $new_record_id = wl_core_add_relation_instance($subject_id, $predicate, $object_id); |
|
| 156 | 156 | $inserted_records_ids[] = $new_record_id; |
| 157 | 157 | } |
| 158 | 158 | |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | * |
| 168 | 168 | * @return (boolean) False for failure. True for success. |
| 169 | 169 | */ |
| 170 | -function wl_core_delete_relation_instances( $subject_id ) { |
|
| 170 | +function wl_core_delete_relation_instances($subject_id) { |
|
| 171 | 171 | |
| 172 | 172 | // Checks on subject and object |
| 173 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 173 | + if ( ! is_numeric($subject_id)) { |
|
| 174 | 174 | return false; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | array( |
| 186 | 186 | 'subject_id' => $subject_id, |
| 187 | 187 | ), |
| 188 | - array( '%d' ) |
|
| 188 | + array('%d') |
|
| 189 | 189 | ); |
| 190 | 190 | |
| 191 | 191 | return true; |
@@ -202,16 +202,16 @@ discard block |
||
| 202 | 202 | * 'status' => null |
| 203 | 203 | * ); |
| 204 | 204 | */ |
| 205 | -function wl_core_validate_filters_for_related( $filters ) { |
|
| 205 | +function wl_core_validate_filters_for_related($filters) { |
|
| 206 | 206 | |
| 207 | - if ( ! is_array( $filters ) ) { |
|
| 207 | + if ( ! is_array($filters)) { |
|
| 208 | 208 | $filters = array(); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - if ( ! isset( $filters['predicate'] ) ) { |
|
| 211 | + if ( ! isset($filters['predicate'])) { |
|
| 212 | 212 | $filters['predicate'] = null; |
| 213 | 213 | } |
| 214 | - if ( ! isset( $filters['status'] ) ) { |
|
| 214 | + if ( ! isset($filters['status'])) { |
|
| 215 | 215 | $filters['status'] = null; |
| 216 | 216 | } |
| 217 | 217 | |
@@ -234,11 +234,11 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return array Array of post entity objects. |
| 236 | 236 | */ |
| 237 | -function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
|
| 237 | +function wl_core_get_related_entities($subject_id, $filters = array()) { |
|
| 238 | 238 | |
| 239 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 239 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 240 | 240 | |
| 241 | - return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 241 | + return wl_core_inner_get_related_entities("posts", $subject_id, $filters['predicate'], $filters['status']); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -257,11 +257,11 @@ discard block |
||
| 257 | 257 | * |
| 258 | 258 | * @return array Array of post entity objects. |
| 259 | 259 | */ |
| 260 | -function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
|
| 260 | +function wl_core_get_related_entity_ids($subject_id, $filters = array()) { |
|
| 261 | 261 | |
| 262 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 262 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 263 | 263 | |
| 264 | - return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 264 | + return wl_core_inner_get_related_entities("post_ids", $subject_id, $filters['predicate'], $filters['status']); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -276,16 +276,16 @@ discard block |
||
| 276 | 276 | * |
| 277 | 277 | * @return array Array of ids. |
| 278 | 278 | */ |
| 279 | -function wl_core_inner_get_related_entities( $get, $item_id, $predicate = null, $post_status = null ) { |
|
| 279 | +function wl_core_inner_get_related_entities($get, $item_id, $predicate = null, $post_status = null) { |
|
| 280 | 280 | |
| 281 | - if ( $results = wl_core_get_posts( array( |
|
| 281 | + if ($results = wl_core_get_posts(array( |
|
| 282 | 282 | 'get' => $get, |
| 283 | 283 | 'post_type' => 'entity', |
| 284 | 284 | 'post_status' => $post_status, |
| 285 | 285 | 'related_to' => $item_id, |
| 286 | 286 | 'as' => 'object', |
| 287 | 287 | 'with_predicate' => $predicate, |
| 288 | - ) ) |
|
| 288 | + )) |
|
| 289 | 289 | ) { |
| 290 | 290 | return $results; |
| 291 | 291 | } |
@@ -310,11 +310,11 @@ discard block |
||
| 310 | 310 | * |
| 311 | 311 | * @return array Array of objects. |
| 312 | 312 | */ |
| 313 | -function wl_core_get_related_posts( $object_id, $filters = array() ) { |
|
| 313 | +function wl_core_get_related_posts($object_id, $filters = array()) { |
|
| 314 | 314 | |
| 315 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 315 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 316 | 316 | |
| 317 | - return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 317 | + return wl_core_inner_get_related_posts("posts", $object_id, $filters['predicate'], $filters['status']); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -333,11 +333,11 @@ discard block |
||
| 333 | 333 | * |
| 334 | 334 | * @return array Array of objects. |
| 335 | 335 | */ |
| 336 | -function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
|
| 336 | +function wl_core_get_related_post_ids($object_id, $filters = array()) { |
|
| 337 | 337 | |
| 338 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 338 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 339 | 339 | |
| 340 | - return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 340 | + return wl_core_inner_get_related_posts("post_ids", $object_id, $filters['predicate'], $filters['status']); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | /** |
@@ -351,36 +351,36 @@ discard block |
||
| 351 | 351 | * |
| 352 | 352 | * @return array Array of objects. |
| 353 | 353 | */ |
| 354 | -function wl_core_inner_get_related_posts( $get, $item_id, $predicate = null, $post_status = null ) { |
|
| 354 | +function wl_core_inner_get_related_posts($get, $item_id, $predicate = null, $post_status = null) { |
|
| 355 | 355 | |
| 356 | 356 | // Retrieve the post object |
| 357 | - $post = get_post( $item_id ); |
|
| 358 | - if ( null === $post ) { |
|
| 357 | + $post = get_post($item_id); |
|
| 358 | + if (null === $post) { |
|
| 359 | 359 | return array(); |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | - if ( "entity" === $post->post_type ) { |
|
| 363 | - if ( $results = wl_core_get_posts( array( |
|
| 362 | + if ("entity" === $post->post_type) { |
|
| 363 | + if ($results = wl_core_get_posts(array( |
|
| 364 | 364 | 'get' => $get, |
| 365 | 365 | 'post_type' => 'post', |
| 366 | 366 | 'post_status' => $post_status, |
| 367 | 367 | 'related_to' => $item_id, |
| 368 | 368 | 'as' => 'subject', |
| 369 | 369 | 'with_predicate' => $predicate, |
| 370 | - ) ) |
|
| 370 | + )) |
|
| 371 | 371 | ) { |
| 372 | 372 | return $results; |
| 373 | 373 | } |
| 374 | 374 | } else { |
| 375 | - if ( $results = wl_core_get_posts( array( |
|
| 375 | + if ($results = wl_core_get_posts(array( |
|
| 376 | 376 | 'get' => $get, |
| 377 | 377 | 'post_type' => 'post', |
| 378 | 378 | 'post_status' => $post_status, |
| 379 | - 'post__not_in' => array( $item_id ), |
|
| 380 | - 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 379 | + 'post__not_in' => array($item_id), |
|
| 380 | + 'related_to__in' => wl_core_get_related_entity_ids($post->ID), |
|
| 381 | 381 | 'as' => 'subject', |
| 382 | 382 | 'with_predicate' => $predicate, |
| 383 | - ) ) |
|
| 383 | + )) |
|
| 384 | 384 | ) { |
| 385 | 385 | return $results; |
| 386 | 386 | } |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | * |
| 416 | 416 | * @return string | false String representing a sql statement, or false in case of error |
| 417 | 417 | */ |
| 418 | -function wl_core_sql_query_builder( $args ) { |
|
| 418 | +function wl_core_sql_query_builder($args) { |
|
| 419 | 419 | |
| 420 | 420 | // Prepare interaction with db |
| 421 | 421 | global $wpdb; |
@@ -425,7 +425,7 @@ discard block |
||
| 425 | 425 | // Sql Action |
| 426 | 426 | $sql = "SELECT "; |
| 427 | 427 | // Determine what has to be returned depending on 'get' argument value |
| 428 | - switch ( $args['get'] ) { |
|
| 428 | + switch ($args['get']) { |
|
| 429 | 429 | case 'posts': |
| 430 | 430 | $sql .= "p.*"; |
| 431 | 431 | break; |
@@ -435,16 +435,16 @@ discard block |
||
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
| 438 | - $join_column = $args['as'] . "_id"; |
|
| 438 | + $join_column = $args['as']."_id"; |
|
| 439 | 439 | |
| 440 | 440 | $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
| 441 | 441 | |
| 442 | 442 | // Sql add post type filter |
| 443 | - $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 443 | + $sql .= $wpdb->prepare(" AND p.post_type = %s AND", $args['post_type']); |
|
| 444 | 444 | |
| 445 | 445 | // Sql add post status filter |
| 446 | - if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | - $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 446 | + if (isset($args['post_status']) && ! is_null($args['post_status'])) { |
|
| 447 | + $sql .= $wpdb->prepare(" p.post_status = %s AND", $args['post_status']); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | // Add filtering conditions |
@@ -453,34 +453,34 @@ discard block |
||
| 453 | 453 | // If we look for posts related as subject this means that |
| 454 | 454 | // related_to is reference for an object: object_id is the filtering column |
| 455 | 455 | |
| 456 | - $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 456 | + $filtering_column = ('object' == $args['as']) ? "subject_id" : "object_id"; |
|
| 457 | 457 | |
| 458 | - if ( isset( $args['related_to'] ) ) { |
|
| 459 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 458 | + if (isset($args['related_to'])) { |
|
| 459 | + $sql .= $wpdb->prepare(" r.$filtering_column = %d", $args['related_to']); |
|
| 460 | 460 | } |
| 461 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 461 | + if (isset($args['related_to']) && isset($args['related_to__in'])) { |
|
| 462 | 462 | $sql .= " AND"; |
| 463 | 463 | } |
| 464 | - if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | - $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 464 | + if (isset($args['related_to__in'])) { |
|
| 465 | + $sql .= " r.$filtering_column IN (".implode(",", $args['related_to__in']).")"; |
|
| 466 | 466 | } |
| 467 | - if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | - $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 467 | + if (isset($args['post__not_in'])) { |
|
| 468 | + $sql .= " AND r.".$args['as']."_id NOT IN (".implode(",", $args['post__not_in']).")"; |
|
| 469 | 469 | } |
| 470 | - if ( isset( $args['post__in'] ) ) { |
|
| 471 | - $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 470 | + if (isset($args['post__in'])) { |
|
| 471 | + $sql .= " AND r.".$args['as']."_id IN (".implode(",", $args['post__in']).")"; |
|
| 472 | 472 | } |
| 473 | 473 | // Add predicate filter if required |
| 474 | - if ( isset( $args['with_predicate'] ) ) { |
|
| 474 | + if (isset($args['with_predicate'])) { |
|
| 475 | 475 | // Sql Inner Join clausole |
| 476 | - $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 476 | + $sql .= $wpdb->prepare(" AND r.predicate = %s", $args['with_predicate']); |
|
| 477 | 477 | } |
| 478 | 478 | // Add a group by clousole to avoid duplicated rows |
| 479 | 479 | $sql .= " GROUP BY p.id"; |
| 480 | 480 | |
| 481 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 481 | + if (isset($args['first']) && is_numeric($args['first'])) { |
|
| 482 | 482 | // Sql Inner Join clausole |
| 483 | - $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 483 | + $sql .= $wpdb->prepare(" LIMIT %d", $args['first']); |
|
| 484 | 484 | } |
| 485 | 485 | // Close sql statement |
| 486 | 486 | $sql .= ";"; |
@@ -499,23 +499,23 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return array|false List of WP_Post objects or list of WP_Post ids. False in case of error or invalid params |
| 501 | 501 | */ |
| 502 | -function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
|
| 502 | +function wl_core_get_posts($args, $returned_type = OBJECT) { |
|
| 503 | 503 | |
| 504 | 504 | // Merge given args with defaults args value |
| 505 | - $args = array_merge( array( |
|
| 505 | + $args = array_merge(array( |
|
| 506 | 506 | 'with_predicate' => null, |
| 507 | 507 | 'as' => 'subject', |
| 508 | 508 | 'post_type' => 'post', |
| 509 | 509 | 'get' => 'posts', |
| 510 | 510 | 'post_status' => null, |
| 511 | - ), $args ); |
|
| 511 | + ), $args); |
|
| 512 | 512 | |
| 513 | 513 | // Arguments validation rules |
| 514 | 514 | // At least one between related_to and related_to__in has to be set |
| 515 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 515 | + if ( ! isset($args['related_to']) && ! isset($args['related_to__in'])) { |
|
| 516 | 516 | return false; |
| 517 | 517 | } |
| 518 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 518 | + if (isset($args['related_to']) && ! is_numeric($args['related_to'])) { |
|
| 519 | 519 | return false; |
| 520 | 520 | } |
| 521 | 521 | |
@@ -529,18 +529,18 @@ discard block |
||
| 529 | 529 | 'related_to__in', |
| 530 | 530 | ) as $option_name |
| 531 | 531 | ) { |
| 532 | - if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | - if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 532 | + if (isset($args[$option_name])) { |
|
| 533 | + if ( ! is_array($args[$option_name]) || 0 == count(array_filter($args[$option_name], "is_numeric"))) { |
|
| 534 | 534 | return false; |
| 535 | 535 | } |
| 536 | 536 | // Sanitize value removing non numeric values from the array |
| 537 | - $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 537 | + $args[$option_name] = array_filter($args[$option_name], "is_numeric"); |
|
| 538 | 538 | } |
| 539 | 539 | } |
| 540 | 540 | // Performing validation rules |
| 541 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | - if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | - if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 541 | + foreach (wl_core_get_validation_rules() as $option_name => $accepeted_values) { |
|
| 542 | + if (isset($args[$option_name]) && ! is_null($args[$option_name])) { |
|
| 543 | + if ( ! in_array($args[$option_name], $accepeted_values)) { |
|
| 544 | 544 | return false; |
| 545 | 545 | } |
| 546 | 546 | } |
@@ -549,21 +549,21 @@ discard block |
||
| 549 | 549 | // Prepare interaction with db |
| 550 | 550 | global $wpdb; |
| 551 | 551 | // Build sql statement with given arguments |
| 552 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 552 | + $sql_statement = wl_core_sql_query_builder($args); |
|
| 553 | 553 | |
| 554 | 554 | // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
| 555 | 555 | // $results = array(); |
| 556 | 556 | |
| 557 | 557 | // If ids are required, returns a one-dimensional array containing ids. |
| 558 | 558 | // Otherwise an array of associative arrays representing the post | relation object |
| 559 | - if ( 'post_ids' == $args['get'] ) { |
|
| 559 | + if ('post_ids' == $args['get']) { |
|
| 560 | 560 | # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
| 561 | - $results = $wpdb->get_col( $sql_statement ); |
|
| 561 | + $results = $wpdb->get_col($sql_statement); |
|
| 562 | 562 | } else { |
| 563 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 563 | + $results = $wpdb->get_results($sql_statement, $returned_type); |
|
| 564 | 564 | } |
| 565 | 565 | // If there were an error performing the query then false is returned |
| 566 | - if ( ! empty( $wpdb->last_error ) ) { |
|
| 566 | + if ( ! empty($wpdb->last_error)) { |
|
| 567 | 567 | return false; |
| 568 | 568 | } |
| 569 | 569 | |
@@ -9,8 +9,8 @@ |
||
| 9 | 9 | ?> |
| 10 | 10 | |
| 11 | 11 | <div class="wrap"> |
| 12 | - <h1><?php esc_html_e( 'Status Report', 'wordlift' ); ?></h1> |
|
| 12 | + <h1><?php esc_html_e('Status Report', 'wordlift'); ?></h1> |
|
| 13 | 13 | |
| 14 | - <p><?php echo sprintf( esc_html__( '%d branch(es) and %d revision(s) deleted.', 'wordlift' ), $branches, $revisions ); ?></p> |
|
| 14 | + <p><?php echo sprintf(esc_html__('%d branch(es) and %d revision(s) deleted.', 'wordlift'), $branches, $revisions); ?></p> |
|
| 15 | 15 | |
| 16 | 16 | </div> |
| 17 | 17 | \ No newline at end of file |
@@ -16,69 +16,69 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Admin_Download_Your_Data_Page { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.9.8 |
|
| 23 | - * |
|
| 24 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | - */ |
|
| 26 | - function __construct( $configuration_service ) { |
|
| 27 | - |
|
| 28 | - $this->configuration_service = $configuration_service; |
|
| 29 | - |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Hook to 'admin_menu' to add the 'Download Your Data' page. |
|
| 34 | - * |
|
| 35 | - * @since 3.6.0 |
|
| 36 | - */ |
|
| 37 | - public function admin_menu() { |
|
| 38 | - |
|
| 39 | - // Add a callback to our 'page' function. |
|
| 40 | - add_submenu_page( |
|
| 41 | - 'wl_admin_menu', |
|
| 42 | - _x( 'Download Your Data', 'Page title', 'wordlift' ), |
|
| 43 | - _x( 'Download Your Data', 'Menu title', 'wordlift' ), |
|
| 44 | - 'manage_options', |
|
| 45 | - 'wl_download_your_data', |
|
| 46 | - array( $this, 'page', ) |
|
| 47 | - ); |
|
| 48 | - |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The admin menu callback to render the page. |
|
| 53 | - * |
|
| 54 | - * @since 3.6.0 |
|
| 55 | - */ |
|
| 56 | - public function page() { |
|
| 57 | - |
|
| 58 | - // Include the partial. |
|
| 59 | - include( plugin_dir_url( __FILE__ ) . 'partials/wordlift-admin-download-your-data.php' ); |
|
| 60 | - |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Ajax call to redirect to a download URL. |
|
| 65 | - * |
|
| 66 | - * @since 3.6.0 |
|
| 67 | - */ |
|
| 68 | - public function download_your_data() { |
|
| 69 | - |
|
| 70 | - ob_end_clean(); |
|
| 71 | - |
|
| 72 | - // Get WL's key. |
|
| 73 | - $key = $this->configuration_service->get_key(); |
|
| 74 | - |
|
| 75 | - // Get the suffix or use json by default. |
|
| 76 | - $suffix = $_GET['out'] ?: 'json'; |
|
| 77 | - |
|
| 78 | - // Redirect. |
|
| 79 | - wp_redirect( WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "datasets/key=$key/dataset.$suffix" ); |
|
| 80 | - exit; |
|
| 81 | - |
|
| 82 | - } |
|
| 19 | + /** |
|
| 20 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.9.8 |
|
| 23 | + * |
|
| 24 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | + */ |
|
| 26 | + function __construct( $configuration_service ) { |
|
| 27 | + |
|
| 28 | + $this->configuration_service = $configuration_service; |
|
| 29 | + |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Hook to 'admin_menu' to add the 'Download Your Data' page. |
|
| 34 | + * |
|
| 35 | + * @since 3.6.0 |
|
| 36 | + */ |
|
| 37 | + public function admin_menu() { |
|
| 38 | + |
|
| 39 | + // Add a callback to our 'page' function. |
|
| 40 | + add_submenu_page( |
|
| 41 | + 'wl_admin_menu', |
|
| 42 | + _x( 'Download Your Data', 'Page title', 'wordlift' ), |
|
| 43 | + _x( 'Download Your Data', 'Menu title', 'wordlift' ), |
|
| 44 | + 'manage_options', |
|
| 45 | + 'wl_download_your_data', |
|
| 46 | + array( $this, 'page', ) |
|
| 47 | + ); |
|
| 48 | + |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The admin menu callback to render the page. |
|
| 53 | + * |
|
| 54 | + * @since 3.6.0 |
|
| 55 | + */ |
|
| 56 | + public function page() { |
|
| 57 | + |
|
| 58 | + // Include the partial. |
|
| 59 | + include( plugin_dir_url( __FILE__ ) . 'partials/wordlift-admin-download-your-data.php' ); |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Ajax call to redirect to a download URL. |
|
| 65 | + * |
|
| 66 | + * @since 3.6.0 |
|
| 67 | + */ |
|
| 68 | + public function download_your_data() { |
|
| 69 | + |
|
| 70 | + ob_end_clean(); |
|
| 71 | + |
|
| 72 | + // Get WL's key. |
|
| 73 | + $key = $this->configuration_service->get_key(); |
|
| 74 | + |
|
| 75 | + // Get the suffix or use json by default. |
|
| 76 | + $suffix = $_GET['out'] ?: 'json'; |
|
| 77 | + |
|
| 78 | + // Redirect. |
|
| 79 | + wp_redirect( WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "datasets/key=$key/dataset.$suffix" ); |
|
| 80 | + exit; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
| 25 | 25 | */ |
| 26 | - function __construct( $configuration_service ) { |
|
| 26 | + function __construct($configuration_service) { |
|
| 27 | 27 | |
| 28 | 28 | $this->configuration_service = $configuration_service; |
| 29 | 29 | |
@@ -39,11 +39,11 @@ discard block |
||
| 39 | 39 | // Add a callback to our 'page' function. |
| 40 | 40 | add_submenu_page( |
| 41 | 41 | 'wl_admin_menu', |
| 42 | - _x( 'Download Your Data', 'Page title', 'wordlift' ), |
|
| 43 | - _x( 'Download Your Data', 'Menu title', 'wordlift' ), |
|
| 42 | + _x('Download Your Data', 'Page title', 'wordlift'), |
|
| 43 | + _x('Download Your Data', 'Menu title', 'wordlift'), |
|
| 44 | 44 | 'manage_options', |
| 45 | 45 | 'wl_download_your_data', |
| 46 | - array( $this, 'page', ) |
|
| 46 | + array($this, 'page',) |
|
| 47 | 47 | ); |
| 48 | 48 | |
| 49 | 49 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | public function page() { |
| 57 | 57 | |
| 58 | 58 | // Include the partial. |
| 59 | - include( plugin_dir_url( __FILE__ ) . 'partials/wordlift-admin-download-your-data.php' ); |
|
| 59 | + include(plugin_dir_url(__FILE__).'partials/wordlift-admin-download-your-data.php'); |
|
| 60 | 60 | |
| 61 | 61 | } |
| 62 | 62 | |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $suffix = $_GET['out'] ?: 'json'; |
| 77 | 77 | |
| 78 | 78 | // Redirect. |
| 79 | - wp_redirect( WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "datasets/key=$key/dataset.$suffix" ); |
|
| 79 | + wp_redirect(WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."datasets/key=$key/dataset.$suffix"); |
|
| 80 | 80 | exit; |
| 81 | 81 | |
| 82 | 82 | } |
@@ -8,54 +8,54 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | function wordlift_ajax_related_posts( $http_raw_data = null ) { |
| 10 | 10 | |
| 11 | - // Extract filtering conditions |
|
| 12 | - if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 13 | - wp_die( 'Post id missing or invalid!' ); |
|
| 11 | + // Extract filtering conditions |
|
| 12 | + if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 13 | + wp_die( 'Post id missing or invalid!' ); |
|
| 14 | 14 | |
| 15 | - return; |
|
| 16 | - } |
|
| 15 | + return; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - $post_id = $_GET["post_id"]; |
|
| 18 | + $post_id = $_GET["post_id"]; |
|
| 19 | 19 | |
| 20 | - // Get the current post |
|
| 21 | - $post = get_post( $post_id ); |
|
| 20 | + // Get the current post |
|
| 21 | + $post = get_post( $post_id ); |
|
| 22 | 22 | |
| 23 | - wl_write_log( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 23 | + wl_write_log( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 24 | 24 | |
| 25 | - // Extract filtering conditions |
|
| 26 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 27 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 25 | + // Extract filtering conditions |
|
| 26 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 27 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 28 | 28 | |
| 29 | - $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 30 | - $related_posts = array(); |
|
| 29 | + $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 30 | + $related_posts = array(); |
|
| 31 | 31 | |
| 32 | - // If the current post is an entity, related posts to the current entity are |
|
| 33 | - // returned. |
|
| 34 | - if ( Wordlift_Entity_Service::TYPE_NAME === $post->post_type ) { |
|
| 35 | - $filtering_entity_ids = array( $post_id ); |
|
| 36 | - } |
|
| 32 | + // If the current post is an entity, related posts to the current entity are |
|
| 33 | + // returned. |
|
| 34 | + if ( Wordlift_Entity_Service::TYPE_NAME === $post->post_type ) { |
|
| 35 | + $filtering_entity_ids = array( $post_id ); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
| 38 | + if ( ! empty( $filtering_entity_ids ) ) { |
|
| 39 | 39 | |
| 40 | - $related_posts = wl_core_get_posts( array( |
|
| 41 | - 'get' => 'posts', |
|
| 42 | - 'related_to__in' => $filtering_entity_ids, |
|
| 43 | - 'post__not_in' => array( $post_id ), |
|
| 44 | - 'post_type' => 'post', |
|
| 45 | - 'post_status' => 'publish', |
|
| 46 | - 'as' => 'subject', |
|
| 47 | - ) ); |
|
| 40 | + $related_posts = wl_core_get_posts( array( |
|
| 41 | + 'get' => 'posts', |
|
| 42 | + 'related_to__in' => $filtering_entity_ids, |
|
| 43 | + 'post__not_in' => array( $post_id ), |
|
| 44 | + 'post_type' => 'post', |
|
| 45 | + 'post_status' => 'publish', |
|
| 46 | + 'as' => 'subject', |
|
| 47 | + ) ); |
|
| 48 | 48 | |
| 49 | - foreach ( $related_posts as $post_obj ) { |
|
| 49 | + foreach ( $related_posts as $post_obj ) { |
|
| 50 | 50 | |
| 51 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 52 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 53 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 54 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 55 | - } |
|
| 56 | - } |
|
| 51 | + $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 52 | + $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 53 | + $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 54 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - wl_core_send_json( $related_posts ); |
|
| 58 | + wl_core_send_json( $related_posts ); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | add_action( 'wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts' ); |
@@ -6,11 +6,11 @@ discard block |
||
| 6 | 6 | * |
| 7 | 7 | * @param null $http_raw_data |
| 8 | 8 | */ |
| 9 | -function wordlift_ajax_related_posts( $http_raw_data = null ) { |
|
| 9 | +function wordlift_ajax_related_posts($http_raw_data = null) { |
|
| 10 | 10 | |
| 11 | 11 | // Extract filtering conditions |
| 12 | - if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 13 | - wp_die( 'Post id missing or invalid!' ); |
|
| 12 | + if ( ! isset($_GET["post_id"]) || ! is_numeric($_GET["post_id"])) { |
|
| 13 | + wp_die('Post id missing or invalid!'); |
|
| 14 | 14 | |
| 15 | 15 | return; |
| 16 | 16 | } |
@@ -18,44 +18,44 @@ discard block |
||
| 18 | 18 | $post_id = $_GET["post_id"]; |
| 19 | 19 | |
| 20 | 20 | // Get the current post |
| 21 | - $post = get_post( $post_id ); |
|
| 21 | + $post = get_post($post_id); |
|
| 22 | 22 | |
| 23 | - wl_write_log( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 23 | + wl_write_log("Going to find posts related to current with post id: $post_id ..."); |
|
| 24 | 24 | |
| 25 | 25 | // Extract filtering conditions |
| 26 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 27 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 26 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents("php://input") : $http_raw_data; |
|
| 27 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 28 | 28 | |
| 29 | - $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 29 | + $filtering_entity_ids = wl_get_entity_post_ids_by_uris($filtering_entity_uris); |
|
| 30 | 30 | $related_posts = array(); |
| 31 | 31 | |
| 32 | 32 | // If the current post is an entity, related posts to the current entity are |
| 33 | 33 | // returned. |
| 34 | - if ( Wordlift_Entity_Service::TYPE_NAME === $post->post_type ) { |
|
| 35 | - $filtering_entity_ids = array( $post_id ); |
|
| 34 | + if (Wordlift_Entity_Service::TYPE_NAME === $post->post_type) { |
|
| 35 | + $filtering_entity_ids = array($post_id); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
| 38 | + if ( ! empty($filtering_entity_ids)) { |
|
| 39 | 39 | |
| 40 | - $related_posts = wl_core_get_posts( array( |
|
| 40 | + $related_posts = wl_core_get_posts(array( |
|
| 41 | 41 | 'get' => 'posts', |
| 42 | 42 | 'related_to__in' => $filtering_entity_ids, |
| 43 | - 'post__not_in' => array( $post_id ), |
|
| 43 | + 'post__not_in' => array($post_id), |
|
| 44 | 44 | 'post_type' => 'post', |
| 45 | 45 | 'post_status' => 'publish', |
| 46 | 46 | 'as' => 'subject', |
| 47 | - ) ); |
|
| 47 | + )); |
|
| 48 | 48 | |
| 49 | - foreach ( $related_posts as $post_obj ) { |
|
| 49 | + foreach ($related_posts as $post_obj) { |
|
| 50 | 50 | |
| 51 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 52 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 53 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 54 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 51 | + $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail')); |
|
| 52 | + $post_obj->thumbnail = ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 53 | + $post_obj->link = get_edit_post_link($post_obj->ID, 'none'); |
|
| 54 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - wl_core_send_json( $related_posts ); |
|
| 58 | + wl_core_send_json($related_posts); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | -add_action( 'wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts' ); |
|
| 61 | +add_action('wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts'); |
|
@@ -13,204 +13,204 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Entity_List_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Size of the entity thumbnail in pixels |
|
| 18 | - * |
|
| 19 | - * @since 3.3.0 |
|
| 20 | - */ |
|
| 21 | - const THUMB_SIZE = 50; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Rating_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.10.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $rating_service; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Create a Wordlift_Entity_List_Service. |
|
| 34 | - * |
|
| 35 | - * @since 3.3.0 |
|
| 36 | - * |
|
| 37 | - * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 38 | - */ |
|
| 39 | - public function __construct( $rating_service ) { |
|
| 40 | - |
|
| 41 | - $this->rating_service = $rating_service; |
|
| 42 | - |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Register custom columns for entity listing in backend. |
|
| 47 | - * |
|
| 48 | - * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column |
|
| 49 | - * |
|
| 50 | - * @since 3.3.0 |
|
| 51 | - * |
|
| 52 | - * @param array $columns the default columns. |
|
| 53 | - * |
|
| 54 | - * @return array Enhanced columns array. |
|
| 55 | - */ |
|
| 56 | - public function register_custom_columns( $columns ) { |
|
| 57 | - |
|
| 58 | - // Take away first column and keep a reference, |
|
| 59 | - // so we can later insert the thumbnail between the first and the rest of columns. |
|
| 60 | - $columns_cb = $columns['cb']; |
|
| 61 | - unset( $columns['cb'] ); |
|
| 62 | - |
|
| 63 | - // Thumbnails column is inserted in second place, while the related posts on the end. |
|
| 64 | - $columns = array_merge( |
|
| 65 | - array( 'cb' => $columns_cb ), // re-add first column |
|
| 66 | - array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
| 67 | - $columns, // default columns (without the first) |
|
| 68 | - array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
| 69 | - array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - return $columns; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Render custom columns. |
|
| 77 | - * |
|
| 78 | - * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column |
|
| 79 | - * |
|
| 80 | - * @since 3.3.0 |
|
| 81 | - * |
|
| 82 | - * @param string $column the current column. |
|
| 83 | - * @param int $entity_id An entity post id. |
|
| 84 | - */ |
|
| 85 | - public function render_custom_columns( $column, $entity_id ) { |
|
| 86 | - |
|
| 87 | - switch ( $column ) { |
|
| 88 | - |
|
| 89 | - case 'wl_column_related_posts': |
|
| 90 | - echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
| 91 | - break; |
|
| 92 | - |
|
| 93 | - case 'wl_column_thumbnail': |
|
| 94 | - |
|
| 95 | - $edit_link = get_edit_post_link( $entity_id ); |
|
| 96 | - $thumb = get_the_post_thumbnail( $entity_id, array( |
|
| 97 | - self::THUMB_SIZE, |
|
| 98 | - self::THUMB_SIZE, |
|
| 99 | - ) ); |
|
| 100 | - |
|
| 101 | - if ( ! $thumb ) { |
|
| 102 | - $thumb = "<img src='" . WL_DEFAULT_THUMBNAIL_PATH . "' width='" . self::THUMB_SIZE . "' />"; |
|
| 103 | - } |
|
| 104 | - echo "<a href='" . esc_url( $edit_link ) . "'>$thumb</a>"; |
|
| 105 | - break; |
|
| 106 | - |
|
| 107 | - case 'wl_column_rating': |
|
| 108 | - |
|
| 109 | - $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
| 110 | - echo '<i class="wl-traffic-light wl-tl-' . $rating['traffic_light_score'] . '">' . $rating['percentage_score'] . '%</i>'; |
|
| 111 | - break; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Add wl-classification-scope select box before the 'Filter' button. |
|
| 118 | - * |
|
| 119 | - * @since 3.3.0 |
|
| 120 | - */ |
|
| 121 | - public function restrict_manage_posts_classification_scope() { |
|
| 122 | - |
|
| 123 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 124 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $screen = get_current_screen(); |
|
| 129 | - // If there is any valid screen nothing to do |
|
| 130 | - if ( null === $screen ) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ( $screen->post_type !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 135 | - return; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Was a W already selected? |
|
| 139 | - $selected = isset( $_GET['wl-classification-scope'] ) ? |
|
| 140 | - $_GET['wl-classification-scope'] : ''; |
|
| 141 | - |
|
| 142 | - // Print select box with the 4W |
|
| 143 | - $all_w = array( |
|
| 144 | - __( "All 'W'" ), |
|
| 145 | - WL_WHAT_RELATION, |
|
| 146 | - WL_WHO_RELATION, |
|
| 147 | - WL_WHERE_RELATION, |
|
| 148 | - WL_WHEN_RELATION, |
|
| 149 | - ); |
|
| 150 | - echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
|
| 151 | - foreach ( $all_w as $w ) { |
|
| 152 | - $default = ( $selected === $w ) ? 'selected' : ''; |
|
| 153 | - echo sprintf( '<option value="%s" %s >%s</option>', $w, $default, __( $w ) ); |
|
| 154 | - } |
|
| 155 | - echo '</select>'; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Server side response operations for the classification filter set in *restrict_manage_posts_classification_scope_filter* |
|
| 160 | - * |
|
| 161 | - * @since 3.3.0 |
|
| 162 | - * |
|
| 163 | - * @param array $clauses WP main query clauses. |
|
| 164 | - * |
|
| 165 | - * @return array Modified clauses. |
|
| 166 | - */ |
|
| 167 | - public function posts_clauses_classification_scope( $clauses ) { |
|
| 168 | - |
|
| 169 | - // Run only on admin page. |
|
| 170 | - if ( ! is_admin() ) { |
|
| 171 | - return $clauses; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // Return safely if get_current_screen() is not defined (yet). |
|
| 175 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 176 | - return $clauses; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
| 180 | - $screen = get_current_screen(); |
|
| 181 | - |
|
| 182 | - // If there is any valid screen nothing to do. |
|
| 183 | - if ( null === $screen ) { |
|
| 184 | - return $clauses; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ( ! ( $screen->post_type === Wordlift_Entity_Service::TYPE_NAME && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { |
|
| 188 | - return $clauses; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - // Check a valid W was requested. |
|
| 192 | - $requested_w = $_GET['wl-classification-scope']; |
|
| 193 | - |
|
| 194 | - $all_w = array( |
|
| 195 | - WL_WHAT_RELATION, |
|
| 196 | - WL_WHO_RELATION, |
|
| 197 | - WL_WHERE_RELATION, |
|
| 198 | - WL_WHEN_RELATION, |
|
| 199 | - ); |
|
| 200 | - |
|
| 201 | - if ( ! in_array( $requested_w, $all_w ) ) { |
|
| 202 | - return $clauses; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - global $wpdb; |
|
| 206 | - $wl_relation_table = wl_core_get_relation_instances_table_name(); |
|
| 207 | - |
|
| 208 | - // Change WP main query clauses. |
|
| 209 | - $clauses['join'] .= "INNER JOIN {$wl_relation_table} ON {$wpdb->posts}.ID = {$wl_relation_table}.object_id"; |
|
| 210 | - $clauses['where'] .= $wpdb->prepare( "AND {$wl_relation_table}.predicate = %s", $requested_w ); |
|
| 211 | - $clauses['distinct'] .= "DISTINCT"; |
|
| 212 | - |
|
| 213 | - return $clauses; |
|
| 214 | - } |
|
| 16 | + /** |
|
| 17 | + * Size of the entity thumbnail in pixels |
|
| 18 | + * |
|
| 19 | + * @since 3.3.0 |
|
| 20 | + */ |
|
| 21 | + const THUMB_SIZE = 50; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Rating_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.10.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $rating_service; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Create a Wordlift_Entity_List_Service. |
|
| 34 | + * |
|
| 35 | + * @since 3.3.0 |
|
| 36 | + * |
|
| 37 | + * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 38 | + */ |
|
| 39 | + public function __construct( $rating_service ) { |
|
| 40 | + |
|
| 41 | + $this->rating_service = $rating_service; |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Register custom columns for entity listing in backend. |
|
| 47 | + * |
|
| 48 | + * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column |
|
| 49 | + * |
|
| 50 | + * @since 3.3.0 |
|
| 51 | + * |
|
| 52 | + * @param array $columns the default columns. |
|
| 53 | + * |
|
| 54 | + * @return array Enhanced columns array. |
|
| 55 | + */ |
|
| 56 | + public function register_custom_columns( $columns ) { |
|
| 57 | + |
|
| 58 | + // Take away first column and keep a reference, |
|
| 59 | + // so we can later insert the thumbnail between the first and the rest of columns. |
|
| 60 | + $columns_cb = $columns['cb']; |
|
| 61 | + unset( $columns['cb'] ); |
|
| 62 | + |
|
| 63 | + // Thumbnails column is inserted in second place, while the related posts on the end. |
|
| 64 | + $columns = array_merge( |
|
| 65 | + array( 'cb' => $columns_cb ), // re-add first column |
|
| 66 | + array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
| 67 | + $columns, // default columns (without the first) |
|
| 68 | + array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
| 69 | + array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + return $columns; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Render custom columns. |
|
| 77 | + * |
|
| 78 | + * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column |
|
| 79 | + * |
|
| 80 | + * @since 3.3.0 |
|
| 81 | + * |
|
| 82 | + * @param string $column the current column. |
|
| 83 | + * @param int $entity_id An entity post id. |
|
| 84 | + */ |
|
| 85 | + public function render_custom_columns( $column, $entity_id ) { |
|
| 86 | + |
|
| 87 | + switch ( $column ) { |
|
| 88 | + |
|
| 89 | + case 'wl_column_related_posts': |
|
| 90 | + echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
| 91 | + break; |
|
| 92 | + |
|
| 93 | + case 'wl_column_thumbnail': |
|
| 94 | + |
|
| 95 | + $edit_link = get_edit_post_link( $entity_id ); |
|
| 96 | + $thumb = get_the_post_thumbnail( $entity_id, array( |
|
| 97 | + self::THUMB_SIZE, |
|
| 98 | + self::THUMB_SIZE, |
|
| 99 | + ) ); |
|
| 100 | + |
|
| 101 | + if ( ! $thumb ) { |
|
| 102 | + $thumb = "<img src='" . WL_DEFAULT_THUMBNAIL_PATH . "' width='" . self::THUMB_SIZE . "' />"; |
|
| 103 | + } |
|
| 104 | + echo "<a href='" . esc_url( $edit_link ) . "'>$thumb</a>"; |
|
| 105 | + break; |
|
| 106 | + |
|
| 107 | + case 'wl_column_rating': |
|
| 108 | + |
|
| 109 | + $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
| 110 | + echo '<i class="wl-traffic-light wl-tl-' . $rating['traffic_light_score'] . '">' . $rating['percentage_score'] . '%</i>'; |
|
| 111 | + break; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Add wl-classification-scope select box before the 'Filter' button. |
|
| 118 | + * |
|
| 119 | + * @since 3.3.0 |
|
| 120 | + */ |
|
| 121 | + public function restrict_manage_posts_classification_scope() { |
|
| 122 | + |
|
| 123 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 124 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $screen = get_current_screen(); |
|
| 129 | + // If there is any valid screen nothing to do |
|
| 130 | + if ( null === $screen ) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ( $screen->post_type !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 135 | + return; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Was a W already selected? |
|
| 139 | + $selected = isset( $_GET['wl-classification-scope'] ) ? |
|
| 140 | + $_GET['wl-classification-scope'] : ''; |
|
| 141 | + |
|
| 142 | + // Print select box with the 4W |
|
| 143 | + $all_w = array( |
|
| 144 | + __( "All 'W'" ), |
|
| 145 | + WL_WHAT_RELATION, |
|
| 146 | + WL_WHO_RELATION, |
|
| 147 | + WL_WHERE_RELATION, |
|
| 148 | + WL_WHEN_RELATION, |
|
| 149 | + ); |
|
| 150 | + echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
|
| 151 | + foreach ( $all_w as $w ) { |
|
| 152 | + $default = ( $selected === $w ) ? 'selected' : ''; |
|
| 153 | + echo sprintf( '<option value="%s" %s >%s</option>', $w, $default, __( $w ) ); |
|
| 154 | + } |
|
| 155 | + echo '</select>'; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Server side response operations for the classification filter set in *restrict_manage_posts_classification_scope_filter* |
|
| 160 | + * |
|
| 161 | + * @since 3.3.0 |
|
| 162 | + * |
|
| 163 | + * @param array $clauses WP main query clauses. |
|
| 164 | + * |
|
| 165 | + * @return array Modified clauses. |
|
| 166 | + */ |
|
| 167 | + public function posts_clauses_classification_scope( $clauses ) { |
|
| 168 | + |
|
| 169 | + // Run only on admin page. |
|
| 170 | + if ( ! is_admin() ) { |
|
| 171 | + return $clauses; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // Return safely if get_current_screen() is not defined (yet). |
|
| 175 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 176 | + return $clauses; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
| 180 | + $screen = get_current_screen(); |
|
| 181 | + |
|
| 182 | + // If there is any valid screen nothing to do. |
|
| 183 | + if ( null === $screen ) { |
|
| 184 | + return $clauses; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ( ! ( $screen->post_type === Wordlift_Entity_Service::TYPE_NAME && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { |
|
| 188 | + return $clauses; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + // Check a valid W was requested. |
|
| 192 | + $requested_w = $_GET['wl-classification-scope']; |
|
| 193 | + |
|
| 194 | + $all_w = array( |
|
| 195 | + WL_WHAT_RELATION, |
|
| 196 | + WL_WHO_RELATION, |
|
| 197 | + WL_WHERE_RELATION, |
|
| 198 | + WL_WHEN_RELATION, |
|
| 199 | + ); |
|
| 200 | + |
|
| 201 | + if ( ! in_array( $requested_w, $all_w ) ) { |
|
| 202 | + return $clauses; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + global $wpdb; |
|
| 206 | + $wl_relation_table = wl_core_get_relation_instances_table_name(); |
|
| 207 | + |
|
| 208 | + // Change WP main query clauses. |
|
| 209 | + $clauses['join'] .= "INNER JOIN {$wl_relation_table} ON {$wpdb->posts}.ID = {$wl_relation_table}.object_id"; |
|
| 210 | + $clauses['where'] .= $wpdb->prepare( "AND {$wl_relation_table}.predicate = %s", $requested_w ); |
|
| 211 | + $clauses['distinct'] .= "DISTINCT"; |
|
| 212 | + |
|
| 213 | + return $clauses; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | 216 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
| 38 | 38 | */ |
| 39 | - public function __construct( $rating_service ) { |
|
| 39 | + public function __construct($rating_service) { |
|
| 40 | 40 | |
| 41 | 41 | $this->rating_service = $rating_service; |
| 42 | 42 | |
@@ -53,20 +53,20 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return array Enhanced columns array. |
| 55 | 55 | */ |
| 56 | - public function register_custom_columns( $columns ) { |
|
| 56 | + public function register_custom_columns($columns) { |
|
| 57 | 57 | |
| 58 | 58 | // Take away first column and keep a reference, |
| 59 | 59 | // so we can later insert the thumbnail between the first and the rest of columns. |
| 60 | 60 | $columns_cb = $columns['cb']; |
| 61 | - unset( $columns['cb'] ); |
|
| 61 | + unset($columns['cb']); |
|
| 62 | 62 | |
| 63 | 63 | // Thumbnails column is inserted in second place, while the related posts on the end. |
| 64 | 64 | $columns = array_merge( |
| 65 | - array( 'cb' => $columns_cb ), // re-add first column |
|
| 66 | - array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
| 67 | - $columns, // default columns (without the first) |
|
| 68 | - array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
| 69 | - array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
| 65 | + array('cb' => $columns_cb), // re-add first column |
|
| 66 | + array('wl_column_thumbnail' => __('Image', 'wordlift')), // thumb |
|
| 67 | + $columns, // default columns (without the first) |
|
| 68 | + array('wl_column_related_posts' => __('Related Posts', 'wordlift')), // related posts |
|
| 69 | + array('wl_column_rating' => __('Rating', 'wordlift')) // related posts |
|
| 70 | 70 | ); |
| 71 | 71 | |
| 72 | 72 | return $columns; |
@@ -82,32 +82,32 @@ discard block |
||
| 82 | 82 | * @param string $column the current column. |
| 83 | 83 | * @param int $entity_id An entity post id. |
| 84 | 84 | */ |
| 85 | - public function render_custom_columns( $column, $entity_id ) { |
|
| 85 | + public function render_custom_columns($column, $entity_id) { |
|
| 86 | 86 | |
| 87 | - switch ( $column ) { |
|
| 87 | + switch ($column) { |
|
| 88 | 88 | |
| 89 | 89 | case 'wl_column_related_posts': |
| 90 | - echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
| 90 | + echo count(wl_core_get_related_post_ids($entity_id)); |
|
| 91 | 91 | break; |
| 92 | 92 | |
| 93 | 93 | case 'wl_column_thumbnail': |
| 94 | 94 | |
| 95 | - $edit_link = get_edit_post_link( $entity_id ); |
|
| 96 | - $thumb = get_the_post_thumbnail( $entity_id, array( |
|
| 95 | + $edit_link = get_edit_post_link($entity_id); |
|
| 96 | + $thumb = get_the_post_thumbnail($entity_id, array( |
|
| 97 | 97 | self::THUMB_SIZE, |
| 98 | 98 | self::THUMB_SIZE, |
| 99 | - ) ); |
|
| 99 | + )); |
|
| 100 | 100 | |
| 101 | - if ( ! $thumb ) { |
|
| 102 | - $thumb = "<img src='" . WL_DEFAULT_THUMBNAIL_PATH . "' width='" . self::THUMB_SIZE . "' />"; |
|
| 101 | + if ( ! $thumb) { |
|
| 102 | + $thumb = "<img src='".WL_DEFAULT_THUMBNAIL_PATH."' width='".self::THUMB_SIZE."' />"; |
|
| 103 | 103 | } |
| 104 | - echo "<a href='" . esc_url( $edit_link ) . "'>$thumb</a>"; |
|
| 104 | + echo "<a href='".esc_url($edit_link)."'>$thumb</a>"; |
|
| 105 | 105 | break; |
| 106 | 106 | |
| 107 | 107 | case 'wl_column_rating': |
| 108 | 108 | |
| 109 | - $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
| 110 | - echo '<i class="wl-traffic-light wl-tl-' . $rating['traffic_light_score'] . '">' . $rating['percentage_score'] . '%</i>'; |
|
| 109 | + $rating = $this->rating_service->get_rating_for($entity_id); |
|
| 110 | + echo '<i class="wl-traffic-light wl-tl-'.$rating['traffic_light_score'].'">'.$rating['percentage_score'].'%</i>'; |
|
| 111 | 111 | break; |
| 112 | 112 | } |
| 113 | 113 | |
@@ -121,36 +121,36 @@ discard block |
||
| 121 | 121 | public function restrict_manage_posts_classification_scope() { |
| 122 | 122 | |
| 123 | 123 | // Return safely if get_current_screen() is not defined (yet) |
| 124 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 124 | + if (false === function_exists('get_current_screen')) { |
|
| 125 | 125 | return; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | $screen = get_current_screen(); |
| 129 | 129 | // If there is any valid screen nothing to do |
| 130 | - if ( null === $screen ) { |
|
| 130 | + if (null === $screen) { |
|
| 131 | 131 | return; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if ( $screen->post_type !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 134 | + if ($screen->post_type !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 135 | 135 | return; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | // Was a W already selected? |
| 139 | - $selected = isset( $_GET['wl-classification-scope'] ) ? |
|
| 139 | + $selected = isset($_GET['wl-classification-scope']) ? |
|
| 140 | 140 | $_GET['wl-classification-scope'] : ''; |
| 141 | 141 | |
| 142 | 142 | // Print select box with the 4W |
| 143 | 143 | $all_w = array( |
| 144 | - __( "All 'W'" ), |
|
| 144 | + __("All 'W'"), |
|
| 145 | 145 | WL_WHAT_RELATION, |
| 146 | 146 | WL_WHO_RELATION, |
| 147 | 147 | WL_WHERE_RELATION, |
| 148 | 148 | WL_WHEN_RELATION, |
| 149 | 149 | ); |
| 150 | 150 | echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
| 151 | - foreach ( $all_w as $w ) { |
|
| 152 | - $default = ( $selected === $w ) ? 'selected' : ''; |
|
| 153 | - echo sprintf( '<option value="%s" %s >%s</option>', $w, $default, __( $w ) ); |
|
| 151 | + foreach ($all_w as $w) { |
|
| 152 | + $default = ($selected === $w) ? 'selected' : ''; |
|
| 153 | + echo sprintf('<option value="%s" %s >%s</option>', $w, $default, __($w)); |
|
| 154 | 154 | } |
| 155 | 155 | echo '</select>'; |
| 156 | 156 | } |
@@ -164,15 +164,15 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return array Modified clauses. |
| 166 | 166 | */ |
| 167 | - public function posts_clauses_classification_scope( $clauses ) { |
|
| 167 | + public function posts_clauses_classification_scope($clauses) { |
|
| 168 | 168 | |
| 169 | 169 | // Run only on admin page. |
| 170 | - if ( ! is_admin() ) { |
|
| 170 | + if ( ! is_admin()) { |
|
| 171 | 171 | return $clauses; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // Return safely if get_current_screen() is not defined (yet). |
| 175 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 175 | + if (false === function_exists('get_current_screen')) { |
|
| 176 | 176 | return $clauses; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | $screen = get_current_screen(); |
| 181 | 181 | |
| 182 | 182 | // If there is any valid screen nothing to do. |
| 183 | - if ( null === $screen ) { |
|
| 183 | + if (null === $screen) { |
|
| 184 | 184 | return $clauses; |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if ( ! ( $screen->post_type === Wordlift_Entity_Service::TYPE_NAME && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { |
|
| 187 | + if ( ! ($screen->post_type === Wordlift_Entity_Service::TYPE_NAME && is_main_query() && isset($_GET['wl-classification-scope']))) { |
|
| 188 | 188 | return $clauses; |
| 189 | 189 | } |
| 190 | 190 | |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | WL_WHEN_RELATION, |
| 199 | 199 | ); |
| 200 | 200 | |
| 201 | - if ( ! in_array( $requested_w, $all_w ) ) { |
|
| 201 | + if ( ! in_array($requested_w, $all_w)) { |
|
| 202 | 202 | return $clauses; |
| 203 | 203 | } |
| 204 | 204 | |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | |
| 208 | 208 | // Change WP main query clauses. |
| 209 | 209 | $clauses['join'] .= "INNER JOIN {$wl_relation_table} ON {$wpdb->posts}.ID = {$wl_relation_table}.object_id"; |
| 210 | - $clauses['where'] .= $wpdb->prepare( "AND {$wl_relation_table}.predicate = %s", $requested_w ); |
|
| 210 | + $clauses['where'] .= $wpdb->prepare("AND {$wl_relation_table}.predicate = %s", $requested_w); |
|
| 211 | 211 | $clauses['distinct'] .= "DISTINCT"; |
| 212 | 212 | |
| 213 | 213 | return $clauses; |