@@ -18,48 +18,48 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Radio_Input_Element implements Wordlift_Admin_Element { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Output the HTML for an input box type settings_page. |
|
| 23 | - * |
|
| 24 | - * @param array $args { |
|
| 25 | - * An array of arguments. |
|
| 26 | - * |
|
| 27 | - * @type string $name The name attribute of the input element. Mandatory. |
|
| 28 | - * @type string $id The id attribute of the input element. Optional, |
|
| 29 | - * randomly generated one is used if not supplied. |
|
| 30 | - * @type string $value The value of the input element. Optional, defaults |
|
| 31 | - * to empty string. |
|
| 32 | - * @type string $css_class The class attribute for the input element. If empty |
|
| 33 | - * string no class attribute will be added. Optional, |
|
| 34 | - * defaults to empty string. |
|
| 35 | - * @type string $description The description text to be displayed below the element. |
|
| 36 | - * Can include some HTML element. If empty string no |
|
| 37 | - * description will be displayed. Optional, defaults to |
|
| 38 | - * empty string. |
|
| 39 | - * } |
|
| 40 | - * @return $this|Wordlift_Admin_Element |
|
| 41 | - * @since 3.13.0 |
|
| 42 | - * |
|
| 43 | - */ |
|
| 44 | - public function render( $args ) { |
|
| 45 | - /* |
|
| 21 | + /** |
|
| 22 | + * Output the HTML for an input box type settings_page. |
|
| 23 | + * |
|
| 24 | + * @param array $args { |
|
| 25 | + * An array of arguments. |
|
| 26 | + * |
|
| 27 | + * @type string $name The name attribute of the input element. Mandatory. |
|
| 28 | + * @type string $id The id attribute of the input element. Optional, |
|
| 29 | + * randomly generated one is used if not supplied. |
|
| 30 | + * @type string $value The value of the input element. Optional, defaults |
|
| 31 | + * to empty string. |
|
| 32 | + * @type string $css_class The class attribute for the input element. If empty |
|
| 33 | + * string no class attribute will be added. Optional, |
|
| 34 | + * defaults to empty string. |
|
| 35 | + * @type string $description The description text to be displayed below the element. |
|
| 36 | + * Can include some HTML element. If empty string no |
|
| 37 | + * description will be displayed. Optional, defaults to |
|
| 38 | + * empty string. |
|
| 39 | + * } |
|
| 40 | + * @return $this|Wordlift_Admin_Element |
|
| 41 | + * @since 3.13.0 |
|
| 42 | + * |
|
| 43 | + */ |
|
| 44 | + public function render( $args ) { |
|
| 45 | + /* |
|
| 46 | 46 | * Parse the arguments and merge with default values. |
| 47 | 47 | * Name intentionally do not have a default as it has to be in SyncEvent |
| 48 | 48 | * with form handling code |
| 49 | 49 | */ |
| 50 | - $params = wp_parse_args( $args, array( |
|
| 51 | - 'id' => uniqid( 'wl-input-' ), |
|
| 52 | - 'value' => '', |
|
| 53 | - 'css_class' => '', |
|
| 54 | - 'description' => '', |
|
| 55 | - ) ); |
|
| 50 | + $params = wp_parse_args( $args, array( |
|
| 51 | + 'id' => uniqid( 'wl-input-' ), |
|
| 52 | + 'value' => '', |
|
| 53 | + 'css_class' => '', |
|
| 54 | + 'description' => '', |
|
| 55 | + ) ); |
|
| 56 | 56 | |
| 57 | - // Set the readonly and class attributes and the description. |
|
| 58 | - $value = $params['value']; |
|
| 59 | - $css_class = ! empty( $params['css_class'] ) ? ' class="' . esc_attr( $params['css_class'] ) . '"' : ''; |
|
| 60 | - $description = ! empty( $params['description'] ) ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 57 | + // Set the readonly and class attributes and the description. |
|
| 58 | + $value = $params['value']; |
|
| 59 | + $css_class = ! empty( $params['css_class'] ) ? ' class="' . esc_attr( $params['css_class'] ) . '"' : ''; |
|
| 60 | + $description = ! empty( $params['description'] ) ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 61 | 61 | |
| 62 | - ?> |
|
| 62 | + ?> |
|
| 63 | 63 | |
| 64 | 64 | <input type="radio" id="<?php echo esc_attr( $params['id'] ); ?>" |
| 65 | 65 | name="<?php echo esc_attr( $params['name'] ); ?>" |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | /> No |
| 74 | 74 | |
| 75 | 75 | <?php |
| 76 | - echo wp_kses( $description, array( 'p' =>array(), 'a' => array( 'href' => array() ) ) ); |
|
| 76 | + echo wp_kses( $description, array( 'p' =>array(), 'a' => array( 'href' => array() ) ) ); |
|
| 77 | 77 | |
| 78 | - return $this; |
|
| 79 | - } |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | } |
@@ -41,39 +41,39 @@ |
||
| 41 | 41 | * @since 3.13.0 |
| 42 | 42 | * |
| 43 | 43 | */ |
| 44 | - public function render( $args ) { |
|
| 44 | + public function render($args) { |
|
| 45 | 45 | /* |
| 46 | 46 | * Parse the arguments and merge with default values. |
| 47 | 47 | * Name intentionally do not have a default as it has to be in SyncEvent |
| 48 | 48 | * with form handling code |
| 49 | 49 | */ |
| 50 | - $params = wp_parse_args( $args, array( |
|
| 51 | - 'id' => uniqid( 'wl-input-' ), |
|
| 50 | + $params = wp_parse_args($args, array( |
|
| 51 | + 'id' => uniqid('wl-input-'), |
|
| 52 | 52 | 'value' => '', |
| 53 | 53 | 'css_class' => '', |
| 54 | 54 | 'description' => '', |
| 55 | - ) ); |
|
| 55 | + )); |
|
| 56 | 56 | |
| 57 | 57 | // Set the readonly and class attributes and the description. |
| 58 | 58 | $value = $params['value']; |
| 59 | - $css_class = ! empty( $params['css_class'] ) ? ' class="' . esc_attr( $params['css_class'] ) . '"' : ''; |
|
| 60 | - $description = ! empty( $params['description'] ) ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
| 59 | + $css_class = ! empty($params['css_class']) ? ' class="'.esc_attr($params['css_class']).'"' : ''; |
|
| 60 | + $description = ! empty($params['description']) ? '<p>'.wp_kses($params['description'], array('a' => array('href' => array()))).'</p>' : ''; |
|
| 61 | 61 | |
| 62 | 62 | ?> |
| 63 | 63 | |
| 64 | - <input type="radio" id="<?php echo esc_attr( $params['id'] ); ?>" |
|
| 65 | - name="<?php echo esc_attr( $params['name'] ); ?>" |
|
| 64 | + <input type="radio" id="<?php echo esc_attr($params['id']); ?>" |
|
| 65 | + name="<?php echo esc_attr($params['name']); ?>" |
|
| 66 | 66 | value="yes" <?php echo $css_class; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 67 | - <?php checked( $value, 'yes' ); ?> |
|
| 67 | + <?php checked($value, 'yes'); ?> |
|
| 68 | 68 | /> Yes |
| 69 | - <input type="radio" id="<?php echo esc_attr( $params['id'] ); ?>" |
|
| 70 | - name="<?php echo esc_attr( $params['name'] ); ?>" |
|
| 69 | + <input type="radio" id="<?php echo esc_attr($params['id']); ?>" |
|
| 70 | + name="<?php echo esc_attr($params['name']); ?>" |
|
| 71 | 71 | value="no" <?php echo $css_class; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped?> |
| 72 | - <?php checked( $value, 'no' ); ?> |
|
| 72 | + <?php checked($value, 'no'); ?> |
|
| 73 | 73 | /> No |
| 74 | 74 | |
| 75 | 75 | <?php |
| 76 | - echo wp_kses( $description, array( 'p' =>array(), 'a' => array( 'href' => array() ) ) ); |
|
| 76 | + echo wp_kses($description, array('p' =>array(), 'a' => array('href' => array()))); |
|
| 77 | 77 | |
| 78 | 78 | return $this; |
| 79 | 79 | } |
@@ -18,100 +18,100 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Dashboard_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Transient Name |
|
| 23 | - * |
|
| 24 | - * @since 3.4.0 |
|
| 25 | - */ |
|
| 26 | - const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Transient Expiration (in seconds) |
|
| 30 | - * |
|
| 31 | - * @since 3.4.0 |
|
| 32 | - */ |
|
| 33 | - const TRANSIENT_EXPIRATION = 86400; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * A {@link Wordlift_Rating_Service} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.10.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 41 | - */ |
|
| 42 | - private $rating_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 46 | - * |
|
| 47 | - * @since 3.15.0 |
|
| 48 | - * @access private |
|
| 49 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 50 | - */ |
|
| 51 | - private $entity_service; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Create a Wordlift_Entity_List_Service. |
|
| 55 | - * |
|
| 56 | - * @since 3.4.0 |
|
| 57 | - * |
|
| 58 | - */ |
|
| 59 | - protected function __construct() { |
|
| 60 | - |
|
| 61 | - $this->rating_service = Wordlift_Rating_Service::get_instance(); |
|
| 62 | - $this->entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - private static $instance; |
|
| 67 | - |
|
| 68 | - public static function get_instance() { |
|
| 69 | - if ( ! isset( self::$instance ) ) { |
|
| 70 | - self::$instance = new self(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return self::$instance; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Return stats layout |
|
| 78 | - * |
|
| 79 | - * @return string Dashboard widget html markup |
|
| 80 | - * @since 3.4.0 |
|
| 81 | - * |
|
| 82 | - */ |
|
| 83 | - public function dashboard_widget_callback( $post ) { |
|
| 21 | + /** |
|
| 22 | + * Transient Name |
|
| 23 | + * |
|
| 24 | + * @since 3.4.0 |
|
| 25 | + */ |
|
| 26 | + const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Transient Expiration (in seconds) |
|
| 30 | + * |
|
| 31 | + * @since 3.4.0 |
|
| 32 | + */ |
|
| 33 | + const TRANSIENT_EXPIRATION = 86400; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * A {@link Wordlift_Rating_Service} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.10.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 41 | + */ |
|
| 42 | + private $rating_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 46 | + * |
|
| 47 | + * @since 3.15.0 |
|
| 48 | + * @access private |
|
| 49 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 50 | + */ |
|
| 51 | + private $entity_service; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Create a Wordlift_Entity_List_Service. |
|
| 55 | + * |
|
| 56 | + * @since 3.4.0 |
|
| 57 | + * |
|
| 58 | + */ |
|
| 59 | + protected function __construct() { |
|
| 60 | + |
|
| 61 | + $this->rating_service = Wordlift_Rating_Service::get_instance(); |
|
| 62 | + $this->entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + private static $instance; |
|
| 67 | + |
|
| 68 | + public static function get_instance() { |
|
| 69 | + if ( ! isset( self::$instance ) ) { |
|
| 70 | + self::$instance = new self(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return self::$instance; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Return stats layout |
|
| 78 | + * |
|
| 79 | + * @return string Dashboard widget html markup |
|
| 80 | + * @since 3.4.0 |
|
| 81 | + * |
|
| 82 | + */ |
|
| 83 | + public function dashboard_widget_callback( $post ) { |
|
| 84 | 84 | $caption_kses = array( 'a' => array( 'href' => array() ) ); |
| 85 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 86 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 87 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 88 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 89 | - $this->render_stat_param( 'annotatedPostsPercentage' ), |
|
| 90 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 91 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 92 | - $this->render_stat_param( 'posts' ) |
|
| 93 | - ); |
|
| 94 | - |
|
| 95 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 96 | - $rating_caption = sprintf( wp_kses( |
|
| 97 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 98 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 99 | - $this->render_stat_param( 'entities' ), |
|
| 100 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 101 | - $this->render_stat_param( 'rating' ) |
|
| 102 | - ); |
|
| 103 | - |
|
| 104 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 105 | - $graph_caption = sprintf( wp_kses( |
|
| 106 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 107 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 108 | - $this->render_stat_param( 'wikidata' ), |
|
| 109 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 113 | - |
|
| 114 | - ?> |
|
| 85 | + $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 86 | + $enriched_posts_caption = sprintf( wp_kses( |
|
| 87 | + __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 88 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 89 | + $this->render_stat_param( 'annotatedPostsPercentage' ), |
|
| 90 | + esc_url( admin_url( 'edit.php' ) ), |
|
| 91 | + $this->render_stat_param( 'annotated_posts' ), |
|
| 92 | + $this->render_stat_param( 'posts' ) |
|
| 93 | + ); |
|
| 94 | + |
|
| 95 | + $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 96 | + $rating_caption = sprintf( wp_kses( |
|
| 97 | + __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 98 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 99 | + $this->render_stat_param( 'entities' ), |
|
| 100 | + esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 101 | + $this->render_stat_param( 'rating' ) |
|
| 102 | + ); |
|
| 103 | + |
|
| 104 | + $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 105 | + $graph_caption = sprintf( wp_kses( |
|
| 106 | + __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 107 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 108 | + $this->render_stat_param( 'wikidata' ), |
|
| 109 | + esc_url( 'https://www.wikidata.org/' ) |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + $triples_label = __( 'triples', 'wordlift' ); |
|
| 113 | + |
|
| 114 | + ?> |
|
| 115 | 115 | <div id="wl-dashboard-widget-inner-wrapper"> |
| 116 | 116 | <div class="wl-stat-card"> |
| 117 | 117 | <div class="wl-stat-graph-wrapper"> |
@@ -150,109 +150,109 @@ discard block |
||
| 150 | 150 | </div> |
| 151 | 151 | <?php |
| 152 | 152 | |
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Return stats |
|
| 157 | - * |
|
| 158 | - * @return string markup |
|
| 159 | - * @since 3.4.0 |
|
| 160 | - * |
|
| 161 | - */ |
|
| 162 | - public function add_dashboard_widgets() { |
|
| 163 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 164 | - $this, |
|
| 165 | - 'dashboard_widget_callback', |
|
| 166 | - ) ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Return stats |
|
| 171 | - * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 172 | - * |
|
| 173 | - * @since 3.4.0 |
|
| 174 | - * |
|
| 175 | - * @return string JSON obj with all available stats. |
|
| 176 | - */ |
|
| 177 | - public function ajax_get_stats() { |
|
| 178 | - |
|
| 179 | - // If needed, the transient is force to reloaded. |
|
| 180 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 181 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // Try to retrieve the transient |
|
| 185 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 186 | - |
|
| 187 | - if ( ! $stats ) { |
|
| 188 | - // Calculate stats |
|
| 189 | - $stats = array( |
|
| 190 | - 'entities' => $this->entity_service->count(), |
|
| 191 | - 'posts' => $this->count_posts(), |
|
| 192 | - 'annotated_posts' => $this->count_annotated_posts(), |
|
| 193 | - 'triples' => $this->count_triples() ?: '-', |
|
| 194 | - 'rating' => $this->average_entities_rating(), |
|
| 195 | - ); |
|
| 196 | - // Cache stats results trough transient |
|
| 197 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 198 | - } |
|
| 199 | - // Return stats as json object |
|
| 200 | - wl_core_send_json( $stats ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Calculate total number of published posts |
|
| 205 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 206 | - * @since 3.4.0 |
|
| 207 | - * |
|
| 208 | - * @return int Total number of published posts. |
|
| 209 | - */ |
|
| 210 | - public function count_posts() { |
|
| 211 | - |
|
| 212 | - return (int) wp_count_posts()->publish; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Calculate total number of annotated published posts |
|
| 217 | - * @return int Total number of annotated published posts. |
|
| 218 | - * @since 3.4.0 |
|
| 219 | - * |
|
| 220 | - */ |
|
| 221 | - public function count_annotated_posts() { |
|
| 222 | - |
|
| 223 | - // Prepare interaction with db |
|
| 224 | - global $wpdb; |
|
| 225 | - // Retrieve Wordlift relation instances table name |
|
| 226 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 227 | - // Calculate sql statement |
|
| 228 | - $sql_statement = " |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Return stats |
|
| 157 | + * |
|
| 158 | + * @return string markup |
|
| 159 | + * @since 3.4.0 |
|
| 160 | + * |
|
| 161 | + */ |
|
| 162 | + public function add_dashboard_widgets() { |
|
| 163 | + wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 164 | + $this, |
|
| 165 | + 'dashboard_widget_callback', |
|
| 166 | + ) ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Return stats |
|
| 171 | + * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 172 | + * |
|
| 173 | + * @since 3.4.0 |
|
| 174 | + * |
|
| 175 | + * @return string JSON obj with all available stats. |
|
| 176 | + */ |
|
| 177 | + public function ajax_get_stats() { |
|
| 178 | + |
|
| 179 | + // If needed, the transient is force to reloaded. |
|
| 180 | + if ( isset( $_GET['force_reload'] ) ) { |
|
| 181 | + delete_transient( self::TRANSIENT_NAME ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // Try to retrieve the transient |
|
| 185 | + $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 186 | + |
|
| 187 | + if ( ! $stats ) { |
|
| 188 | + // Calculate stats |
|
| 189 | + $stats = array( |
|
| 190 | + 'entities' => $this->entity_service->count(), |
|
| 191 | + 'posts' => $this->count_posts(), |
|
| 192 | + 'annotated_posts' => $this->count_annotated_posts(), |
|
| 193 | + 'triples' => $this->count_triples() ?: '-', |
|
| 194 | + 'rating' => $this->average_entities_rating(), |
|
| 195 | + ); |
|
| 196 | + // Cache stats results trough transient |
|
| 197 | + set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 198 | + } |
|
| 199 | + // Return stats as json object |
|
| 200 | + wl_core_send_json( $stats ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Calculate total number of published posts |
|
| 205 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 206 | + * @since 3.4.0 |
|
| 207 | + * |
|
| 208 | + * @return int Total number of published posts. |
|
| 209 | + */ |
|
| 210 | + public function count_posts() { |
|
| 211 | + |
|
| 212 | + return (int) wp_count_posts()->publish; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Calculate total number of annotated published posts |
|
| 217 | + * @return int Total number of annotated published posts. |
|
| 218 | + * @since 3.4.0 |
|
| 219 | + * |
|
| 220 | + */ |
|
| 221 | + public function count_annotated_posts() { |
|
| 222 | + |
|
| 223 | + // Prepare interaction with db |
|
| 224 | + global $wpdb; |
|
| 225 | + // Retrieve Wordlift relation instances table name |
|
| 226 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 227 | + // Calculate sql statement |
|
| 228 | + $sql_statement = " |
|
| 229 | 229 | 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'; |
| 230 | 230 | "; |
| 231 | 231 | |
| 232 | - // Perform the query |
|
| 233 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 234 | - |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Calculate the average entities rating. |
|
| 239 | - * |
|
| 240 | - * @return int Average entities rating. |
|
| 241 | - * @since 3.4.0 |
|
| 242 | - * |
|
| 243 | - * @since 3.20.0 this method is public. |
|
| 244 | - */ |
|
| 245 | - public function average_entities_rating() { |
|
| 246 | - |
|
| 247 | - // Prepare interaction with db |
|
| 248 | - global $wpdb; |
|
| 249 | - $query = $wpdb->prepare( |
|
| 250 | - "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 251 | - Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - // Perform the query. |
|
| 255 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 256 | - } |
|
| 232 | + // Perform the query |
|
| 233 | + return (int) $wpdb->get_var( $sql_statement ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Calculate the average entities rating. |
|
| 239 | + * |
|
| 240 | + * @return int Average entities rating. |
|
| 241 | + * @since 3.4.0 |
|
| 242 | + * |
|
| 243 | + * @since 3.20.0 this method is public. |
|
| 244 | + */ |
|
| 245 | + public function average_entities_rating() { |
|
| 246 | + |
|
| 247 | + // Prepare interaction with db |
|
| 248 | + global $wpdb; |
|
| 249 | + $query = $wpdb->prepare( |
|
| 250 | + "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 251 | + Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + // Perform the query. |
|
| 255 | + return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | 258 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | private static $instance; |
| 67 | 67 | |
| 68 | 68 | public static function get_instance() { |
| 69 | - if ( ! isset( self::$instance ) ) { |
|
| 69 | + if ( ! isset(self::$instance)) { |
|
| 70 | 70 | self::$instance = new self(); |
| 71 | 71 | } |
| 72 | 72 | |
@@ -80,53 +80,53 @@ discard block |
||
| 80 | 80 | * @since 3.4.0 |
| 81 | 81 | * |
| 82 | 82 | */ |
| 83 | - public function dashboard_widget_callback( $post ) { |
|
| 84 | - $caption_kses = array( 'a' => array( 'href' => array() ) ); |
|
| 85 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 86 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 87 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 88 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 89 | - $this->render_stat_param( 'annotatedPostsPercentage' ), |
|
| 90 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 91 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 92 | - $this->render_stat_param( 'posts' ) |
|
| 83 | + public function dashboard_widget_callback($post) { |
|
| 84 | + $caption_kses = array('a' => array('href' => array())); |
|
| 85 | + $enriched_posts_title = __('enriched posts', 'wordlift'); |
|
| 86 | + $enriched_posts_caption = sprintf(wp_kses( |
|
| 87 | + __('%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift'), |
|
| 88 | + array('a' => array('href' => array())) ), |
|
| 89 | + $this->render_stat_param('annotatedPostsPercentage'), |
|
| 90 | + esc_url(admin_url('edit.php')), |
|
| 91 | + $this->render_stat_param('annotated_posts'), |
|
| 92 | + $this->render_stat_param('posts') |
|
| 93 | 93 | ); |
| 94 | 94 | |
| 95 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 96 | - $rating_caption = sprintf( wp_kses( |
|
| 97 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 98 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 99 | - $this->render_stat_param( 'entities' ), |
|
| 100 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 101 | - $this->render_stat_param( 'rating' ) |
|
| 95 | + $rating_title = __('average entity rating', 'wordlift'); |
|
| 96 | + $rating_caption = sprintf(wp_kses( |
|
| 97 | + __('You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift'), |
|
| 98 | + array('a' => array('href' => array())) ), |
|
| 99 | + $this->render_stat_param('entities'), |
|
| 100 | + esc_url(admin_url('edit.php?post_type=entity')), |
|
| 101 | + $this->render_stat_param('rating') |
|
| 102 | 102 | ); |
| 103 | 103 | |
| 104 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 105 | - $graph_caption = sprintf( wp_kses( |
|
| 106 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 107 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 108 | - $this->render_stat_param( 'wikidata' ), |
|
| 109 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 104 | + $graph_title = __('triples in your graph', 'wordlift'); |
|
| 105 | + $graph_caption = sprintf(wp_kses( |
|
| 106 | + __('Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift'), |
|
| 107 | + array('a' => array('href' => array())) ), |
|
| 108 | + $this->render_stat_param('wikidata'), |
|
| 109 | + esc_url('https://www.wikidata.org/') |
|
| 110 | 110 | ); |
| 111 | 111 | |
| 112 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 112 | + $triples_label = __('triples', 'wordlift'); |
|
| 113 | 113 | |
| 114 | 114 | ?> |
| 115 | 115 | <div id="wl-dashboard-widget-inner-wrapper"> |
| 116 | 116 | <div class="wl-stat-card"> |
| 117 | 117 | <div class="wl-stat-graph-wrapper"> |
| 118 | - <h4><?php echo esc_html( $enriched_posts_title ); ?> <a |
|
| 118 | + <h4><?php echo esc_html($enriched_posts_title); ?> <a |
|
| 119 | 119 | href="http://docs.wordlift.it/en/latest/faq.html#what-is-content-enrichment" |
| 120 | 120 | target="_blank"><i class="wl-info"></i></a></h4> |
| 121 | 121 | <svg id="wl-posts-pie-chart" viewBox="0 0 32 32"> |
| 122 | 122 | <circle r="16" cx="16" cy="16"/> |
| 123 | 123 | </svg> |
| 124 | 124 | </div> |
| 125 | - <p><?php echo wp_kses( $enriched_posts_caption, $caption_kses ); ?> </p> |
|
| 125 | + <p><?php echo wp_kses($enriched_posts_caption, $caption_kses); ?> </p> |
|
| 126 | 126 | </div> |
| 127 | 127 | <div class="wl-stat-card"> |
| 128 | 128 | <div class="wl-stat-graph-wrapper"> |
| 129 | - <h4><?php echo esc_html( $rating_title ); ?> <a |
|
| 129 | + <h4><?php echo esc_html($rating_title); ?> <a |
|
| 130 | 130 | href="http://docs.wordlift.it/en/latest/faq.html#what-factors-determine-the-rating-of-an-entity" |
| 131 | 131 | target="_blank"><i class="wl-info"></i></a></h4> |
| 132 | 132 | <svg id="wl-entities-gauge-chart" viewBox="0 0 32 32"> |
@@ -134,18 +134,18 @@ discard block |
||
| 134 | 134 | <circle r="16" cx="16" cy="16" class="stat"/> |
| 135 | 135 | </svg> |
| 136 | 136 | </div> |
| 137 | - <p><?php echo wp_kses( $rating_caption, $caption_kses ); ?></p> |
|
| 137 | + <p><?php echo wp_kses($rating_caption, $caption_kses); ?></p> |
|
| 138 | 138 | </div> |
| 139 | 139 | <div class="wl-stat-card"> |
| 140 | 140 | <div class="wl-stat-graph-wrapper"> |
| 141 | - <h4><?php echo esc_html( $graph_title ); ?><a href="http://docs.wordlift.it/en/latest/faq.html#what-is-a-triple" |
|
| 141 | + <h4><?php echo esc_html($graph_title); ?><a href="http://docs.wordlift.it/en/latest/faq.html#what-is-a-triple" |
|
| 142 | 142 | target="_blank"><i class="wl-info"></i></a></h4> |
| 143 | 143 | <div class="wl-triples"> |
| 144 | 144 | <span id="wl-dashboard-widget-triples"></span> |
| 145 | - <span class="wl-triples-label"><?php echo esc_html( $triples_label ); ?></span> |
|
| 145 | + <span class="wl-triples-label"><?php echo esc_html($triples_label); ?></span> |
|
| 146 | 146 | </div> |
| 147 | 147 | </div> |
| 148 | - <p><?php echo wp_kses( $graph_caption, $caption_kses ); ?></p> |
|
| 148 | + <p><?php echo wp_kses($graph_caption, $caption_kses); ?></p> |
|
| 149 | 149 | </div> |
| 150 | 150 | </div> |
| 151 | 151 | <?php |
@@ -160,10 +160,10 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | */ |
| 162 | 162 | public function add_dashboard_widgets() { |
| 163 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 163 | + wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 164 | 164 | $this, |
| 165 | 165 | 'dashboard_widget_callback', |
| 166 | - ) ); |
|
| 166 | + )); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -177,14 +177,14 @@ discard block |
||
| 177 | 177 | public function ajax_get_stats() { |
| 178 | 178 | |
| 179 | 179 | // If needed, the transient is force to reloaded. |
| 180 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 181 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 180 | + if (isset($_GET['force_reload'])) { |
|
| 181 | + delete_transient(self::TRANSIENT_NAME); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Try to retrieve the transient |
| 185 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 185 | + $stats = get_transient(self::TRANSIENT_NAME); |
|
| 186 | 186 | |
| 187 | - if ( ! $stats ) { |
|
| 187 | + if ( ! $stats) { |
|
| 188 | 188 | // Calculate stats |
| 189 | 189 | $stats = array( |
| 190 | 190 | 'entities' => $this->entity_service->count(), |
@@ -194,10 +194,10 @@ discard block |
||
| 194 | 194 | 'rating' => $this->average_entities_rating(), |
| 195 | 195 | ); |
| 196 | 196 | // Cache stats results trough transient |
| 197 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 197 | + set_transient(self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION); |
|
| 198 | 198 | } |
| 199 | 199 | // Return stats as json object |
| 200 | - wl_core_send_json( $stats ); |
|
| 200 | + wl_core_send_json($stats); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | "; |
| 231 | 231 | |
| 232 | 232 | // Perform the query |
| 233 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 233 | + return (int) $wpdb->get_var($sql_statement); |
|
| 234 | 234 | |
| 235 | 235 | } |
| 236 | 236 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | 254 | // Perform the query. |
| 255 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 255 | + return $this->rating_service->convert_raw_score_to_percentage($wpdb->get_var($query)); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | } |
@@ -4,53 +4,53 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Wordlift_Admin_Not_Enriched_Filter { |
| 6 | 6 | |
| 7 | - const PARAMETER_NAME = 'wl_enriched'; |
|
| 7 | + const PARAMETER_NAME = 'wl_enriched'; |
|
| 8 | 8 | |
| 9 | - public function __construct() { |
|
| 9 | + public function __construct() { |
|
| 10 | 10 | |
| 11 | - // Hook to `views_edit-post`. |
|
| 12 | - add_filter( 'views_edit-post', array( $this, 'view_edit' ) ); |
|
| 11 | + // Hook to `views_edit-post`. |
|
| 12 | + add_filter( 'views_edit-post', array( $this, 'view_edit' ) ); |
|
| 13 | 13 | |
| 14 | - // Add the `posts_where` filter if the filter is active. |
|
| 15 | - if ( $this->is_filter_active() ) { |
|
| 16 | - add_filter( 'posts_where', array( $this, 'posts_where' ) ); |
|
| 17 | - } |
|
| 14 | + // Add the `posts_where` filter if the filter is active. |
|
| 15 | + if ( $this->is_filter_active() ) { |
|
| 16 | + add_filter( 'posts_where', array( $this, 'posts_where' ) ); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - } |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function posts_where( $where ) { |
|
| 22 | - global $wpdb; |
|
| 21 | + public function posts_where( $where ) { |
|
| 22 | + global $wpdb; |
|
| 23 | 23 | |
| 24 | - return $where . |
|
| 25 | - " AND {$wpdb->posts}.ID NOT IN ( SELECT DISTINCT subject_id FROM {$wpdb->prefix}wl_relation_instances )"; |
|
| 26 | - } |
|
| 24 | + return $where . |
|
| 25 | + " AND {$wpdb->posts}.ID NOT IN ( SELECT DISTINCT subject_id FROM {$wpdb->prefix}wl_relation_instances )"; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function view_edit( $views ) { |
|
| 29 | - global $wpdb; |
|
| 28 | + public function view_edit( $views ) { |
|
| 29 | + global $wpdb; |
|
| 30 | 30 | |
| 31 | - $url = add_query_arg( array( self::PARAMETER_NAME => 'no', 'post_type' => 'post', ), 'edit.php' ); |
|
| 31 | + $url = add_query_arg( array( self::PARAMETER_NAME => 'no', 'post_type' => 'post', ), 'edit.php' ); |
|
| 32 | 32 | |
| 33 | - $not_enriched_count = $wpdb->get_var( " |
|
| 33 | + $not_enriched_count = $wpdb->get_var( " |
|
| 34 | 34 | SELECT COUNT( 1 ) FROM $wpdb->posts p |
| 35 | 35 | WHERE p.post_type = 'post' |
| 36 | 36 | AND p.post_status <> 'trash' |
| 37 | 37 | AND p.post_status <> 'auto-draft' |
| 38 | 38 | AND p.ID NOT IN ( SELECT DISTINCT subject_id FROM {$wpdb->prefix}wl_relation_instances ) |
| 39 | 39 | " |
| 40 | - ); |
|
| 40 | + ); |
|
| 41 | 41 | |
| 42 | - $link = '<a href="' |
|
| 43 | - . esc_url( $url ) . '"' |
|
| 44 | - . ( $this->is_filter_active() ? ' class="current" aria-current="page"' : '' ) |
|
| 45 | - . '>' . esc_html( __( 'Not enriched', 'wordlift' ) ) . '</a> (' . $not_enriched_count . ')'; |
|
| 42 | + $link = '<a href="' |
|
| 43 | + . esc_url( $url ) . '"' |
|
| 44 | + . ( $this->is_filter_active() ? ' class="current" aria-current="page"' : '' ) |
|
| 45 | + . '>' . esc_html( __( 'Not enriched', 'wordlift' ) ) . '</a> (' . $not_enriched_count . ')'; |
|
| 46 | 46 | |
| 47 | - $views['wl_not_enriched'] = $link; |
|
| 47 | + $views['wl_not_enriched'] = $link; |
|
| 48 | 48 | |
| 49 | - return $views; |
|
| 50 | - } |
|
| 49 | + return $views; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function is_filter_active() { |
|
| 53 | - return 'no' === filter_input( INPUT_GET, self::PARAMETER_NAME ); |
|
| 54 | - } |
|
| 52 | + public function is_filter_active() { |
|
| 53 | + return 'no' === filter_input( INPUT_GET, self::PARAMETER_NAME ); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | } |
| 57 | 57 | \ No newline at end of file |
@@ -9,28 +9,28 @@ discard block |
||
| 9 | 9 | public function __construct() { |
| 10 | 10 | |
| 11 | 11 | // Hook to `views_edit-post`. |
| 12 | - add_filter( 'views_edit-post', array( $this, 'view_edit' ) ); |
|
| 12 | + add_filter('views_edit-post', array($this, 'view_edit')); |
|
| 13 | 13 | |
| 14 | 14 | // Add the `posts_where` filter if the filter is active. |
| 15 | - if ( $this->is_filter_active() ) { |
|
| 16 | - add_filter( 'posts_where', array( $this, 'posts_where' ) ); |
|
| 15 | + if ($this->is_filter_active()) { |
|
| 16 | + add_filter('posts_where', array($this, 'posts_where')); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - public function posts_where( $where ) { |
|
| 21 | + public function posts_where($where) { |
|
| 22 | 22 | global $wpdb; |
| 23 | 23 | |
| 24 | - return $where . |
|
| 24 | + return $where. |
|
| 25 | 25 | " AND {$wpdb->posts}.ID NOT IN ( SELECT DISTINCT subject_id FROM {$wpdb->prefix}wl_relation_instances )"; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - public function view_edit( $views ) { |
|
| 28 | + public function view_edit($views) { |
|
| 29 | 29 | global $wpdb; |
| 30 | 30 | |
| 31 | - $url = add_query_arg( array( self::PARAMETER_NAME => 'no', 'post_type' => 'post', ), 'edit.php' ); |
|
| 31 | + $url = add_query_arg(array(self::PARAMETER_NAME => 'no', 'post_type' => 'post',), 'edit.php'); |
|
| 32 | 32 | |
| 33 | - $not_enriched_count = $wpdb->get_var( " |
|
| 33 | + $not_enriched_count = $wpdb->get_var(" |
|
| 34 | 34 | SELECT COUNT( 1 ) FROM $wpdb->posts p |
| 35 | 35 | WHERE p.post_type = 'post' |
| 36 | 36 | AND p.post_status <> 'trash' |
@@ -40,9 +40,9 @@ discard block |
||
| 40 | 40 | ); |
| 41 | 41 | |
| 42 | 42 | $link = '<a href="' |
| 43 | - . esc_url( $url ) . '"' |
|
| 44 | - . ( $this->is_filter_active() ? ' class="current" aria-current="page"' : '' ) |
|
| 45 | - . '>' . esc_html( __( 'Not enriched', 'wordlift' ) ) . '</a> (' . $not_enriched_count . ')'; |
|
| 43 | + . esc_url($url).'"' |
|
| 44 | + . ($this->is_filter_active() ? ' class="current" aria-current="page"' : '') |
|
| 45 | + . '>'.esc_html(__('Not enriched', 'wordlift')).'</a> ('.$not_enriched_count.')'; |
|
| 46 | 46 | |
| 47 | 47 | $views['wl_not_enriched'] = $link; |
| 48 | 48 | |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | public function is_filter_active() { |
| 53 | - return 'no' === filter_input( INPUT_GET, self::PARAMETER_NAME ); |
|
| 53 | + return 'no' === filter_input(INPUT_GET, self::PARAMETER_NAME); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | } |
| 57 | 57 | \ No newline at end of file |
@@ -4,208 +4,208 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | class Wordlift_Products_Navigator_Shortcode_REST extends Wordlift_Shortcode_REST { |
| 6 | 6 | |
| 7 | - const CACHE_TTL = 3600; // 1 hour |
|
| 8 | - |
|
| 9 | - public function __construct() { |
|
| 10 | - parent::__construct( |
|
| 11 | - '/products-navigator', |
|
| 12 | - array( |
|
| 13 | - 'post_id' => array( |
|
| 14 | - 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 15 | - 'type' => 'integer', |
|
| 16 | - 'required' => true |
|
| 17 | - ), |
|
| 18 | - 'uniqid' => array( |
|
| 19 | - 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 20 | - 'type' => 'string', |
|
| 21 | - 'required' => true |
|
| 22 | - ), |
|
| 23 | - 'limit' => array( |
|
| 24 | - 'default' => 4, |
|
| 25 | - 'type' => 'integer', |
|
| 26 | - 'sanitize_callback' => 'absint' |
|
| 27 | - ), |
|
| 28 | - 'offset' => array( |
|
| 29 | - 'default' => 0, |
|
| 30 | - 'type' => 'integer', |
|
| 31 | - 'sanitize_callback' => 'absint' |
|
| 32 | - ), |
|
| 33 | - 'sort' => array( |
|
| 34 | - 'default' => 'ID DESC', |
|
| 35 | - 'sanitize_callback' => 'sanitize_sql_orderby' |
|
| 36 | - ), |
|
| 37 | - 'amp' => array( |
|
| 38 | - 'sanitize_callback' => 'rest_sanitize_boolean' |
|
| 39 | - ) |
|
| 40 | - ) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function get_data( $request ) { |
|
| 45 | - |
|
| 46 | - // Sanitize and set defaults |
|
| 47 | - $navigator_length = $request['limit']; |
|
| 48 | - $navigator_offset = $request['offset']; |
|
| 49 | - $order_by = $request['sort']; |
|
| 50 | - $post_id = $request['post_id']; |
|
| 51 | - $navigator_id = $request['uniqid']; |
|
| 52 | - $amp = $request['amp']; |
|
| 53 | - |
|
| 54 | - $post = get_post( $post_id ); |
|
| 55 | - |
|
| 56 | - // Post ID has to match an existing item |
|
| 57 | - if ( null === $post ) { |
|
| 58 | - return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Determine navigator type and call respective get_*_results |
|
| 62 | - if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | - $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 64 | - 'ID', |
|
| 65 | - 'post_title', |
|
| 66 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 67 | - } else { |
|
| 68 | - $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 69 | - 'ID', |
|
| 70 | - 'post_title', |
|
| 71 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - // Fetch directly referencing posts excluding referencing posts via entities |
|
| 75 | - $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 76 | - return $referencing_post->ID; |
|
| 77 | - }, $referencing_posts ) ); |
|
| 78 | - |
|
| 79 | - // Combine directly referencing posts and referencing posts via entities |
|
| 80 | - $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 81 | - |
|
| 82 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 83 | - $results = array(); |
|
| 84 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | - $product = wc_get_product( $referencing_post->ID ); |
|
| 87 | - |
|
| 88 | - $result = array( |
|
| 89 | - 'product' => array( |
|
| 90 | - 'id' => $referencing_post->ID, |
|
| 91 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 92 | - 'title' => $referencing_post->post_title, |
|
| 93 | - 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 94 | - 'regular_price' => $product->get_regular_price(), |
|
| 95 | - 'sale_price' => $product->get_sale_price(), |
|
| 96 | - 'price' => $product->get_price(), |
|
| 97 | - 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 98 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 99 | - 'average_rating' => $product->get_average_rating(), |
|
| 100 | - 'rating_count' => $product->get_rating_count(), |
|
| 101 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 102 | - ), |
|
| 103 | - 'entity' => array( |
|
| 104 | - 'id' => $referencing_post->entity_id, |
|
| 105 | - 'label' => $serialized_entity['label'], |
|
| 106 | - 'mainType' => $serialized_entity['mainType'], |
|
| 107 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 108 | - ), |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $results[] = $result; |
|
| 112 | - |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( count( $results ) < $navigator_length ) { |
|
| 116 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - // Add filler posts if needed |
|
| 121 | - $filler_count = $navigator_length - count( $results ); |
|
| 122 | - if ( $filler_count > 0 ) { |
|
| 123 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 124 | - return $p->ID; |
|
| 125 | - }, $referencing_posts ); |
|
| 126 | - /** |
|
| 127 | - * @since 3.28.0 |
|
| 128 | - * Filler posts are fetched using this util. |
|
| 129 | - */ |
|
| 130 | - $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | - $results = array_merge( $results, $filler_posts ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Apply filters after fillers are added |
|
| 137 | - foreach ( $results as $result_index => $result ) { |
|
| 138 | - $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 143 | - |
|
| 144 | - return $amp ? array( |
|
| 145 | - 'items' => array( |
|
| 146 | - array( 'values' => $results ), |
|
| 147 | - ), |
|
| 148 | - ) : $results; |
|
| 149 | - |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 153 | - |
|
| 154 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 155 | - |
|
| 156 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 157 | - |
|
| 158 | - $directly_referencing_posts = get_posts( array( |
|
| 159 | - 'meta_query' => array( |
|
| 160 | - array( |
|
| 161 | - 'key' => '_thumbnail_id' |
|
| 162 | - ), |
|
| 163 | - array( |
|
| 164 | - 'key' => '_stock_status', |
|
| 165 | - 'value' => 'instock' |
|
| 166 | - ) |
|
| 167 | - ), |
|
| 168 | - 'post__in' => $post__in, |
|
| 169 | - 'post_type' => 'product', |
|
| 170 | - 'ignore_sticky_posts' => 1 |
|
| 171 | - ) ); |
|
| 172 | - |
|
| 173 | - $results = array(); |
|
| 174 | - |
|
| 175 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 176 | - $result = new stdClass(); |
|
| 177 | - $result->ID = $post->ID; |
|
| 178 | - $result->post_title = $post->post_title; |
|
| 179 | - $result->entity_id = $post->ID; |
|
| 180 | - $results[] = $result; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $results; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - private function get_entity_results( |
|
| 187 | - $post_id, |
|
| 188 | - $fields = array( |
|
| 189 | - 'ID', |
|
| 190 | - 'post_title', |
|
| 191 | - ), |
|
| 192 | - $order_by = 'ID DESC', |
|
| 193 | - $limit = 10, |
|
| 194 | - $offset = 0 |
|
| 195 | - ) { |
|
| 196 | - global $wpdb; |
|
| 197 | - |
|
| 198 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 199 | - return "p.$item AS $item"; |
|
| 200 | - }, (array) $fields ) ); |
|
| 201 | - |
|
| 202 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 203 | - return "p.$item"; |
|
| 204 | - }, (array) $order_by ) ); |
|
| 205 | - |
|
| 206 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 207 | - return $wpdb->get_results( |
|
| 208 | - $wpdb->prepare( " |
|
| 7 | + const CACHE_TTL = 3600; // 1 hour |
|
| 8 | + |
|
| 9 | + public function __construct() { |
|
| 10 | + parent::__construct( |
|
| 11 | + '/products-navigator', |
|
| 12 | + array( |
|
| 13 | + 'post_id' => array( |
|
| 14 | + 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 15 | + 'type' => 'integer', |
|
| 16 | + 'required' => true |
|
| 17 | + ), |
|
| 18 | + 'uniqid' => array( |
|
| 19 | + 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 20 | + 'type' => 'string', |
|
| 21 | + 'required' => true |
|
| 22 | + ), |
|
| 23 | + 'limit' => array( |
|
| 24 | + 'default' => 4, |
|
| 25 | + 'type' => 'integer', |
|
| 26 | + 'sanitize_callback' => 'absint' |
|
| 27 | + ), |
|
| 28 | + 'offset' => array( |
|
| 29 | + 'default' => 0, |
|
| 30 | + 'type' => 'integer', |
|
| 31 | + 'sanitize_callback' => 'absint' |
|
| 32 | + ), |
|
| 33 | + 'sort' => array( |
|
| 34 | + 'default' => 'ID DESC', |
|
| 35 | + 'sanitize_callback' => 'sanitize_sql_orderby' |
|
| 36 | + ), |
|
| 37 | + 'amp' => array( |
|
| 38 | + 'sanitize_callback' => 'rest_sanitize_boolean' |
|
| 39 | + ) |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function get_data( $request ) { |
|
| 45 | + |
|
| 46 | + // Sanitize and set defaults |
|
| 47 | + $navigator_length = $request['limit']; |
|
| 48 | + $navigator_offset = $request['offset']; |
|
| 49 | + $order_by = $request['sort']; |
|
| 50 | + $post_id = $request['post_id']; |
|
| 51 | + $navigator_id = $request['uniqid']; |
|
| 52 | + $amp = $request['amp']; |
|
| 53 | + |
|
| 54 | + $post = get_post( $post_id ); |
|
| 55 | + |
|
| 56 | + // Post ID has to match an existing item |
|
| 57 | + if ( null === $post ) { |
|
| 58 | + return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Determine navigator type and call respective get_*_results |
|
| 62 | + if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | + $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 64 | + 'ID', |
|
| 65 | + 'post_title', |
|
| 66 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 67 | + } else { |
|
| 68 | + $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 69 | + 'ID', |
|
| 70 | + 'post_title', |
|
| 71 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + // Fetch directly referencing posts excluding referencing posts via entities |
|
| 75 | + $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 76 | + return $referencing_post->ID; |
|
| 77 | + }, $referencing_posts ) ); |
|
| 78 | + |
|
| 79 | + // Combine directly referencing posts and referencing posts via entities |
|
| 80 | + $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 81 | + |
|
| 82 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 83 | + $results = array(); |
|
| 84 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | + $product = wc_get_product( $referencing_post->ID ); |
|
| 87 | + |
|
| 88 | + $result = array( |
|
| 89 | + 'product' => array( |
|
| 90 | + 'id' => $referencing_post->ID, |
|
| 91 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 92 | + 'title' => $referencing_post->post_title, |
|
| 93 | + 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 94 | + 'regular_price' => $product->get_regular_price(), |
|
| 95 | + 'sale_price' => $product->get_sale_price(), |
|
| 96 | + 'price' => $product->get_price(), |
|
| 97 | + 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 98 | + 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 99 | + 'average_rating' => $product->get_average_rating(), |
|
| 100 | + 'rating_count' => $product->get_rating_count(), |
|
| 101 | + 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 102 | + ), |
|
| 103 | + 'entity' => array( |
|
| 104 | + 'id' => $referencing_post->entity_id, |
|
| 105 | + 'label' => $serialized_entity['label'], |
|
| 106 | + 'mainType' => $serialized_entity['mainType'], |
|
| 107 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 108 | + ), |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $results[] = $result; |
|
| 112 | + |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( count( $results ) < $navigator_length ) { |
|
| 116 | + $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + // Add filler posts if needed |
|
| 121 | + $filler_count = $navigator_length - count( $results ); |
|
| 122 | + if ( $filler_count > 0 ) { |
|
| 123 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 124 | + return $p->ID; |
|
| 125 | + }, $referencing_posts ); |
|
| 126 | + /** |
|
| 127 | + * @since 3.28.0 |
|
| 128 | + * Filler posts are fetched using this util. |
|
| 129 | + */ |
|
| 130 | + $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | + $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | + $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | + $results = array_merge( $results, $filler_posts ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Apply filters after fillers are added |
|
| 137 | + foreach ( $results as $result_index => $result ) { |
|
| 138 | + $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 143 | + |
|
| 144 | + return $amp ? array( |
|
| 145 | + 'items' => array( |
|
| 146 | + array( 'values' => $results ), |
|
| 147 | + ), |
|
| 148 | + ) : $results; |
|
| 149 | + |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 153 | + |
|
| 154 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 155 | + |
|
| 156 | + $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 157 | + |
|
| 158 | + $directly_referencing_posts = get_posts( array( |
|
| 159 | + 'meta_query' => array( |
|
| 160 | + array( |
|
| 161 | + 'key' => '_thumbnail_id' |
|
| 162 | + ), |
|
| 163 | + array( |
|
| 164 | + 'key' => '_stock_status', |
|
| 165 | + 'value' => 'instock' |
|
| 166 | + ) |
|
| 167 | + ), |
|
| 168 | + 'post__in' => $post__in, |
|
| 169 | + 'post_type' => 'product', |
|
| 170 | + 'ignore_sticky_posts' => 1 |
|
| 171 | + ) ); |
|
| 172 | + |
|
| 173 | + $results = array(); |
|
| 174 | + |
|
| 175 | + foreach ( $directly_referencing_posts as $post ) { |
|
| 176 | + $result = new stdClass(); |
|
| 177 | + $result->ID = $post->ID; |
|
| 178 | + $result->post_title = $post->post_title; |
|
| 179 | + $result->entity_id = $post->ID; |
|
| 180 | + $results[] = $result; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $results; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + private function get_entity_results( |
|
| 187 | + $post_id, |
|
| 188 | + $fields = array( |
|
| 189 | + 'ID', |
|
| 190 | + 'post_title', |
|
| 191 | + ), |
|
| 192 | + $order_by = 'ID DESC', |
|
| 193 | + $limit = 10, |
|
| 194 | + $offset = 0 |
|
| 195 | + ) { |
|
| 196 | + global $wpdb; |
|
| 197 | + |
|
| 198 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 199 | + return "p.$item AS $item"; |
|
| 200 | + }, (array) $fields ) ); |
|
| 201 | + |
|
| 202 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 203 | + return "p.$item"; |
|
| 204 | + }, (array) $order_by ) ); |
|
| 205 | + |
|
| 206 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 207 | + return $wpdb->get_results( |
|
| 208 | + $wpdb->prepare( " |
|
| 209 | 209 | SELECT %4\$s, p2.ID as entity_id |
| 210 | 210 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 211 | 211 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -238,33 +238,33 @@ discard block |
||
| 238 | 238 | LIMIT %2\$d |
| 239 | 239 | OFFSET %3\$d |
| 240 | 240 | " |
| 241 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - private function get_post_results( |
|
| 246 | - $post_id, |
|
| 247 | - $fields = array( |
|
| 248 | - 'ID', |
|
| 249 | - 'post_title', |
|
| 250 | - ), |
|
| 251 | - $order_by = 'ID DESC', |
|
| 252 | - $limit = 10, |
|
| 253 | - $offset = 0 |
|
| 254 | - ) { |
|
| 255 | - global $wpdb; |
|
| 256 | - |
|
| 257 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 258 | - return "p.$item AS $item"; |
|
| 259 | - }, (array) $fields ) ); |
|
| 260 | - |
|
| 261 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 262 | - return "p.$item"; |
|
| 263 | - }, (array) $order_by ) ); |
|
| 264 | - |
|
| 265 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 266 | - return $wpdb->get_results( |
|
| 267 | - $wpdb->prepare( " |
|
| 241 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + private function get_post_results( |
|
| 246 | + $post_id, |
|
| 247 | + $fields = array( |
|
| 248 | + 'ID', |
|
| 249 | + 'post_title', |
|
| 250 | + ), |
|
| 251 | + $order_by = 'ID DESC', |
|
| 252 | + $limit = 10, |
|
| 253 | + $offset = 0 |
|
| 254 | + ) { |
|
| 255 | + global $wpdb; |
|
| 256 | + |
|
| 257 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 258 | + return "p.$item AS $item"; |
|
| 259 | + }, (array) $fields ) ); |
|
| 260 | + |
|
| 261 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 262 | + return "p.$item"; |
|
| 263 | + }, (array) $order_by ) ); |
|
| 264 | + |
|
| 265 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 266 | + return $wpdb->get_results( |
|
| 267 | + $wpdb->prepare( " |
|
| 268 | 268 | SELECT %4\$s, p2.ID as entity_id |
| 269 | 269 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 270 | 270 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -300,8 +300,8 @@ discard block |
||
| 300 | 300 | LIMIT %2\$d |
| 301 | 301 | OFFSET %3\$d |
| 302 | 302 | " |
| 303 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 304 | - ); |
|
| 305 | - } |
|
| 303 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 304 | + ); |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | 307 | } |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | '/products-navigator', |
| 12 | 12 | array( |
| 13 | 13 | 'post_id' => array( |
| 14 | - 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 14 | + 'description' => __('Post ID for which Navigator has to be queried', 'wordlift'), |
|
| 15 | 15 | 'type' => 'integer', |
| 16 | 16 | 'required' => true |
| 17 | 17 | ), |
| 18 | 18 | 'uniqid' => array( |
| 19 | - 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 19 | + 'description' => __('Navigator uniqueid', 'wordlift'), |
|
| 20 | 20 | 'type' => 'string', |
| 21 | 21 | 'required' => true |
| 22 | 22 | ), |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | ); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function get_data( $request ) { |
|
| 44 | + public function get_data($request) { |
|
| 45 | 45 | |
| 46 | 46 | // Sanitize and set defaults |
| 47 | 47 | $navigator_length = $request['limit']; |
@@ -51,60 +51,60 @@ discard block |
||
| 51 | 51 | $navigator_id = $request['uniqid']; |
| 52 | 52 | $amp = $request['amp']; |
| 53 | 53 | |
| 54 | - $post = get_post( $post_id ); |
|
| 54 | + $post = get_post($post_id); |
|
| 55 | 55 | |
| 56 | 56 | // Post ID has to match an existing item |
| 57 | - if ( null === $post ) { |
|
| 58 | - return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 57 | + if (null === $post) { |
|
| 58 | + return new WP_Error('rest_invalid_post_id', __('Invalid post_id', 'wordlift'), array('status' => 404)); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Determine navigator type and call respective get_*_results |
| 62 | - if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | - $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 62 | + if (get_post_type($post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 63 | + $referencing_posts = $this->get_entity_results($post_id, array( |
|
| 64 | 64 | 'ID', |
| 65 | 65 | 'post_title', |
| 66 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 66 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 67 | 67 | } else { |
| 68 | - $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 68 | + $referencing_posts = $this->get_post_results($post_id, array( |
|
| 69 | 69 | 'ID', |
| 70 | 70 | 'post_title', |
| 71 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 71 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | // Fetch directly referencing posts excluding referencing posts via entities |
| 75 | - $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 75 | + $directly_referencing_posts = $this->get_directly_referencing_posts($post_id, array_map(function($referencing_post) { |
|
| 76 | 76 | return $referencing_post->ID; |
| 77 | - }, $referencing_posts ) ); |
|
| 77 | + }, $referencing_posts)); |
|
| 78 | 78 | |
| 79 | 79 | // Combine directly referencing posts and referencing posts via entities |
| 80 | - $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 80 | + $referencing_posts = array_merge($directly_referencing_posts, $referencing_posts); |
|
| 81 | 81 | |
| 82 | 82 | // loop over them and take the first one which is not already in the $related_posts |
| 83 | 83 | $results = array(); |
| 84 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | - $product = wc_get_product( $referencing_post->ID ); |
|
| 84 | + foreach ($referencing_posts as $referencing_post) { |
|
| 85 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 86 | + $product = wc_get_product($referencing_post->ID); |
|
| 87 | 87 | |
| 88 | 88 | $result = array( |
| 89 | 89 | 'product' => array( |
| 90 | 90 | 'id' => $referencing_post->ID, |
| 91 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 91 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 92 | 92 | 'title' => $referencing_post->post_title, |
| 93 | - 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 93 | + 'thumbnail' => get_the_post_thumbnail_url($referencing_post, 'medium'), |
|
| 94 | 94 | 'regular_price' => $product->get_regular_price(), |
| 95 | 95 | 'sale_price' => $product->get_sale_price(), |
| 96 | 96 | 'price' => $product->get_price(), |
| 97 | 97 | 'currency_symbol' => get_woocommerce_currency_symbol(), |
| 98 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 98 | + 'discount_pc' => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0, |
|
| 99 | 99 | 'average_rating' => $product->get_average_rating(), |
| 100 | 100 | 'rating_count' => $product->get_rating_count(), |
| 101 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 101 | + 'rating_html' => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count()) |
|
| 102 | 102 | ), |
| 103 | 103 | 'entity' => array( |
| 104 | 104 | 'id' => $referencing_post->entity_id, |
| 105 | 105 | 'label' => $serialized_entity['label'], |
| 106 | 106 | 'mainType' => $serialized_entity['mainType'], |
| 107 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 107 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 108 | 108 | ), |
| 109 | 109 | ); |
| 110 | 110 | |
@@ -112,50 +112,50 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - if ( count( $results ) < $navigator_length ) { |
|
| 116 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 115 | + if (count($results) < $navigator_length) { |
|
| 116 | + $results = apply_filters('wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | // Add filler posts if needed |
| 121 | - $filler_count = $navigator_length - count( $results ); |
|
| 122 | - if ( $filler_count > 0 ) { |
|
| 123 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 121 | + $filler_count = $navigator_length - count($results); |
|
| 122 | + if ($filler_count > 0) { |
|
| 123 | + $referencing_post_ids = array_map(function($p) { |
|
| 124 | 124 | return $p->ID; |
| 125 | - }, $referencing_posts ); |
|
| 125 | + }, $referencing_posts); |
|
| 126 | 126 | /** |
| 127 | 127 | * @since 3.28.0 |
| 128 | 128 | * Filler posts are fetched using this util. |
| 129 | 129 | */ |
| 130 | - $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | - $results = array_merge( $results, $filler_posts ); |
|
| 130 | + $filler_posts_util = new Filler_Posts_Util($post_id, 'product'); |
|
| 131 | + $post_ids_to_be_excluded = array_merge(array($post_id), $referencing_post_ids); |
|
| 132 | + $filler_posts = $filler_posts_util->get_product_navigator_response($filler_count, $post_ids_to_be_excluded); |
|
| 133 | + $results = array_merge($results, $filler_posts); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // Apply filters after fillers are added |
| 137 | - foreach ( $results as $result_index => $result ) { |
|
| 138 | - $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 137 | + foreach ($results as $result_index => $result) { |
|
| 138 | + $results[$result_index]['product'] = apply_filters('wl_products_navigator_data_post', $result['product'], intval($result['product']['id']), $navigator_id); |
|
| 139 | + $results[$result_index]['entity'] = apply_filters('wl_products_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 142 | + $results = apply_filters('wl_products_navigator_results', $results, $navigator_id); |
|
| 143 | 143 | |
| 144 | 144 | return $amp ? array( |
| 145 | 145 | 'items' => array( |
| 146 | - array( 'values' => $results ), |
|
| 146 | + array('values' => $results), |
|
| 147 | 147 | ), |
| 148 | 148 | ) : $results; |
| 149 | 149 | |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 152 | + private function get_directly_referencing_posts($post_id, $referencing_post_ids) { |
|
| 153 | 153 | |
| 154 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 154 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities($post_id); |
|
| 155 | 155 | |
| 156 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 156 | + $post__in = array_diff($directly_referencing_post_ids, $referencing_post_ids); |
|
| 157 | 157 | |
| 158 | - $directly_referencing_posts = get_posts( array( |
|
| 158 | + $directly_referencing_posts = get_posts(array( |
|
| 159 | 159 | 'meta_query' => array( |
| 160 | 160 | array( |
| 161 | 161 | 'key' => '_thumbnail_id' |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | 'post__in' => $post__in, |
| 169 | 169 | 'post_type' => 'product', |
| 170 | 170 | 'ignore_sticky_posts' => 1 |
| 171 | - ) ); |
|
| 171 | + )); |
|
| 172 | 172 | |
| 173 | 173 | $results = array(); |
| 174 | 174 | |
| 175 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 175 | + foreach ($directly_referencing_posts as $post) { |
|
| 176 | 176 | $result = new stdClass(); |
| 177 | 177 | $result->ID = $post->ID; |
| 178 | 178 | $result->post_title = $post->post_title; |
@@ -195,17 +195,17 @@ discard block |
||
| 195 | 195 | ) { |
| 196 | 196 | global $wpdb; |
| 197 | 197 | |
| 198 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 198 | + $select = implode(', ', array_map(function($item) { |
|
| 199 | 199 | return "p.$item AS $item"; |
| 200 | - }, (array) $fields ) ); |
|
| 200 | + }, (array) $fields)); |
|
| 201 | 201 | |
| 202 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 202 | + $order_by = implode(', ', array_map(function($item) { |
|
| 203 | 203 | return "p.$item"; |
| 204 | - }, (array) $order_by ) ); |
|
| 204 | + }, (array) $order_by)); |
|
| 205 | 205 | |
| 206 | 206 | /** @noinspection SqlNoDataSourceInspection */ |
| 207 | 207 | return $wpdb->get_results( |
| 208 | - $wpdb->prepare( " |
|
| 208 | + $wpdb->prepare(" |
|
| 209 | 209 | SELECT %4\$s, p2.ID as entity_id |
| 210 | 210 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 211 | 211 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | LIMIT %2\$d |
| 239 | 239 | OFFSET %3\$d |
| 240 | 240 | " |
| 241 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 241 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 242 | 242 | ); |
| 243 | 243 | } |
| 244 | 244 | |
@@ -254,17 +254,17 @@ discard block |
||
| 254 | 254 | ) { |
| 255 | 255 | global $wpdb; |
| 256 | 256 | |
| 257 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 257 | + $select = implode(', ', array_map(function($item) { |
|
| 258 | 258 | return "p.$item AS $item"; |
| 259 | - }, (array) $fields ) ); |
|
| 259 | + }, (array) $fields)); |
|
| 260 | 260 | |
| 261 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 261 | + $order_by = implode(', ', array_map(function($item) { |
|
| 262 | 262 | return "p.$item"; |
| 263 | - }, (array) $order_by ) ); |
|
| 263 | + }, (array) $order_by)); |
|
| 264 | 264 | |
| 265 | 265 | /** @noinspection SqlNoDataSourceInspection */ |
| 266 | 266 | return $wpdb->get_results( |
| 267 | - $wpdb->prepare( " |
|
| 267 | + $wpdb->prepare(" |
|
| 268 | 268 | SELECT %4\$s, p2.ID as entity_id |
| 269 | 269 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 270 | 270 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | LIMIT %2\$d |
| 301 | 301 | OFFSET %3\$d |
| 302 | 302 | " |
| 303 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 303 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 304 | 304 | ); |
| 305 | 305 | } |
| 306 | 306 | |
@@ -20,32 +20,32 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_navigator_data() { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key_params = $_REQUEST; |
|
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key_params = $_REQUEST; |
|
| 25 | + unset( $cache_key_params['uniqid'] ); |
|
| 26 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - // So that the endpoint can be used remotely |
|
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 32 | + // So that the endpoint can be used remotely |
|
| 33 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if ( isset( $cache_results ) ) { |
|
| 36 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | 37 | |
| 38 | - return $cache_results; |
|
| 39 | - } |
|
| 38 | + return $cache_results; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 42 | 42 | |
| 43 | - $results = _wl_navigator_get_data(); |
|
| 43 | + $results = _wl_navigator_get_data(); |
|
| 44 | 44 | |
| 45 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | - $cache->put( $cache_key, $results ); |
|
| 45 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | + $cache->put( $cache_key, $results ); |
|
| 47 | 47 | |
| 48 | - return $results; |
|
| 48 | + return $results; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -60,240 +60,240 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | function wl_network_navigator_wp_json( $request ) { |
| 62 | 62 | |
| 63 | - // Create the cache key. |
|
| 64 | - $cache_key_params = $_REQUEST; |
|
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 63 | + // Create the cache key. |
|
| 64 | + $cache_key_params = $_REQUEST; |
|
| 65 | + unset( $cache_key_params['uniqid'] ); |
|
| 66 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 67 | 67 | |
| 68 | - // Create the TTL cache and try to get the results. |
|
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 68 | + // Create the TTL cache and try to get the results. |
|
| 69 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | + $cache_results = $cache->get( $cache_key ); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if ( isset( $cache_results ) ) { |
|
| 73 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 74 | 74 | |
| 75 | - return $cache_results; |
|
| 76 | - } |
|
| 75 | + return $cache_results; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 81 | 81 | |
| 82 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | - $cache->put( $cache_key, $results ); |
|
| 82 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | + $cache->put( $cache_key, $results ); |
|
| 84 | 84 | |
| 85 | - return $results; |
|
| 85 | + return $results; |
|
| 86 | 86 | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | - // Post ID must be defined |
|
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 94 | - |
|
| 95 | - return array(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // Post ID must be defined |
|
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 101 | - |
|
| 102 | - return array(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Limit the results (defaults to 4) |
|
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 111 | - $existing_post_types = get_post_types(); |
|
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | - $current_post_id = (int) $_GET['post_id']; |
|
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 115 | - |
|
| 116 | - $navigator_id = (string) $_GET['uniqid']; |
|
| 117 | - |
|
| 118 | - // Post ID has to match an existing item |
|
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | - |
|
| 122 | - return array(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Determine navigator type and call respective _get_results |
|
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | - |
|
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | - 'ID', |
|
| 130 | - 'post_title', |
|
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | - } else { |
|
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | - 'ID', |
|
| 135 | - 'post_title', |
|
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | - $results = array(); |
|
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Use the thumbnail. |
|
| 147 | - * |
|
| 148 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | - * |
|
| 151 | - * @since 3.19.3 We're using the medium size image. |
|
| 152 | - */ |
|
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | - |
|
| 155 | - $result = array( |
|
| 156 | - 'post' => array( |
|
| 157 | - 'id' => $referencing_post->ID, |
|
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 160 | - 'thumbnail' => $thumbnail, |
|
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | - ), |
|
| 163 | - 'entity' => array( |
|
| 164 | - 'id' => $referencing_post->entity_id, |
|
| 165 | - 'label' => $serialized_entity['label'], |
|
| 166 | - 'mainType' => $serialized_entity['mainType'], |
|
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | - ), |
|
| 169 | - ); |
|
| 170 | - |
|
| 171 | - $results[] = $result; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Add filler posts if needed |
|
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | - return $p->ID; |
|
| 184 | - }, $referencing_posts ); |
|
| 185 | - /** |
|
| 186 | - * @since 3.27.8 |
|
| 187 | - * Filler posts are fetched using this util. |
|
| 188 | - */ |
|
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Apply filters after fillers are added |
|
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 202 | - |
|
| 203 | - return $results; |
|
| 91 | + // Post ID must be defined |
|
| 92 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | + wp_send_json_error( 'No post_id given' ); |
|
| 94 | + |
|
| 95 | + return array(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // Post ID must be defined |
|
| 99 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | + wp_send_json_error( 'No uniqid given' ); |
|
| 101 | + |
|
| 102 | + return array(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Limit the results (defaults to 4) |
|
| 106 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode( ',', $post_types ); |
|
| 111 | + $existing_post_types = get_post_types(); |
|
| 112 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | + $current_post_id = (int) $_GET['post_id']; |
|
| 114 | + $current_post = get_post( $current_post_id ); |
|
| 115 | + |
|
| 116 | + $navigator_id = (string) $_GET['uniqid']; |
|
| 117 | + |
|
| 118 | + // Post ID has to match an existing item |
|
| 119 | + if ( null === $current_post ) { |
|
| 120 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | + |
|
| 122 | + return array(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Determine navigator type and call respective _get_results |
|
| 126 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | + |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | + 'ID', |
|
| 130 | + 'post_title', |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | + } else { |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | + 'ID', |
|
| 135 | + 'post_title', |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | + $results = array(); |
|
| 142 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Use the thumbnail. |
|
| 147 | + * |
|
| 148 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | + * |
|
| 151 | + * @since 3.19.3 We're using the medium size image. |
|
| 152 | + */ |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | + |
|
| 155 | + $result = array( |
|
| 156 | + 'post' => array( |
|
| 157 | + 'id' => $referencing_post->ID, |
|
| 158 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | + 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 160 | + 'thumbnail' => $thumbnail, |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | + ), |
|
| 163 | + 'entity' => array( |
|
| 164 | + 'id' => $referencing_post->entity_id, |
|
| 165 | + 'label' => $serialized_entity['label'], |
|
| 166 | + 'mainType' => $serialized_entity['mainType'], |
|
| 167 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | + ), |
|
| 169 | + ); |
|
| 170 | + |
|
| 171 | + $results[] = $result; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + if ( count( $results ) < $navigator_length ) { |
|
| 176 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Add filler posts if needed |
|
| 180 | + $filler_count = $navigator_length - count( $results ); |
|
| 181 | + if ( $filler_count > 0 ) { |
|
| 182 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | + return $p->ID; |
|
| 184 | + }, $referencing_posts ); |
|
| 185 | + /** |
|
| 186 | + * @since 3.27.8 |
|
| 187 | + * Filler posts are fetched using this util. |
|
| 188 | + */ |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | + $results = array_merge( $results, $filler_posts ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Apply filters after fillers are added |
|
| 196 | + foreach ( $results as $result_index => $result ) { |
|
| 197 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 202 | + |
|
| 203 | + return $results; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | function _wl_network_navigator_get_data( $request ) { |
| 207 | 207 | |
| 208 | - // Limit the results (defaults to 4) |
|
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | - $navigator_id = $request['uniqid']; |
|
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | - |
|
| 214 | - $entities = $request['entities']; |
|
| 215 | - |
|
| 216 | - // Post ID has to match an existing item |
|
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | - 'ID', |
|
| 223 | - 'post_title', |
|
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | - |
|
| 226 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | - $results = array(); |
|
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Use the thumbnail. |
|
| 233 | - * |
|
| 234 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | - * |
|
| 237 | - * @since 3.19.3 We're using the medium size image. |
|
| 238 | - */ |
|
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | - |
|
| 241 | - $result = array( |
|
| 242 | - 'post' => array( |
|
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | - 'title' => $referencing_post->post_title, |
|
| 245 | - 'thumbnail' => $thumbnail, |
|
| 246 | - ), |
|
| 247 | - 'entity' => array( |
|
| 248 | - 'label' => $serialized_entity['label'], |
|
| 249 | - 'mainType' => $serialized_entity['mainType'], |
|
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | - ), |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | - |
|
| 257 | - $results[] = $result; |
|
| 258 | - |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | - |
|
| 267 | - return $results; |
|
| 208 | + // Limit the results (defaults to 4) |
|
| 209 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | + $navigator_id = $request['uniqid']; |
|
| 212 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | + |
|
| 214 | + $entities = $request['entities']; |
|
| 215 | + |
|
| 216 | + // Post ID has to match an existing item |
|
| 217 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | + 'ID', |
|
| 223 | + 'post_title', |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | + |
|
| 226 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | + $results = array(); |
|
| 228 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Use the thumbnail. |
|
| 233 | + * |
|
| 234 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | + * |
|
| 237 | + * @since 3.19.3 We're using the medium size image. |
|
| 238 | + */ |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | + |
|
| 241 | + $result = array( |
|
| 242 | + 'post' => array( |
|
| 243 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | + 'title' => $referencing_post->post_title, |
|
| 245 | + 'thumbnail' => $thumbnail, |
|
| 246 | + ), |
|
| 247 | + 'entity' => array( |
|
| 248 | + 'label' => $serialized_entity['label'], |
|
| 249 | + 'mainType' => $serialized_entity['mainType'], |
|
| 250 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | + ), |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | + |
|
| 257 | + $results[] = $result; |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + if ( count( $results ) < $navigator_length ) { |
|
| 262 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | + |
|
| 267 | + return $results; |
|
| 268 | 268 | |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | function _wl_network_navigator_get_results( |
| 272 | - $entities, $fields = array( |
|
| 273 | - 'ID', |
|
| 274 | - 'post_title', |
|
| 272 | + $entities, $fields = array( |
|
| 273 | + 'ID', |
|
| 274 | + 'post_title', |
|
| 275 | 275 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 276 | 276 | ) { |
| 277 | - global $wpdb; |
|
| 278 | - |
|
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | - return "p.$item AS $item"; |
|
| 281 | - }, (array) $fields ) ); |
|
| 282 | - |
|
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | - return "p.$item"; |
|
| 285 | - }, (array) $order_by ) ); |
|
| 286 | - |
|
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 290 | - return $entity->ID; |
|
| 291 | - } |
|
| 292 | - }, $entities ) ); |
|
| 293 | - |
|
| 294 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | - return $wpdb->get_results( |
|
| 296 | - $wpdb->prepare( " |
|
| 277 | + global $wpdb; |
|
| 278 | + |
|
| 279 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | + return "p.$item AS $item"; |
|
| 281 | + }, (array) $fields ) ); |
|
| 282 | + |
|
| 283 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | + return "p.$item"; |
|
| 285 | + }, (array) $order_by ) ); |
|
| 286 | + |
|
| 287 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | + if ( isset( $entity ) ) { |
|
| 290 | + return $entity->ID; |
|
| 291 | + } |
|
| 292 | + }, $entities ) ); |
|
| 293 | + |
|
| 294 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | + return $wpdb->get_results( |
|
| 296 | + $wpdb->prepare( " |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | " |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 323 | - ); |
|
| 322 | + , $limit, $offset, $select, $order_by ) |
|
| 323 | + ); |
|
| 324 | 324 | |
| 325 | 325 | } |
| 326 | 326 | |
@@ -332,9 +332,9 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | function wl_shortcode_navigator_ajax() { |
| 334 | 334 | |
| 335 | - // Temporary blocking the Navigator. |
|
| 336 | - $results = wl_shortcode_navigator_data(); |
|
| 337 | - wl_core_send_json( $results ); |
|
| 335 | + // Temporary blocking the Navigator. |
|
| 336 | + $results = wl_shortcode_navigator_data(); |
|
| 337 | + wl_core_send_json( $results ); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | - $results = wl_shortcode_navigator_data(); |
|
| 350 | - if ( ob_get_contents() ) { |
|
| 351 | - ob_clean(); |
|
| 352 | - } |
|
| 349 | + $results = wl_shortcode_navigator_data(); |
|
| 350 | + if ( ob_get_contents() ) { |
|
| 351 | + ob_clean(); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - return array( |
|
| 355 | - 'items' => array( |
|
| 356 | - array( 'values' => $results ), |
|
| 357 | - ), |
|
| 358 | - ); |
|
| 354 | + return array( |
|
| 355 | + 'items' => array( |
|
| 356 | + array( 'values' => $results ), |
|
| 357 | + ), |
|
| 358 | + ); |
|
| 359 | 359 | |
| 360 | 360 | } |
| 361 | 361 | |
@@ -363,22 +363,22 @@ discard block |
||
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | 365 | add_action( 'rest_api_init', function () { |
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | - 'methods' => 'GET', |
|
| 368 | - 'permission_callback' => '__return_true', |
|
| 369 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | - ) ); |
|
| 366 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | + 'methods' => 'GET', |
|
| 368 | + 'permission_callback' => '__return_true', |
|
| 369 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | + ) ); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | 376 | add_action( 'rest_api_init', function () { |
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | - 'methods' => 'GET', |
|
| 379 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | - 'permission_callback' => '__return_true', |
|
| 381 | - ) ); |
|
| 377 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | + 'methods' => 'GET', |
|
| 379 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | + 'permission_callback' => '__return_true', |
|
| 381 | + ) ); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -387,23 +387,23 @@ discard block |
||
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | 389 | add_action( 'plugins_loaded', function () { |
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | - return; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 390 | + $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | + return; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | 398 | }, 0 ); |
| 399 | 399 | |
| 400 | 400 | add_action( 'init', function () { |
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | - return; |
|
| 404 | - } |
|
| 401 | + $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | + return; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 406 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | 408 | }, 0 ); |
| 409 | 409 | |
@@ -22,28 +22,28 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // Create the cache key. |
| 24 | 24 | $cache_key_params = $_REQUEST; |
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 25 | + unset($cache_key_params['uniqid']); |
|
| 26 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | 32 | // So that the endpoint can be used remotely |
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 33 | + header('Access-Control-Allow-Origin: *'); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if (isset($cache_results)) { |
|
| 36 | + header('X-WordLift-Cache: HIT'); |
|
| 37 | 37 | |
| 38 | 38 | return $cache_results; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header('X-WordLift-Cache: MISS'); |
|
| 42 | 42 | |
| 43 | 43 | $results = _wl_navigator_get_data(); |
| 44 | 44 | |
| 45 | 45 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 46 | - $cache->put( $cache_key, $results ); |
|
| 46 | + $cache->put($cache_key, $results); |
|
| 47 | 47 | |
| 48 | 48 | return $results; |
| 49 | 49 | } |
@@ -58,29 +58,29 @@ discard block |
||
| 58 | 58 | * @since 3.22.6 |
| 59 | 59 | * |
| 60 | 60 | */ |
| 61 | -function wl_network_navigator_wp_json( $request ) { |
|
| 61 | +function wl_network_navigator_wp_json($request) { |
|
| 62 | 62 | |
| 63 | 63 | // Create the cache key. |
| 64 | 64 | $cache_key_params = $_REQUEST; |
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 65 | + unset($cache_key_params['uniqid']); |
|
| 66 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 67 | 67 | |
| 68 | 68 | // Create the TTL cache and try to get the results. |
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 69 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 70 | + $cache_results = $cache->get($cache_key); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if (isset($cache_results)) { |
|
| 73 | + header('X-WordLift-Cache: HIT'); |
|
| 74 | 74 | |
| 75 | 75 | return $cache_results; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header('X-WordLift-Cache: MISS'); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data($request); |
|
| 81 | 81 | |
| 82 | 82 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 83 | - $cache->put( $cache_key, $results ); |
|
| 83 | + $cache->put($cache_key, $results); |
|
| 84 | 84 | |
| 85 | 85 | return $results; |
| 86 | 86 | |
@@ -89,58 +89,58 @@ discard block |
||
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | 91 | // Post ID must be defined |
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 92 | + if ( ! isset($_GET['post_id'])) { |
|
| 93 | + wp_send_json_error('No post_id given'); |
|
| 94 | 94 | |
| 95 | 95 | return array(); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // Post ID must be defined |
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 99 | + if ( ! isset($_GET['uniqid'])) { |
|
| 100 | + wp_send_json_error('No uniqid given'); |
|
| 101 | 101 | |
| 102 | 102 | return array(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Limit the results (defaults to 4) |
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 106 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 107 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 108 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 109 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode(',', $post_types); |
|
| 111 | 111 | $existing_post_types = get_post_types(); |
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 112 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 113 | 113 | $current_post_id = (int) $_GET['post_id']; |
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 114 | + $current_post = get_post($current_post_id); |
|
| 115 | 115 | |
| 116 | 116 | $navigator_id = (string) $_GET['uniqid']; |
| 117 | 117 | |
| 118 | 118 | // Post ID has to match an existing item |
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 119 | + if (null === $current_post) { |
|
| 120 | + wp_send_json_error('No valid post_id given'); |
|
| 121 | 121 | |
| 122 | 122 | return array(); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Determine navigator type and call respective _get_results |
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 126 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 127 | 127 | |
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 129 | 129 | 'ID', |
| 130 | 130 | 'post_title', |
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 132 | 132 | } else { |
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 134 | 134 | 'ID', |
| 135 | 135 | 'post_title', |
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // loop over them and take the first one which is not already in the $related_posts |
| 141 | 141 | $results = array(); |
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 142 | + foreach ($referencing_posts as $referencing_post) { |
|
| 143 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * Use the thumbnail. |
@@ -150,21 +150,21 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.19.3 We're using the medium size image. |
| 152 | 152 | */ |
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 154 | 154 | |
| 155 | 155 | $result = array( |
| 156 | 156 | 'post' => array( |
| 157 | 157 | 'id' => $referencing_post->ID, |
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 158 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 159 | + 'title' => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'), |
|
| 160 | 160 | 'thumbnail' => $thumbnail, |
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 162 | 162 | ), |
| 163 | 163 | 'entity' => array( |
| 164 | 164 | 'id' => $referencing_post->entity_id, |
| 165 | 165 | 'label' => $serialized_entity['label'], |
| 166 | 166 | 'mainType' => $serialized_entity['mainType'], |
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 167 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 168 | 168 | ), |
| 169 | 169 | ); |
| 170 | 170 | |
@@ -172,61 +172,61 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 175 | + if (count($results) < $navigator_length) { |
|
| 176 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Add filler posts if needed |
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 180 | + $filler_count = $navigator_length - count($results); |
|
| 181 | + if ($filler_count > 0) { |
|
| 182 | + $referencing_post_ids = array_map(function($p) { |
|
| 183 | 183 | return $p->ID; |
| 184 | - }, $referencing_posts ); |
|
| 184 | + }, $referencing_posts); |
|
| 185 | 185 | /** |
| 186 | 186 | * @since 3.27.8 |
| 187 | 187 | * Filler posts are fetched using this util. |
| 188 | 188 | */ |
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util($current_post_id, $post_types); |
|
| 190 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 192 | + $results = array_merge($results, $filler_posts); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // Apply filters after fillers are added |
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 196 | + foreach ($results as $result_index => $result) { |
|
| 197 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 198 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 201 | + $results = apply_filters('wl_navigator_results', $results, $navigator_id, $current_post_id); |
|
| 202 | 202 | |
| 203 | 203 | return $results; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | -function _wl_network_navigator_get_data( $request ) { |
|
| 206 | +function _wl_network_navigator_get_data($request) { |
|
| 207 | 207 | |
| 208 | 208 | // Limit the results (defaults to 4) |
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 209 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 210 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 211 | 211 | $navigator_id = $request['uniqid']; |
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 212 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 213 | 213 | |
| 214 | 214 | $entities = $request['entities']; |
| 215 | 215 | |
| 216 | 216 | // Post ID has to match an existing item |
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 217 | + if ( ! isset($entities) || empty($entities)) { |
|
| 218 | + wp_send_json_error('No valid entities provided'); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 222 | 222 | 'ID', |
| 223 | 223 | 'post_title', |
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 225 | 225 | |
| 226 | 226 | // loop over them and take the first one which is not already in the $related_posts |
| 227 | 227 | $results = array(); |
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 228 | + foreach ($referencing_posts as $referencing_post) { |
|
| 229 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * Use the thumbnail. |
@@ -236,33 +236,33 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @since 3.19.3 We're using the medium size image. |
| 238 | 238 | */ |
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 240 | 240 | |
| 241 | 241 | $result = array( |
| 242 | 242 | 'post' => array( |
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 243 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 244 | 244 | 'title' => $referencing_post->post_title, |
| 245 | 245 | 'thumbnail' => $thumbnail, |
| 246 | 246 | ), |
| 247 | 247 | 'entity' => array( |
| 248 | 248 | 'label' => $serialized_entity['label'], |
| 249 | 249 | 'mainType' => $serialized_entity['mainType'], |
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 250 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 251 | 251 | ), |
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 254 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 255 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 256 | 256 | |
| 257 | 257 | $results[] = $result; |
| 258 | 258 | |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 261 | + if (count($results) < $navigator_length) { |
|
| 262 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 265 | + $results = apply_filters('wl_network_navigator_results', $results, $navigator_id); |
|
| 266 | 266 | |
| 267 | 267 | return $results; |
| 268 | 268 | |
@@ -276,24 +276,24 @@ discard block |
||
| 276 | 276 | ) { |
| 277 | 277 | global $wpdb; |
| 278 | 278 | |
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 279 | + $select = implode(', ', array_map(function($item) { |
|
| 280 | 280 | return "p.$item AS $item"; |
| 281 | - }, (array) $fields ) ); |
|
| 281 | + }, (array) $fields)); |
|
| 282 | 282 | |
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 283 | + $order_by = implode(', ', array_map(function($item) { |
|
| 284 | 284 | return "p.$item"; |
| 285 | - }, (array) $order_by ) ); |
|
| 285 | + }, (array) $order_by)); |
|
| 286 | 286 | |
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 287 | + $entities_in = implode(',', array_map(function($item) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 289 | + if (isset($entity)) { |
|
| 290 | 290 | return $entity->ID; |
| 291 | 291 | } |
| 292 | - }, $entities ) ); |
|
| 292 | + }, $entities)); |
|
| 293 | 293 | |
| 294 | 294 | /** @noinspection SqlNoDataSourceInspection */ |
| 295 | 295 | return $wpdb->get_results( |
| 296 | - $wpdb->prepare( " |
|
| 296 | + $wpdb->prepare(" |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | " |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 322 | + , $limit, $offset, $select, $order_by) |
|
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | 325 | } |
@@ -334,12 +334,12 @@ discard block |
||
| 334 | 334 | |
| 335 | 335 | // Temporary blocking the Navigator. |
| 336 | 336 | $results = wl_shortcode_navigator_data(); |
| 337 | - wl_core_send_json( $results ); |
|
| 337 | + wl_core_send_json($results); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 342 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 341 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 342 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 343 | 343 | |
| 344 | 344 | /** |
| 345 | 345 | * wp-json call for the navigator widget |
@@ -347,13 +347,13 @@ discard block |
||
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | 349 | $results = wl_shortcode_navigator_data(); |
| 350 | - if ( ob_get_contents() ) { |
|
| 350 | + if (ob_get_contents()) { |
|
| 351 | 351 | ob_clean(); |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | return array( |
| 355 | 355 | 'items' => array( |
| 356 | - array( 'values' => $results ), |
|
| 356 | + array('values' => $results), |
|
| 357 | 357 | ), |
| 358 | 358 | ); |
| 359 | 359 | |
@@ -362,23 +362,23 @@ discard block |
||
| 362 | 362 | /** |
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | -add_action( 'rest_api_init', function () { |
|
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 365 | +add_action('rest_api_init', function() { |
|
| 366 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | 367 | 'methods' => 'GET', |
| 368 | 368 | 'permission_callback' => '__return_true', |
| 369 | 369 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 370 | - ) ); |
|
| 370 | + )); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | -add_action( 'rest_api_init', function () { |
|
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 376 | +add_action('rest_api_init', function() { |
|
| 377 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | 378 | 'methods' => 'GET', |
| 379 | 379 | 'callback' => 'wl_network_navigator_wp_json', |
| 380 | 380 | 'permission_callback' => '__return_true', |
| 381 | - ) ); |
|
| 381 | + )); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,24 +386,24 @@ discard block |
||
| 386 | 386 | * |
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | -add_action( 'plugins_loaded', function () { |
|
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 389 | +add_action('plugins_loaded', function() { |
|
| 390 | + $action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 392 | 392 | return; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | -}, 0 ); |
|
| 395 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 396 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 397 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 398 | +}, 0); |
|
| 399 | 399 | |
| 400 | -add_action( 'init', function () { |
|
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 400 | +add_action('init', function() { |
|
| 401 | + $action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 403 | 403 | return; |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | -}, 0 ); |
|
| 406 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 407 | + remove_action('init', 'gglcptch_init'); |
|
| 408 | +}, 0); |
|
| 409 | 409 | |
@@ -7,39 +7,39 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 12 | - $post_types = get_post_types(); |
|
| 13 | - } |
|
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 17 | - |
|
| 18 | - return implode( ',', $post_types ); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public static function post_navigator_get_results( |
|
| 22 | - $post_id, $fields = array( |
|
| 23 | - 'ID', |
|
| 24 | - 'post_title', |
|
| 25 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | - ) { |
|
| 27 | - |
|
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | - global $wpdb; |
|
| 30 | - |
|
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | - return "p.$item AS $item"; |
|
| 33 | - }, (array) $fields ) ); |
|
| 34 | - |
|
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | - return "p.$item"; |
|
| 37 | - }, (array) $order_by ) ); |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | - return $wpdb->get_results( |
|
| 42 | - $wpdb->prepare( " |
|
| 10 | + public static function get_post_types_as_string( $post_types ) { |
|
| 11 | + if ( $post_types === array() ) { |
|
| 12 | + $post_types = get_post_types(); |
|
| 13 | + } |
|
| 14 | + $post_types = array_map( function ( $post_type ) { |
|
| 15 | + return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | + }, $post_types ); |
|
| 17 | + |
|
| 18 | + return implode( ',', $post_types ); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public static function post_navigator_get_results( |
|
| 22 | + $post_id, $fields = array( |
|
| 23 | + 'ID', |
|
| 24 | + 'post_title', |
|
| 25 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | + ) { |
|
| 27 | + |
|
| 28 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | + global $wpdb; |
|
| 30 | + |
|
| 31 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | + return "p.$item AS $item"; |
|
| 33 | + }, (array) $fields ) ); |
|
| 34 | + |
|
| 35 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | + return "p.$item"; |
|
| 37 | + }, (array) $order_by ) ); |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | + return $wpdb->get_results( |
|
| 42 | + $wpdb->prepare( " |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,33 +73,33 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | " |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | - ); |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | + ); |
|
| 78 | 78 | |
| 79 | - } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - public static function entity_navigator_get_results( |
|
| 83 | - $post_id, $fields = array( |
|
| 84 | - 'ID', |
|
| 85 | - 'post_title', |
|
| 86 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | - ) { |
|
| 88 | - global $wpdb; |
|
| 82 | + public static function entity_navigator_get_results( |
|
| 83 | + $post_id, $fields = array( |
|
| 84 | + 'ID', |
|
| 85 | + 'post_title', |
|
| 86 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | + ) { |
|
| 88 | + global $wpdb; |
|
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | - return "p.$item AS $item"; |
|
| 92 | - }, (array) $fields ) ); |
|
| 90 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | + return "p.$item AS $item"; |
|
| 92 | + }, (array) $fields ) ); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | - return "p.$item"; |
|
| 96 | - }, (array) $order_by ) ); |
|
| 94 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | + return "p.$item"; |
|
| 96 | + }, (array) $order_by ) ); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 99 | 99 | |
| 100 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | - return $wpdb->get_results( |
|
| 102 | - $wpdb->prepare( " |
|
| 100 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | + return $wpdb->get_results( |
|
| 102 | + $wpdb->prepare( " |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,9 +130,9 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | " |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | - ); |
|
| 135 | - } |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | |
| 138 | 138 | } |
@@ -7,15 +7,15 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 10 | + public static function get_post_types_as_string($post_types) { |
|
| 11 | + if ($post_types === array()) { |
|
| 12 | 12 | $post_types = get_post_types(); |
| 13 | 13 | } |
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 14 | + $post_types = array_map(function($post_type) { |
|
| 15 | + return "'".esc_sql($post_type)."'"; |
|
| 16 | + }, $post_types); |
|
| 17 | 17 | |
| 18 | - return implode( ',', $post_types ); |
|
| 18 | + return implode(',', $post_types); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public static function post_navigator_get_results( |
@@ -25,21 +25,21 @@ discard block |
||
| 25 | 25 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
| 26 | 26 | ) { |
| 27 | 27 | |
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 28 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 29 | 29 | global $wpdb; |
| 30 | 30 | |
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 31 | + $select = implode(', ', array_map(function($item) { |
|
| 32 | 32 | return "p.$item AS $item"; |
| 33 | - }, (array) $fields ) ); |
|
| 33 | + }, (array) $fields)); |
|
| 34 | 34 | |
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 35 | + $order_by = implode(', ', array_map(function($item) { |
|
| 36 | 36 | return "p.$item"; |
| 37 | - }, (array) $order_by ) ); |
|
| 37 | + }, (array) $order_by)); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | /** @noinspection SqlNoDataSourceInspection */ |
| 41 | 41 | return $wpdb->get_results( |
| 42 | - $wpdb->prepare( " |
|
| 42 | + $wpdb->prepare(" |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | " |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 77 | 77 | ); |
| 78 | 78 | |
| 79 | 79 | } |
@@ -87,19 +87,19 @@ discard block |
||
| 87 | 87 | ) { |
| 88 | 88 | global $wpdb; |
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 90 | + $select = implode(', ', array_map(function($item) { |
|
| 91 | 91 | return "p.$item AS $item"; |
| 92 | - }, (array) $fields ) ); |
|
| 92 | + }, (array) $fields)); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 94 | + $order_by = implode(', ', array_map(function($item) { |
|
| 95 | 95 | return "p.$item"; |
| 96 | - }, (array) $order_by ) ); |
|
| 96 | + }, (array) $order_by)); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 99 | 99 | |
| 100 | 100 | /** @noinspection SqlNoDataSourceInspection */ |
| 101 | 101 | return $wpdb->get_results( |
| 102 | - $wpdb->prepare( " |
|
| 102 | + $wpdb->prepare(" |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | " |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 134 | 134 | ); |
| 135 | 135 | } |
| 136 | 136 | |
@@ -15,48 +15,48 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | class Entity_Type { |
| 17 | 17 | |
| 18 | - public function __construct() { |
|
| 19 | - |
|
| 20 | - $that = $this; |
|
| 21 | - |
|
| 22 | - add_action( |
|
| 23 | - 'init', |
|
| 24 | - function () use ( $that ) { |
|
| 25 | - $that->init_ui_and_save_handlers(); |
|
| 26 | - } |
|
| 27 | - ); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param $term \WP_Term |
|
| 33 | - */ |
|
| 34 | - public function render_ui( $term ) { |
|
| 35 | - |
|
| 36 | - $entity_types_text = __( 'Entity Types', 'wordlift' ); |
|
| 37 | - $selected_entity_types = get_term_meta( $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Thing should be the default selected entity type |
|
| 41 | - * when this feature is activated. |
|
| 42 | - */ |
|
| 43 | - if ( count( $selected_entity_types ) === 0 ) { |
|
| 44 | - $selected_entity_types[] = 'thing'; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $entity_type_taxonomy = Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME; |
|
| 48 | - $types = Terms_Compat::get_terms( |
|
| 49 | - $entity_type_taxonomy, |
|
| 50 | - array( |
|
| 51 | - 'taxonomy' => $entity_type_taxonomy, |
|
| 52 | - 'parent' => 0, |
|
| 53 | - 'hide_empty' => false, |
|
| 54 | - ) |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - $terms_html = Term_Checklist::render( 'tax_input[wl_entity_type]', $types, $selected_entity_types ); |
|
| 58 | - |
|
| 59 | - $template = ' |
|
| 18 | + public function __construct() { |
|
| 19 | + |
|
| 20 | + $that = $this; |
|
| 21 | + |
|
| 22 | + add_action( |
|
| 23 | + 'init', |
|
| 24 | + function () use ( $that ) { |
|
| 25 | + $that->init_ui_and_save_handlers(); |
|
| 26 | + } |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param $term \WP_Term |
|
| 33 | + */ |
|
| 34 | + public function render_ui( $term ) { |
|
| 35 | + |
|
| 36 | + $entity_types_text = __( 'Entity Types', 'wordlift' ); |
|
| 37 | + $selected_entity_types = get_term_meta( $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Thing should be the default selected entity type |
|
| 41 | + * when this feature is activated. |
|
| 42 | + */ |
|
| 43 | + if ( count( $selected_entity_types ) === 0 ) { |
|
| 44 | + $selected_entity_types[] = 'thing'; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $entity_type_taxonomy = Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME; |
|
| 48 | + $types = Terms_Compat::get_terms( |
|
| 49 | + $entity_type_taxonomy, |
|
| 50 | + array( |
|
| 51 | + 'taxonomy' => $entity_type_taxonomy, |
|
| 52 | + 'parent' => 0, |
|
| 53 | + 'hide_empty' => false, |
|
| 54 | + ) |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + $terms_html = Term_Checklist::render( 'tax_input[wl_entity_type]', $types, $selected_entity_types ); |
|
| 58 | + |
|
| 59 | + $template = ' |
|
| 60 | 60 | <tr class="form-field term-name-wrap"> |
| 61 | 61 | <th scope="row"><label for="wl-entity-type__checklist">%s</label></th> |
| 62 | 62 | <td> |
@@ -64,42 +64,42 @@ discard block |
||
| 64 | 64 | </td> |
| 65 | 65 | </tr> |
| 66 | 66 | '; |
| 67 | - echo sprintf( $template, esc_html( $entity_types_text ), $terms_html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 68 | - |
|
| 69 | - $this->enqueue_script_and_style(); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function save_field( $term_id ) { |
|
| 73 | - if ( ! isset( $_REQUEST['tax_input'] ) ) { |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - $entity_types = isset( $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] ) |
|
| 77 | - ? (array) $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] : array(); |
|
| 78 | - if ( isset( $entity_types ) && is_array( $entity_types ) ) { |
|
| 79 | - // Save the taxonomies. |
|
| 80 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 81 | - foreach ( $entity_types as $entity_type ) { |
|
| 82 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, (string) $entity_type ); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function init_ui_and_save_handlers() { |
|
| 88 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 89 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 90 | - add_action( "${taxonomy}_edit_form_fields", array( $this, 'render_ui' ), 1 ); |
|
| 91 | - add_action( "edited_${taxonomy}", array( $this, 'save_field' ) ); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - private function enqueue_script_and_style() { |
|
| 96 | - |
|
| 97 | - Scripts_Helper::enqueue_based_on_wordpress_version( 'wl-vocabulary-term', |
|
| 98 | - plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term', |
|
| 99 | - array('wp-polyfill') |
|
| 100 | - ); |
|
| 101 | - wp_enqueue_style( 'wl-vocabulary-term', |
|
| 102 | - plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term.css' ); |
|
| 103 | - } |
|
| 67 | + echo sprintf( $template, esc_html( $entity_types_text ), $terms_html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 68 | + |
|
| 69 | + $this->enqueue_script_and_style(); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function save_field( $term_id ) { |
|
| 73 | + if ( ! isset( $_REQUEST['tax_input'] ) ) { |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + $entity_types = isset( $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] ) |
|
| 77 | + ? (array) $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] : array(); |
|
| 78 | + if ( isset( $entity_types ) && is_array( $entity_types ) ) { |
|
| 79 | + // Save the taxonomies. |
|
| 80 | + delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 81 | + foreach ( $entity_types as $entity_type ) { |
|
| 82 | + add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, (string) $entity_type ); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function init_ui_and_save_handlers() { |
|
| 88 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 89 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 90 | + add_action( "${taxonomy}_edit_form_fields", array( $this, 'render_ui' ), 1 ); |
|
| 91 | + add_action( "edited_${taxonomy}", array( $this, 'save_field' ) ); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + private function enqueue_script_and_style() { |
|
| 96 | + |
|
| 97 | + Scripts_Helper::enqueue_based_on_wordpress_version( 'wl-vocabulary-term', |
|
| 98 | + plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term', |
|
| 99 | + array('wp-polyfill') |
|
| 100 | + ); |
|
| 101 | + wp_enqueue_style( 'wl-vocabulary-term', |
|
| 102 | + plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term.css' ); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | add_action( |
| 23 | 23 | 'init', |
| 24 | - function () use ( $that ) { |
|
| 24 | + function() use ($that) { |
|
| 25 | 25 | $that->init_ui_and_save_handlers(); |
| 26 | 26 | } |
| 27 | 27 | ); |
@@ -31,16 +31,16 @@ discard block |
||
| 31 | 31 | /** |
| 32 | 32 | * @param $term \WP_Term |
| 33 | 33 | */ |
| 34 | - public function render_ui( $term ) { |
|
| 34 | + public function render_ui($term) { |
|
| 35 | 35 | |
| 36 | - $entity_types_text = __( 'Entity Types', 'wordlift' ); |
|
| 37 | - $selected_entity_types = get_term_meta( $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 36 | + $entity_types_text = __('Entity Types', 'wordlift'); |
|
| 37 | + $selected_entity_types = get_term_meta($term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Thing should be the default selected entity type |
| 41 | 41 | * when this feature is activated. |
| 42 | 42 | */ |
| 43 | - if ( count( $selected_entity_types ) === 0 ) { |
|
| 43 | + if (count($selected_entity_types) === 0) { |
|
| 44 | 44 | $selected_entity_types[] = 'thing'; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | ) |
| 55 | 55 | ); |
| 56 | 56 | |
| 57 | - $terms_html = Term_Checklist::render( 'tax_input[wl_entity_type]', $types, $selected_entity_types ); |
|
| 57 | + $terms_html = Term_Checklist::render('tax_input[wl_entity_type]', $types, $selected_entity_types); |
|
| 58 | 58 | |
| 59 | 59 | $template = ' |
| 60 | 60 | <tr class="form-field term-name-wrap"> |
@@ -64,42 +64,42 @@ discard block |
||
| 64 | 64 | </td> |
| 65 | 65 | </tr> |
| 66 | 66 | '; |
| 67 | - echo sprintf( $template, esc_html( $entity_types_text ), $terms_html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 67 | + echo sprintf($template, esc_html($entity_types_text), $terms_html); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 68 | 68 | |
| 69 | 69 | $this->enqueue_script_and_style(); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - public function save_field( $term_id ) { |
|
| 73 | - if ( ! isset( $_REQUEST['tax_input'] ) ) { |
|
| 72 | + public function save_field($term_id) { |
|
| 73 | + if ( ! isset($_REQUEST['tax_input'])) { |
|
| 74 | 74 | return; |
| 75 | 75 | } |
| 76 | - $entity_types = isset( $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] ) |
|
| 77 | - ? (array) $_REQUEST['tax_input'][ Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ] : array(); |
|
| 78 | - if ( isset( $entity_types ) && is_array( $entity_types ) ) { |
|
| 76 | + $entity_types = isset($_REQUEST['tax_input'][Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME]) |
|
| 77 | + ? (array) $_REQUEST['tax_input'][Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME] : array(); |
|
| 78 | + if (isset($entity_types) && is_array($entity_types)) { |
|
| 79 | 79 | // Save the taxonomies. |
| 80 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 81 | - foreach ( $entity_types as $entity_type ) { |
|
| 82 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, (string) $entity_type ); |
|
| 80 | + delete_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 81 | + foreach ($entity_types as $entity_type) { |
|
| 82 | + add_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, (string) $entity_type); |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | public function init_ui_and_save_handlers() { |
| 88 | 88 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 89 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 90 | - add_action( "${taxonomy}_edit_form_fields", array( $this, 'render_ui' ), 1 ); |
|
| 91 | - add_action( "edited_${taxonomy}", array( $this, 'save_field' ) ); |
|
| 89 | + foreach ($taxonomies as $taxonomy) { |
|
| 90 | + add_action("${taxonomy}_edit_form_fields", array($this, 'render_ui'), 1); |
|
| 91 | + add_action("edited_${taxonomy}", array($this, 'save_field')); |
|
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | private function enqueue_script_and_style() { |
| 96 | 96 | |
| 97 | - Scripts_Helper::enqueue_based_on_wordpress_version( 'wl-vocabulary-term', |
|
| 98 | - plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term', |
|
| 97 | + Scripts_Helper::enqueue_based_on_wordpress_version('wl-vocabulary-term', |
|
| 98 | + plugin_dir_url(dirname(__DIR__)).'/js/dist/vocabulary-term', |
|
| 99 | 99 | array('wp-polyfill') |
| 100 | 100 | ); |
| 101 | - wp_enqueue_style( 'wl-vocabulary-term', |
|
| 102 | - plugin_dir_url( dirname( __DIR__ ) ) . '/js/dist/vocabulary-term.css' ); |
|
| 101 | + wp_enqueue_style('wl-vocabulary-term', |
|
| 102 | + plugin_dir_url(dirname(__DIR__)).'/js/dist/vocabulary-term.css'); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | } |
@@ -10,76 +10,76 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Video_Sitemap { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Ttl_Cache |
|
| 15 | - */ |
|
| 16 | - private $sitemap_cache; |
|
| 17 | - |
|
| 18 | - const XML_CACHE_KEY = 'video_sitemap'; |
|
| 19 | - |
|
| 20 | - public function __construct( $sitemap_cache ) { |
|
| 21 | - $this->sitemap_cache = $sitemap_cache; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - public function init() { |
|
| 25 | - if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | - add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 27 | - } |
|
| 28 | - add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function flush_cache() { |
|
| 32 | - $this->sitemap_cache->flush(); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Print video sitemap. |
|
| 37 | - */ |
|
| 38 | - public function print_video_sitemap() { |
|
| 39 | - global $wp; |
|
| 40 | - |
|
| 41 | - $url = home_url( $wp->request ); |
|
| 42 | - |
|
| 43 | - $pattern = '/wl-video-sitemap\.xml$/m'; |
|
| 44 | - |
|
| 45 | - if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - header( "Content-type: text/xml" ); |
|
| 50 | - // set 200 status code. |
|
| 51 | - status_header(200); |
|
| 52 | - |
|
| 53 | - $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 54 | - |
|
| 55 | - if ( ! $xml ) { |
|
| 56 | - $xml = $this->get_sitemap_xml(); |
|
| 57 | - $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 61 | - die(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public static function is_video_sitemap_enabled() { |
|
| 65 | - return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - private function get_sitemap_xml() { |
|
| 72 | - $sitemap_start_tag = ' |
|
| 13 | + /** |
|
| 14 | + * @var Ttl_Cache |
|
| 15 | + */ |
|
| 16 | + private $sitemap_cache; |
|
| 17 | + |
|
| 18 | + const XML_CACHE_KEY = 'video_sitemap'; |
|
| 19 | + |
|
| 20 | + public function __construct( $sitemap_cache ) { |
|
| 21 | + $this->sitemap_cache = $sitemap_cache; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + public function init() { |
|
| 25 | + if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | + add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 27 | + } |
|
| 28 | + add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function flush_cache() { |
|
| 32 | + $this->sitemap_cache->flush(); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Print video sitemap. |
|
| 37 | + */ |
|
| 38 | + public function print_video_sitemap() { |
|
| 39 | + global $wp; |
|
| 40 | + |
|
| 41 | + $url = home_url( $wp->request ); |
|
| 42 | + |
|
| 43 | + $pattern = '/wl-video-sitemap\.xml$/m'; |
|
| 44 | + |
|
| 45 | + if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + header( "Content-type: text/xml" ); |
|
| 50 | + // set 200 status code. |
|
| 51 | + status_header(200); |
|
| 52 | + |
|
| 53 | + $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 54 | + |
|
| 55 | + if ( ! $xml ) { |
|
| 56 | + $xml = $this->get_sitemap_xml(); |
|
| 57 | + $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 61 | + die(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public static function is_video_sitemap_enabled() { |
|
| 65 | + return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + private function get_sitemap_xml() { |
|
| 72 | + $sitemap_start_tag = ' |
|
| 73 | 73 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 74 | 74 | xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 75 | 75 | '; |
| 76 | - $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 76 | + $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 77 | 77 | |
| 78 | - $sitemap_end_tag = "</urlset>"; |
|
| 78 | + $sitemap_end_tag = "</urlset>"; |
|
| 79 | 79 | |
| 80 | - $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 80 | + $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 81 | 81 | |
| 82 | - return $xml; |
|
| 83 | - } |
|
| 82 | + return $xml; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | 85 | } |
| 86 | 86 | \ No newline at end of file |
@@ -17,15 +17,15 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | const XML_CACHE_KEY = 'video_sitemap'; |
| 19 | 19 | |
| 20 | - public function __construct( $sitemap_cache ) { |
|
| 20 | + public function __construct($sitemap_cache) { |
|
| 21 | 21 | $this->sitemap_cache = $sitemap_cache; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public function init() { |
| 25 | - if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | - add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 25 | + if (self::is_video_sitemap_enabled()) { |
|
| 26 | + add_action('template_redirect', array($this, 'print_video_sitemap'), 1); |
|
| 27 | 27 | } |
| 28 | - add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 28 | + add_action('wordlift_videoobject_video_storage_updated', array($this, 'flush_cache')); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function flush_cache() { |
@@ -38,23 +38,23 @@ discard block |
||
| 38 | 38 | public function print_video_sitemap() { |
| 39 | 39 | global $wp; |
| 40 | 40 | |
| 41 | - $url = home_url( $wp->request ); |
|
| 41 | + $url = home_url($wp->request); |
|
| 42 | 42 | |
| 43 | 43 | $pattern = '/wl-video-sitemap\.xml$/m'; |
| 44 | 44 | |
| 45 | - if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 45 | + if (preg_match($pattern, $url) !== 1) { |
|
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - header( "Content-type: text/xml" ); |
|
| 49 | + header("Content-type: text/xml"); |
|
| 50 | 50 | // set 200 status code. |
| 51 | 51 | status_header(200); |
| 52 | 52 | |
| 53 | - $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 53 | + $xml = $this->sitemap_cache->get(self::XML_CACHE_KEY); |
|
| 54 | 54 | |
| 55 | - if ( ! $xml ) { |
|
| 55 | + if ( ! $xml) { |
|
| 56 | 56 | $xml = $this->get_sitemap_xml(); |
| 57 | - $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 57 | + $this->sitemap_cache->put(self::XML_CACHE_KEY, $xml); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | public static function is_video_sitemap_enabled() { |
| 65 | - return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 65 | + return intval(get_option('_wl_video_sitemap_generation', false)) === 1; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 74 | 74 | xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 75 | 75 | '; |
| 76 | - $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 76 | + $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 77 | 77 | |
| 78 | 78 | $sitemap_end_tag = "</urlset>"; |
| 79 | 79 | |
| 80 | - $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 80 | + $xml = $sitemap_start_tag.$sitemap_body.$sitemap_end_tag; |
|
| 81 | 81 | |
| 82 | 82 | return $xml; |
| 83 | 83 | } |
@@ -12,110 +12,110 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | class Xml_Generator { |
| 14 | 14 | |
| 15 | - private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | - try { |
|
| 17 | - $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | - } catch ( \Exception $e ) { |
|
| 19 | - return 0; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | - $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | - $minutes_to_seconds = $interval->i * 60; |
|
| 25 | - $seconds = $interval->s; |
|
| 26 | - |
|
| 27 | - return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | - |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | - /** |
|
| 33 | - * @since 3.31.6 |
|
| 34 | - * Filter the query args, add support for all custom public post types. |
|
| 35 | - */ |
|
| 36 | - $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | - 'fields' => 'ids', |
|
| 38 | - 'numberposts' => - 1, |
|
| 39 | - 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | - 'meta_query' => array( |
|
| 41 | - array( |
|
| 42 | - 'key' => Meta_Storage::META_KEY, |
|
| 43 | - 'compare' => 'EXISTS' |
|
| 44 | - ) |
|
| 45 | - ) |
|
| 46 | - ) ); |
|
| 47 | - |
|
| 48 | - $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | - |
|
| 50 | - $all_posts_xml = ""; |
|
| 51 | - |
|
| 52 | - if ( ! $posts ) { |
|
| 53 | - return $all_posts_xml; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - foreach ( $posts as $post_id ) { |
|
| 57 | - $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $all_posts_xml; |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $post_id |
|
| 67 | - * |
|
| 68 | - * @return string XML string for single post. |
|
| 69 | - */ |
|
| 70 | - public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | - $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | - if ( ! $videos ) { |
|
| 73 | - return ""; |
|
| 74 | - } |
|
| 75 | - $single_post_xml = ""; |
|
| 76 | - foreach ( $videos as $video ) { |
|
| 77 | - $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $single_post_xml; |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $video Video |
|
| 87 | - * @param $post_id int |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | - |
|
| 93 | - $permalink = get_permalink( $post_id ); |
|
| 94 | - $title = esc_html( $video->name ); |
|
| 95 | - $description = esc_html( $video->description ); |
|
| 96 | - $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | - |
|
| 98 | - // If description is empty use title. |
|
| 99 | - if ( ! $description ) { |
|
| 100 | - $description = $title; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $optional_fields = array( |
|
| 104 | - 'content_loc' => $video->content_url, |
|
| 105 | - 'player_loc' => $video->embed_url, |
|
| 106 | - 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | - 'view_count' => $video->views, |
|
| 108 | - 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $optional_data = ""; |
|
| 112 | - foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | - if ( $xml_value ) { |
|
| 114 | - $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return " |
|
| 15 | + private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | + try { |
|
| 17 | + $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | + } catch ( \Exception $e ) { |
|
| 19 | + return 0; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | + $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | + $minutes_to_seconds = $interval->i * 60; |
|
| 25 | + $seconds = $interval->s; |
|
| 26 | + |
|
| 27 | + return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | + |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | + /** |
|
| 33 | + * @since 3.31.6 |
|
| 34 | + * Filter the query args, add support for all custom public post types. |
|
| 35 | + */ |
|
| 36 | + $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | + 'fields' => 'ids', |
|
| 38 | + 'numberposts' => - 1, |
|
| 39 | + 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | + 'meta_query' => array( |
|
| 41 | + array( |
|
| 42 | + 'key' => Meta_Storage::META_KEY, |
|
| 43 | + 'compare' => 'EXISTS' |
|
| 44 | + ) |
|
| 45 | + ) |
|
| 46 | + ) ); |
|
| 47 | + |
|
| 48 | + $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | + |
|
| 50 | + $all_posts_xml = ""; |
|
| 51 | + |
|
| 52 | + if ( ! $posts ) { |
|
| 53 | + return $all_posts_xml; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + foreach ( $posts as $post_id ) { |
|
| 57 | + $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $all_posts_xml; |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $post_id |
|
| 67 | + * |
|
| 68 | + * @return string XML string for single post. |
|
| 69 | + */ |
|
| 70 | + public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | + $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | + if ( ! $videos ) { |
|
| 73 | + return ""; |
|
| 74 | + } |
|
| 75 | + $single_post_xml = ""; |
|
| 76 | + foreach ( $videos as $video ) { |
|
| 77 | + $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $single_post_xml; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $video Video |
|
| 87 | + * @param $post_id int |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | + |
|
| 93 | + $permalink = get_permalink( $post_id ); |
|
| 94 | + $title = esc_html( $video->name ); |
|
| 95 | + $description = esc_html( $video->description ); |
|
| 96 | + $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | + |
|
| 98 | + // If description is empty use title. |
|
| 99 | + if ( ! $description ) { |
|
| 100 | + $description = $title; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $optional_fields = array( |
|
| 104 | + 'content_loc' => $video->content_url, |
|
| 105 | + 'player_loc' => $video->embed_url, |
|
| 106 | + 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | + 'view_count' => $video->views, |
|
| 108 | + 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $optional_data = ""; |
|
| 112 | + foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | + if ( $xml_value ) { |
|
| 114 | + $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return " |
|
| 119 | 119 | <url> |
| 120 | 120 | <loc>${permalink}</loc> |
| 121 | 121 | <video:video> |
@@ -127,6 +127,6 @@ discard block |
||
| 127 | 127 | </url> |
| 128 | 128 | "; |
| 129 | 129 | |
| 130 | - } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |
| 133 | 133 | \ No newline at end of file |