@@ -3,45 +3,45 @@ |
||
3 | 3 | namespace Wordlift\Modules\Food_Kg; |
4 | 4 | |
5 | 5 | class Notices { |
6 | - /** |
|
7 | - * @var array{'type': string, 'html': string} |
|
8 | - */ |
|
9 | - private $notices = array(); |
|
6 | + /** |
|
7 | + * @var array{'type': string, 'html': string} |
|
8 | + */ |
|
9 | + private $notices = array(); |
|
10 | 10 | |
11 | - public function register_hooks() { |
|
12 | - add_action( 'admin_notices', array( $this, '__admin_notices' ) ); |
|
13 | - } |
|
11 | + public function register_hooks() { |
|
12 | + add_action( 'admin_notices', array( $this, '__admin_notices' ) ); |
|
13 | + } |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param 'info'|'warning'|'error'|'success' $type |
|
17 | - * @param string $html |
|
18 | - * |
|
19 | - * @return void |
|
20 | - */ |
|
21 | - public function queue( $type, $html ) { |
|
22 | - set_transient( |
|
23 | - '_wl_notices', |
|
24 | - array( |
|
25 | - 'type' => $type, |
|
26 | - 'html' => $html, |
|
27 | - ), |
|
28 | - 60 |
|
29 | - ); |
|
30 | - } |
|
15 | + /** |
|
16 | + * @param 'info'|'warning'|'error'|'success' $type |
|
17 | + * @param string $html |
|
18 | + * |
|
19 | + * @return void |
|
20 | + */ |
|
21 | + public function queue( $type, $html ) { |
|
22 | + set_transient( |
|
23 | + '_wl_notices', |
|
24 | + array( |
|
25 | + 'type' => $type, |
|
26 | + 'html' => $html, |
|
27 | + ), |
|
28 | + 60 |
|
29 | + ); |
|
30 | + } |
|
31 | 31 | |
32 | - public function __admin_notices() { |
|
33 | - /** @var false|array{'type': string, 'html': string} $notice */ |
|
34 | - $notice = get_transient( '_wl_notices' ); |
|
35 | - if ( ! $notice ) { |
|
36 | - return; |
|
37 | - } |
|
32 | + public function __admin_notices() { |
|
33 | + /** @var false|array{'type': string, 'html': string} $notice */ |
|
34 | + $notice = get_transient( '_wl_notices' ); |
|
35 | + if ( ! $notice ) { |
|
36 | + return; |
|
37 | + } |
|
38 | 38 | |
39 | - $type_e = esc_attr( $notice['type'] ); |
|
40 | - ?> |
|
39 | + $type_e = esc_attr( $notice['type'] ); |
|
40 | + ?> |
|
41 | 41 | <div class="notice notice-<?php echo esc_attr( $type_e ); ?> is-dismissible"> |
42 | 42 | <p><?php echo wp_kses( $notice['html'], array( 'a' ) ); ?></p> |
43 | 43 | </div> |
44 | 44 | <?php |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | 47 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | private $notices = array(); |
10 | 10 | |
11 | 11 | public function register_hooks() { |
12 | - add_action( 'admin_notices', array( $this, '__admin_notices' ) ); |
|
12 | + add_action('admin_notices', array($this, '__admin_notices')); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | /** |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @return void |
20 | 20 | */ |
21 | - public function queue( $type, $html ) { |
|
21 | + public function queue($type, $html) { |
|
22 | 22 | set_transient( |
23 | 23 | '_wl_notices', |
24 | 24 | array( |
@@ -31,15 +31,15 @@ discard block |
||
31 | 31 | |
32 | 32 | public function __admin_notices() { |
33 | 33 | /** @var false|array{'type': string, 'html': string} $notice */ |
34 | - $notice = get_transient( '_wl_notices' ); |
|
35 | - if ( ! $notice ) { |
|
34 | + $notice = get_transient('_wl_notices'); |
|
35 | + if ( ! $notice) { |
|
36 | 36 | return; |
37 | 37 | } |
38 | 38 | |
39 | - $type_e = esc_attr( $notice['type'] ); |
|
39 | + $type_e = esc_attr($notice['type']); |
|
40 | 40 | ?> |
41 | - <div class="notice notice-<?php echo esc_attr( $type_e ); ?> is-dismissible"> |
|
42 | - <p><?php echo wp_kses( $notice['html'], array( 'a' ) ); ?></p> |
|
41 | + <div class="notice notice-<?php echo esc_attr($type_e); ?> is-dismissible"> |
|
42 | + <p><?php echo wp_kses($notice['html'], array('a')); ?></p> |
|
43 | 43 | </div> |
44 | 44 | <?php |
45 | 45 | } |
@@ -21,44 +21,44 @@ |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Activator { |
23 | 23 | |
24 | - /** |
|
25 | - * Short Description. (use period) |
|
26 | - * |
|
27 | - * Long Description. |
|
28 | - * |
|
29 | - * @since 1.0.0 |
|
30 | - */ |
|
31 | - public static function activate() { |
|
24 | + /** |
|
25 | + * Short Description. (use period) |
|
26 | + * |
|
27 | + * Long Description. |
|
28 | + * |
|
29 | + * @since 1.0.0 |
|
30 | + */ |
|
31 | + public static function activate() { |
|
32 | 32 | |
33 | - // Do not let the plugin be activate on WordPress versions before 4.4. |
|
34 | - $version = get_bloginfo( 'version' ); |
|
35 | - if ( version_compare( $version, '4.9', '<' ) ) { |
|
36 | - die( esc_html__( 'The WordLift plugin requires WordPress version 4.9 or above.', 'wordlift' ) ); |
|
37 | - } |
|
33 | + // Do not let the plugin be activate on WordPress versions before 4.4. |
|
34 | + $version = get_bloginfo( 'version' ); |
|
35 | + if ( version_compare( $version, '4.9', '<' ) ) { |
|
36 | + die( esc_html__( 'The WordLift plugin requires WordPress version 4.9 or above.', 'wordlift' ) ); |
|
37 | + } |
|
38 | 38 | |
39 | - $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
39 | + $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
40 | 40 | |
41 | - // Create a blank application key if there is none. |
|
42 | - $key = $configuration_service->get_key(); |
|
41 | + // Create a blank application key if there is none. |
|
42 | + $key = $configuration_service->get_key(); |
|
43 | 43 | |
44 | - if ( empty( $key ) ) { |
|
45 | - $configuration_service->set_key( '' ); |
|
46 | - } |
|
44 | + if ( empty( $key ) ) { |
|
45 | + $configuration_service->set_key( '' ); |
|
46 | + } |
|
47 | 47 | |
48 | - // Intentionally go through the whole upgrade procedure to be DRY. |
|
49 | - // The following function is called also from `init` so it's not necessary |
|
50 | - // here. |
|
51 | - // wl_core_update_db_check. |
|
48 | + // Intentionally go through the whole upgrade procedure to be DRY. |
|
49 | + // The following function is called also from `init` so it's not necessary |
|
50 | + // here. |
|
51 | + // wl_core_update_db_check. |
|
52 | 52 | |
53 | - // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
|
54 | - // for granted that we're in a browser admin session. |
|
55 | - if ( '' === $configuration_service->get_key() ) { |
|
56 | - set_transient( '_wl_activation_redirect', true, 30 ); |
|
57 | - } |
|
53 | + // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
|
54 | + // for granted that we're in a browser admin session. |
|
55 | + if ( '' === $configuration_service->get_key() ) { |
|
56 | + set_transient( '_wl_activation_redirect', true, 30 ); |
|
57 | + } |
|
58 | 58 | |
59 | - // Clear caches. |
|
60 | - Wordlift_File_Cache_Service::flush_all(); |
|
59 | + // Clear caches. |
|
60 | + Wordlift_File_Cache_Service::flush_all(); |
|
61 | 61 | |
62 | - } |
|
62 | + } |
|
63 | 63 | |
64 | 64 | } |
@@ -31,9 +31,9 @@ discard block |
||
31 | 31 | public static function activate() { |
32 | 32 | |
33 | 33 | // Do not let the plugin be activate on WordPress versions before 4.4. |
34 | - $version = get_bloginfo( 'version' ); |
|
35 | - if ( version_compare( $version, '4.9', '<' ) ) { |
|
36 | - die( esc_html__( 'The WordLift plugin requires WordPress version 4.9 or above.', 'wordlift' ) ); |
|
34 | + $version = get_bloginfo('version'); |
|
35 | + if (version_compare($version, '4.9', '<')) { |
|
36 | + die(esc_html__('The WordLift plugin requires WordPress version 4.9 or above.', 'wordlift')); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | $configuration_service = Wordlift_Configuration_Service::get_instance(); |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | // Create a blank application key if there is none. |
42 | 42 | $key = $configuration_service->get_key(); |
43 | 43 | |
44 | - if ( empty( $key ) ) { |
|
45 | - $configuration_service->set_key( '' ); |
|
44 | + if (empty($key)) { |
|
45 | + $configuration_service->set_key(''); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | // Intentionally go through the whole upgrade procedure to be DRY. |
@@ -52,8 +52,8 @@ discard block |
||
52 | 52 | |
53 | 53 | // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
54 | 54 | // for granted that we're in a browser admin session. |
55 | - if ( '' === $configuration_service->get_key() ) { |
|
56 | - set_transient( '_wl_activation_redirect', true, 30 ); |
|
55 | + if ('' === $configuration_service->get_key()) { |
|
56 | + set_transient('_wl_activation_redirect', true, 30); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | // Clear caches. |
@@ -16,353 +16,353 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Timeline_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * The Log service. |
|
21 | - * |
|
22 | - * @since 3.1.0 |
|
23 | - * @access private |
|
24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
25 | - */ |
|
26 | - private $log; |
|
27 | - |
|
28 | - /** |
|
29 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
30 | - * and used by a filter. |
|
31 | - * |
|
32 | - * @since 3.7.0 |
|
33 | - * @access private |
|
34 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
35 | - */ |
|
36 | - private $excerpt_length; |
|
37 | - |
|
38 | - /** |
|
39 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
40 | - * |
|
41 | - * @since 3.1.0 |
|
42 | - * @access private |
|
43 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
44 | - */ |
|
45 | - private static $instance; |
|
46 | - |
|
47 | - /** |
|
48 | - * Create a Wordlift_Timeline_Service instance. |
|
49 | - * |
|
50 | - * @since 3.1.0 |
|
51 | - */ |
|
52 | - public function __construct() { |
|
53 | - |
|
54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
55 | - |
|
56 | - self::$instance = $this; |
|
57 | - |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
62 | - * |
|
63 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
64 | - * @since 3.1.0 |
|
65 | - */ |
|
66 | - public static function get_instance() { |
|
67 | - |
|
68 | - return self::$instance; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Retrieve timeline events and output them in JSON. |
|
73 | - * |
|
74 | - * @since 3.1.0 |
|
75 | - */ |
|
76 | - public function ajax_timeline() { |
|
77 | - |
|
78 | - check_ajax_referer( 'wl_timeline' ); |
|
79 | - |
|
80 | - // Get the ID of the post who requested the timeline. |
|
81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
82 | - |
|
83 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
85 | - |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Retrieve timeline events. |
|
90 | - * |
|
91 | - * @param int $post_id The post ID. |
|
92 | - * |
|
93 | - * @return array An array of event posts. |
|
94 | - * @since 3.1.0 |
|
95 | - * |
|
96 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
97 | - */ |
|
98 | - public function get_events( $post_id = null ) { |
|
99 | - |
|
100 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
101 | - // posts if no post has been specified. |
|
102 | - $ids = ( is_numeric( $post_id ) |
|
103 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
104 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
105 | - |
|
106 | - // Add the post itself if it's an entity. |
|
107 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
108 | - $ids[] = $post_id; |
|
109 | - } |
|
110 | - |
|
111 | - // If there's no entities, return an empty array right away. |
|
112 | - if ( 0 === count( $ids ) ) { |
|
113 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
114 | - |
|
115 | - return array(); |
|
116 | - } |
|
117 | - |
|
118 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
119 | - |
|
120 | - $args = array( |
|
121 | - 'post__in' => $ids, |
|
122 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
123 | - 'post_status' => 'publish', |
|
124 | - 'posts_per_page' => - 1, |
|
125 | - 'meta_query' => array( |
|
126 | - 'relation' => 'AND', |
|
127 | - array( |
|
128 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
129 | - 'value' => null, |
|
130 | - 'compare' => '!=', |
|
131 | - ), |
|
132 | - array( |
|
133 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
134 | - 'value' => null, |
|
135 | - 'compare' => '!=', |
|
136 | - ), |
|
137 | - ), |
|
138 | - 'tax_query' => array( |
|
139 | - array( |
|
140 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
141 | - 'field' => 'slug', |
|
142 | - 'terms' => 'event', |
|
143 | - ), |
|
144 | - ), |
|
145 | - 'orderby' => 'meta_value', |
|
146 | - 'order' => 'DESC', |
|
147 | - 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
148 | - ); |
|
149 | - |
|
150 | - return get_posts( $args ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Convert timeline events to JSON. This function sets the global post in order |
|
155 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
156 | - * we're not taking care of restoring any previous post: there isn't any. |
|
157 | - * |
|
158 | - * @param array $posts An array of posts. |
|
159 | - * |
|
160 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
161 | - * @since 3.1.0 |
|
162 | - */ |
|
163 | - public function to_json( $posts ) { |
|
164 | - check_ajax_referer( 'wl_timeline' ); |
|
165 | - // If there are no events, return empty JSON |
|
166 | - if ( empty( $posts ) || $posts === null ) { |
|
167 | - return ''; |
|
168 | - } |
|
169 | - |
|
170 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
171 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
172 | - |
|
173 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
174 | - $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
175 | - $this->excerpt_length = $excerpt_length; |
|
176 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 ); |
|
177 | - |
|
178 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
179 | - // a link to the post itself. |
|
180 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 ); |
|
181 | - |
|
182 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
183 | - $start_at_slide = 0; |
|
184 | - $event_index = - 1; |
|
185 | - $now = time(); |
|
186 | - |
|
187 | - // Prepare the timeline variable. |
|
188 | - $timeline = array(); |
|
189 | - |
|
190 | - // Populate the arrays. |
|
191 | - $timeline['events'] = array_map( |
|
192 | - function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
193 | - |
|
194 | - $date = array(); |
|
195 | - |
|
196 | - // Get the start and end dates. |
|
197 | - // We have to remove double quotes from date to make timeline work properly |
|
198 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
199 | - $end_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
200 | - |
|
201 | - // Set the starting slide. |
|
202 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
203 | - $event_index ++; |
|
204 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
205 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
206 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
207 | - $start_at_slide = $event_index; |
|
208 | - } |
|
209 | - |
|
210 | - // Load thumbnail |
|
211 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
212 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
213 | - if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id |
|
214 | - && false !== $attachment |
|
215 | - ) { |
|
216 | - |
|
217 | - // Set the thumbnail URL. |
|
218 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
219 | - if ( 'background' === $display_images_as ) { |
|
220 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
221 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
222 | - } else { |
|
223 | - $date['media'] = array( |
|
224 | - 'url' => $attachment[0], |
|
225 | - 'thumbnail' => $attachment[0], |
|
226 | - ); |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
231 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
232 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
233 | - |
|
234 | - setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found,WordPress.WP.GlobalVariablesOverride.Prohibited |
|
235 | - |
|
236 | - $more_link_text = sprintf( |
|
237 | - '<span aria-label="%1$s">%2$s</span>', |
|
238 | - sprintf( |
|
239 | - /* translators: %s: Name of current post */ |
|
240 | - __( 'Continue reading %s', 'wordlift' ), |
|
241 | - the_title_attribute( array( 'echo' => false ) ) |
|
242 | - ), |
|
243 | - __( '(more…)', 'wordlift' ) |
|
244 | - ); |
|
245 | - |
|
246 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
247 | - $date['text'] = array( |
|
248 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
249 | - ); |
|
250 | - |
|
251 | - // If we have an excerpt, set it. |
|
252 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
253 | - if ( 0 < $excerpt_length ) { |
|
254 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
255 | - } |
|
256 | - |
|
257 | - return $date; |
|
258 | - |
|
259 | - }, |
|
260 | - $posts |
|
261 | - ); |
|
262 | - |
|
263 | - // Finally remove the excerpt filter. |
|
264 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
265 | - |
|
266 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
267 | - return array( |
|
268 | - 'timeline' => $timeline, |
|
269 | - 'start_at_slide' => $start_at_slide, |
|
270 | - ); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
275 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
276 | - * |
|
277 | - * @return string An empty string. |
|
278 | - * @since 3.7.0 |
|
279 | - */ |
|
280 | - public function excerpt_more() { |
|
281 | - |
|
282 | - return ''; |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
287 | - * how many words to return according to the client setting. |
|
288 | - * |
|
289 | - * @return int The number of words for the preset. |
|
290 | - * @since 3.7.0 |
|
291 | - */ |
|
292 | - public function excerpt_length() { |
|
293 | - |
|
294 | - return $this->excerpt_length; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Convert the date to a date array. |
|
299 | - * |
|
300 | - * @param $value int A date value. |
|
301 | - * |
|
302 | - * @return array An array containing year, month and day values. |
|
303 | - * @since 3.7.0 |
|
304 | - */ |
|
305 | - public static function date( $value ) { |
|
306 | - |
|
307 | - return array( |
|
308 | - 'year' => (int) gmdate( 'Y', $value ), |
|
309 | - 'month' => (int) gmdate( 'm', $value ), |
|
310 | - 'day' => (int) gmdate( 'd', $value ), |
|
311 | - |
|
312 | - ); |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
317 | - * its specific function). |
|
318 | - * |
|
319 | - * @return array An array of post IDs. |
|
320 | - * @since 3.1.0 |
|
321 | - */ |
|
322 | - public function get_all_related_to_last_50_published_posts() { |
|
323 | - |
|
324 | - // Global timeline. Get entities from the latest posts. |
|
325 | - $latest_posts_ids = get_posts( |
|
326 | - array( |
|
327 | - 'numberposts' => 50, |
|
328 | - 'fields' => 'ids', // only get post IDs |
|
329 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
330 | - 'tax_query' => array( |
|
331 | - 'relation' => 'OR', |
|
332 | - array( |
|
333 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
334 | - 'operator' => 'NOT EXISTS', |
|
335 | - ), |
|
336 | - array( |
|
337 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
338 | - 'field' => 'slug', |
|
339 | - 'terms' => 'article', |
|
340 | - ), |
|
341 | - ), |
|
342 | - 'post_status' => 'publish', |
|
343 | - ) |
|
344 | - ); |
|
345 | - |
|
346 | - if ( empty( $latest_posts_ids ) ) { |
|
347 | - // There are no posts. |
|
348 | - return array(); |
|
349 | - } |
|
350 | - |
|
351 | - // Collect entities related to latest posts |
|
352 | - $entity_ids = array(); |
|
353 | - foreach ( $latest_posts_ids as $id ) { |
|
354 | - $entity_ids = array_merge( |
|
355 | - $entity_ids, |
|
356 | - wl_core_get_related_entity_ids( |
|
357 | - $id, |
|
358 | - array( |
|
359 | - 'status' => 'publish', |
|
360 | - ) |
|
361 | - ) |
|
362 | - ); |
|
363 | - } |
|
364 | - |
|
365 | - return $entity_ids; |
|
366 | - } |
|
19 | + /** |
|
20 | + * The Log service. |
|
21 | + * |
|
22 | + * @since 3.1.0 |
|
23 | + * @access private |
|
24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
25 | + */ |
|
26 | + private $log; |
|
27 | + |
|
28 | + /** |
|
29 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
30 | + * and used by a filter. |
|
31 | + * |
|
32 | + * @since 3.7.0 |
|
33 | + * @access private |
|
34 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
35 | + */ |
|
36 | + private $excerpt_length; |
|
37 | + |
|
38 | + /** |
|
39 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
40 | + * |
|
41 | + * @since 3.1.0 |
|
42 | + * @access private |
|
43 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
44 | + */ |
|
45 | + private static $instance; |
|
46 | + |
|
47 | + /** |
|
48 | + * Create a Wordlift_Timeline_Service instance. |
|
49 | + * |
|
50 | + * @since 3.1.0 |
|
51 | + */ |
|
52 | + public function __construct() { |
|
53 | + |
|
54 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
55 | + |
|
56 | + self::$instance = $this; |
|
57 | + |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
62 | + * |
|
63 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
64 | + * @since 3.1.0 |
|
65 | + */ |
|
66 | + public static function get_instance() { |
|
67 | + |
|
68 | + return self::$instance; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Retrieve timeline events and output them in JSON. |
|
73 | + * |
|
74 | + * @since 3.1.0 |
|
75 | + */ |
|
76 | + public function ajax_timeline() { |
|
77 | + |
|
78 | + check_ajax_referer( 'wl_timeline' ); |
|
79 | + |
|
80 | + // Get the ID of the post who requested the timeline. |
|
81 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
82 | + |
|
83 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
84 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
85 | + |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Retrieve timeline events. |
|
90 | + * |
|
91 | + * @param int $post_id The post ID. |
|
92 | + * |
|
93 | + * @return array An array of event posts. |
|
94 | + * @since 3.1.0 |
|
95 | + * |
|
96 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
97 | + */ |
|
98 | + public function get_events( $post_id = null ) { |
|
99 | + |
|
100 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
101 | + // posts if no post has been specified. |
|
102 | + $ids = ( is_numeric( $post_id ) |
|
103 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
104 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
105 | + |
|
106 | + // Add the post itself if it's an entity. |
|
107 | + if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
108 | + $ids[] = $post_id; |
|
109 | + } |
|
110 | + |
|
111 | + // If there's no entities, return an empty array right away. |
|
112 | + if ( 0 === count( $ids ) ) { |
|
113 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
114 | + |
|
115 | + return array(); |
|
116 | + } |
|
117 | + |
|
118 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
119 | + |
|
120 | + $args = array( |
|
121 | + 'post__in' => $ids, |
|
122 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
123 | + 'post_status' => 'publish', |
|
124 | + 'posts_per_page' => - 1, |
|
125 | + 'meta_query' => array( |
|
126 | + 'relation' => 'AND', |
|
127 | + array( |
|
128 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
129 | + 'value' => null, |
|
130 | + 'compare' => '!=', |
|
131 | + ), |
|
132 | + array( |
|
133 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
134 | + 'value' => null, |
|
135 | + 'compare' => '!=', |
|
136 | + ), |
|
137 | + ), |
|
138 | + 'tax_query' => array( |
|
139 | + array( |
|
140 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
141 | + 'field' => 'slug', |
|
142 | + 'terms' => 'event', |
|
143 | + ), |
|
144 | + ), |
|
145 | + 'orderby' => 'meta_value', |
|
146 | + 'order' => 'DESC', |
|
147 | + 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
148 | + ); |
|
149 | + |
|
150 | + return get_posts( $args ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Convert timeline events to JSON. This function sets the global post in order |
|
155 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
156 | + * we're not taking care of restoring any previous post: there isn't any. |
|
157 | + * |
|
158 | + * @param array $posts An array of posts. |
|
159 | + * |
|
160 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
161 | + * @since 3.1.0 |
|
162 | + */ |
|
163 | + public function to_json( $posts ) { |
|
164 | + check_ajax_referer( 'wl_timeline' ); |
|
165 | + // If there are no events, return empty JSON |
|
166 | + if ( empty( $posts ) || $posts === null ) { |
|
167 | + return ''; |
|
168 | + } |
|
169 | + |
|
170 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
171 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
172 | + |
|
173 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
174 | + $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
175 | + $this->excerpt_length = $excerpt_length; |
|
176 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 ); |
|
177 | + |
|
178 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
179 | + // a link to the post itself. |
|
180 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 ); |
|
181 | + |
|
182 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
183 | + $start_at_slide = 0; |
|
184 | + $event_index = - 1; |
|
185 | + $now = time(); |
|
186 | + |
|
187 | + // Prepare the timeline variable. |
|
188 | + $timeline = array(); |
|
189 | + |
|
190 | + // Populate the arrays. |
|
191 | + $timeline['events'] = array_map( |
|
192 | + function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
193 | + |
|
194 | + $date = array(); |
|
195 | + |
|
196 | + // Get the start and end dates. |
|
197 | + // We have to remove double quotes from date to make timeline work properly |
|
198 | + $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
199 | + $end_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
200 | + |
|
201 | + // Set the starting slide. |
|
202 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
203 | + $event_index ++; |
|
204 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
205 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
206 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
207 | + $start_at_slide = $event_index; |
|
208 | + } |
|
209 | + |
|
210 | + // Load thumbnail |
|
211 | + $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
212 | + $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
213 | + if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id |
|
214 | + && false !== $attachment |
|
215 | + ) { |
|
216 | + |
|
217 | + // Set the thumbnail URL. |
|
218 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
219 | + if ( 'background' === $display_images_as ) { |
|
220 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
221 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
222 | + } else { |
|
223 | + $date['media'] = array( |
|
224 | + 'url' => $attachment[0], |
|
225 | + 'thumbnail' => $attachment[0], |
|
226 | + ); |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
231 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
232 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
233 | + |
|
234 | + setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found,WordPress.WP.GlobalVariablesOverride.Prohibited |
|
235 | + |
|
236 | + $more_link_text = sprintf( |
|
237 | + '<span aria-label="%1$s">%2$s</span>', |
|
238 | + sprintf( |
|
239 | + /* translators: %s: Name of current post */ |
|
240 | + __( 'Continue reading %s', 'wordlift' ), |
|
241 | + the_title_attribute( array( 'echo' => false ) ) |
|
242 | + ), |
|
243 | + __( '(more…)', 'wordlift' ) |
|
244 | + ); |
|
245 | + |
|
246 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
247 | + $date['text'] = array( |
|
248 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
249 | + ); |
|
250 | + |
|
251 | + // If we have an excerpt, set it. |
|
252 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
253 | + if ( 0 < $excerpt_length ) { |
|
254 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
255 | + } |
|
256 | + |
|
257 | + return $date; |
|
258 | + |
|
259 | + }, |
|
260 | + $posts |
|
261 | + ); |
|
262 | + |
|
263 | + // Finally remove the excerpt filter. |
|
264 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
265 | + |
|
266 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
267 | + return array( |
|
268 | + 'timeline' => $timeline, |
|
269 | + 'start_at_slide' => $start_at_slide, |
|
270 | + ); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
275 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
276 | + * |
|
277 | + * @return string An empty string. |
|
278 | + * @since 3.7.0 |
|
279 | + */ |
|
280 | + public function excerpt_more() { |
|
281 | + |
|
282 | + return ''; |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
287 | + * how many words to return according to the client setting. |
|
288 | + * |
|
289 | + * @return int The number of words for the preset. |
|
290 | + * @since 3.7.0 |
|
291 | + */ |
|
292 | + public function excerpt_length() { |
|
293 | + |
|
294 | + return $this->excerpt_length; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Convert the date to a date array. |
|
299 | + * |
|
300 | + * @param $value int A date value. |
|
301 | + * |
|
302 | + * @return array An array containing year, month and day values. |
|
303 | + * @since 3.7.0 |
|
304 | + */ |
|
305 | + public static function date( $value ) { |
|
306 | + |
|
307 | + return array( |
|
308 | + 'year' => (int) gmdate( 'Y', $value ), |
|
309 | + 'month' => (int) gmdate( 'm', $value ), |
|
310 | + 'day' => (int) gmdate( 'd', $value ), |
|
311 | + |
|
312 | + ); |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
317 | + * its specific function). |
|
318 | + * |
|
319 | + * @return array An array of post IDs. |
|
320 | + * @since 3.1.0 |
|
321 | + */ |
|
322 | + public function get_all_related_to_last_50_published_posts() { |
|
323 | + |
|
324 | + // Global timeline. Get entities from the latest posts. |
|
325 | + $latest_posts_ids = get_posts( |
|
326 | + array( |
|
327 | + 'numberposts' => 50, |
|
328 | + 'fields' => 'ids', // only get post IDs |
|
329 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
330 | + 'tax_query' => array( |
|
331 | + 'relation' => 'OR', |
|
332 | + array( |
|
333 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
334 | + 'operator' => 'NOT EXISTS', |
|
335 | + ), |
|
336 | + array( |
|
337 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
338 | + 'field' => 'slug', |
|
339 | + 'terms' => 'article', |
|
340 | + ), |
|
341 | + ), |
|
342 | + 'post_status' => 'publish', |
|
343 | + ) |
|
344 | + ); |
|
345 | + |
|
346 | + if ( empty( $latest_posts_ids ) ) { |
|
347 | + // There are no posts. |
|
348 | + return array(); |
|
349 | + } |
|
350 | + |
|
351 | + // Collect entities related to latest posts |
|
352 | + $entity_ids = array(); |
|
353 | + foreach ( $latest_posts_ids as $id ) { |
|
354 | + $entity_ids = array_merge( |
|
355 | + $entity_ids, |
|
356 | + wl_core_get_related_entity_ids( |
|
357 | + $id, |
|
358 | + array( |
|
359 | + 'status' => 'publish', |
|
360 | + ) |
|
361 | + ) |
|
362 | + ); |
|
363 | + } |
|
364 | + |
|
365 | + return $entity_ids; |
|
366 | + } |
|
367 | 367 | |
368 | 368 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function __construct() { |
53 | 53 | |
54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
54 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service'); |
|
55 | 55 | |
56 | 56 | self::$instance = $this; |
57 | 57 | |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function ajax_timeline() { |
77 | 77 | |
78 | - check_ajax_referer( 'wl_timeline' ); |
|
78 | + check_ajax_referer('wl_timeline'); |
|
79 | 79 | |
80 | 80 | // Get the ID of the post who requested the timeline. |
81 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
81 | + $post_id = (isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : null); |
|
82 | 82 | |
83 | 83 | // Get the events and transform them for the JSON response, then send them to the client. |
84 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
84 | + wp_send_json($this->to_json($this->get_events($post_id))); |
|
85 | 85 | |
86 | 86 | } |
87 | 87 | |
@@ -95,33 +95,33 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
97 | 97 | */ |
98 | - public function get_events( $post_id = null ) { |
|
98 | + public function get_events($post_id = null) { |
|
99 | 99 | |
100 | 100 | // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
101 | 101 | // posts if no post has been specified. |
102 | - $ids = ( is_numeric( $post_id ) |
|
103 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
104 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
102 | + $ids = (is_numeric($post_id) |
|
103 | + ? wl_core_get_related_entity_ids($post_id) |
|
104 | + : $this->get_all_related_to_last_50_published_posts()); |
|
105 | 105 | |
106 | 106 | // Add the post itself if it's an entity. |
107 | - if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
107 | + if (is_numeric($post_id) && Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
108 | 108 | $ids[] = $post_id; |
109 | 109 | } |
110 | 110 | |
111 | 111 | // If there's no entities, return an empty array right away. |
112 | - if ( 0 === count( $ids ) ) { |
|
113 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
112 | + if (0 === count($ids)) { |
|
113 | + $this->log->trace("No events found [ post id :: $post_id ]"); |
|
114 | 114 | |
115 | 115 | return array(); |
116 | 116 | } |
117 | 117 | |
118 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
118 | + $this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]'); |
|
119 | 119 | |
120 | 120 | $args = array( |
121 | 121 | 'post__in' => $ids, |
122 | 122 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
123 | 123 | 'post_status' => 'publish', |
124 | - 'posts_per_page' => - 1, |
|
124 | + 'posts_per_page' => -1, |
|
125 | 125 | 'meta_query' => array( |
126 | 126 | 'relation' => 'AND', |
127 | 127 | array( |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
148 | 148 | ); |
149 | 149 | |
150 | - return get_posts( $args ); |
|
150 | + return get_posts($args); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -160,24 +160,24 @@ discard block |
||
160 | 160 | * @return array|string An array of timeline events or an empty string if no posts are provided. |
161 | 161 | * @since 3.1.0 |
162 | 162 | */ |
163 | - public function to_json( $posts ) { |
|
164 | - check_ajax_referer( 'wl_timeline' ); |
|
163 | + public function to_json($posts) { |
|
164 | + check_ajax_referer('wl_timeline'); |
|
165 | 165 | // If there are no events, return empty JSON |
166 | - if ( empty( $posts ) || $posts === null ) { |
|
166 | + if (empty($posts) || $posts === null) { |
|
167 | 167 | return ''; |
168 | 168 | } |
169 | 169 | |
170 | 170 | // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
171 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media'; |
|
171 | + $display_images_as = isset($_REQUEST['display_images_as']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['display_images_as'])) : 'media'; |
|
172 | 172 | |
173 | 173 | // The number of words for the excerpt (by default 55, as WordPress). |
174 | - $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55; |
|
174 | + $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? sanitize_text_field(wp_unslash($_REQUEST['excerpt_length'])) : 55; |
|
175 | 175 | $this->excerpt_length = $excerpt_length; |
176 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 ); |
|
176 | + add_filter('excerpt_length', array($this, 'excerpt_length'), 10, 0); |
|
177 | 177 | |
178 | 178 | // Add a filter to remove the [...] after excerpts, since we're adding |
179 | 179 | // a link to the post itself. |
180 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 ); |
|
180 | + add_filter('excerpt_more', array($this, 'excerpt_more'), 10, 0); |
|
181 | 181 | |
182 | 182 | // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
183 | 183 | $start_at_slide = 0; |
@@ -189,36 +189,36 @@ discard block |
||
189 | 189 | |
190 | 190 | // Populate the arrays. |
191 | 191 | $timeline['events'] = array_map( |
192 | - function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
192 | + function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) { |
|
193 | 193 | |
194 | 194 | $date = array(); |
195 | 195 | |
196 | 196 | // Get the start and end dates. |
197 | 197 | // We have to remove double quotes from date to make timeline work properly |
198 | - $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) ); |
|
199 | - $end_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) ); |
|
198 | + $start_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true))); |
|
199 | + $end_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true))); |
|
200 | 200 | |
201 | 201 | // Set the starting slide. |
202 | 202 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
203 | - $event_index ++; |
|
203 | + $event_index++; |
|
204 | 204 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
205 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
205 | + if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) { |
|
206 | 206 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
207 | 207 | $start_at_slide = $event_index; |
208 | 208 | } |
209 | 209 | |
210 | 210 | // Load thumbnail |
211 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
212 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
213 | - if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id |
|
211 | + $thumbnail_id = get_post_thumbnail_id($item->ID); |
|
212 | + $attachment = wp_get_attachment_image_src($thumbnail_id); |
|
213 | + if ('' !== (string) $thumbnail_id && 0 !== $thumbnail_id |
|
214 | 214 | && false !== $attachment |
215 | 215 | ) { |
216 | 216 | |
217 | 217 | // Set the thumbnail URL. |
218 | 218 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
219 | - if ( 'background' === $display_images_as ) { |
|
220 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
221 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
219 | + if ('background' === $display_images_as) { |
|
220 | + $date['background'] = array('url' => $attachment[0]); |
|
221 | + $date['media'] = array('thumbnail' => $attachment[0]); |
|
222 | 222 | } else { |
223 | 223 | $date['media'] = array( |
224 | 224 | 'url' => $attachment[0], |
@@ -228,30 +228,30 @@ discard block |
||
228 | 228 | } |
229 | 229 | |
230 | 230 | // Set the start/end dates by converting them to TimelineJS required format. |
231 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
232 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
231 | + $date['start_date'] = Wordlift_Timeline_Service::date($start_date); |
|
232 | + $date['end_date'] = Wordlift_Timeline_Service::date($end_date); |
|
233 | 233 | |
234 | - setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found,WordPress.WP.GlobalVariablesOverride.Prohibited |
|
234 | + setup_postdata($GLOBALS['post'] = $item); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found,WordPress.WP.GlobalVariablesOverride.Prohibited |
|
235 | 235 | |
236 | 236 | $more_link_text = sprintf( |
237 | 237 | '<span aria-label="%1$s">%2$s</span>', |
238 | 238 | sprintf( |
239 | 239 | /* translators: %s: Name of current post */ |
240 | - __( 'Continue reading %s', 'wordlift' ), |
|
241 | - the_title_attribute( array( 'echo' => false ) ) |
|
240 | + __('Continue reading %s', 'wordlift'), |
|
241 | + the_title_attribute(array('echo' => false)) |
|
242 | 242 | ), |
243 | - __( '(more…)', 'wordlift' ) |
|
243 | + __('(more…)', 'wordlift') |
|
244 | 244 | ); |
245 | 245 | |
246 | 246 | // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
247 | 247 | $date['text'] = array( |
248 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
248 | + 'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>', |
|
249 | 249 | ); |
250 | 250 | |
251 | 251 | // If we have an excerpt, set it. |
252 | 252 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
253 | - if ( 0 < $excerpt_length ) { |
|
254 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
253 | + if (0 < $excerpt_length) { |
|
254 | + $date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | return $date; |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | ); |
262 | 262 | |
263 | 263 | // Finally remove the excerpt filter. |
264 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
264 | + remove_filter('excerpt_length', array($this, 'excerpt_length')); |
|
265 | 265 | |
266 | 266 | // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
267 | 267 | return array( |
@@ -302,12 +302,12 @@ discard block |
||
302 | 302 | * @return array An array containing year, month and day values. |
303 | 303 | * @since 3.7.0 |
304 | 304 | */ |
305 | - public static function date( $value ) { |
|
305 | + public static function date($value) { |
|
306 | 306 | |
307 | 307 | return array( |
308 | - 'year' => (int) gmdate( 'Y', $value ), |
|
309 | - 'month' => (int) gmdate( 'm', $value ), |
|
310 | - 'day' => (int) gmdate( 'd', $value ), |
|
308 | + 'year' => (int) gmdate('Y', $value), |
|
309 | + 'month' => (int) gmdate('m', $value), |
|
310 | + 'day' => (int) gmdate('d', $value), |
|
311 | 311 | |
312 | 312 | ); |
313 | 313 | } |
@@ -343,14 +343,14 @@ discard block |
||
343 | 343 | ) |
344 | 344 | ); |
345 | 345 | |
346 | - if ( empty( $latest_posts_ids ) ) { |
|
346 | + if (empty($latest_posts_ids)) { |
|
347 | 347 | // There are no posts. |
348 | 348 | return array(); |
349 | 349 | } |
350 | 350 | |
351 | 351 | // Collect entities related to latest posts |
352 | 352 | $entity_ids = array(); |
353 | - foreach ( $latest_posts_ids as $id ) { |
|
353 | + foreach ($latest_posts_ids as $id) { |
|
354 | 354 | $entity_ids = array_merge( |
355 | 355 | $entity_ids, |
356 | 356 | wl_core_get_related_entity_ids( |
@@ -16,232 +16,232 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_File_Cache_Service implements Wordlift_Cache_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * The cache directory. |
|
21 | - * |
|
22 | - * @since 3.16.0 |
|
23 | - * @access private |
|
24 | - * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
25 | - */ |
|
26 | - private $cache_dir; |
|
27 | - |
|
28 | - /** |
|
29 | - * The file extension for cache files (e.g. `.wlcache`). |
|
30 | - * |
|
31 | - * @since 3.16.0 |
|
32 | - * @access private |
|
33 | - * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
34 | - */ |
|
35 | - private $file_extension; |
|
36 | - |
|
37 | - /** |
|
38 | - * A {@link Wordlift_Log_Service} instance. |
|
39 | - * |
|
40 | - * @since 3.16.0 |
|
41 | - * @access private |
|
42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | - */ |
|
44 | - private $log; |
|
45 | - |
|
46 | - /** |
|
47 | - * The {@link Wordlift_File_Cache_Service} registered instances. |
|
48 | - * |
|
49 | - * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
50 | - * instances. |
|
51 | - * |
|
52 | - * @since 3.16.3 |
|
53 | - * @access private |
|
54 | - * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
55 | - */ |
|
56 | - private static $instances = array(); |
|
57 | - |
|
58 | - private static $instance; |
|
59 | - |
|
60 | - /** |
|
61 | - * Create a {@link Wordlift_File_Cache_Service} instance. |
|
62 | - * |
|
63 | - * The File Cache Service requires a base cache directory (to which a unique |
|
64 | - * id for the current site will be appended) and a file extension for cache |
|
65 | - * files (by default `.wlcache`) is used. |
|
66 | - * |
|
67 | - * @param string $cache_dir The base cache directory. |
|
68 | - * @param string $file_extension The file extension, by default `.wlcache`. |
|
69 | - * |
|
70 | - * @since 3.16.0 |
|
71 | - */ |
|
72 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
73 | - |
|
74 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
75 | - |
|
76 | - // Set the cache directory using the base directory provided by the caller |
|
77 | - // and appending a hash for the unique site id. |
|
78 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
79 | - $this->file_extension = $file_extension; |
|
80 | - |
|
81 | - // Create the cache dir. |
|
82 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
83 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
85 | - } |
|
86 | - |
|
87 | - // Add ourselves to the list of instances. |
|
88 | - self::$instances[] = $this; |
|
89 | - |
|
90 | - // Initialize the singleton and the ajax method. |
|
91 | - if ( ! isset( self::$instance ) ) { |
|
92 | - self::$instance = $this; |
|
93 | - |
|
94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
95 | - } |
|
96 | - |
|
97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
98 | - |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Get the cached response for the specified `id`. |
|
103 | - * |
|
104 | - * @param int $id The cache `id`. |
|
105 | - * |
|
106 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
107 | - * @since 3.16.0 |
|
108 | - */ |
|
109 | - public function get_cache( $id ) { |
|
110 | - |
|
111 | - // Bail out if we don't have the cache. |
|
112 | - if ( ! $this->has_cache( $id ) ) { |
|
113 | - return false; |
|
114 | - } |
|
115 | - |
|
116 | - // Get the filename. |
|
117 | - $filename = $this->get_filename( $id ); |
|
118 | - |
|
119 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
120 | - |
|
121 | - // Try to decode the contents. |
|
122 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
124 | - |
|
125 | - // Return false if decoding failed, otherwise the decoded contents. |
|
126 | - return $contents ? $contents : false; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Set the cache contents for the specified `id`. |
|
131 | - * |
|
132 | - * @param int $id The cache id. |
|
133 | - * |
|
134 | - * @return bool True if the `id` has a cache. |
|
135 | - * @since 3.16.0 |
|
136 | - */ |
|
137 | - public function has_cache( $id ) { |
|
138 | - |
|
139 | - // Get the filename. |
|
140 | - $filename = $this->get_filename( $id ); |
|
141 | - |
|
142 | - // Bail out if the file doesn't exist. |
|
143 | - return file_exists( $filename ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @inheritdoc |
|
148 | - */ |
|
149 | - public function set_cache( $id, $contents ) { |
|
150 | - |
|
151 | - $filename = $this->get_filename( $id ); |
|
152 | - |
|
153 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
154 | - |
|
155 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
157 | - |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Delete the cache for the specified `id`. |
|
162 | - * |
|
163 | - * @param int $id The cache `id`. |
|
164 | - * |
|
165 | - * @since 3.16.0 |
|
166 | - */ |
|
167 | - public function delete_cache( $id ) { |
|
168 | - |
|
169 | - $filename = $this->get_filename( $id ); |
|
170 | - |
|
171 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
19 | + /** |
|
20 | + * The cache directory. |
|
21 | + * |
|
22 | + * @since 3.16.0 |
|
23 | + * @access private |
|
24 | + * @var string $cache_dir The root cache directory (ending with a trailing slash). |
|
25 | + */ |
|
26 | + private $cache_dir; |
|
27 | + |
|
28 | + /** |
|
29 | + * The file extension for cache files (e.g. `.wlcache`). |
|
30 | + * |
|
31 | + * @since 3.16.0 |
|
32 | + * @access private |
|
33 | + * @var string $file_extension The file extension for cache files (e.g. `.wlcache`). |
|
34 | + */ |
|
35 | + private $file_extension; |
|
36 | + |
|
37 | + /** |
|
38 | + * A {@link Wordlift_Log_Service} instance. |
|
39 | + * |
|
40 | + * @since 3.16.0 |
|
41 | + * @access private |
|
42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | + */ |
|
44 | + private $log; |
|
45 | + |
|
46 | + /** |
|
47 | + * The {@link Wordlift_File_Cache_Service} registered instances. |
|
48 | + * |
|
49 | + * Each {@link Wordlift_File_Cache_Service} adds itself to the registered |
|
50 | + * instances. |
|
51 | + * |
|
52 | + * @since 3.16.3 |
|
53 | + * @access private |
|
54 | + * @var array $instances An array of {@link Wordlift_File_Cache_Service} instances. |
|
55 | + */ |
|
56 | + private static $instances = array(); |
|
57 | + |
|
58 | + private static $instance; |
|
59 | + |
|
60 | + /** |
|
61 | + * Create a {@link Wordlift_File_Cache_Service} instance. |
|
62 | + * |
|
63 | + * The File Cache Service requires a base cache directory (to which a unique |
|
64 | + * id for the current site will be appended) and a file extension for cache |
|
65 | + * files (by default `.wlcache`) is used. |
|
66 | + * |
|
67 | + * @param string $cache_dir The base cache directory. |
|
68 | + * @param string $file_extension The file extension, by default `.wlcache`. |
|
69 | + * |
|
70 | + * @since 3.16.0 |
|
71 | + */ |
|
72 | + public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
73 | + |
|
74 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
75 | + |
|
76 | + // Set the cache directory using the base directory provided by the caller |
|
77 | + // and appending a hash for the unique site id. |
|
78 | + $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
79 | + $this->file_extension = $file_extension; |
|
80 | + |
|
81 | + // Create the cache dir. |
|
82 | + if ( ! file_exists( $this->cache_dir ) ) { |
|
83 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
84 | + @mkdir( $this->cache_dir, 0755, true ); |
|
85 | + } |
|
86 | + |
|
87 | + // Add ourselves to the list of instances. |
|
88 | + self::$instances[] = $this; |
|
89 | + |
|
90 | + // Initialize the singleton and the ajax method. |
|
91 | + if ( ! isset( self::$instance ) ) { |
|
92 | + self::$instance = $this; |
|
93 | + |
|
94 | + add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
95 | + } |
|
96 | + |
|
97 | + $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
98 | + |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Get the cached response for the specified `id`. |
|
103 | + * |
|
104 | + * @param int $id The cache `id`. |
|
105 | + * |
|
106 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
107 | + * @since 3.16.0 |
|
108 | + */ |
|
109 | + public function get_cache( $id ) { |
|
110 | + |
|
111 | + // Bail out if we don't have the cache. |
|
112 | + if ( ! $this->has_cache( $id ) ) { |
|
113 | + return false; |
|
114 | + } |
|
115 | + |
|
116 | + // Get the filename. |
|
117 | + $filename = $this->get_filename( $id ); |
|
118 | + |
|
119 | + $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
120 | + |
|
121 | + // Try to decode the contents. |
|
122 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
123 | + $contents = json_decode( file_get_contents( $filename ), true ); |
|
124 | + |
|
125 | + // Return false if decoding failed, otherwise the decoded contents. |
|
126 | + return $contents ? $contents : false; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Set the cache contents for the specified `id`. |
|
131 | + * |
|
132 | + * @param int $id The cache id. |
|
133 | + * |
|
134 | + * @return bool True if the `id` has a cache. |
|
135 | + * @since 3.16.0 |
|
136 | + */ |
|
137 | + public function has_cache( $id ) { |
|
138 | + |
|
139 | + // Get the filename. |
|
140 | + $filename = $this->get_filename( $id ); |
|
141 | + |
|
142 | + // Bail out if the file doesn't exist. |
|
143 | + return file_exists( $filename ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @inheritdoc |
|
148 | + */ |
|
149 | + public function set_cache( $id, $contents ) { |
|
150 | + |
|
151 | + $filename = $this->get_filename( $id ); |
|
152 | + |
|
153 | + $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
154 | + |
|
155 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
156 | + @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
157 | + |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Delete the cache for the specified `id`. |
|
162 | + * |
|
163 | + * @param int $id The cache `id`. |
|
164 | + * |
|
165 | + * @since 3.16.0 |
|
166 | + */ |
|
167 | + public function delete_cache( $id ) { |
|
168 | + |
|
169 | + $filename = $this->get_filename( $id ); |
|
170 | + |
|
171 | + $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
172 | 172 | |
173 | - if ( file_exists( $filename ) ) { |
|
174 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
175 | - @unlink( $filename ); |
|
176 | - } |
|
177 | - |
|
178 | - } |
|
173 | + if ( file_exists( $filename ) ) { |
|
174 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
175 | + @unlink( $filename ); |
|
176 | + } |
|
177 | + |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Flush the whole cache. |
|
182 | - * |
|
183 | - * @since 3.16.0 |
|
184 | - */ |
|
185 | - public function flush() { |
|
180 | + /** |
|
181 | + * Flush the whole cache. |
|
182 | + * |
|
183 | + * @since 3.16.0 |
|
184 | + */ |
|
185 | + public function flush() { |
|
186 | 186 | |
187 | - // Bail out if the cache dir isn't set. |
|
188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
189 | - return; |
|
190 | - } |
|
187 | + // Bail out if the cache dir isn't set. |
|
188 | + if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
189 | + return; |
|
190 | + } |
|
191 | 191 | |
192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
192 | + $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
193 | 193 | |
194 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
195 | - $handle = @opendir( $this->cache_dir ); |
|
194 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
195 | + $handle = @opendir( $this->cache_dir ); |
|
196 | 196 | |
197 | - // Bail out if the directory can't be opened. |
|
198 | - if ( false === $handle ) { |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - // Calculate the file extension length for matching file names. |
|
203 | - $file_extension_length = strlen( $this->file_extension ); |
|
204 | - |
|
205 | - // Loop into the directory to delete files. |
|
206 | - while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
197 | + // Bail out if the directory can't be opened. |
|
198 | + if ( false === $handle ) { |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + // Calculate the file extension length for matching file names. |
|
203 | + $file_extension_length = strlen( $this->file_extension ); |
|
204 | + |
|
205 | + // Loop into the directory to delete files. |
|
206 | + while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | + if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | + && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | + $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
210 | 210 | |
211 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
212 | - @unlink( $this->cache_dir . $entry ); |
|
213 | - } |
|
214 | - } |
|
211 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
212 | + @unlink( $this->cache_dir . $entry ); |
|
213 | + } |
|
214 | + } |
|
215 | 215 | |
216 | - // Finally closed the directory. |
|
217 | - closedir( $handle ); |
|
216 | + // Finally closed the directory. |
|
217 | + closedir( $handle ); |
|
218 | 218 | |
219 | - } |
|
219 | + } |
|
220 | 220 | |
221 | - public static function flush_all() { |
|
222 | - |
|
223 | - foreach ( self::$instances as $instance ) { |
|
224 | - $instance->flush(); |
|
225 | - } |
|
226 | - |
|
227 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | - wp_send_json_success(); |
|
230 | - } |
|
221 | + public static function flush_all() { |
|
222 | + |
|
223 | + foreach ( self::$instances as $instance ) { |
|
224 | + $instance->flush(); |
|
225 | + } |
|
226 | + |
|
227 | + if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | + && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | + wp_send_json_success(); |
|
230 | + } |
|
231 | 231 | |
232 | - } |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Get the filename holding the cache contents for the specified `id`. |
|
236 | - * |
|
237 | - * @param int $id The cache `id`. |
|
238 | - * |
|
239 | - * @return string The filename. |
|
240 | - * @since 3.16.0 |
|
241 | - */ |
|
242 | - private function get_filename( $id ) { |
|
234 | + /** |
|
235 | + * Get the filename holding the cache contents for the specified `id`. |
|
236 | + * |
|
237 | + * @param int $id The cache `id`. |
|
238 | + * |
|
239 | + * @return string The filename. |
|
240 | + * @since 3.16.0 |
|
241 | + */ |
|
242 | + private function get_filename( $id ) { |
|
243 | 243 | |
244 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
245 | - } |
|
244 | + return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
245 | + } |
|
246 | 246 | |
247 | 247 | } |
@@ -69,32 +69,32 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @since 3.16.0 |
71 | 71 | */ |
72 | - public function __construct( $cache_dir, $file_extension = '.wlcache' ) { |
|
72 | + public function __construct($cache_dir, $file_extension = '.wlcache') { |
|
73 | 73 | |
74 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
74 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
75 | 75 | |
76 | 76 | // Set the cache directory using the base directory provided by the caller |
77 | 77 | // and appending a hash for the unique site id. |
78 | - $this->cache_dir = trailingslashit( $cache_dir ) . md5( get_site_url() ) . '/'; |
|
78 | + $this->cache_dir = trailingslashit($cache_dir).md5(get_site_url()).'/'; |
|
79 | 79 | $this->file_extension = $file_extension; |
80 | 80 | |
81 | 81 | // Create the cache dir. |
82 | - if ( ! file_exists( $this->cache_dir ) ) { |
|
82 | + if ( ! file_exists($this->cache_dir)) { |
|
83 | 83 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
84 | - @mkdir( $this->cache_dir, 0755, true ); |
|
84 | + @mkdir($this->cache_dir, 0755, true); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | // Add ourselves to the list of instances. |
88 | 88 | self::$instances[] = $this; |
89 | 89 | |
90 | 90 | // Initialize the singleton and the ajax method. |
91 | - if ( ! isset( self::$instance ) ) { |
|
91 | + if ( ! isset(self::$instance)) { |
|
92 | 92 | self::$instance = $this; |
93 | 93 | |
94 | - add_action( 'wp_ajax_wl_file_cache__flush_all', array( 'Wordlift_File_Cache_Service', 'flush_all' ) ); |
|
94 | + add_action('wp_ajax_wl_file_cache__flush_all', array('Wordlift_File_Cache_Service', 'flush_all')); |
|
95 | 95 | } |
96 | 96 | |
97 | - $this->log->debug( "File Cache service initialized on $this->cache_dir." ); |
|
97 | + $this->log->debug("File Cache service initialized on $this->cache_dir."); |
|
98 | 98 | |
99 | 99 | } |
100 | 100 | |
@@ -106,21 +106,21 @@ discard block |
||
106 | 106 | * @return mixed|false The cached contents or false if the cache isn't found. |
107 | 107 | * @since 3.16.0 |
108 | 108 | */ |
109 | - public function get_cache( $id ) { |
|
109 | + public function get_cache($id) { |
|
110 | 110 | |
111 | 111 | // Bail out if we don't have the cache. |
112 | - if ( ! $this->has_cache( $id ) ) { |
|
112 | + if ( ! $this->has_cache($id)) { |
|
113 | 113 | return false; |
114 | 114 | } |
115 | 115 | |
116 | 116 | // Get the filename. |
117 | - $filename = $this->get_filename( $id ); |
|
117 | + $filename = $this->get_filename($id); |
|
118 | 118 | |
119 | - $this->log->trace( "Trying to get cache contents for $id from $filename..." ); |
|
119 | + $this->log->trace("Trying to get cache contents for $id from $filename..."); |
|
120 | 120 | |
121 | 121 | // Try to decode the contents. |
122 | 122 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
123 | - $contents = json_decode( file_get_contents( $filename ), true ); |
|
123 | + $contents = json_decode(file_get_contents($filename), true); |
|
124 | 124 | |
125 | 125 | // Return false if decoding failed, otherwise the decoded contents. |
126 | 126 | return $contents ? $contents : false; |
@@ -134,26 +134,26 @@ discard block |
||
134 | 134 | * @return bool True if the `id` has a cache. |
135 | 135 | * @since 3.16.0 |
136 | 136 | */ |
137 | - public function has_cache( $id ) { |
|
137 | + public function has_cache($id) { |
|
138 | 138 | |
139 | 139 | // Get the filename. |
140 | - $filename = $this->get_filename( $id ); |
|
140 | + $filename = $this->get_filename($id); |
|
141 | 141 | |
142 | 142 | // Bail out if the file doesn't exist. |
143 | - return file_exists( $filename ); |
|
143 | + return file_exists($filename); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
147 | 147 | * @inheritdoc |
148 | 148 | */ |
149 | - public function set_cache( $id, $contents ) { |
|
149 | + public function set_cache($id, $contents) { |
|
150 | 150 | |
151 | - $filename = $this->get_filename( $id ); |
|
151 | + $filename = $this->get_filename($id); |
|
152 | 152 | |
153 | - $this->log->trace( "Writing cache contents for $id to $filename..." ); |
|
153 | + $this->log->trace("Writing cache contents for $id to $filename..."); |
|
154 | 154 | |
155 | 155 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
156 | - @file_put_contents( $filename, wp_json_encode( $contents ) ); |
|
156 | + @file_put_contents($filename, wp_json_encode($contents)); |
|
157 | 157 | |
158 | 158 | } |
159 | 159 | |
@@ -164,15 +164,15 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @since 3.16.0 |
166 | 166 | */ |
167 | - public function delete_cache( $id ) { |
|
167 | + public function delete_cache($id) { |
|
168 | 168 | |
169 | - $filename = $this->get_filename( $id ); |
|
169 | + $filename = $this->get_filename($id); |
|
170 | 170 | |
171 | - $this->log->trace( "Deleting cache contents for $id, file $filename..." ); |
|
171 | + $this->log->trace("Deleting cache contents for $id, file $filename..."); |
|
172 | 172 | |
173 | - if ( file_exists( $filename ) ) { |
|
173 | + if (file_exists($filename)) { |
|
174 | 174 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
175 | - @unlink( $filename ); |
|
175 | + @unlink($filename); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | } |
@@ -185,47 +185,47 @@ discard block |
||
185 | 185 | public function flush() { |
186 | 186 | |
187 | 187 | // Bail out if the cache dir isn't set. |
188 | - if ( empty( $this->cache_dir ) || '/' === $this->cache_dir ) { |
|
188 | + if (empty($this->cache_dir) || '/' === $this->cache_dir) { |
|
189 | 189 | return; |
190 | 190 | } |
191 | 191 | |
192 | - $this->log->trace( "Flushing cache contents from $this->cache_dir..." ); |
|
192 | + $this->log->trace("Flushing cache contents from $this->cache_dir..."); |
|
193 | 193 | |
194 | 194 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
195 | - $handle = @opendir( $this->cache_dir ); |
|
195 | + $handle = @opendir($this->cache_dir); |
|
196 | 196 | |
197 | 197 | // Bail out if the directory can't be opened. |
198 | - if ( false === $handle ) { |
|
198 | + if (false === $handle) { |
|
199 | 199 | return; |
200 | 200 | } |
201 | 201 | |
202 | 202 | // Calculate the file extension length for matching file names. |
203 | - $file_extension_length = strlen( $this->file_extension ); |
|
203 | + $file_extension_length = strlen($this->file_extension); |
|
204 | 204 | |
205 | 205 | // Loop into the directory to delete files. |
206 | - while ( false !== ( $entry = readdir( $handle ) ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | - if ( substr( $entry, - $file_extension_length ) === $this->file_extension |
|
208 | - && file_exists( $this->cache_dir . $entry ) ) { |
|
209 | - $this->log->trace( "Deleting file {$this->cache_dir}{$entry}..." ); |
|
206 | + while (false !== ($entry = readdir($handle))) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
|
207 | + if (substr($entry, - $file_extension_length) === $this->file_extension |
|
208 | + && file_exists($this->cache_dir.$entry)) { |
|
209 | + $this->log->trace("Deleting file {$this->cache_dir}{$entry}..."); |
|
210 | 210 | |
211 | 211 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
212 | - @unlink( $this->cache_dir . $entry ); |
|
212 | + @unlink($this->cache_dir.$entry); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | 216 | // Finally closed the directory. |
217 | - closedir( $handle ); |
|
217 | + closedir($handle); |
|
218 | 218 | |
219 | 219 | } |
220 | 220 | |
221 | 221 | public static function flush_all() { |
222 | 222 | |
223 | - foreach ( self::$instances as $instance ) { |
|
223 | + foreach (self::$instances as $instance) { |
|
224 | 224 | $instance->flush(); |
225 | 225 | } |
226 | 226 | |
227 | - if ( defined( 'DOING_AJAX' ) && DOING_AJAX |
|
228 | - && isset( $_REQUEST['action'] ) && 'wl_file_cache__flush_all' === $_REQUEST['action'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
227 | + if (defined('DOING_AJAX') && DOING_AJAX |
|
228 | + && isset($_REQUEST['action']) && 'wl_file_cache__flush_all' === $_REQUEST['action']) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
229 | 229 | wp_send_json_success(); |
230 | 230 | } |
231 | 231 | |
@@ -239,9 +239,9 @@ discard block |
||
239 | 239 | * @return string The filename. |
240 | 240 | * @since 3.16.0 |
241 | 241 | */ |
242 | - private function get_filename( $id ) { |
|
242 | + private function get_filename($id) { |
|
243 | 243 | |
244 | - return $this->cache_dir . md5( $id ) . $this->file_extension; |
|
244 | + return $this->cache_dir.md5($id).$this->file_extension; |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | } |
@@ -20,308 +20,308 @@ |
||
20 | 20 | */ |
21 | 21 | class Wordlift_Entity_List_Service { |
22 | 22 | |
23 | - /** |
|
24 | - * Size of the entity thumbnail in pixels |
|
25 | - * |
|
26 | - * @since 3.3.0 |
|
27 | - */ |
|
28 | - const THUMB_SIZE = 50; |
|
29 | - |
|
30 | - /** |
|
31 | - * A {@link Wordlift_Rating_Service} instance. |
|
32 | - * |
|
33 | - * @since 3.10.0 |
|
34 | - * @access private |
|
35 | - * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
36 | - */ |
|
37 | - private $rating_service; |
|
38 | - |
|
39 | - /** |
|
40 | - * Create a Wordlift_Entity_List_Service. |
|
41 | - * |
|
42 | - * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
43 | - * |
|
44 | - * @since 3.3.0 |
|
45 | - */ |
|
46 | - public function __construct( $rating_service ) { |
|
47 | - |
|
48 | - $this->rating_service = $rating_service; |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Detects if the entities list admin screen is being displayed |
|
54 | - * |
|
55 | - * @return bool True if the screen is being displayed, false otherwis. |
|
56 | - * |
|
57 | - * @since 3.15.0 |
|
58 | - */ |
|
59 | - private function is_entity_list_screen() { |
|
60 | - |
|
61 | - // Run only on admin page. |
|
62 | - if ( ! is_admin() ) { |
|
63 | - return false; |
|
64 | - } |
|
65 | - |
|
66 | - // Return safely if get_current_screen() is not defined (yet). |
|
67 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
68 | - return false; |
|
69 | - } |
|
70 | - |
|
71 | - // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
72 | - $screen = get_current_screen(); |
|
73 | - |
|
74 | - // If there is any valid screen nothing to do. |
|
75 | - if ( null === $screen ) { |
|
76 | - return false; |
|
77 | - } |
|
78 | - |
|
79 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->post_type ) { |
|
80 | - return false; |
|
81 | - } |
|
82 | - |
|
83 | - return true; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Register custom columns for entity listing in backend. |
|
88 | - * |
|
89 | - * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column |
|
90 | - * |
|
91 | - * @since 3.3.0 |
|
92 | - * |
|
93 | - * @param array $columns the default columns. |
|
94 | - * |
|
95 | - * @return array Enhanced columns array. |
|
96 | - */ |
|
97 | - public function register_custom_columns( $columns ) { |
|
98 | - |
|
99 | - // Bail out if custom columns disabled via hook |
|
100 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
101 | - if ( ! apply_filters( 'wl_feature__enable__custom-columns', true ) ) { |
|
102 | - return $columns; |
|
103 | - } |
|
104 | - |
|
105 | - // Take away first column and keep a reference, |
|
106 | - // so we can later insert the thumbnail between the first and the rest of columns. |
|
107 | - $columns_cb = $columns['cb']; |
|
108 | - unset( $columns['cb'] ); |
|
109 | - |
|
110 | - // Thumbnails column is inserted in second place, while the related posts on the end. |
|
111 | - $columns = array_merge( |
|
112 | - array( 'cb' => $columns_cb ), // re-add first column |
|
113 | - array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
114 | - $columns, // default columns (without the first) |
|
115 | - array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
116 | - array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
117 | - ); |
|
118 | - |
|
119 | - return $columns; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Render custom columns. |
|
124 | - * |
|
125 | - * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column |
|
126 | - * |
|
127 | - * @since 3.3.0 |
|
128 | - * |
|
129 | - * @param string $column The current column. |
|
130 | - * @param int $entity_id An entity post id. |
|
131 | - */ |
|
132 | - public function render_custom_columns( $column, $entity_id ) { |
|
133 | - |
|
134 | - switch ( $column ) { |
|
135 | - |
|
136 | - case 'wl_column_related_posts': |
|
137 | - echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
138 | - break; |
|
139 | - |
|
140 | - case 'wl_column_thumbnail': |
|
141 | - $edit_link = get_edit_post_link( $entity_id ); |
|
142 | - $thumb = get_the_post_thumbnail( |
|
143 | - $entity_id, |
|
144 | - array( |
|
145 | - self::THUMB_SIZE, |
|
146 | - self::THUMB_SIZE, |
|
147 | - ) |
|
148 | - ); |
|
149 | - |
|
150 | - if ( ! $thumb ) { |
|
151 | - $thumb = sprintf( '<img src="%s" width="%d">', WL_DEFAULT_THUMBNAIL_PATH, self::THUMB_SIZE ); |
|
152 | - } |
|
153 | - |
|
154 | - echo wp_kses( |
|
155 | - sprintf( '<a href="%s">%s</a>', esc_url( $edit_link ), $thumb ), |
|
156 | - array( |
|
157 | - 'a' => array( 'href' => array() ), |
|
158 | - 'img' => array( |
|
159 | - 'src' => array(), |
|
160 | - 'width' => array(), |
|
161 | - ), |
|
162 | - ) |
|
163 | - ); |
|
164 | - break; |
|
165 | - |
|
166 | - case 'wl_column_rating': |
|
167 | - $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
168 | - echo '<i class="wl-traffic-light wl-tl-' . esc_attr( $rating['traffic_light_score'] ) . '">' . esc_html( $rating['percentage_score'] ) . '%</i>'; |
|
169 | - break; |
|
170 | - } |
|
171 | - |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Add wl-classification-scope select box before the 'Filter' button. |
|
176 | - * |
|
177 | - * @since 3.3.0 |
|
178 | - */ |
|
179 | - public function restrict_manage_posts_classification_scope() { |
|
180 | - |
|
181 | - if ( ! $this->is_entity_list_screen() ) { |
|
182 | - return; |
|
183 | - } |
|
184 | - |
|
185 | - // Was a W already selected? |
|
186 | - $selected = isset( $_GET['wl-classification-scope'] ) ? //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
187 | - sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
188 | - |
|
189 | - // Print select box with the 4W |
|
190 | - $all_w = array( |
|
191 | - "All 'W'" => __( "All 'W'", 'wordlift' ), |
|
192 | - WL_WHAT_RELATION => __( 'What', 'wordlift' ), |
|
193 | - WL_WHO_RELATION => __( 'Who', 'wordlift' ), |
|
194 | - WL_WHERE_RELATION => __( 'Where', 'wordlift' ), |
|
195 | - WL_WHEN_RELATION => __( 'When', 'wordlift' ), |
|
196 | - ); |
|
197 | - echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
|
198 | - foreach ( $all_w as $v => $w ) { |
|
199 | - $default = ( $selected === $v ) ? 'selected' : ''; |
|
200 | - echo sprintf( '<option value="%s" %s >%s</option>', esc_attr( $v ), esc_html( $default ), esc_html( $w ) ); |
|
201 | - } |
|
202 | - echo '</select>'; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Server side response operations for the classification filter set in |
|
207 | - * *restrict_manage_posts_classification_scope_filter*. |
|
208 | - * |
|
209 | - * @param array $clauses WP main query clauses. |
|
210 | - * |
|
211 | - * @return array Modified clauses. |
|
212 | - * @since 3.3.0 |
|
213 | - */ |
|
214 | - public function posts_clauses_classification_scope( $clauses ) { |
|
215 | - |
|
216 | - if ( ! ( $this->is_entity_list_screen() && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
217 | - return $clauses; |
|
218 | - } |
|
219 | - |
|
220 | - // Check a valid W was requested. |
|
221 | - $requested_w = sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
222 | - |
|
223 | - $all_w = array( |
|
224 | - WL_WHAT_RELATION, |
|
225 | - WL_WHO_RELATION, |
|
226 | - WL_WHERE_RELATION, |
|
227 | - WL_WHEN_RELATION, |
|
228 | - ); |
|
229 | - |
|
230 | - if ( ! in_array( $requested_w, $all_w, true ) ) { |
|
231 | - return $clauses; |
|
232 | - } |
|
233 | - |
|
234 | - global $wpdb; |
|
235 | - |
|
236 | - // Change WP main query clauses. |
|
237 | - $clauses['join'] .= "INNER JOIN {$wpdb->prefix}wl_relation_instances ON {$wpdb->posts}.ID = {$wpdb->prefix}wl_relation_instances.object_id"; |
|
238 | - $clauses['where'] .= $wpdb->prepare( "AND {$wpdb->prefix}wl_relation_instances.predicate = %s", $requested_w ); |
|
239 | - $clauses['distinct'] .= 'DISTINCT'; |
|
240 | - |
|
241 | - return $clauses; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Amend the "all entities" list admin screen with entities from other |
|
246 | - * post types, not only the entities one. |
|
247 | - * |
|
248 | - * @param WP_Query $query The WP_Query instance (passed by reference). |
|
249 | - * |
|
250 | - * @since 3.15.0 |
|
251 | - */ |
|
252 | - public function pre_get_posts( $query ) { |
|
253 | - |
|
254 | - if ( ! ( $this->is_entity_list_screen() && $query->is_main_query() ) ) { |
|
255 | - return; |
|
256 | - } |
|
257 | - |
|
258 | - // Add to the post type all the types considered to be valid post types. |
|
259 | - $query->set( 'post_type', Wordlift_Entity_Service::valid_entity_post_types() ); |
|
260 | - |
|
261 | - // Do not show however entities of type `Article`. |
|
262 | - $query->set( |
|
263 | - 'tax_query', |
|
264 | - array( |
|
265 | - 'relation' => 'AND', |
|
266 | - array( |
|
267 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
268 | - 'operator' => 'EXISTS', |
|
269 | - ), |
|
270 | - array( |
|
271 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
272 | - 'field' => 'slug', |
|
273 | - 'terms' => 'article', |
|
274 | - 'operator' => 'NOT IN', |
|
275 | - ), |
|
276 | - ) |
|
277 | - ); |
|
278 | - |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * An hack to fix wrong post type on the entities list admin screen. |
|
283 | - * |
|
284 | - * This is hooked on the admin entity list page load hook and sets the |
|
285 | - * post type to "entity" as it is expected on that page. |
|
286 | - * |
|
287 | - * @since 3.15.0 |
|
288 | - */ |
|
289 | - public function load_edit() { |
|
290 | - |
|
291 | - // Return safely if get_current_screen() is not defined (yet). |
|
292 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
293 | - return; |
|
294 | - } |
|
295 | - |
|
296 | - // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
297 | - $screen = get_current_screen(); |
|
298 | - |
|
299 | - // If there is any valid screen nothing to do. |
|
300 | - if ( null === $screen ) { |
|
301 | - return; |
|
302 | - } |
|
303 | - |
|
304 | - if ( ! ( Wordlift_Entity_Service::TYPE_NAME === $screen->post_type && is_main_query() ) ) { |
|
305 | - return; |
|
306 | - } |
|
307 | - |
|
308 | - /* |
|
23 | + /** |
|
24 | + * Size of the entity thumbnail in pixels |
|
25 | + * |
|
26 | + * @since 3.3.0 |
|
27 | + */ |
|
28 | + const THUMB_SIZE = 50; |
|
29 | + |
|
30 | + /** |
|
31 | + * A {@link Wordlift_Rating_Service} instance. |
|
32 | + * |
|
33 | + * @since 3.10.0 |
|
34 | + * @access private |
|
35 | + * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
36 | + */ |
|
37 | + private $rating_service; |
|
38 | + |
|
39 | + /** |
|
40 | + * Create a Wordlift_Entity_List_Service. |
|
41 | + * |
|
42 | + * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
43 | + * |
|
44 | + * @since 3.3.0 |
|
45 | + */ |
|
46 | + public function __construct( $rating_service ) { |
|
47 | + |
|
48 | + $this->rating_service = $rating_service; |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Detects if the entities list admin screen is being displayed |
|
54 | + * |
|
55 | + * @return bool True if the screen is being displayed, false otherwis. |
|
56 | + * |
|
57 | + * @since 3.15.0 |
|
58 | + */ |
|
59 | + private function is_entity_list_screen() { |
|
60 | + |
|
61 | + // Run only on admin page. |
|
62 | + if ( ! is_admin() ) { |
|
63 | + return false; |
|
64 | + } |
|
65 | + |
|
66 | + // Return safely if get_current_screen() is not defined (yet). |
|
67 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
68 | + return false; |
|
69 | + } |
|
70 | + |
|
71 | + // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
72 | + $screen = get_current_screen(); |
|
73 | + |
|
74 | + // If there is any valid screen nothing to do. |
|
75 | + if ( null === $screen ) { |
|
76 | + return false; |
|
77 | + } |
|
78 | + |
|
79 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->post_type ) { |
|
80 | + return false; |
|
81 | + } |
|
82 | + |
|
83 | + return true; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Register custom columns for entity listing in backend. |
|
88 | + * |
|
89 | + * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column |
|
90 | + * |
|
91 | + * @since 3.3.0 |
|
92 | + * |
|
93 | + * @param array $columns the default columns. |
|
94 | + * |
|
95 | + * @return array Enhanced columns array. |
|
96 | + */ |
|
97 | + public function register_custom_columns( $columns ) { |
|
98 | + |
|
99 | + // Bail out if custom columns disabled via hook |
|
100 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
101 | + if ( ! apply_filters( 'wl_feature__enable__custom-columns', true ) ) { |
|
102 | + return $columns; |
|
103 | + } |
|
104 | + |
|
105 | + // Take away first column and keep a reference, |
|
106 | + // so we can later insert the thumbnail between the first and the rest of columns. |
|
107 | + $columns_cb = $columns['cb']; |
|
108 | + unset( $columns['cb'] ); |
|
109 | + |
|
110 | + // Thumbnails column is inserted in second place, while the related posts on the end. |
|
111 | + $columns = array_merge( |
|
112 | + array( 'cb' => $columns_cb ), // re-add first column |
|
113 | + array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
114 | + $columns, // default columns (without the first) |
|
115 | + array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
116 | + array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
117 | + ); |
|
118 | + |
|
119 | + return $columns; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Render custom columns. |
|
124 | + * |
|
125 | + * @see https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column |
|
126 | + * |
|
127 | + * @since 3.3.0 |
|
128 | + * |
|
129 | + * @param string $column The current column. |
|
130 | + * @param int $entity_id An entity post id. |
|
131 | + */ |
|
132 | + public function render_custom_columns( $column, $entity_id ) { |
|
133 | + |
|
134 | + switch ( $column ) { |
|
135 | + |
|
136 | + case 'wl_column_related_posts': |
|
137 | + echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
138 | + break; |
|
139 | + |
|
140 | + case 'wl_column_thumbnail': |
|
141 | + $edit_link = get_edit_post_link( $entity_id ); |
|
142 | + $thumb = get_the_post_thumbnail( |
|
143 | + $entity_id, |
|
144 | + array( |
|
145 | + self::THUMB_SIZE, |
|
146 | + self::THUMB_SIZE, |
|
147 | + ) |
|
148 | + ); |
|
149 | + |
|
150 | + if ( ! $thumb ) { |
|
151 | + $thumb = sprintf( '<img src="%s" width="%d">', WL_DEFAULT_THUMBNAIL_PATH, self::THUMB_SIZE ); |
|
152 | + } |
|
153 | + |
|
154 | + echo wp_kses( |
|
155 | + sprintf( '<a href="%s">%s</a>', esc_url( $edit_link ), $thumb ), |
|
156 | + array( |
|
157 | + 'a' => array( 'href' => array() ), |
|
158 | + 'img' => array( |
|
159 | + 'src' => array(), |
|
160 | + 'width' => array(), |
|
161 | + ), |
|
162 | + ) |
|
163 | + ); |
|
164 | + break; |
|
165 | + |
|
166 | + case 'wl_column_rating': |
|
167 | + $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
168 | + echo '<i class="wl-traffic-light wl-tl-' . esc_attr( $rating['traffic_light_score'] ) . '">' . esc_html( $rating['percentage_score'] ) . '%</i>'; |
|
169 | + break; |
|
170 | + } |
|
171 | + |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Add wl-classification-scope select box before the 'Filter' button. |
|
176 | + * |
|
177 | + * @since 3.3.0 |
|
178 | + */ |
|
179 | + public function restrict_manage_posts_classification_scope() { |
|
180 | + |
|
181 | + if ( ! $this->is_entity_list_screen() ) { |
|
182 | + return; |
|
183 | + } |
|
184 | + |
|
185 | + // Was a W already selected? |
|
186 | + $selected = isset( $_GET['wl-classification-scope'] ) ? //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
187 | + sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
188 | + |
|
189 | + // Print select box with the 4W |
|
190 | + $all_w = array( |
|
191 | + "All 'W'" => __( "All 'W'", 'wordlift' ), |
|
192 | + WL_WHAT_RELATION => __( 'What', 'wordlift' ), |
|
193 | + WL_WHO_RELATION => __( 'Who', 'wordlift' ), |
|
194 | + WL_WHERE_RELATION => __( 'Where', 'wordlift' ), |
|
195 | + WL_WHEN_RELATION => __( 'When', 'wordlift' ), |
|
196 | + ); |
|
197 | + echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
|
198 | + foreach ( $all_w as $v => $w ) { |
|
199 | + $default = ( $selected === $v ) ? 'selected' : ''; |
|
200 | + echo sprintf( '<option value="%s" %s >%s</option>', esc_attr( $v ), esc_html( $default ), esc_html( $w ) ); |
|
201 | + } |
|
202 | + echo '</select>'; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Server side response operations for the classification filter set in |
|
207 | + * *restrict_manage_posts_classification_scope_filter*. |
|
208 | + * |
|
209 | + * @param array $clauses WP main query clauses. |
|
210 | + * |
|
211 | + * @return array Modified clauses. |
|
212 | + * @since 3.3.0 |
|
213 | + */ |
|
214 | + public function posts_clauses_classification_scope( $clauses ) { |
|
215 | + |
|
216 | + if ( ! ( $this->is_entity_list_screen() && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
217 | + return $clauses; |
|
218 | + } |
|
219 | + |
|
220 | + // Check a valid W was requested. |
|
221 | + $requested_w = sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
222 | + |
|
223 | + $all_w = array( |
|
224 | + WL_WHAT_RELATION, |
|
225 | + WL_WHO_RELATION, |
|
226 | + WL_WHERE_RELATION, |
|
227 | + WL_WHEN_RELATION, |
|
228 | + ); |
|
229 | + |
|
230 | + if ( ! in_array( $requested_w, $all_w, true ) ) { |
|
231 | + return $clauses; |
|
232 | + } |
|
233 | + |
|
234 | + global $wpdb; |
|
235 | + |
|
236 | + // Change WP main query clauses. |
|
237 | + $clauses['join'] .= "INNER JOIN {$wpdb->prefix}wl_relation_instances ON {$wpdb->posts}.ID = {$wpdb->prefix}wl_relation_instances.object_id"; |
|
238 | + $clauses['where'] .= $wpdb->prepare( "AND {$wpdb->prefix}wl_relation_instances.predicate = %s", $requested_w ); |
|
239 | + $clauses['distinct'] .= 'DISTINCT'; |
|
240 | + |
|
241 | + return $clauses; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Amend the "all entities" list admin screen with entities from other |
|
246 | + * post types, not only the entities one. |
|
247 | + * |
|
248 | + * @param WP_Query $query The WP_Query instance (passed by reference). |
|
249 | + * |
|
250 | + * @since 3.15.0 |
|
251 | + */ |
|
252 | + public function pre_get_posts( $query ) { |
|
253 | + |
|
254 | + if ( ! ( $this->is_entity_list_screen() && $query->is_main_query() ) ) { |
|
255 | + return; |
|
256 | + } |
|
257 | + |
|
258 | + // Add to the post type all the types considered to be valid post types. |
|
259 | + $query->set( 'post_type', Wordlift_Entity_Service::valid_entity_post_types() ); |
|
260 | + |
|
261 | + // Do not show however entities of type `Article`. |
|
262 | + $query->set( |
|
263 | + 'tax_query', |
|
264 | + array( |
|
265 | + 'relation' => 'AND', |
|
266 | + array( |
|
267 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
268 | + 'operator' => 'EXISTS', |
|
269 | + ), |
|
270 | + array( |
|
271 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
272 | + 'field' => 'slug', |
|
273 | + 'terms' => 'article', |
|
274 | + 'operator' => 'NOT IN', |
|
275 | + ), |
|
276 | + ) |
|
277 | + ); |
|
278 | + |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * An hack to fix wrong post type on the entities list admin screen. |
|
283 | + * |
|
284 | + * This is hooked on the admin entity list page load hook and sets the |
|
285 | + * post type to "entity" as it is expected on that page. |
|
286 | + * |
|
287 | + * @since 3.15.0 |
|
288 | + */ |
|
289 | + public function load_edit() { |
|
290 | + |
|
291 | + // Return safely if get_current_screen() is not defined (yet). |
|
292 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
293 | + return; |
|
294 | + } |
|
295 | + |
|
296 | + // Only apply on entity list page, only if this is the main query and if the wl-classification-scope query param is set. |
|
297 | + $screen = get_current_screen(); |
|
298 | + |
|
299 | + // If there is any valid screen nothing to do. |
|
300 | + if ( null === $screen ) { |
|
301 | + return; |
|
302 | + } |
|
303 | + |
|
304 | + if ( ! ( Wordlift_Entity_Service::TYPE_NAME === $screen->post_type && is_main_query() ) ) { |
|
305 | + return; |
|
306 | + } |
|
307 | + |
|
308 | + /* |
|
309 | 309 | * The main wp initialization sets the post type to the post type used in the main query, |
310 | 310 | * but the admin edit pages fail to handle such a situation gracefully. |
311 | 311 | * Since this is exactly what we do on the entity page, we have to reset |
312 | 312 | * the global $post_type variable to the "entity" value after the modifications |
313 | 313 | * initialization was finished. |
314 | 314 | */ |
315 | - add_action( |
|
316 | - 'wp', |
|
317 | - function () { |
|
318 | - global $post_type; |
|
319 | - $post_type = Wordlift_Entity_Service::TYPE_NAME; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
320 | - }, |
|
321 | - 10, |
|
322 | - 0 |
|
323 | - ); |
|
324 | - |
|
325 | - } |
|
315 | + add_action( |
|
316 | + 'wp', |
|
317 | + function () { |
|
318 | + global $post_type; |
|
319 | + $post_type = Wordlift_Entity_Service::TYPE_NAME; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
320 | + }, |
|
321 | + 10, |
|
322 | + 0 |
|
323 | + ); |
|
324 | + |
|
325 | + } |
|
326 | 326 | |
327 | 327 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @since 3.3.0 |
45 | 45 | */ |
46 | - public function __construct( $rating_service ) { |
|
46 | + public function __construct($rating_service) { |
|
47 | 47 | |
48 | 48 | $this->rating_service = $rating_service; |
49 | 49 | |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | private function is_entity_list_screen() { |
60 | 60 | |
61 | 61 | // Run only on admin page. |
62 | - if ( ! is_admin() ) { |
|
62 | + if ( ! is_admin()) { |
|
63 | 63 | return false; |
64 | 64 | } |
65 | 65 | |
66 | 66 | // Return safely if get_current_screen() is not defined (yet). |
67 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
67 | + if (false === function_exists('get_current_screen')) { |
|
68 | 68 | return false; |
69 | 69 | } |
70 | 70 | |
@@ -72,11 +72,11 @@ discard block |
||
72 | 72 | $screen = get_current_screen(); |
73 | 73 | |
74 | 74 | // If there is any valid screen nothing to do. |
75 | - if ( null === $screen ) { |
|
75 | + if (null === $screen) { |
|
76 | 76 | return false; |
77 | 77 | } |
78 | 78 | |
79 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->post_type ) { |
|
79 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->post_type) { |
|
80 | 80 | return false; |
81 | 81 | } |
82 | 82 | |
@@ -94,26 +94,26 @@ discard block |
||
94 | 94 | * |
95 | 95 | * @return array Enhanced columns array. |
96 | 96 | */ |
97 | - public function register_custom_columns( $columns ) { |
|
97 | + public function register_custom_columns($columns) { |
|
98 | 98 | |
99 | 99 | // Bail out if custom columns disabled via hook |
100 | 100 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
101 | - if ( ! apply_filters( 'wl_feature__enable__custom-columns', true ) ) { |
|
101 | + if ( ! apply_filters('wl_feature__enable__custom-columns', true)) { |
|
102 | 102 | return $columns; |
103 | 103 | } |
104 | 104 | |
105 | 105 | // Take away first column and keep a reference, |
106 | 106 | // so we can later insert the thumbnail between the first and the rest of columns. |
107 | 107 | $columns_cb = $columns['cb']; |
108 | - unset( $columns['cb'] ); |
|
108 | + unset($columns['cb']); |
|
109 | 109 | |
110 | 110 | // Thumbnails column is inserted in second place, while the related posts on the end. |
111 | 111 | $columns = array_merge( |
112 | - array( 'cb' => $columns_cb ), // re-add first column |
|
113 | - array( 'wl_column_thumbnail' => __( 'Image', 'wordlift' ) ), // thumb |
|
114 | - $columns, // default columns (without the first) |
|
115 | - array( 'wl_column_related_posts' => __( 'Related Posts', 'wordlift' ) ), // related posts |
|
116 | - array( 'wl_column_rating' => __( 'Rating', 'wordlift' ) ) // related posts |
|
112 | + array('cb' => $columns_cb), // re-add first column |
|
113 | + array('wl_column_thumbnail' => __('Image', 'wordlift')), // thumb |
|
114 | + $columns, // default columns (without the first) |
|
115 | + array('wl_column_related_posts' => __('Related Posts', 'wordlift')), // related posts |
|
116 | + array('wl_column_rating' => __('Rating', 'wordlift')) // related posts |
|
117 | 117 | ); |
118 | 118 | |
119 | 119 | return $columns; |
@@ -129,16 +129,16 @@ discard block |
||
129 | 129 | * @param string $column The current column. |
130 | 130 | * @param int $entity_id An entity post id. |
131 | 131 | */ |
132 | - public function render_custom_columns( $column, $entity_id ) { |
|
132 | + public function render_custom_columns($column, $entity_id) { |
|
133 | 133 | |
134 | - switch ( $column ) { |
|
134 | + switch ($column) { |
|
135 | 135 | |
136 | 136 | case 'wl_column_related_posts': |
137 | - echo count( wl_core_get_related_post_ids( $entity_id ) ); |
|
137 | + echo count(wl_core_get_related_post_ids($entity_id)); |
|
138 | 138 | break; |
139 | 139 | |
140 | 140 | case 'wl_column_thumbnail': |
141 | - $edit_link = get_edit_post_link( $entity_id ); |
|
141 | + $edit_link = get_edit_post_link($entity_id); |
|
142 | 142 | $thumb = get_the_post_thumbnail( |
143 | 143 | $entity_id, |
144 | 144 | array( |
@@ -147,14 +147,14 @@ discard block |
||
147 | 147 | ) |
148 | 148 | ); |
149 | 149 | |
150 | - if ( ! $thumb ) { |
|
151 | - $thumb = sprintf( '<img src="%s" width="%d">', WL_DEFAULT_THUMBNAIL_PATH, self::THUMB_SIZE ); |
|
150 | + if ( ! $thumb) { |
|
151 | + $thumb = sprintf('<img src="%s" width="%d">', WL_DEFAULT_THUMBNAIL_PATH, self::THUMB_SIZE); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | echo wp_kses( |
155 | - sprintf( '<a href="%s">%s</a>', esc_url( $edit_link ), $thumb ), |
|
155 | + sprintf('<a href="%s">%s</a>', esc_url($edit_link), $thumb), |
|
156 | 156 | array( |
157 | - 'a' => array( 'href' => array() ), |
|
157 | + 'a' => array('href' => array()), |
|
158 | 158 | 'img' => array( |
159 | 159 | 'src' => array(), |
160 | 160 | 'width' => array(), |
@@ -164,8 +164,8 @@ discard block |
||
164 | 164 | break; |
165 | 165 | |
166 | 166 | case 'wl_column_rating': |
167 | - $rating = $this->rating_service->get_rating_for( $entity_id ); |
|
168 | - echo '<i class="wl-traffic-light wl-tl-' . esc_attr( $rating['traffic_light_score'] ) . '">' . esc_html( $rating['percentage_score'] ) . '%</i>'; |
|
167 | + $rating = $this->rating_service->get_rating_for($entity_id); |
|
168 | + echo '<i class="wl-traffic-light wl-tl-'.esc_attr($rating['traffic_light_score']).'">'.esc_html($rating['percentage_score']).'%</i>'; |
|
169 | 169 | break; |
170 | 170 | } |
171 | 171 | |
@@ -178,26 +178,26 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public function restrict_manage_posts_classification_scope() { |
180 | 180 | |
181 | - if ( ! $this->is_entity_list_screen() ) { |
|
181 | + if ( ! $this->is_entity_list_screen()) { |
|
182 | 182 | return; |
183 | 183 | } |
184 | 184 | |
185 | 185 | // Was a W already selected? |
186 | - $selected = isset( $_GET['wl-classification-scope'] ) ? //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
187 | - sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
186 | + $selected = isset($_GET['wl-classification-scope']) ? //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
187 | + sanitize_text_field(wp_unslash((string) $_GET['wl-classification-scope'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
188 | 188 | |
189 | 189 | // Print select box with the 4W |
190 | 190 | $all_w = array( |
191 | - "All 'W'" => __( "All 'W'", 'wordlift' ), |
|
192 | - WL_WHAT_RELATION => __( 'What', 'wordlift' ), |
|
193 | - WL_WHO_RELATION => __( 'Who', 'wordlift' ), |
|
194 | - WL_WHERE_RELATION => __( 'Where', 'wordlift' ), |
|
195 | - WL_WHEN_RELATION => __( 'When', 'wordlift' ), |
|
191 | + "All 'W'" => __("All 'W'", 'wordlift'), |
|
192 | + WL_WHAT_RELATION => __('What', 'wordlift'), |
|
193 | + WL_WHO_RELATION => __('Who', 'wordlift'), |
|
194 | + WL_WHERE_RELATION => __('Where', 'wordlift'), |
|
195 | + WL_WHEN_RELATION => __('When', 'wordlift'), |
|
196 | 196 | ); |
197 | 197 | echo '<select name="wl-classification-scope" id="wl-dropdown-classification-scope">'; |
198 | - foreach ( $all_w as $v => $w ) { |
|
199 | - $default = ( $selected === $v ) ? 'selected' : ''; |
|
200 | - echo sprintf( '<option value="%s" %s >%s</option>', esc_attr( $v ), esc_html( $default ), esc_html( $w ) ); |
|
198 | + foreach ($all_w as $v => $w) { |
|
199 | + $default = ($selected === $v) ? 'selected' : ''; |
|
200 | + echo sprintf('<option value="%s" %s >%s</option>', esc_attr($v), esc_html($default), esc_html($w)); |
|
201 | 201 | } |
202 | 202 | echo '</select>'; |
203 | 203 | } |
@@ -211,14 +211,14 @@ discard block |
||
211 | 211 | * @return array Modified clauses. |
212 | 212 | * @since 3.3.0 |
213 | 213 | */ |
214 | - public function posts_clauses_classification_scope( $clauses ) { |
|
214 | + public function posts_clauses_classification_scope($clauses) { |
|
215 | 215 | |
216 | - if ( ! ( $this->is_entity_list_screen() && is_main_query() && isset( $_GET['wl-classification-scope'] ) ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
216 | + if ( ! ($this->is_entity_list_screen() && is_main_query() && isset($_GET['wl-classification-scope']))) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
217 | 217 | return $clauses; |
218 | 218 | } |
219 | 219 | |
220 | 220 | // Check a valid W was requested. |
221 | - $requested_w = sanitize_text_field( wp_unslash( (string) $_GET['wl-classification-scope'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
221 | + $requested_w = sanitize_text_field(wp_unslash((string) $_GET['wl-classification-scope'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
222 | 222 | |
223 | 223 | $all_w = array( |
224 | 224 | WL_WHAT_RELATION, |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | WL_WHEN_RELATION, |
228 | 228 | ); |
229 | 229 | |
230 | - if ( ! in_array( $requested_w, $all_w, true ) ) { |
|
230 | + if ( ! in_array($requested_w, $all_w, true)) { |
|
231 | 231 | return $clauses; |
232 | 232 | } |
233 | 233 | |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | |
236 | 236 | // Change WP main query clauses. |
237 | 237 | $clauses['join'] .= "INNER JOIN {$wpdb->prefix}wl_relation_instances ON {$wpdb->posts}.ID = {$wpdb->prefix}wl_relation_instances.object_id"; |
238 | - $clauses['where'] .= $wpdb->prepare( "AND {$wpdb->prefix}wl_relation_instances.predicate = %s", $requested_w ); |
|
238 | + $clauses['where'] .= $wpdb->prepare("AND {$wpdb->prefix}wl_relation_instances.predicate = %s", $requested_w); |
|
239 | 239 | $clauses['distinct'] .= 'DISTINCT'; |
240 | 240 | |
241 | 241 | return $clauses; |
@@ -249,14 +249,14 @@ discard block |
||
249 | 249 | * |
250 | 250 | * @since 3.15.0 |
251 | 251 | */ |
252 | - public function pre_get_posts( $query ) { |
|
252 | + public function pre_get_posts($query) { |
|
253 | 253 | |
254 | - if ( ! ( $this->is_entity_list_screen() && $query->is_main_query() ) ) { |
|
254 | + if ( ! ($this->is_entity_list_screen() && $query->is_main_query())) { |
|
255 | 255 | return; |
256 | 256 | } |
257 | 257 | |
258 | 258 | // Add to the post type all the types considered to be valid post types. |
259 | - $query->set( 'post_type', Wordlift_Entity_Service::valid_entity_post_types() ); |
|
259 | + $query->set('post_type', Wordlift_Entity_Service::valid_entity_post_types()); |
|
260 | 260 | |
261 | 261 | // Do not show however entities of type `Article`. |
262 | 262 | $query->set( |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | public function load_edit() { |
290 | 290 | |
291 | 291 | // Return safely if get_current_screen() is not defined (yet). |
292 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
292 | + if (false === function_exists('get_current_screen')) { |
|
293 | 293 | return; |
294 | 294 | } |
295 | 295 | |
@@ -297,11 +297,11 @@ discard block |
||
297 | 297 | $screen = get_current_screen(); |
298 | 298 | |
299 | 299 | // If there is any valid screen nothing to do. |
300 | - if ( null === $screen ) { |
|
300 | + if (null === $screen) { |
|
301 | 301 | return; |
302 | 302 | } |
303 | 303 | |
304 | - if ( ! ( Wordlift_Entity_Service::TYPE_NAME === $screen->post_type && is_main_query() ) ) { |
|
304 | + if ( ! (Wordlift_Entity_Service::TYPE_NAME === $screen->post_type && is_main_query())) { |
|
305 | 305 | return; |
306 | 306 | } |
307 | 307 | |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | */ |
315 | 315 | add_action( |
316 | 316 | 'wp', |
317 | - function () { |
|
317 | + function() { |
|
318 | 318 | global $post_type; |
319 | 319 | $post_type = Wordlift_Entity_Service::TYPE_NAME; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
320 | 320 | }, |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | */ |
14 | 14 | |
15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
16 | - exit; |
|
16 | + exit; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | /** |
@@ -30,217 +30,217 @@ discard block |
||
30 | 30 | */ |
31 | 31 | class Wordlift_Admin_Entity_Type_Settings { |
32 | 32 | |
33 | - /** |
|
34 | - * A {@link Wordlift_Log_Service} instance. |
|
35 | - * |
|
36 | - * @since 3.14.0 |
|
37 | - * @access private |
|
38 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
39 | - */ |
|
40 | - private $log; |
|
41 | - |
|
42 | - /** |
|
43 | - * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance. |
|
44 | - * |
|
45 | - * @since 3.14.0 |
|
46 | - */ |
|
47 | - public function __construct() { |
|
48 | - |
|
49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
50 | - |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Handle menu registration. |
|
55 | - * |
|
56 | - * The registration is required, although we do not want to actually to add |
|
57 | - * an item to the menu, in order to "whitelist" the access to the settings page in |
|
58 | - * the admin. |
|
59 | - * |
|
60 | - * @since 3.11.0 |
|
61 | - */ |
|
62 | - public function admin_menu() { |
|
63 | - |
|
64 | - /* |
|
33 | + /** |
|
34 | + * A {@link Wordlift_Log_Service} instance. |
|
35 | + * |
|
36 | + * @since 3.14.0 |
|
37 | + * @access private |
|
38 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
39 | + */ |
|
40 | + private $log; |
|
41 | + |
|
42 | + /** |
|
43 | + * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance. |
|
44 | + * |
|
45 | + * @since 3.14.0 |
|
46 | + */ |
|
47 | + public function __construct() { |
|
48 | + |
|
49 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
50 | + |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Handle menu registration. |
|
55 | + * |
|
56 | + * The registration is required, although we do not want to actually to add |
|
57 | + * an item to the menu, in order to "whitelist" the access to the settings page in |
|
58 | + * the admin. |
|
59 | + * |
|
60 | + * @since 3.11.0 |
|
61 | + */ |
|
62 | + public function admin_menu() { |
|
63 | + |
|
64 | + /* |
|
65 | 65 | * Before anything else check if an settings form was submitted. |
66 | 66 | * This has to be done before any output happens in order to be able to |
67 | 67 | * display proper "die" error messages and redirect. |
68 | 68 | */ |
69 | - if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
70 | - |
|
71 | - // Validate inputs. Do not return on invalid parameters or capabilities. |
|
72 | - $this->validate_proper_term(); |
|
73 | - |
|
74 | - // If proper form submission, handle it and redirect back to the settings page. |
|
75 | - if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
76 | - $this->handle_form_submission(); |
|
77 | - } |
|
78 | - /** |
|
79 | - * Filter: wl_feature__enable__notices. |
|
80 | - * |
|
81 | - * @param bool whether the notices needs to be enabled or not. |
|
82 | - * |
|
83 | - * @return bool |
|
84 | - * @since 3.27.6 |
|
85 | - */ |
|
86 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
87 | - // Register admin notices handler. |
|
88 | - add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /* |
|
69 | + if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
70 | + |
|
71 | + // Validate inputs. Do not return on invalid parameters or capabilities. |
|
72 | + $this->validate_proper_term(); |
|
73 | + |
|
74 | + // If proper form submission, handle it and redirect back to the settings page. |
|
75 | + if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
76 | + $this->handle_form_submission(); |
|
77 | + } |
|
78 | + /** |
|
79 | + * Filter: wl_feature__enable__notices. |
|
80 | + * |
|
81 | + * @param bool whether the notices needs to be enabled or not. |
|
82 | + * |
|
83 | + * @return bool |
|
84 | + * @since 3.27.6 |
|
85 | + */ |
|
86 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
87 | + // Register admin notices handler. |
|
88 | + add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /* |
|
93 | 93 | * Use a null parent slug to prevent the menu from actually appearing |
94 | 94 | * in the admin menu. |
95 | 95 | */ |
96 | - // @todo: use the new {@link Wordlift_Admin_Page}. |
|
97 | - add_submenu_page( |
|
98 | - null, |
|
99 | - __( 'Edit Entity term', 'wordlift' ), |
|
100 | - __( 'Edit Entity term', 'wordlift' ), |
|
101 | - 'manage_options', |
|
102 | - 'wl_entity_type_settings', |
|
103 | - array( $this, 'render' ) |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Output admin notices if needed, based on the message url parameter. |
|
109 | - * A value of 1 indicates that a successful save was done. |
|
110 | - * |
|
111 | - * @since 3.11.0 |
|
112 | - */ |
|
113 | - public function admin_notice() { |
|
114 | - if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
115 | - ?> |
|
96 | + // @todo: use the new {@link Wordlift_Admin_Page}. |
|
97 | + add_submenu_page( |
|
98 | + null, |
|
99 | + __( 'Edit Entity term', 'wordlift' ), |
|
100 | + __( 'Edit Entity term', 'wordlift' ), |
|
101 | + 'manage_options', |
|
102 | + 'wl_entity_type_settings', |
|
103 | + array( $this, 'render' ) |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Output admin notices if needed, based on the message url parameter. |
|
109 | + * A value of 1 indicates that a successful save was done. |
|
110 | + * |
|
111 | + * @since 3.11.0 |
|
112 | + */ |
|
113 | + public function admin_notice() { |
|
114 | + if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
115 | + ?> |
|
116 | 116 | <div class="notice notice-success is-dismissible"> |
117 | 117 | <p><?php esc_html_e( 'Settings saved', 'wordlift' ); ?></p> |
118 | 118 | </div> |
119 | 119 | <?php |
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Validate the existence of the entity type indicated by the tag_ID url |
|
125 | - * parameter before doing any processing. Done before any output to mimic |
|
126 | - * the way WordPress handles same situation with "normal" term editing screens. |
|
127 | - * |
|
128 | - * @since 3.11.0 |
|
129 | - */ |
|
130 | - public function validate_proper_term() { |
|
131 | - |
|
132 | - // Validate capabilities. |
|
133 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
134 | - wp_die( |
|
135 | - '<h1>' . esc_html__( 'Cheatin’ uh?', 'default' ) . '</h1>' . |
|
136 | - '<p>' . esc_html__( 'Sorry, you are not allowed to edit this item.', 'default' ) . '</p>', |
|
137 | - 403 |
|
138 | - ); |
|
139 | - } |
|
140 | - |
|
141 | - // Get the term id and the actual term. |
|
142 | - $term_id = isset( $_REQUEST['tag_ID'] ) ? (int) $_REQUEST['tag_ID'] : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
143 | - |
|
144 | - if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
145 | - wp_die( esc_html__( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
146 | - } |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Handle the form submission of the settings form. On successful |
|
152 | - * handling redirect tp the setting edit page. |
|
153 | - * |
|
154 | - * @since 3.11.0 |
|
155 | - */ |
|
156 | - public function handle_form_submission() { |
|
157 | - |
|
158 | - $term_id = isset( $_POST['tag_ID'] ) ? (int) $_POST['tag_ID'] : 0; |
|
159 | - |
|
160 | - // Check the nonce. |
|
161 | - check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
162 | - |
|
163 | - $term = get_term( $term_id, 'wl_entity_type' ); |
|
164 | - |
|
165 | - $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['title'] ) ) : ''; |
|
166 | - $description = isset( $_POST['description'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['description'] ) ) : ''; |
|
167 | - $this->set_setting( |
|
168 | - $term_id, |
|
169 | - trim( wp_unslash( $title ) ), |
|
170 | - wp_unslash( $description ) |
|
171 | - ); |
|
172 | - |
|
173 | - // Redirect back to the term settings page and indicate a save was done. |
|
174 | - $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
175 | - |
|
176 | - wp_safe_redirect( $url ); |
|
177 | - exit(); |
|
178 | - |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Render the settings page for the term. |
|
183 | - * |
|
184 | - * Access and parameter validity is assumed to be done earlier. |
|
185 | - * |
|
186 | - * @since 3.11.0 |
|
187 | - */ |
|
188 | - public function render() { |
|
189 | - |
|
190 | - // Set variables used by the partial |
|
191 | - $term_id = isset( $_REQUEST['tag_ID'] ) ? absint( $_REQUEST['tag_ID'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Validate the existence of the entity type indicated by the tag_ID url |
|
125 | + * parameter before doing any processing. Done before any output to mimic |
|
126 | + * the way WordPress handles same situation with "normal" term editing screens. |
|
127 | + * |
|
128 | + * @since 3.11.0 |
|
129 | + */ |
|
130 | + public function validate_proper_term() { |
|
131 | + |
|
132 | + // Validate capabilities. |
|
133 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
134 | + wp_die( |
|
135 | + '<h1>' . esc_html__( 'Cheatin’ uh?', 'default' ) . '</h1>' . |
|
136 | + '<p>' . esc_html__( 'Sorry, you are not allowed to edit this item.', 'default' ) . '</p>', |
|
137 | + 403 |
|
138 | + ); |
|
139 | + } |
|
140 | + |
|
141 | + // Get the term id and the actual term. |
|
142 | + $term_id = isset( $_REQUEST['tag_ID'] ) ? (int) $_REQUEST['tag_ID'] : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
143 | + |
|
144 | + if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
145 | + wp_die( esc_html__( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
146 | + } |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Handle the form submission of the settings form. On successful |
|
152 | + * handling redirect tp the setting edit page. |
|
153 | + * |
|
154 | + * @since 3.11.0 |
|
155 | + */ |
|
156 | + public function handle_form_submission() { |
|
157 | + |
|
158 | + $term_id = isset( $_POST['tag_ID'] ) ? (int) $_POST['tag_ID'] : 0; |
|
159 | + |
|
160 | + // Check the nonce. |
|
161 | + check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
162 | + |
|
163 | + $term = get_term( $term_id, 'wl_entity_type' ); |
|
164 | + |
|
165 | + $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['title'] ) ) : ''; |
|
166 | + $description = isset( $_POST['description'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['description'] ) ) : ''; |
|
167 | + $this->set_setting( |
|
168 | + $term_id, |
|
169 | + trim( wp_unslash( $title ) ), |
|
170 | + wp_unslash( $description ) |
|
171 | + ); |
|
172 | + |
|
173 | + // Redirect back to the term settings page and indicate a save was done. |
|
174 | + $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
175 | + |
|
176 | + wp_safe_redirect( $url ); |
|
177 | + exit(); |
|
178 | + |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Render the settings page for the term. |
|
183 | + * |
|
184 | + * Access and parameter validity is assumed to be done earlier. |
|
185 | + * |
|
186 | + * @since 3.11.0 |
|
187 | + */ |
|
188 | + public function render() { |
|
189 | + |
|
190 | + // Set variables used by the partial |
|
191 | + $term_id = isset( $_REQUEST['tag_ID'] ) ? absint( $_REQUEST['tag_ID'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
192 | 192 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
193 | - $settings = $this->get_setting( $term_id ); |
|
194 | - |
|
195 | - include plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
196 | - |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Store the entity type term settings in the DB |
|
201 | - * |
|
202 | - * @param integer $term_id The ID of the entity type term |
|
203 | - * @param string $title The override for the terms title. |
|
204 | - * @param string $description The override for the terms description. |
|
205 | - * |
|
206 | - * @since 3.11.0 |
|
207 | - */ |
|
208 | - public function set_setting( $term_id, $title, $description ) { |
|
209 | - |
|
210 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
211 | - $settings[ $term_id ] = array( |
|
212 | - 'title' => $title, |
|
213 | - 'description' => $description, |
|
214 | - ); |
|
215 | - update_option( 'wl_entity_type_settings', $settings ); |
|
216 | - |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Retrieve the entity type term settings from the DB |
|
221 | - * |
|
222 | - * @param integer $term_id The ID of the entity type term |
|
223 | - * |
|
224 | - * @return null|array { |
|
225 | - * null is returned when there are no settings otherwise |
|
226 | - * an array is returned with following fields |
|
227 | - * |
|
228 | - * @type string title The overriding title for the term |
|
229 | - * @type string description The overriding description for the term |
|
230 | - * } |
|
231 | - * @since 3.11.0 |
|
232 | - */ |
|
233 | - public function get_setting( $term_id ) { |
|
234 | - |
|
235 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
236 | - |
|
237 | - if ( isset( $settings[ $term_id ] ) ) { |
|
238 | - return $settings[ $term_id ]; |
|
239 | - } |
|
240 | - |
|
241 | - $this->log->warn( "No settings found for term id $term_id." ); |
|
242 | - |
|
243 | - return null; |
|
244 | - } |
|
193 | + $settings = $this->get_setting( $term_id ); |
|
194 | + |
|
195 | + include plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
196 | + |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Store the entity type term settings in the DB |
|
201 | + * |
|
202 | + * @param integer $term_id The ID of the entity type term |
|
203 | + * @param string $title The override for the terms title. |
|
204 | + * @param string $description The override for the terms description. |
|
205 | + * |
|
206 | + * @since 3.11.0 |
|
207 | + */ |
|
208 | + public function set_setting( $term_id, $title, $description ) { |
|
209 | + |
|
210 | + $settings = get_option( 'wl_entity_type_settings', array() ); |
|
211 | + $settings[ $term_id ] = array( |
|
212 | + 'title' => $title, |
|
213 | + 'description' => $description, |
|
214 | + ); |
|
215 | + update_option( 'wl_entity_type_settings', $settings ); |
|
216 | + |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Retrieve the entity type term settings from the DB |
|
221 | + * |
|
222 | + * @param integer $term_id The ID of the entity type term |
|
223 | + * |
|
224 | + * @return null|array { |
|
225 | + * null is returned when there are no settings otherwise |
|
226 | + * an array is returned with following fields |
|
227 | + * |
|
228 | + * @type string title The overriding title for the term |
|
229 | + * @type string description The overriding description for the term |
|
230 | + * } |
|
231 | + * @since 3.11.0 |
|
232 | + */ |
|
233 | + public function get_setting( $term_id ) { |
|
234 | + |
|
235 | + $settings = get_option( 'wl_entity_type_settings', array() ); |
|
236 | + |
|
237 | + if ( isset( $settings[ $term_id ] ) ) { |
|
238 | + return $settings[ $term_id ]; |
|
239 | + } |
|
240 | + |
|
241 | + $this->log->warn( "No settings found for term id $term_id." ); |
|
242 | + |
|
243 | + return null; |
|
244 | + } |
|
245 | 245 | |
246 | 246 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @since 3.11.0 |
13 | 13 | */ |
14 | 14 | |
15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
15 | +if ( ! defined('ABSPATH')) { |
|
16 | 16 | exit; |
17 | 17 | } |
18 | 18 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function __construct() { |
48 | 48 | |
49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
49 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Admin_Entity_Type_Settings'); |
|
50 | 50 | |
51 | 51 | } |
52 | 52 | |
@@ -66,13 +66,13 @@ discard block |
||
66 | 66 | * This has to be done before any output happens in order to be able to |
67 | 67 | * display proper "die" error messages and redirect. |
68 | 68 | */ |
69 | - if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
69 | + if (isset($_GET['page']) && ('wl_entity_type_settings' === $_GET['page'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
70 | 70 | |
71 | 71 | // Validate inputs. Do not return on invalid parameters or capabilities. |
72 | 72 | $this->validate_proper_term(); |
73 | 73 | |
74 | 74 | // If proper form submission, handle it and redirect back to the settings page. |
75 | - if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
75 | + if (isset($_POST['action']) && ('wl_edit_entity_type_term' === $_POST['action'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
76 | 76 | $this->handle_form_submission(); |
77 | 77 | } |
78 | 78 | /** |
@@ -83,9 +83,9 @@ discard block |
||
83 | 83 | * @return bool |
84 | 84 | * @since 3.27.6 |
85 | 85 | */ |
86 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
86 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
87 | 87 | // Register admin notices handler. |
88 | - add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
88 | + add_action('admin_notices', array($this, 'admin_notice')); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
@@ -96,11 +96,11 @@ discard block |
||
96 | 96 | // @todo: use the new {@link Wordlift_Admin_Page}. |
97 | 97 | add_submenu_page( |
98 | 98 | null, |
99 | - __( 'Edit Entity term', 'wordlift' ), |
|
100 | - __( 'Edit Entity term', 'wordlift' ), |
|
99 | + __('Edit Entity term', 'wordlift'), |
|
100 | + __('Edit Entity term', 'wordlift'), |
|
101 | 101 | 'manage_options', |
102 | 102 | 'wl_entity_type_settings', |
103 | - array( $this, 'render' ) |
|
103 | + array($this, 'render') |
|
104 | 104 | ); |
105 | 105 | } |
106 | 106 | |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | * @since 3.11.0 |
112 | 112 | */ |
113 | 113 | public function admin_notice() { |
114 | - if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
114 | + if (isset($_GET['message']) && ('1' === $_GET['message'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
115 | 115 | ?> |
116 | 116 | <div class="notice notice-success is-dismissible"> |
117 | - <p><?php esc_html_e( 'Settings saved', 'wordlift' ); ?></p> |
|
117 | + <p><?php esc_html_e('Settings saved', 'wordlift'); ?></p> |
|
118 | 118 | </div> |
119 | 119 | <?php |
120 | 120 | } |
@@ -130,19 +130,19 @@ discard block |
||
130 | 130 | public function validate_proper_term() { |
131 | 131 | |
132 | 132 | // Validate capabilities. |
133 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
133 | + if ( ! current_user_can('manage_options')) { |
|
134 | 134 | wp_die( |
135 | - '<h1>' . esc_html__( 'Cheatin’ uh?', 'default' ) . '</h1>' . |
|
136 | - '<p>' . esc_html__( 'Sorry, you are not allowed to edit this item.', 'default' ) . '</p>', |
|
135 | + '<h1>'.esc_html__('Cheatin’ uh?', 'default').'</h1>'. |
|
136 | + '<p>'.esc_html__('Sorry, you are not allowed to edit this item.', 'default').'</p>', |
|
137 | 137 | 403 |
138 | 138 | ); |
139 | 139 | } |
140 | 140 | |
141 | 141 | // Get the term id and the actual term. |
142 | - $term_id = isset( $_REQUEST['tag_ID'] ) ? (int) $_REQUEST['tag_ID'] : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
142 | + $term_id = isset($_REQUEST['tag_ID']) ? (int) $_REQUEST['tag_ID'] : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
143 | 143 | |
144 | - if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
145 | - wp_die( esc_html__( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
144 | + if ( ! term_exists($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)) { |
|
145 | + wp_die(esc_html__('You attempted to edit an entity type term that doesn’t exist.', 'wordlift')); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | } |
@@ -155,25 +155,25 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function handle_form_submission() { |
157 | 157 | |
158 | - $term_id = isset( $_POST['tag_ID'] ) ? (int) $_POST['tag_ID'] : 0; |
|
158 | + $term_id = isset($_POST['tag_ID']) ? (int) $_POST['tag_ID'] : 0; |
|
159 | 159 | |
160 | 160 | // Check the nonce. |
161 | - check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
161 | + check_admin_referer('update-entity_type_term_'.$term_id); |
|
162 | 162 | |
163 | - $term = get_term( $term_id, 'wl_entity_type' ); |
|
163 | + $term = get_term($term_id, 'wl_entity_type'); |
|
164 | 164 | |
165 | - $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['title'] ) ) : ''; |
|
166 | - $description = isset( $_POST['description'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['description'] ) ) : ''; |
|
165 | + $title = isset($_POST['title']) ? sanitize_text_field(wp_unslash((string) $_POST['title'])) : ''; |
|
166 | + $description = isset($_POST['description']) ? sanitize_text_field(wp_unslash((string) $_POST['description'])) : ''; |
|
167 | 167 | $this->set_setting( |
168 | 168 | $term_id, |
169 | - trim( wp_unslash( $title ) ), |
|
170 | - wp_unslash( $description ) |
|
169 | + trim(wp_unslash($title)), |
|
170 | + wp_unslash($description) |
|
171 | 171 | ); |
172 | 172 | |
173 | 173 | // Redirect back to the term settings page and indicate a save was done. |
174 | - $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
174 | + $url = admin_url("admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1"); |
|
175 | 175 | |
176 | - wp_safe_redirect( $url ); |
|
176 | + wp_safe_redirect($url); |
|
177 | 177 | exit(); |
178 | 178 | |
179 | 179 | } |
@@ -188,11 +188,11 @@ discard block |
||
188 | 188 | public function render() { |
189 | 189 | |
190 | 190 | // Set variables used by the partial |
191 | - $term_id = isset( $_REQUEST['tag_ID'] ) ? absint( $_REQUEST['tag_ID'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
191 | + $term_id = isset($_REQUEST['tag_ID']) ? absint($_REQUEST['tag_ID']) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
192 | 192 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
193 | - $settings = $this->get_setting( $term_id ); |
|
193 | + $settings = $this->get_setting($term_id); |
|
194 | 194 | |
195 | - include plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
195 | + include plugin_dir_path(__DIR__).'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
196 | 196 | |
197 | 197 | } |
198 | 198 | |
@@ -205,14 +205,14 @@ discard block |
||
205 | 205 | * |
206 | 206 | * @since 3.11.0 |
207 | 207 | */ |
208 | - public function set_setting( $term_id, $title, $description ) { |
|
208 | + public function set_setting($term_id, $title, $description) { |
|
209 | 209 | |
210 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
211 | - $settings[ $term_id ] = array( |
|
210 | + $settings = get_option('wl_entity_type_settings', array()); |
|
211 | + $settings[$term_id] = array( |
|
212 | 212 | 'title' => $title, |
213 | 213 | 'description' => $description, |
214 | 214 | ); |
215 | - update_option( 'wl_entity_type_settings', $settings ); |
|
215 | + update_option('wl_entity_type_settings', $settings); |
|
216 | 216 | |
217 | 217 | } |
218 | 218 | |
@@ -230,15 +230,15 @@ discard block |
||
230 | 230 | * } |
231 | 231 | * @since 3.11.0 |
232 | 232 | */ |
233 | - public function get_setting( $term_id ) { |
|
233 | + public function get_setting($term_id) { |
|
234 | 234 | |
235 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
235 | + $settings = get_option('wl_entity_type_settings', array()); |
|
236 | 236 | |
237 | - if ( isset( $settings[ $term_id ] ) ) { |
|
238 | - return $settings[ $term_id ]; |
|
237 | + if (isset($settings[$term_id])) { |
|
238 | + return $settings[$term_id]; |
|
239 | 239 | } |
240 | 240 | |
241 | - $this->log->warn( "No settings found for term id $term_id." ); |
|
241 | + $this->log->warn("No settings found for term id $term_id."); |
|
242 | 242 | |
243 | 243 | return null; |
244 | 244 | } |
@@ -16,82 +16,82 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Admin_Term_Adapter { |
18 | 18 | |
19 | - /** |
|
20 | - * The meta key holding the entity id. |
|
21 | - * |
|
22 | - * @since 3.20.0 |
|
23 | - */ |
|
24 | - const META_KEY = '_wl_entity_id'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Create a Wordlift_Admin_Term_Adapter instance. |
|
28 | - * |
|
29 | - * @since 3.20.0 |
|
30 | - */ |
|
31 | - public function __construct() { |
|
32 | - |
|
33 | - add_action( 'registered_taxonomy', array( $this, 'add_action' ) ); |
|
34 | - add_action( 'edit_term', array( $this, 'edit_term' ), 10 ); |
|
35 | - $this->add_settings(); |
|
36 | - |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Hook in to WordLift admin settings and add the term page specific |
|
41 | - * settings. |
|
42 | - * |
|
43 | - * @since 3.26.1 |
|
44 | - */ |
|
45 | - public function add_settings() { |
|
46 | - add_filter( |
|
47 | - 'wl_admin_settings', |
|
48 | - function ( $params ) { |
|
49 | - $params['show_local_entities'] = true; |
|
50 | - |
|
51 | - return $params; |
|
52 | - } |
|
53 | - ); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Add the form fields to the entity edit screen. |
|
58 | - * |
|
59 | - * @param object $tag Current taxonomy term object. |
|
60 | - * |
|
61 | - * @since 3.20.0 |
|
62 | - */ |
|
63 | - public function edit_form_fields( $tag ) { |
|
64 | - |
|
65 | - // If disabled via filter, return; |
|
66 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
67 | - if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
68 | - return; |
|
69 | - } |
|
70 | - |
|
71 | - global $wp_version; |
|
72 | - |
|
73 | - // Enqueue the JavaScript app. |
|
74 | - if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
75 | - $term_asset = include plugin_dir_path( __DIR__ ) . 'js/dist/term.asset.php'; |
|
76 | - wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
77 | - } else { |
|
78 | - wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
79 | - } |
|
80 | - |
|
81 | - wp_enqueue_style( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
82 | - |
|
83 | - $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
84 | - |
|
85 | - /** |
|
86 | - * @since 3.31.3 |
|
87 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1446 |
|
88 | - * This field should be hidden by default |
|
89 | - */ |
|
90 | - if ( ! $values ) { |
|
91 | - return; |
|
92 | - } |
|
93 | - |
|
94 | - ?> |
|
19 | + /** |
|
20 | + * The meta key holding the entity id. |
|
21 | + * |
|
22 | + * @since 3.20.0 |
|
23 | + */ |
|
24 | + const META_KEY = '_wl_entity_id'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Create a Wordlift_Admin_Term_Adapter instance. |
|
28 | + * |
|
29 | + * @since 3.20.0 |
|
30 | + */ |
|
31 | + public function __construct() { |
|
32 | + |
|
33 | + add_action( 'registered_taxonomy', array( $this, 'add_action' ) ); |
|
34 | + add_action( 'edit_term', array( $this, 'edit_term' ), 10 ); |
|
35 | + $this->add_settings(); |
|
36 | + |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Hook in to WordLift admin settings and add the term page specific |
|
41 | + * settings. |
|
42 | + * |
|
43 | + * @since 3.26.1 |
|
44 | + */ |
|
45 | + public function add_settings() { |
|
46 | + add_filter( |
|
47 | + 'wl_admin_settings', |
|
48 | + function ( $params ) { |
|
49 | + $params['show_local_entities'] = true; |
|
50 | + |
|
51 | + return $params; |
|
52 | + } |
|
53 | + ); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Add the form fields to the entity edit screen. |
|
58 | + * |
|
59 | + * @param object $tag Current taxonomy term object. |
|
60 | + * |
|
61 | + * @since 3.20.0 |
|
62 | + */ |
|
63 | + public function edit_form_fields( $tag ) { |
|
64 | + |
|
65 | + // If disabled via filter, return; |
|
66 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
67 | + if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
68 | + return; |
|
69 | + } |
|
70 | + |
|
71 | + global $wp_version; |
|
72 | + |
|
73 | + // Enqueue the JavaScript app. |
|
74 | + if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
75 | + $term_asset = include plugin_dir_path( __DIR__ ) . 'js/dist/term.asset.php'; |
|
76 | + wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
77 | + } else { |
|
78 | + wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
79 | + } |
|
80 | + |
|
81 | + wp_enqueue_style( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
82 | + |
|
83 | + $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
84 | + |
|
85 | + /** |
|
86 | + * @since 3.31.3 |
|
87 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1446 |
|
88 | + * This field should be hidden by default |
|
89 | + */ |
|
90 | + if ( ! $values ) { |
|
91 | + return; |
|
92 | + } |
|
93 | + |
|
94 | + ?> |
|
95 | 95 | <tr class="form-field term-name-wrap"> |
96 | 96 | <th scope="row"><label |
97 | 97 | for="wl-entity-id"><?php echo esc_html_x( 'Entity', 'term entity', 'wordlift' ); ?></label></th> |
@@ -105,81 +105,81 @@ discard block |
||
105 | 105 | </tr> |
106 | 106 | <?php wp_nonce_field( 'wordlift_term_entity_edit', 'wordlift_term_entity_edit_nonce' ); ?> |
107 | 107 | <?php |
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Bind the new fields to the edit term screen. |
|
112 | - * |
|
113 | - * @param string $taxonomy The taxonomy name. |
|
114 | - * |
|
115 | - * @since 3.20.0 |
|
116 | - */ |
|
117 | - public function add_action( $taxonomy ) { |
|
118 | - /** |
|
119 | - * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
|
120 | - */ |
|
121 | - |
|
122 | - add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10 ); |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Hook to the edit term to handle our own custom fields. |
|
128 | - * |
|
129 | - * We turn off nonce verification here because we're responding to a WordPress hook. |
|
130 | - * |
|
131 | - * @param int $term_id The term id. |
|
132 | - * |
|
133 | - * @since 3.20.0 |
|
134 | - */ |
|
135 | - public function edit_term( $term_id ) { |
|
136 | - |
|
137 | - // Bail if the action isn't related to the term currently being edited. |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Bind the new fields to the edit term screen. |
|
112 | + * |
|
113 | + * @param string $taxonomy The taxonomy name. |
|
114 | + * |
|
115 | + * @since 3.20.0 |
|
116 | + */ |
|
117 | + public function add_action( $taxonomy ) { |
|
118 | + /** |
|
119 | + * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
|
120 | + */ |
|
121 | + |
|
122 | + add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10 ); |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Hook to the edit term to handle our own custom fields. |
|
128 | + * |
|
129 | + * We turn off nonce verification here because we're responding to a WordPress hook. |
|
130 | + * |
|
131 | + * @param int $term_id The term id. |
|
132 | + * |
|
133 | + * @since 3.20.0 |
|
134 | + */ |
|
135 | + public function edit_term( $term_id ) { |
|
136 | + |
|
137 | + // Bail if the action isn't related to the term currently being edited. |
|
138 | 138 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
139 | - if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
140 | - return; |
|
141 | - } |
|
142 | - |
|
143 | - // Delete. |
|
144 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
145 | - if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
146 | - delete_term_meta( $term_id, self::META_KEY ); |
|
147 | - |
|
148 | - return; |
|
149 | - } |
|
150 | - |
|
151 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
152 | - $entity_ids = array_map( 'esc_url_raw', wp_unslash( $_POST['wl_entity_id'] ) ); |
|
153 | - // Update. |
|
154 | - // |
|
155 | - // Only use mb_* functions when mbstring is available. |
|
156 | - // |
|
157 | - // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
158 | - if ( extension_loaded( 'mbstring' ) ) { |
|
159 | - mb_regex_encoding( 'UTF-8' ); |
|
160 | - |
|
161 | - $merged = array_reduce( |
|
162 | - $entity_ids, |
|
163 | - function ( $carry, $item ) { |
|
164 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
165 | - }, |
|
166 | - array() |
|
167 | - ); |
|
168 | - } else { |
|
169 | - $merged = array_reduce( |
|
170 | - $entity_ids, |
|
171 | - function ( $carry, $item ) { |
|
172 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
173 | - }, |
|
174 | - array() |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - delete_term_meta( $term_id, self::META_KEY ); |
|
179 | - foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
180 | - add_term_meta( $term_id, self::META_KEY, $single ); |
|
181 | - } |
|
182 | - |
|
183 | - } |
|
139 | + if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
140 | + return; |
|
141 | + } |
|
142 | + |
|
143 | + // Delete. |
|
144 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
145 | + if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
146 | + delete_term_meta( $term_id, self::META_KEY ); |
|
147 | + |
|
148 | + return; |
|
149 | + } |
|
150 | + |
|
151 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
152 | + $entity_ids = array_map( 'esc_url_raw', wp_unslash( $_POST['wl_entity_id'] ) ); |
|
153 | + // Update. |
|
154 | + // |
|
155 | + // Only use mb_* functions when mbstring is available. |
|
156 | + // |
|
157 | + // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
158 | + if ( extension_loaded( 'mbstring' ) ) { |
|
159 | + mb_regex_encoding( 'UTF-8' ); |
|
160 | + |
|
161 | + $merged = array_reduce( |
|
162 | + $entity_ids, |
|
163 | + function ( $carry, $item ) { |
|
164 | + return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
165 | + }, |
|
166 | + array() |
|
167 | + ); |
|
168 | + } else { |
|
169 | + $merged = array_reduce( |
|
170 | + $entity_ids, |
|
171 | + function ( $carry, $item ) { |
|
172 | + return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
173 | + }, |
|
174 | + array() |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + delete_term_meta( $term_id, self::META_KEY ); |
|
179 | + foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
180 | + add_term_meta( $term_id, self::META_KEY, $single ); |
|
181 | + } |
|
182 | + |
|
183 | + } |
|
184 | 184 | |
185 | 185 | } |
@@ -30,8 +30,8 @@ discard block |
||
30 | 30 | */ |
31 | 31 | public function __construct() { |
32 | 32 | |
33 | - add_action( 'registered_taxonomy', array( $this, 'add_action' ) ); |
|
34 | - add_action( 'edit_term', array( $this, 'edit_term' ), 10 ); |
|
33 | + add_action('registered_taxonomy', array($this, 'add_action')); |
|
34 | + add_action('edit_term', array($this, 'edit_term'), 10); |
|
35 | 35 | $this->add_settings(); |
36 | 36 | |
37 | 37 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | public function add_settings() { |
46 | 46 | add_filter( |
47 | 47 | 'wl_admin_settings', |
48 | - function ( $params ) { |
|
48 | + function($params) { |
|
49 | 49 | $params['show_local_entities'] = true; |
50 | 50 | |
51 | 51 | return $params; |
@@ -60,50 +60,50 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @since 3.20.0 |
62 | 62 | */ |
63 | - public function edit_form_fields( $tag ) { |
|
63 | + public function edit_form_fields($tag) { |
|
64 | 64 | |
65 | 65 | // If disabled via filter, return; |
66 | 66 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
67 | - if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
67 | + if ( ! apply_filters('wl_feature__enable__term-entity', true)) { |
|
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
71 | 71 | global $wp_version; |
72 | 72 | |
73 | 73 | // Enqueue the JavaScript app. |
74 | - if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
75 | - $term_asset = include plugin_dir_path( __DIR__ ) . 'js/dist/term.asset.php'; |
|
76 | - wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
74 | + if (version_compare($wp_version, '5.0', '>=')) { |
|
75 | + $term_asset = include plugin_dir_path(__DIR__).'js/dist/term.asset.php'; |
|
76 | + wp_enqueue_script('wl-term', plugin_dir_url(__DIR__).'js/dist/term.js', array_merge(array('wp-util'), $term_asset['dependencies']), Wordlift::get_instance()->get_version(), true); |
|
77 | 77 | } else { |
78 | - wp_enqueue_script( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
78 | + wp_enqueue_script('wl-term', plugin_dir_url(__DIR__).'js/dist/term.full.js', array('wp-util'), Wordlift::get_instance()->get_version(), true); |
|
79 | 79 | } |
80 | 80 | |
81 | - wp_enqueue_style( 'wl-term', plugin_dir_url( __DIR__ ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
81 | + wp_enqueue_style('wl-term', plugin_dir_url(__DIR__).'js/dist/term.css', array(), Wordlift::get_instance()->get_version()); |
|
82 | 82 | |
83 | - $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
83 | + $values = get_term_meta($tag->term_id, self::META_KEY); |
|
84 | 84 | |
85 | 85 | /** |
86 | 86 | * @since 3.31.3 |
87 | 87 | * @see https://github.com/insideout10/wordlift-plugin/issues/1446 |
88 | 88 | * This field should be hidden by default |
89 | 89 | */ |
90 | - if ( ! $values ) { |
|
90 | + if ( ! $values) { |
|
91 | 91 | return; |
92 | 92 | } |
93 | 93 | |
94 | 94 | ?> |
95 | 95 | <tr class="form-field term-name-wrap"> |
96 | 96 | <th scope="row"><label |
97 | - for="wl-entity-id"><?php echo esc_html_x( 'Entity', 'term entity', 'wordlift' ); ?></label></th> |
|
97 | + for="wl-entity-id"><?php echo esc_html_x('Entity', 'term entity', 'wordlift'); ?></label></th> |
|
98 | 98 | <td> |
99 | - <?php foreach ( $values as $value ) { ?> |
|
100 | - <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr( $value ); ?>"/> |
|
99 | + <?php foreach ($values as $value) { ?> |
|
100 | + <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr($value); ?>"/> |
|
101 | 101 | <?php } ?> |
102 | 102 | <div id="wl-term-entity-id"></div> |
103 | - <p class="description"><?php esc_html_e( 'The entity bound to the term.', 'wordlift' ); ?></p> |
|
103 | + <p class="description"><?php esc_html_e('The entity bound to the term.', 'wordlift'); ?></p> |
|
104 | 104 | </td> |
105 | 105 | </tr> |
106 | - <?php wp_nonce_field( 'wordlift_term_entity_edit', 'wordlift_term_entity_edit_nonce' ); ?> |
|
106 | + <?php wp_nonce_field('wordlift_term_entity_edit', 'wordlift_term_entity_edit_nonce'); ?> |
|
107 | 107 | <?php |
108 | 108 | } |
109 | 109 | |
@@ -114,12 +114,12 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @since 3.20.0 |
116 | 116 | */ |
117 | - public function add_action( $taxonomy ) { |
|
117 | + public function add_action($taxonomy) { |
|
118 | 118 | /** |
119 | 119 | * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
120 | 120 | */ |
121 | 121 | |
122 | - add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10 ); |
|
122 | + add_action("{$taxonomy}_edit_form_fields", array($this, 'edit_form_fields'), 10); |
|
123 | 123 | |
124 | 124 | } |
125 | 125 | |
@@ -132,52 +132,52 @@ discard block |
||
132 | 132 | * |
133 | 133 | * @since 3.20.0 |
134 | 134 | */ |
135 | - public function edit_term( $term_id ) { |
|
135 | + public function edit_term($term_id) { |
|
136 | 136 | |
137 | 137 | // Bail if the action isn't related to the term currently being edited. |
138 | 138 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
139 | - if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
139 | + if ( ! isset($_POST['tag_ID']) || $term_id !== (int) $_POST['tag_ID']) { |
|
140 | 140 | return; |
141 | 141 | } |
142 | 142 | |
143 | 143 | // Delete. |
144 | 144 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
145 | - if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
146 | - delete_term_meta( $term_id, self::META_KEY ); |
|
145 | + if ( ! isset($_POST['wl_entity_id']) || ! is_array($_POST['wl_entity_id']) || empty($_POST['wl_entity_id'])) { |
|
146 | + delete_term_meta($term_id, self::META_KEY); |
|
147 | 147 | |
148 | 148 | return; |
149 | 149 | } |
150 | 150 | |
151 | 151 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
152 | - $entity_ids = array_map( 'esc_url_raw', wp_unslash( $_POST['wl_entity_id'] ) ); |
|
152 | + $entity_ids = array_map('esc_url_raw', wp_unslash($_POST['wl_entity_id'])); |
|
153 | 153 | // Update. |
154 | 154 | // |
155 | 155 | // Only use mb_* functions when mbstring is available. |
156 | 156 | // |
157 | 157 | // See https://github.com/insideout10/wordlift-plugin/issues/693. |
158 | - if ( extension_loaded( 'mbstring' ) ) { |
|
159 | - mb_regex_encoding( 'UTF-8' ); |
|
158 | + if (extension_loaded('mbstring')) { |
|
159 | + mb_regex_encoding('UTF-8'); |
|
160 | 160 | |
161 | 161 | $merged = array_reduce( |
162 | 162 | $entity_ids, |
163 | - function ( $carry, $item ) { |
|
164 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
163 | + function($carry, $item) { |
|
164 | + return array_merge($carry, mb_split("\x{2063}", wp_unslash($item))); |
|
165 | 165 | }, |
166 | 166 | array() |
167 | 167 | ); |
168 | 168 | } else { |
169 | 169 | $merged = array_reduce( |
170 | 170 | $entity_ids, |
171 | - function ( $carry, $item ) { |
|
172 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
171 | + function($carry, $item) { |
|
172 | + return array_merge($carry, preg_split("/\x{2063}/u", wp_unslash($item))); |
|
173 | 173 | }, |
174 | 174 | array() |
175 | 175 | ); |
176 | 176 | } |
177 | 177 | |
178 | - delete_term_meta( $term_id, self::META_KEY ); |
|
179 | - foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
180 | - add_term_meta( $term_id, self::META_KEY, $single ); |
|
178 | + delete_term_meta($term_id, self::META_KEY); |
|
179 | + foreach (array_unique(array_filter($merged)) as $single) { |
|
180 | + add_term_meta($term_id, self::META_KEY, $single); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | } |
@@ -17,71 +17,71 @@ |
||
17 | 17 | */ |
18 | 18 | function wordlift_ajax_related_posts( $http_raw_data = null ) { |
19 | 19 | |
20 | - // Extract filtering conditions |
|
21 | - if ( ! isset( $_GET['post_id'] ) || ! is_numeric( $_GET['post_id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
22 | - wp_die( 'Post id missing or invalid!' ); |
|
23 | - |
|
24 | - return; |
|
25 | - } |
|
26 | - |
|
27 | - // Get the current post |
|
28 | - $post_id = (int) $_GET['post_id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
29 | - $post = get_post( $post_id ); |
|
30 | - |
|
31 | - Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
32 | - |
|
33 | - // Extract filtering conditions |
|
34 | - $filtering_entity_uris = empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
35 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
36 | - |
|
37 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
38 | - $filtering_entity_ids = array_filter( |
|
39 | - array_map( |
|
40 | - function ( $uri ) use ( $content_service ) { |
|
41 | - $content = $content_service->get_by_entity_id( $uri ); |
|
42 | - if ( isset( $content ) && is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
43 | - return $content->get_bag()->ID; |
|
44 | - } else { |
|
45 | - return null; |
|
46 | - } |
|
47 | - }, |
|
48 | - $filtering_entity_uris |
|
49 | - ) |
|
50 | - ); |
|
51 | - |
|
52 | - $related_posts = array(); |
|
53 | - |
|
54 | - // If the current post is an entity, related posts to the current entity are |
|
55 | - // returned. |
|
56 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
57 | - |
|
58 | - if ( $entity_service->is_entity( $post->ID ) ) { |
|
59 | - $filtering_entity_ids = array( $post_id ); |
|
60 | - } |
|
61 | - |
|
62 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
63 | - |
|
64 | - $related_posts = Wordlift_Relation_Service::get_instance() |
|
65 | - ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
66 | - |
|
67 | - foreach ( $related_posts as $post_obj ) { |
|
68 | - |
|
69 | - /** |
|
70 | - * Use the thumbnail. |
|
71 | - * |
|
72 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
73 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
74 | - * |
|
75 | - * @since 3.19.3 We're using the medium size image. |
|
76 | - */ |
|
77 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
78 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
79 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
80 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - wl_core_send_json( $related_posts ); |
|
20 | + // Extract filtering conditions |
|
21 | + if ( ! isset( $_GET['post_id'] ) || ! is_numeric( $_GET['post_id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
22 | + wp_die( 'Post id missing or invalid!' ); |
|
23 | + |
|
24 | + return; |
|
25 | + } |
|
26 | + |
|
27 | + // Get the current post |
|
28 | + $post_id = (int) $_GET['post_id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
29 | + $post = get_post( $post_id ); |
|
30 | + |
|
31 | + Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
32 | + |
|
33 | + // Extract filtering conditions |
|
34 | + $filtering_entity_uris = empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
35 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
36 | + |
|
37 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
38 | + $filtering_entity_ids = array_filter( |
|
39 | + array_map( |
|
40 | + function ( $uri ) use ( $content_service ) { |
|
41 | + $content = $content_service->get_by_entity_id( $uri ); |
|
42 | + if ( isset( $content ) && is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
43 | + return $content->get_bag()->ID; |
|
44 | + } else { |
|
45 | + return null; |
|
46 | + } |
|
47 | + }, |
|
48 | + $filtering_entity_uris |
|
49 | + ) |
|
50 | + ); |
|
51 | + |
|
52 | + $related_posts = array(); |
|
53 | + |
|
54 | + // If the current post is an entity, related posts to the current entity are |
|
55 | + // returned. |
|
56 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
57 | + |
|
58 | + if ( $entity_service->is_entity( $post->ID ) ) { |
|
59 | + $filtering_entity_ids = array( $post_id ); |
|
60 | + } |
|
61 | + |
|
62 | + if ( ! empty( $filtering_entity_ids ) ) { |
|
63 | + |
|
64 | + $related_posts = Wordlift_Relation_Service::get_instance() |
|
65 | + ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
66 | + |
|
67 | + foreach ( $related_posts as $post_obj ) { |
|
68 | + |
|
69 | + /** |
|
70 | + * Use the thumbnail. |
|
71 | + * |
|
72 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
73 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
74 | + * |
|
75 | + * @since 3.19.3 We're using the medium size image. |
|
76 | + */ |
|
77 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
78 | + $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
79 | + $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
80 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + wl_core_send_json( $related_posts ); |
|
85 | 85 | |
86 | 86 | } |
87 | 87 |
@@ -15,31 +15,31 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @since 3.0.0 |
17 | 17 | */ |
18 | -function wordlift_ajax_related_posts( $http_raw_data = null ) { |
|
18 | +function wordlift_ajax_related_posts($http_raw_data = null) { |
|
19 | 19 | |
20 | 20 | // Extract filtering conditions |
21 | - if ( ! isset( $_GET['post_id'] ) || ! is_numeric( $_GET['post_id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
22 | - wp_die( 'Post id missing or invalid!' ); |
|
21 | + if ( ! isset($_GET['post_id']) || ! is_numeric($_GET['post_id'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
22 | + wp_die('Post id missing or invalid!'); |
|
23 | 23 | |
24 | 24 | return; |
25 | 25 | } |
26 | 26 | |
27 | 27 | // Get the current post |
28 | 28 | $post_id = (int) $_GET['post_id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
29 | - $post = get_post( $post_id ); |
|
29 | + $post = get_post($post_id); |
|
30 | 30 | |
31 | - Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
31 | + Wordlift_Log_Service::get_logger('wordlift_ajax_related_posts')->trace("Going to find posts related to current with post id: $post_id ..."); |
|
32 | 32 | |
33 | 33 | // Extract filtering conditions |
34 | - $filtering_entity_uris = empty( $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
35 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
34 | + $filtering_entity_uris = empty($http_raw_data) ? file_get_contents('php://input') : $http_raw_data; |
|
35 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
36 | 36 | |
37 | 37 | $content_service = Wordpress_Content_Service::get_instance(); |
38 | 38 | $filtering_entity_ids = array_filter( |
39 | 39 | array_map( |
40 | - function ( $uri ) use ( $content_service ) { |
|
41 | - $content = $content_service->get_by_entity_id( $uri ); |
|
42 | - if ( isset( $content ) && is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
40 | + function($uri) use ($content_service) { |
|
41 | + $content = $content_service->get_by_entity_id($uri); |
|
42 | + if (isset($content) && is_a($content->get_bag(), '\WP_Post')) { |
|
43 | 43 | return $content->get_bag()->ID; |
44 | 44 | } else { |
45 | 45 | return null; |
@@ -55,16 +55,16 @@ discard block |
||
55 | 55 | // returned. |
56 | 56 | $entity_service = Wordlift_Entity_Service::get_instance(); |
57 | 57 | |
58 | - if ( $entity_service->is_entity( $post->ID ) ) { |
|
59 | - $filtering_entity_ids = array( $post_id ); |
|
58 | + if ($entity_service->is_entity($post->ID)) { |
|
59 | + $filtering_entity_ids = array($post_id); |
|
60 | 60 | } |
61 | 61 | |
62 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
62 | + if ( ! empty($filtering_entity_ids)) { |
|
63 | 63 | |
64 | 64 | $related_posts = Wordlift_Relation_Service::get_instance() |
65 | - ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
65 | + ->get_article_subjects($filtering_entity_ids, '*', null, 'publish', array($post_id), 5); |
|
66 | 66 | |
67 | - foreach ( $related_posts as $post_obj ) { |
|
67 | + foreach ($related_posts as $post_obj) { |
|
68 | 68 | |
69 | 69 | /** |
70 | 70 | * Use the thumbnail. |
@@ -74,15 +74,15 @@ discard block |
||
74 | 74 | * |
75 | 75 | * @since 3.19.3 We're using the medium size image. |
76 | 76 | */ |
77 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
78 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
79 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
80 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
77 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
78 | + $post_obj->thumbnail = ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
79 | + $post_obj->link = get_edit_post_link($post_obj->ID, 'none'); |
|
80 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | - wl_core_send_json( $related_posts ); |
|
84 | + wl_core_send_json($related_posts); |
|
85 | 85 | |
86 | 86 | } |
87 | 87 | |
88 | -add_action( 'wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts' ); |
|
88 | +add_action('wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts'); |
@@ -4,9 +4,9 @@ |
||
4 | 4 | <div id='news_container'> |
5 | 5 | <?php if ( ! empty( $articles['posts_data'] ) ) { ?> |
6 | 6 | <?php |
7 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
8 | - foreach ( $articles['posts_data'] as $key => $item ) { |
|
9 | - ?> |
|
7 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
8 | + foreach ( $articles['posts_data'] as $key => $item ) { |
|
9 | + ?> |
|
10 | 10 | <div> |
11 | 11 | <a target="_blank" |
12 | 12 | href="<?php echo esc_url( $item['post_url'] ); ?>"><?php echo esc_html( $item['post_title'] ); ?></a> |
@@ -2,20 +2,20 @@ |
||
2 | 2 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
3 | 3 | $articles = $this->get_last_wordlift_articles(); ?> |
4 | 4 | <div id='news_container'> |
5 | - <?php if ( ! empty( $articles['posts_data'] ) ) { ?> |
|
5 | + <?php if ( ! empty($articles['posts_data'])) { ?> |
|
6 | 6 | <?php |
7 | 7 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
8 | - foreach ( $articles['posts_data'] as $key => $item ) { |
|
8 | + foreach ($articles['posts_data'] as $key => $item) { |
|
9 | 9 | ?> |
10 | 10 | <div> |
11 | 11 | <a target="_blank" |
12 | - href="<?php echo esc_url( $item['post_url'] ); ?>"><?php echo esc_html( $item['post_title'] ); ?></a> |
|
13 | - <p><?php echo wp_kses( $item['post_description'], wp_kses_allowed_html( 'post' ) ); ?></p> |
|
12 | + href="<?php echo esc_url($item['post_url']); ?>"><?php echo esc_html($item['post_title']); ?></a> |
|
13 | + <p><?php echo wp_kses($item['post_description'], wp_kses_allowed_html('post')); ?></p> |
|
14 | 14 | </div> |
15 | 15 | <?php } ?> |
16 | 16 | <?php } ?> |
17 | 17 | <div> |
18 | 18 | <a href="#" id="max_posts_count_3" |
19 | - class="wl_more_posts"><?php echo esc_html__( 'More posts', 'wordlift' ); ?></a> |
|
19 | + class="wl_more_posts"><?php echo esc_html__('More posts', 'wordlift'); ?></a> |
|
20 | 20 | </div> |
21 | 21 | </div> |