@@ -16,185 +16,185 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Http_Api { |
18 | 18 | |
19 | - /** |
|
20 | - * A {@link Wordlift_Log_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.15.3 |
|
23 | - * |
|
24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | - */ |
|
26 | - private $log; |
|
27 | - |
|
28 | - /** |
|
29 | - * Create a {@link Wordlift_End_Point} instance. |
|
30 | - * |
|
31 | - * @since 3.15.3 |
|
32 | - */ |
|
33 | - public function __construct() { |
|
34 | - |
|
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | - |
|
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | - |
|
40 | - // region SAMPLE ACTIONS. |
|
41 | - add_action( |
|
42 | - 'admin_post_wl_hello_world', |
|
43 | - array( |
|
44 | - $this, |
|
45 | - 'hello_world', |
|
46 | - ) |
|
47 | - ); |
|
48 | - add_action( |
|
49 | - 'admin_post_nopriv_wl_hello_world', |
|
50 | - array( |
|
51 | - $this, |
|
52 | - 'nopriv_hello_world', |
|
53 | - ) |
|
54 | - ); |
|
55 | - // endregion |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Add the `wl-api` rewrite end-point. |
|
60 | - * |
|
61 | - * @since 3.15.3 |
|
62 | - */ |
|
63 | - public function add_rewrite_endpoint() { |
|
64 | - |
|
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | - $this->ensure_rewrite_rules_are_flushed(); |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Handle `template_redirect` hooks. |
|
72 | - * |
|
73 | - * @since 3.15.3 |
|
74 | - */ |
|
75 | - public function template_redirect() { |
|
76 | - |
|
77 | - global $wp_query; |
|
78 | - |
|
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | - |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
87 | - |
|
88 | - exit; |
|
89 | - |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Do the requested action. |
|
94 | - * |
|
95 | - * @param string $action The action to execute. |
|
96 | - * |
|
97 | - * @since 3.15.3 |
|
98 | - */ |
|
99 | - private function do_action( $action ) { |
|
100 | - |
|
101 | - if ( empty( $action ) ) { |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | - /** |
|
107 | - * Fires on a non-authenticated admin post request for the given action. |
|
108 | - * |
|
109 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | - * request action. |
|
111 | - * |
|
112 | - * @since 2.6.0 |
|
113 | - */ |
|
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
115 | - } else { |
|
116 | - /** |
|
117 | - * Fires on an authenticated admin post request for the given action. |
|
118 | - * |
|
119 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | - * request action. |
|
121 | - * |
|
122 | - * @since 2.6.0 |
|
123 | - */ |
|
124 | - do_action( "admin_post_{$action}" ); |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Test function, anonymous. |
|
131 | - * |
|
132 | - * @since 3.15.3 |
|
133 | - */ |
|
134 | - public function nopriv_hello_world() { |
|
135 | - |
|
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
137 | - |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Test function, authenticated. |
|
142 | - * |
|
143 | - * @since 3.15.3 |
|
144 | - */ |
|
145 | - public function hello_world() { |
|
146 | - |
|
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
148 | - |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Ensure that the rewrite rules are flushed the first time. |
|
153 | - * |
|
154 | - * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | - * @since 3.15.3 |
|
156 | - */ |
|
157 | - public static function ensure_rewrite_rules_are_flushed() { |
|
158 | - |
|
159 | - // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
162 | - add_action( |
|
163 | - 'wp_loaded', |
|
164 | - function () { |
|
165 | - flush_rewrite_rules(); |
|
166 | - } |
|
167 | - ); |
|
168 | - } |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | - * reinitialize the `wl-api` route. |
|
175 | - * |
|
176 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | - * |
|
178 | - * @since 3.19.2 |
|
179 | - */ |
|
180 | - public static function activate() { |
|
181 | - |
|
182 | - // Force the plugin to reinitialize the rewrite rules. |
|
183 | - update_option( 'wl_http_api', 'no' ); |
|
184 | - |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Delete the option when the plugin is deactivated. |
|
189 | - * |
|
190 | - * @since 3.19.4 |
|
191 | - * |
|
192 | - * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | - */ |
|
194 | - public static function deactivate() { |
|
195 | - |
|
196 | - delete_option( 'wl_http_api' ); |
|
197 | - |
|
198 | - } |
|
19 | + /** |
|
20 | + * A {@link Wordlift_Log_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.15.3 |
|
23 | + * |
|
24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
25 | + */ |
|
26 | + private $log; |
|
27 | + |
|
28 | + /** |
|
29 | + * Create a {@link Wordlift_End_Point} instance. |
|
30 | + * |
|
31 | + * @since 3.15.3 |
|
32 | + */ |
|
33 | + public function __construct() { |
|
34 | + |
|
35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
36 | + |
|
37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
39 | + |
|
40 | + // region SAMPLE ACTIONS. |
|
41 | + add_action( |
|
42 | + 'admin_post_wl_hello_world', |
|
43 | + array( |
|
44 | + $this, |
|
45 | + 'hello_world', |
|
46 | + ) |
|
47 | + ); |
|
48 | + add_action( |
|
49 | + 'admin_post_nopriv_wl_hello_world', |
|
50 | + array( |
|
51 | + $this, |
|
52 | + 'nopriv_hello_world', |
|
53 | + ) |
|
54 | + ); |
|
55 | + // endregion |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Add the `wl-api` rewrite end-point. |
|
60 | + * |
|
61 | + * @since 3.15.3 |
|
62 | + */ |
|
63 | + public function add_rewrite_endpoint() { |
|
64 | + |
|
65 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
66 | + $this->ensure_rewrite_rules_are_flushed(); |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Handle `template_redirect` hooks. |
|
72 | + * |
|
73 | + * @since 3.15.3 |
|
74 | + */ |
|
75 | + public function template_redirect() { |
|
76 | + |
|
77 | + global $wp_query; |
|
78 | + |
|
79 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
81 | + |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action( $action ); |
|
87 | + |
|
88 | + exit; |
|
89 | + |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Do the requested action. |
|
94 | + * |
|
95 | + * @param string $action The action to execute. |
|
96 | + * |
|
97 | + * @since 3.15.3 |
|
98 | + */ |
|
99 | + private function do_action( $action ) { |
|
100 | + |
|
101 | + if ( empty( $action ) ) { |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
106 | + /** |
|
107 | + * Fires on a non-authenticated admin post request for the given action. |
|
108 | + * |
|
109 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
110 | + * request action. |
|
111 | + * |
|
112 | + * @since 2.6.0 |
|
113 | + */ |
|
114 | + do_action( "admin_post_nopriv_{$action}" ); |
|
115 | + } else { |
|
116 | + /** |
|
117 | + * Fires on an authenticated admin post request for the given action. |
|
118 | + * |
|
119 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
120 | + * request action. |
|
121 | + * |
|
122 | + * @since 2.6.0 |
|
123 | + */ |
|
124 | + do_action( "admin_post_{$action}" ); |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Test function, anonymous. |
|
131 | + * |
|
132 | + * @since 3.15.3 |
|
133 | + */ |
|
134 | + public function nopriv_hello_world() { |
|
135 | + |
|
136 | + wp_die( 'Hello World! (from anonymous)' ); |
|
137 | + |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Test function, authenticated. |
|
142 | + * |
|
143 | + * @since 3.15.3 |
|
144 | + */ |
|
145 | + public function hello_world() { |
|
146 | + |
|
147 | + wp_die( 'Hello World! (from authenticated)' ); |
|
148 | + |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Ensure that the rewrite rules are flushed the first time. |
|
153 | + * |
|
154 | + * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues. |
|
155 | + * @since 3.15.3 |
|
156 | + */ |
|
157 | + public static function ensure_rewrite_rules_are_flushed() { |
|
158 | + |
|
159 | + // See https://github.com/insideout10/wordlift-plugin/issues/698. |
|
160 | + if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | + update_option( 'wl_http_api', 'yes' ); |
|
162 | + add_action( |
|
163 | + 'wp_loaded', |
|
164 | + function () { |
|
165 | + flush_rewrite_rules(); |
|
166 | + } |
|
167 | + ); |
|
168 | + } |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to |
|
174 | + * reinitialize the `wl-api` route. |
|
175 | + * |
|
176 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
177 | + * |
|
178 | + * @since 3.19.2 |
|
179 | + */ |
|
180 | + public static function activate() { |
|
181 | + |
|
182 | + // Force the plugin to reinitialize the rewrite rules. |
|
183 | + update_option( 'wl_http_api', 'no' ); |
|
184 | + |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Delete the option when the plugin is deactivated. |
|
189 | + * |
|
190 | + * @since 3.19.4 |
|
191 | + * |
|
192 | + * @see https://github.com/insideout10/wordlift-plugin/issues/846 |
|
193 | + */ |
|
194 | + public static function deactivate() { |
|
195 | + |
|
196 | + delete_option( 'wl_http_api' ); |
|
197 | + |
|
198 | + } |
|
199 | 199 | |
200 | 200 | } |
@@ -32,10 +32,10 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function __construct() { |
34 | 34 | |
35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
36 | 36 | |
37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
39 | 39 | |
40 | 40 | // region SAMPLE ACTIONS. |
41 | 41 | add_action( |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function add_rewrite_endpoint() { |
64 | 64 | |
65 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
65 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
66 | 66 | $this->ensure_rewrite_rules_are_flushed(); |
67 | 67 | |
68 | 68 | } |
@@ -76,14 +76,14 @@ discard block |
||
76 | 76 | |
77 | 77 | global $wp_query; |
78 | 78 | |
79 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
80 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
79 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
80 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
81 | 81 | |
82 | 82 | return; |
83 | 83 | } |
84 | 84 | |
85 | - $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | - $this->do_action( $action ); |
|
85 | + $action = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
86 | + $this->do_action($action); |
|
87 | 87 | |
88 | 88 | exit; |
89 | 89 | |
@@ -96,13 +96,13 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @since 3.15.3 |
98 | 98 | */ |
99 | - private function do_action( $action ) { |
|
99 | + private function do_action($action) { |
|
100 | 100 | |
101 | - if ( empty( $action ) ) { |
|
101 | + if (empty($action)) { |
|
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
105 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
106 | 106 | /** |
107 | 107 | * Fires on a non-authenticated admin post request for the given action. |
108 | 108 | * |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @since 2.6.0 |
113 | 113 | */ |
114 | - do_action( "admin_post_nopriv_{$action}" ); |
|
114 | + do_action("admin_post_nopriv_{$action}"); |
|
115 | 115 | } else { |
116 | 116 | /** |
117 | 117 | * Fires on an authenticated admin post request for the given action. |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @since 2.6.0 |
123 | 123 | */ |
124 | - do_action( "admin_post_{$action}" ); |
|
124 | + do_action("admin_post_{$action}"); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function nopriv_hello_world() { |
135 | 135 | |
136 | - wp_die( 'Hello World! (from anonymous)' ); |
|
136 | + wp_die('Hello World! (from anonymous)'); |
|
137 | 137 | |
138 | 138 | } |
139 | 139 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function hello_world() { |
146 | 146 | |
147 | - wp_die( 'Hello World! (from authenticated)' ); |
|
147 | + wp_die('Hello World! (from authenticated)'); |
|
148 | 148 | |
149 | 149 | } |
150 | 150 | |
@@ -157,11 +157,11 @@ discard block |
||
157 | 157 | public static function ensure_rewrite_rules_are_flushed() { |
158 | 158 | |
159 | 159 | // See https://github.com/insideout10/wordlift-plugin/issues/698. |
160 | - if ( 'yes' !== get_option( 'wl_http_api' ) ) { |
|
161 | - update_option( 'wl_http_api', 'yes' ); |
|
160 | + if ('yes' !== get_option('wl_http_api')) { |
|
161 | + update_option('wl_http_api', 'yes'); |
|
162 | 162 | add_action( |
163 | 163 | 'wp_loaded', |
164 | - function () { |
|
164 | + function() { |
|
165 | 165 | flush_rewrite_rules(); |
166 | 166 | } |
167 | 167 | ); |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | public static function activate() { |
181 | 181 | |
182 | 182 | // Force the plugin to reinitialize the rewrite rules. |
183 | - update_option( 'wl_http_api', 'no' ); |
|
183 | + update_option('wl_http_api', 'no'); |
|
184 | 184 | |
185 | 185 | } |
186 | 186 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | public static function deactivate() { |
195 | 195 | |
196 | - delete_option( 'wl_http_api' ); |
|
196 | + delete_option('wl_http_api'); |
|
197 | 197 | |
198 | 198 | } |
199 | 199 |
@@ -8,80 +8,80 @@ |
||
8 | 8 | |
9 | 9 | class Wordlift_Mapping_Ajax_Adapter { |
10 | 10 | |
11 | - /** |
|
12 | - * The {@link Wordlift_Mapping_Service} instance. |
|
13 | - * |
|
14 | - * @since 3.20.0 |
|
15 | - * @access private |
|
16 | - * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | - */ |
|
18 | - private $mapping_service; |
|
11 | + /** |
|
12 | + * The {@link Wordlift_Mapping_Service} instance. |
|
13 | + * |
|
14 | + * @since 3.20.0 |
|
15 | + * @access private |
|
16 | + * @var \Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
17 | + */ |
|
18 | + private $mapping_service; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | - * |
|
23 | - * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | - * |
|
25 | - * @since 3.20.0 |
|
26 | - */ |
|
27 | - public function __construct( $mapping_service ) { |
|
20 | + /** |
|
21 | + * Create a {@link Wordlift_Mapping_Ajax_Adapter} instance. |
|
22 | + * |
|
23 | + * @param Wordlift_Mapping_Service $mapping_service The {@link Wordlift_Mapping_Service} instance. |
|
24 | + * |
|
25 | + * @since 3.20.0 |
|
26 | + */ |
|
27 | + public function __construct( $mapping_service ) { |
|
28 | 28 | |
29 | - $this->mapping_service = $mapping_service; |
|
29 | + $this->mapping_service = $mapping_service; |
|
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | + add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
33 | 33 | |
34 | - } |
|
34 | + } |
|
35 | 35 | |
36 | - public function set_entity_types_for_post_type() { |
|
36 | + public function set_entity_types_for_post_type() { |
|
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | - return; |
|
40 | - } |
|
38 | + if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | + return; |
|
40 | + } |
|
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
46 | 46 | |
47 | - wp_send_json_success(); |
|
47 | + wp_send_json_success(); |
|
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - public function update_post_type_entity_types() { |
|
51 | + public function update_post_type_entity_types() { |
|
52 | 52 | |
53 | - // If the nonce is invalid, return an error. |
|
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | - } |
|
53 | + // If the nonce is invalid, return an error. |
|
54 | + $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | + if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
57 | + } |
|
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | - } |
|
59 | + if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | + wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
61 | + } |
|
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | - } |
|
63 | + if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | + wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
65 | + } |
|
66 | 66 | |
67 | - // Get the post type. |
|
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
67 | + // Get the post type. |
|
68 | + $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
69 | 69 | |
70 | - // Get the entity types URIs. |
|
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
70 | + // Get the entity types URIs. |
|
71 | + $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
72 | 72 | |
73 | - // Get the offset. |
|
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
73 | + // Get the offset. |
|
74 | + $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
75 | 75 | |
76 | - // Update and get the results. |
|
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
76 | + // Update and get the results. |
|
77 | + $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
78 | 78 | |
79 | - // Add our nonce to the result. |
|
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
79 | + // Add our nonce to the result. |
|
80 | + $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
81 | 81 | |
82 | - // Finally send the results. |
|
83 | - wp_send_json_success( $result ); |
|
82 | + // Finally send the results. |
|
83 | + wp_send_json_success( $result ); |
|
84 | 84 | |
85 | - } |
|
85 | + } |
|
86 | 86 | |
87 | 87 | } |
@@ -24,25 +24,25 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @since 3.20.0 |
26 | 26 | */ |
27 | - public function __construct( $mapping_service ) { |
|
27 | + public function __construct($mapping_service) { |
|
28 | 28 | |
29 | 29 | $this->mapping_service = $mapping_service; |
30 | 30 | |
31 | - add_action( 'wp_ajax_wl_set_entity_types_for_post_type', array( $this, 'set_entity_types_for_post_type' ) ); |
|
32 | - add_action( 'wp_ajax_wl_update_post_type_entity_types', array( $this, 'update_post_type_entity_types' ) ); |
|
31 | + add_action('wp_ajax_wl_set_entity_types_for_post_type', array($this, 'set_entity_types_for_post_type')); |
|
32 | + add_action('wp_ajax_wl_update_post_type_entity_types', array($this, 'update_post_type_entity_types')); |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | |
36 | 36 | public function set_entity_types_for_post_type() { |
37 | 37 | |
38 | - if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['entity_types'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
38 | + if ( ! isset($_REQUEST['post_type']) || ! isset($_REQUEST['entity_types'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | - $entity_types = array_map( 'sanitize_text_field', wp_unslash( (array) $_REQUEST['entity_types'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
42 | + $post_type = sanitize_text_field(wp_unslash($_REQUEST['post_type'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
43 | + $entity_types = array_map('sanitize_text_field', wp_unslash((array) $_REQUEST['entity_types'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
44 | 44 | |
45 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
45 | + $this->mapping_service->set_entity_types_for_post_type($post_type, $entity_types); |
|
46 | 46 | |
47 | 47 | wp_send_json_success(); |
48 | 48 | |
@@ -51,36 +51,36 @@ discard block |
||
51 | 51 | public function update_post_type_entity_types() { |
52 | 52 | |
53 | 53 | // If the nonce is invalid, return an error. |
54 | - $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ) : ''; |
|
55 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
56 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
54 | + $nonce = isset($_REQUEST['_nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_nonce'])) : ''; |
|
55 | + if ( ! wp_verify_nonce($nonce, 'update_post_type_entity_types')) { |
|
56 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
57 | 57 | } |
58 | 58 | |
59 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
60 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
59 | + if (empty($_REQUEST['post_type'])) { |
|
60 | + wp_send_json_error(__('`post_type` is required', 'wordlift')); |
|
61 | 61 | } |
62 | 62 | |
63 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
64 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
63 | + if (empty($_REQUEST['entity_types'])) { |
|
64 | + wp_send_json_error(__('`entity_types` is required', 'wordlift')); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | // Get the post type. |
68 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : ''; |
|
68 | + $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field(wp_unslash($_REQUEST['post_type'])) : ''; |
|
69 | 69 | |
70 | 70 | // Get the entity types URIs. |
71 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? filter_var( wp_unslash( $_REQUEST['entity_types'] ), FILTER_REQUIRE_ARRAY ) : array(); |
|
71 | + $entity_types = isset($_REQUEST['entity_types']) ? filter_var(wp_unslash($_REQUEST['entity_types']), FILTER_REQUIRE_ARRAY) : array(); |
|
72 | 72 | |
73 | 73 | // Get the offset. |
74 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
74 | + $offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0; |
|
75 | 75 | |
76 | 76 | // Update and get the results. |
77 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
77 | + $result = $this->mapping_service->update($post_type, $entity_types, $offset); |
|
78 | 78 | |
79 | 79 | // Add our nonce to the result. |
80 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
80 | + $result['_nonce'] = wp_create_nonce('update_post_type_entity_types'); |
|
81 | 81 | |
82 | 82 | // Finally send the results. |
83 | - wp_send_json_success( $result ); |
|
83 | + wp_send_json_success($result); |
|
84 | 84 | |
85 | 85 | } |
86 | 86 |
@@ -14,223 +14,223 @@ |
||
14 | 14 | */ |
15 | 15 | class Wordlift_Mapping_Service { |
16 | 16 | |
17 | - /** |
|
18 | - * The mapping's options. |
|
19 | - * |
|
20 | - * @since 3.20.0 |
|
21 | - * @access private |
|
22 | - * @var array $options The mapping's options. |
|
23 | - */ |
|
24 | - private $options; |
|
25 | - |
|
26 | - /** |
|
27 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
28 | - * |
|
29 | - * @since 3.20.0 |
|
30 | - * @access private |
|
31 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
32 | - */ |
|
33 | - private $entity_type_service; |
|
34 | - |
|
35 | - /** |
|
36 | - * The singleton instance. |
|
37 | - * |
|
38 | - * @since 3.20.0 |
|
39 | - * @access private |
|
40 | - * @var \Wordlift_Mapping_Service $instance The singleton instance. |
|
41 | - */ |
|
42 | - private static $instance; |
|
43 | - |
|
44 | - /** |
|
45 | - * Create a {@link Wordlift_Mapping_Service} instance. |
|
46 | - * |
|
47 | - * @since 3.20.0 |
|
48 | - * |
|
49 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
50 | - */ |
|
51 | - public function __construct( $entity_type_service ) { |
|
52 | - |
|
53 | - // Set the entity type service instance. |
|
54 | - $this->entity_type_service = $entity_type_service; |
|
55 | - |
|
56 | - // Load the options. |
|
57 | - $this->options = get_option( 'wl_mappings', array() ); |
|
58 | - |
|
59 | - // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
|
60 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
61 | - add_filter( |
|
62 | - 'wl_default_entity_types_for_post_type', |
|
63 | - array( |
|
64 | - $this, |
|
65 | - 'default_entity_types_for_post_type', |
|
66 | - ), |
|
67 | - 9, |
|
68 | - 2 |
|
69 | - ); |
|
70 | - |
|
71 | - // Set the singleton instance. |
|
72 | - self::$instance = $this; |
|
73 | - |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get the singleton instance. |
|
78 | - * |
|
79 | - * @since 3.20.0 |
|
80 | - * |
|
81 | - * @return \Wordlift_Mapping_Service The singleton instance. |
|
82 | - */ |
|
83 | - public static function get_instance() { |
|
84 | - |
|
85 | - return self::$instance; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Save the options. |
|
90 | - * |
|
91 | - * @since 3.20.0 |
|
92 | - */ |
|
93 | - private function save_options() { |
|
94 | - |
|
95 | - update_option( 'wl_mappings', $this->options, true ); |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Set the default entity types for a post type. |
|
101 | - * |
|
102 | - * @since 3.20.0 |
|
103 | - * |
|
104 | - * @param string $post_type Post type. |
|
105 | - * @param array $entity_types An array of entity types slugs. |
|
106 | - */ |
|
107 | - public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
108 | - |
|
109 | - $this->options[ $post_type ] = $entity_types; |
|
110 | - $this->save_options(); |
|
111 | - |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types. |
|
116 | - * |
|
117 | - * @since 3.20.0 |
|
118 | - * |
|
119 | - * @param array $post_types The default post types. |
|
120 | - * |
|
121 | - * @return array The supported post types. |
|
122 | - */ |
|
123 | - public function valid_entity_post_types( $post_types ) { |
|
124 | - |
|
125 | - return array_merge( $post_types, array_keys( $this->options ) ); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type. |
|
130 | - * |
|
131 | - * @since 3.20.0 |
|
132 | - * |
|
133 | - * @param array $default The default entity types. |
|
134 | - * @param string $post_type The post type. |
|
135 | - * |
|
136 | - * @return array The default entity types. |
|
137 | - */ |
|
138 | - public function default_entity_types_for_post_type( $default, $post_type ) { |
|
139 | - |
|
140 | - return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Update the post type with the entity types, starting at the specified offset. |
|
145 | - * |
|
146 | - * @since 3.20.0 |
|
147 | - * |
|
148 | - * @param string $post_type The post type. |
|
149 | - * @param array $entity_types The entity types. |
|
150 | - * @param int $offset The offset (0 by default). |
|
151 | - * |
|
152 | - * @return array { |
|
153 | - * The result array. |
|
154 | - * |
|
155 | - * @type int $current The current offset. |
|
156 | - * @type int $next The next offset. |
|
157 | - * @type int $count The total element count. |
|
158 | - * } |
|
159 | - */ |
|
160 | - public function update( $post_type, $entity_types, $offset = 0 ) { |
|
161 | - |
|
162 | - $entity_type_service = $this->entity_type_service; |
|
163 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
164 | - |
|
165 | - return Wordlift_Batch_Action::process( |
|
166 | - $post_type, |
|
167 | - $offset, |
|
168 | - $tax_query, |
|
169 | - function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | - foreach ( $entity_types as $entity_type ) { |
|
171 | - $entity_type_service->set( $post_id, $entity_type, false ); |
|
172 | - } |
|
173 | - } |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Count the number of posts that need to be assigned with the entity types. |
|
179 | - * |
|
180 | - * @since 3.20.0 |
|
181 | - * |
|
182 | - * @param string $post_type The post type. |
|
183 | - * @param array $entity_types An array of entity types. |
|
184 | - * |
|
185 | - * @return int The number of posts to be assigned with entity types. |
|
186 | - */ |
|
187 | - public function count( $post_type, $entity_types ) { |
|
188 | - |
|
189 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
190 | - |
|
191 | - return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get the taxonomy query for the specified entity types. |
|
196 | - * |
|
197 | - * @since 3.20.0 |
|
198 | - * |
|
199 | - * @param array $entity_types The entity types. |
|
200 | - * |
|
201 | - * @return array The tax query. |
|
202 | - */ |
|
203 | - private function get_tax_query( $entity_types ) { |
|
204 | - |
|
205 | - $entity_type_service = $this->entity_type_service; |
|
206 | - $entity_types_terms = array_filter( |
|
207 | - array_map( |
|
208 | - function ( $item ) use ( $entity_type_service ) { |
|
209 | - return $entity_type_service->get_term_by_uri( $item ); |
|
210 | - }, |
|
211 | - $entity_types |
|
212 | - ) |
|
213 | - ); |
|
214 | - |
|
215 | - $entity_types_terms_ids = array_map( |
|
216 | - function ( $term ) { |
|
217 | - return $term->term_id; |
|
218 | - }, |
|
219 | - $entity_types_terms |
|
220 | - ); |
|
221 | - |
|
222 | - $tax_query = array( |
|
223 | - 'tax_query' => array( |
|
224 | - array( |
|
225 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
226 | - 'field' => 'term_id', |
|
227 | - 'terms' => $entity_types_terms_ids, |
|
228 | - 'operator' => 'NOT IN', |
|
229 | - ), |
|
230 | - ), |
|
231 | - ); |
|
232 | - |
|
233 | - return $tax_query; |
|
234 | - } |
|
17 | + /** |
|
18 | + * The mapping's options. |
|
19 | + * |
|
20 | + * @since 3.20.0 |
|
21 | + * @access private |
|
22 | + * @var array $options The mapping's options. |
|
23 | + */ |
|
24 | + private $options; |
|
25 | + |
|
26 | + /** |
|
27 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
28 | + * |
|
29 | + * @since 3.20.0 |
|
30 | + * @access private |
|
31 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
32 | + */ |
|
33 | + private $entity_type_service; |
|
34 | + |
|
35 | + /** |
|
36 | + * The singleton instance. |
|
37 | + * |
|
38 | + * @since 3.20.0 |
|
39 | + * @access private |
|
40 | + * @var \Wordlift_Mapping_Service $instance The singleton instance. |
|
41 | + */ |
|
42 | + private static $instance; |
|
43 | + |
|
44 | + /** |
|
45 | + * Create a {@link Wordlift_Mapping_Service} instance. |
|
46 | + * |
|
47 | + * @since 3.20.0 |
|
48 | + * |
|
49 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
50 | + */ |
|
51 | + public function __construct( $entity_type_service ) { |
|
52 | + |
|
53 | + // Set the entity type service instance. |
|
54 | + $this->entity_type_service = $entity_type_service; |
|
55 | + |
|
56 | + // Load the options. |
|
57 | + $this->options = get_option( 'wl_mappings', array() ); |
|
58 | + |
|
59 | + // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
|
60 | + add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
61 | + add_filter( |
|
62 | + 'wl_default_entity_types_for_post_type', |
|
63 | + array( |
|
64 | + $this, |
|
65 | + 'default_entity_types_for_post_type', |
|
66 | + ), |
|
67 | + 9, |
|
68 | + 2 |
|
69 | + ); |
|
70 | + |
|
71 | + // Set the singleton instance. |
|
72 | + self::$instance = $this; |
|
73 | + |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get the singleton instance. |
|
78 | + * |
|
79 | + * @since 3.20.0 |
|
80 | + * |
|
81 | + * @return \Wordlift_Mapping_Service The singleton instance. |
|
82 | + */ |
|
83 | + public static function get_instance() { |
|
84 | + |
|
85 | + return self::$instance; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Save the options. |
|
90 | + * |
|
91 | + * @since 3.20.0 |
|
92 | + */ |
|
93 | + private function save_options() { |
|
94 | + |
|
95 | + update_option( 'wl_mappings', $this->options, true ); |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Set the default entity types for a post type. |
|
101 | + * |
|
102 | + * @since 3.20.0 |
|
103 | + * |
|
104 | + * @param string $post_type Post type. |
|
105 | + * @param array $entity_types An array of entity types slugs. |
|
106 | + */ |
|
107 | + public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
108 | + |
|
109 | + $this->options[ $post_type ] = $entity_types; |
|
110 | + $this->save_options(); |
|
111 | + |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types. |
|
116 | + * |
|
117 | + * @since 3.20.0 |
|
118 | + * |
|
119 | + * @param array $post_types The default post types. |
|
120 | + * |
|
121 | + * @return array The supported post types. |
|
122 | + */ |
|
123 | + public function valid_entity_post_types( $post_types ) { |
|
124 | + |
|
125 | + return array_merge( $post_types, array_keys( $this->options ) ); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type. |
|
130 | + * |
|
131 | + * @since 3.20.0 |
|
132 | + * |
|
133 | + * @param array $default The default entity types. |
|
134 | + * @param string $post_type The post type. |
|
135 | + * |
|
136 | + * @return array The default entity types. |
|
137 | + */ |
|
138 | + public function default_entity_types_for_post_type( $default, $post_type ) { |
|
139 | + |
|
140 | + return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Update the post type with the entity types, starting at the specified offset. |
|
145 | + * |
|
146 | + * @since 3.20.0 |
|
147 | + * |
|
148 | + * @param string $post_type The post type. |
|
149 | + * @param array $entity_types The entity types. |
|
150 | + * @param int $offset The offset (0 by default). |
|
151 | + * |
|
152 | + * @return array { |
|
153 | + * The result array. |
|
154 | + * |
|
155 | + * @type int $current The current offset. |
|
156 | + * @type int $next The next offset. |
|
157 | + * @type int $count The total element count. |
|
158 | + * } |
|
159 | + */ |
|
160 | + public function update( $post_type, $entity_types, $offset = 0 ) { |
|
161 | + |
|
162 | + $entity_type_service = $this->entity_type_service; |
|
163 | + $tax_query = $this->get_tax_query( $entity_types ); |
|
164 | + |
|
165 | + return Wordlift_Batch_Action::process( |
|
166 | + $post_type, |
|
167 | + $offset, |
|
168 | + $tax_query, |
|
169 | + function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | + foreach ( $entity_types as $entity_type ) { |
|
171 | + $entity_type_service->set( $post_id, $entity_type, false ); |
|
172 | + } |
|
173 | + } |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Count the number of posts that need to be assigned with the entity types. |
|
179 | + * |
|
180 | + * @since 3.20.0 |
|
181 | + * |
|
182 | + * @param string $post_type The post type. |
|
183 | + * @param array $entity_types An array of entity types. |
|
184 | + * |
|
185 | + * @return int The number of posts to be assigned with entity types. |
|
186 | + */ |
|
187 | + public function count( $post_type, $entity_types ) { |
|
188 | + |
|
189 | + $tax_query = $this->get_tax_query( $entity_types ); |
|
190 | + |
|
191 | + return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get the taxonomy query for the specified entity types. |
|
196 | + * |
|
197 | + * @since 3.20.0 |
|
198 | + * |
|
199 | + * @param array $entity_types The entity types. |
|
200 | + * |
|
201 | + * @return array The tax query. |
|
202 | + */ |
|
203 | + private function get_tax_query( $entity_types ) { |
|
204 | + |
|
205 | + $entity_type_service = $this->entity_type_service; |
|
206 | + $entity_types_terms = array_filter( |
|
207 | + array_map( |
|
208 | + function ( $item ) use ( $entity_type_service ) { |
|
209 | + return $entity_type_service->get_term_by_uri( $item ); |
|
210 | + }, |
|
211 | + $entity_types |
|
212 | + ) |
|
213 | + ); |
|
214 | + |
|
215 | + $entity_types_terms_ids = array_map( |
|
216 | + function ( $term ) { |
|
217 | + return $term->term_id; |
|
218 | + }, |
|
219 | + $entity_types_terms |
|
220 | + ); |
|
221 | + |
|
222 | + $tax_query = array( |
|
223 | + 'tax_query' => array( |
|
224 | + array( |
|
225 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
226 | + 'field' => 'term_id', |
|
227 | + 'terms' => $entity_types_terms_ids, |
|
228 | + 'operator' => 'NOT IN', |
|
229 | + ), |
|
230 | + ), |
|
231 | + ); |
|
232 | + |
|
233 | + return $tax_query; |
|
234 | + } |
|
235 | 235 | |
236 | 236 | } |
@@ -48,16 +48,16 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
50 | 50 | */ |
51 | - public function __construct( $entity_type_service ) { |
|
51 | + public function __construct($entity_type_service) { |
|
52 | 52 | |
53 | 53 | // Set the entity type service instance. |
54 | 54 | $this->entity_type_service = $entity_type_service; |
55 | 55 | |
56 | 56 | // Load the options. |
57 | - $this->options = get_option( 'wl_mappings', array() ); |
|
57 | + $this->options = get_option('wl_mappings', array()); |
|
58 | 58 | |
59 | 59 | // Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`. |
60 | - add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types' ), 9 ); |
|
60 | + add_filter('wl_valid_entity_post_types', array($this, 'valid_entity_post_types'), 9); |
|
61 | 61 | add_filter( |
62 | 62 | 'wl_default_entity_types_for_post_type', |
63 | 63 | array( |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | private function save_options() { |
94 | 94 | |
95 | - update_option( 'wl_mappings', $this->options, true ); |
|
95 | + update_option('wl_mappings', $this->options, true); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
@@ -104,9 +104,9 @@ discard block |
||
104 | 104 | * @param string $post_type Post type. |
105 | 105 | * @param array $entity_types An array of entity types slugs. |
106 | 106 | */ |
107 | - public function set_entity_types_for_post_type( $post_type, $entity_types ) { |
|
107 | + public function set_entity_types_for_post_type($post_type, $entity_types) { |
|
108 | 108 | |
109 | - $this->options[ $post_type ] = $entity_types; |
|
109 | + $this->options[$post_type] = $entity_types; |
|
110 | 110 | $this->save_options(); |
111 | 111 | |
112 | 112 | } |
@@ -120,9 +120,9 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @return array The supported post types. |
122 | 122 | */ |
123 | - public function valid_entity_post_types( $post_types ) { |
|
123 | + public function valid_entity_post_types($post_types) { |
|
124 | 124 | |
125 | - return array_merge( $post_types, array_keys( $this->options ) ); |
|
125 | + return array_merge($post_types, array_keys($this->options)); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return array The default entity types. |
137 | 137 | */ |
138 | - public function default_entity_types_for_post_type( $default, $post_type ) { |
|
138 | + public function default_entity_types_for_post_type($default, $post_type) { |
|
139 | 139 | |
140 | - return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default; |
|
140 | + return isset($this->options[$post_type]) ? $this->options[$post_type] : $default; |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | /** |
@@ -157,18 +157,18 @@ discard block |
||
157 | 157 | * @type int $count The total element count. |
158 | 158 | * } |
159 | 159 | */ |
160 | - public function update( $post_type, $entity_types, $offset = 0 ) { |
|
160 | + public function update($post_type, $entity_types, $offset = 0) { |
|
161 | 161 | |
162 | 162 | $entity_type_service = $this->entity_type_service; |
163 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
163 | + $tax_query = $this->get_tax_query($entity_types); |
|
164 | 164 | |
165 | 165 | return Wordlift_Batch_Action::process( |
166 | 166 | $post_type, |
167 | 167 | $offset, |
168 | 168 | $tax_query, |
169 | - function ( $post_id ) use ( $entity_type_service, $entity_types ) { |
|
170 | - foreach ( $entity_types as $entity_type ) { |
|
171 | - $entity_type_service->set( $post_id, $entity_type, false ); |
|
169 | + function($post_id) use ($entity_type_service, $entity_types) { |
|
170 | + foreach ($entity_types as $entity_type) { |
|
171 | + $entity_type_service->set($post_id, $entity_type, false); |
|
172 | 172 | } |
173 | 173 | } |
174 | 174 | ); |
@@ -184,11 +184,11 @@ discard block |
||
184 | 184 | * |
185 | 185 | * @return int The number of posts to be assigned with entity types. |
186 | 186 | */ |
187 | - public function count( $post_type, $entity_types ) { |
|
187 | + public function count($post_type, $entity_types) { |
|
188 | 188 | |
189 | - $tax_query = $this->get_tax_query( $entity_types ); |
|
189 | + $tax_query = $this->get_tax_query($entity_types); |
|
190 | 190 | |
191 | - return Wordlift_Batch_Action::count( $post_type, $tax_query ); |
|
191 | + return Wordlift_Batch_Action::count($post_type, $tax_query); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -200,20 +200,20 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @return array The tax query. |
202 | 202 | */ |
203 | - private function get_tax_query( $entity_types ) { |
|
203 | + private function get_tax_query($entity_types) { |
|
204 | 204 | |
205 | 205 | $entity_type_service = $this->entity_type_service; |
206 | 206 | $entity_types_terms = array_filter( |
207 | 207 | array_map( |
208 | - function ( $item ) use ( $entity_type_service ) { |
|
209 | - return $entity_type_service->get_term_by_uri( $item ); |
|
208 | + function($item) use ($entity_type_service) { |
|
209 | + return $entity_type_service->get_term_by_uri($item); |
|
210 | 210 | }, |
211 | 211 | $entity_types |
212 | 212 | ) |
213 | 213 | ); |
214 | 214 | |
215 | 215 | $entity_types_terms_ids = array_map( |
216 | - function ( $term ) { |
|
216 | + function($term) { |
|
217 | 217 | return $term->term_id; |
218 | 218 | }, |
219 | 219 | $entity_types_terms |
@@ -19,258 +19,258 @@ |
||
19 | 19 | */ |
20 | 20 | class Wordlift_Post_Adapter { |
21 | 21 | |
22 | - /** |
|
23 | - * The post id to which the adopter relates. |
|
24 | - * |
|
25 | - * @since 3.14.0 |
|
26 | - * |
|
27 | - * @var integer $post_id . |
|
28 | - */ |
|
29 | - private $post_id; |
|
30 | - |
|
31 | - const TYPE_ENTITY_LINK = 0; |
|
32 | - const TYPE_TERM_LINK = 1; |
|
33 | - |
|
34 | - /** |
|
35 | - * Create the {@link Wordlift_Post_Adatpter} instance. |
|
36 | - * |
|
37 | - * @param integer $post_id the post ID of the post the adopter relates to. |
|
38 | - * |
|
39 | - * @since 3.14.0 |
|
40 | - */ |
|
41 | - public function __construct( $post_id ) { |
|
42 | - |
|
43 | - $this->post_id = $post_id; |
|
44 | - |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Get the word count of the post content. |
|
49 | - * |
|
50 | - * The count is calculated over the post content after stripping shortcodes and html tags. |
|
51 | - * |
|
52 | - * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
53 | - * @since 3.14.0 |
|
54 | - */ |
|
55 | - public function word_count() { |
|
56 | - |
|
57 | - $post = get_post( $this->post_id ); |
|
58 | - |
|
59 | - /* |
|
22 | + /** |
|
23 | + * The post id to which the adopter relates. |
|
24 | + * |
|
25 | + * @since 3.14.0 |
|
26 | + * |
|
27 | + * @var integer $post_id . |
|
28 | + */ |
|
29 | + private $post_id; |
|
30 | + |
|
31 | + const TYPE_ENTITY_LINK = 0; |
|
32 | + const TYPE_TERM_LINK = 1; |
|
33 | + |
|
34 | + /** |
|
35 | + * Create the {@link Wordlift_Post_Adatpter} instance. |
|
36 | + * |
|
37 | + * @param integer $post_id the post ID of the post the adopter relates to. |
|
38 | + * |
|
39 | + * @since 3.14.0 |
|
40 | + */ |
|
41 | + public function __construct( $post_id ) { |
|
42 | + |
|
43 | + $this->post_id = $post_id; |
|
44 | + |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Get the word count of the post content. |
|
49 | + * |
|
50 | + * The count is calculated over the post content after stripping shortcodes and html tags. |
|
51 | + * |
|
52 | + * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
53 | + * @since 3.14.0 |
|
54 | + */ |
|
55 | + public function word_count() { |
|
56 | + |
|
57 | + $post = get_post( $this->post_id ); |
|
58 | + |
|
59 | + /* |
|
60 | 60 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
61 | 61 | * because the content is written elsewhere. |
62 | 62 | * |
63 | 63 | * @since 3.20.0 |
64 | 64 | */ |
65 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
66 | - |
|
67 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Count words in the string, taking into account UTF-8 characters. |
|
72 | - * |
|
73 | - * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
74 | - * |
|
75 | - * @since 3.20.0 |
|
76 | - * |
|
77 | - * @param string $str The target string. |
|
78 | - * |
|
79 | - * @return int The number of words. |
|
80 | - */ |
|
81 | - private static function str_word_count_utf8( $str ) { |
|
82 | - |
|
83 | - $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
84 | - |
|
85 | - return $words ? count( $words ) : 0; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
90 | - * |
|
91 | - * @param int $post_id Post ID. |
|
92 | - * |
|
93 | - * @return string The post permalink. |
|
94 | - * @since 3.20.0 |
|
95 | - */ |
|
96 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
97 | - |
|
98 | - $object_link_service = Object_Link_Provider::get_instance(); |
|
99 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
100 | - |
|
101 | - /** |
|
102 | - * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
103 | - * |
|
104 | - * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
105 | - * post permalink if we find them. |
|
106 | - */ |
|
107 | - global $wp_version; |
|
108 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
114 | - * when the production environment is copied over from a staging environment with staging |
|
115 | - * URLs. |
|
116 | - * |
|
117 | - * @param string $permalink_url The default permalink. |
|
118 | - * @param int $post_id The post id. |
|
119 | - * |
|
120 | - * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
121 | - * @since 3.20.0 |
|
122 | - * |
|
123 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
124 | - */ |
|
125 | - |
|
126 | - $uris = $object_link_service->get_same_as_uris( |
|
127 | - $post_id, |
|
128 | - $object_type |
|
129 | - ); |
|
130 | - |
|
131 | - // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
132 | - $terms = array(); |
|
133 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
134 | - // Try to find one term matching the entity. |
|
135 | - $terms = get_terms( |
|
136 | - array( |
|
137 | - 'number' => 1, |
|
138 | - 'hide_empty' => false, |
|
139 | - 'update_term_meta_cache' => false, |
|
140 | - 'meta_key' => '_wl_entity_id', |
|
141 | - 'meta_value' => $uris, |
|
142 | - 'meta_compare' => 'IN', |
|
143 | - ) |
|
144 | - ); |
|
145 | - } |
|
146 | - |
|
147 | - $type = self::TYPE_ENTITY_LINK; |
|
148 | - $term = null; |
|
149 | - // If found use the term link, otherwise the permalink. |
|
150 | - if ( 1 === count( $terms ) ) { |
|
151 | - $term = current( $terms ); |
|
152 | - $permalink = get_term_link( $term ); |
|
153 | - $type = self::TYPE_TERM_LINK; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Apply the `wl_production_permalink` filter. |
|
158 | - * |
|
159 | - * @param string $permalink The permalink. |
|
160 | - * @param int $post_id The post id. |
|
161 | - * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
162 | - * @param WP_Term $term The term if type is term link, otherwise null. |
|
163 | - * |
|
164 | - * @since 3.23.0 add the permalink type and term parameters. |
|
165 | - */ |
|
166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get comma separated tags to be used as keywords |
|
171 | - * |
|
172 | - * @return string|void Comma separated tags |
|
173 | - * |
|
174 | - * @since 3.27.2 |
|
175 | - */ |
|
176 | - public function keywords() { |
|
177 | - $tags = get_the_tags( $this->post_id ); |
|
178 | - |
|
179 | - if ( empty( $tags ) ) { |
|
180 | - return; |
|
181 | - } |
|
182 | - |
|
183 | - return implode( |
|
184 | - ',', |
|
185 | - array_map( |
|
186 | - function ( $tag ) { |
|
187 | - return $tag->name; |
|
188 | - }, |
|
189 | - $tags |
|
190 | - ) |
|
191 | - ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get comma separated categories to be used as article section |
|
196 | - * |
|
197 | - * @return string[] Comma separated categories |
|
198 | - * |
|
199 | - * @since 3.27.2 |
|
200 | - */ |
|
201 | - public function article_section() { |
|
202 | - $categories = get_the_category( $this->post_id ); |
|
203 | - |
|
204 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Get comment count |
|
209 | - * |
|
210 | - * @return string|void Comment count |
|
211 | - * |
|
212 | - * @since 3.27.2 |
|
213 | - */ |
|
214 | - public function comment_count() { |
|
215 | - return get_comments_number( $this->post_id ); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Get Language |
|
220 | - * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
221 | - * |
|
222 | - * @return string|void Language code (locale) |
|
223 | - * |
|
224 | - * @since 3.27.2 |
|
225 | - */ |
|
226 | - public function locale() { |
|
227 | - $language = 'en-US'; |
|
228 | - |
|
229 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
230 | - // WPML handling |
|
231 | - // WPML: Updated function signature. |
|
232 | - // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
233 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | - if ( ! $post_language instanceof WP_Error ) { |
|
235 | - $language = $post_language['locale']; |
|
236 | - } |
|
237 | - } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
238 | - // Polylang handling |
|
239 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
240 | - } else { |
|
241 | - $language = get_locale(); |
|
242 | - } |
|
243 | - |
|
244 | - return str_replace( '_', '-', $language ); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Add mentions to an article. |
|
249 | - * To add mentions we just push the referenced entities to mentions. |
|
250 | - * |
|
251 | - * @param $post_id int |
|
252 | - * @param $references int[] |
|
253 | - */ |
|
254 | - public function add_references( $post_id, &$references ) { |
|
255 | - $tags = get_the_tags( $post_id ); |
|
256 | - |
|
257 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
258 | - // Loop through the tags and push it to references. |
|
259 | - foreach ( $tags as $tag ) { |
|
260 | - /** |
|
261 | - * @var $tag WP_Term |
|
262 | - */ |
|
263 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | - foreach ( $entity_uris as $uri ) { |
|
265 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | - if ( $referenced_entity instanceof WP_Post ) { |
|
267 | - // push the referenced entities to references. |
|
268 | - $references[] = $referenced_entity->ID; |
|
269 | - } |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - } |
|
65 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
66 | + |
|
67 | + return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Count words in the string, taking into account UTF-8 characters. |
|
72 | + * |
|
73 | + * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
|
74 | + * |
|
75 | + * @since 3.20.0 |
|
76 | + * |
|
77 | + * @param string $str The target string. |
|
78 | + * |
|
79 | + * @return int The number of words. |
|
80 | + */ |
|
81 | + private static function str_word_count_utf8( $str ) { |
|
82 | + |
|
83 | + $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
84 | + |
|
85 | + return $words ? count( $words ) : 0; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
90 | + * |
|
91 | + * @param int $post_id Post ID. |
|
92 | + * |
|
93 | + * @return string The post permalink. |
|
94 | + * @since 3.20.0 |
|
95 | + */ |
|
96 | + public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
97 | + |
|
98 | + $object_link_service = Object_Link_Provider::get_instance(); |
|
99 | + $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
100 | + |
|
101 | + /** |
|
102 | + * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
|
103 | + * |
|
104 | + * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the |
|
105 | + * post permalink if we find them. |
|
106 | + */ |
|
107 | + global $wp_version; |
|
108 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
114 | + * when the production environment is copied over from a staging environment with staging |
|
115 | + * URLs. |
|
116 | + * |
|
117 | + * @param string $permalink_url The default permalink. |
|
118 | + * @param int $post_id The post id. |
|
119 | + * |
|
120 | + * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term. |
|
121 | + * @since 3.20.0 |
|
122 | + * |
|
123 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
124 | + */ |
|
125 | + |
|
126 | + $uris = $object_link_service->get_same_as_uris( |
|
127 | + $post_id, |
|
128 | + $object_type |
|
129 | + ); |
|
130 | + |
|
131 | + // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
|
132 | + $terms = array(); |
|
133 | + if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
134 | + // Try to find one term matching the entity. |
|
135 | + $terms = get_terms( |
|
136 | + array( |
|
137 | + 'number' => 1, |
|
138 | + 'hide_empty' => false, |
|
139 | + 'update_term_meta_cache' => false, |
|
140 | + 'meta_key' => '_wl_entity_id', |
|
141 | + 'meta_value' => $uris, |
|
142 | + 'meta_compare' => 'IN', |
|
143 | + ) |
|
144 | + ); |
|
145 | + } |
|
146 | + |
|
147 | + $type = self::TYPE_ENTITY_LINK; |
|
148 | + $term = null; |
|
149 | + // If found use the term link, otherwise the permalink. |
|
150 | + if ( 1 === count( $terms ) ) { |
|
151 | + $term = current( $terms ); |
|
152 | + $permalink = get_term_link( $term ); |
|
153 | + $type = self::TYPE_TERM_LINK; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Apply the `wl_production_permalink` filter. |
|
158 | + * |
|
159 | + * @param string $permalink The permalink. |
|
160 | + * @param int $post_id The post id. |
|
161 | + * @param int $type The permalink type: 0 = entity permalink, 1 = term link. |
|
162 | + * @param WP_Term $term The term if type is term link, otherwise null. |
|
163 | + * |
|
164 | + * @since 3.23.0 add the permalink type and term parameters. |
|
165 | + */ |
|
166 | + return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get comma separated tags to be used as keywords |
|
171 | + * |
|
172 | + * @return string|void Comma separated tags |
|
173 | + * |
|
174 | + * @since 3.27.2 |
|
175 | + */ |
|
176 | + public function keywords() { |
|
177 | + $tags = get_the_tags( $this->post_id ); |
|
178 | + |
|
179 | + if ( empty( $tags ) ) { |
|
180 | + return; |
|
181 | + } |
|
182 | + |
|
183 | + return implode( |
|
184 | + ',', |
|
185 | + array_map( |
|
186 | + function ( $tag ) { |
|
187 | + return $tag->name; |
|
188 | + }, |
|
189 | + $tags |
|
190 | + ) |
|
191 | + ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get comma separated categories to be used as article section |
|
196 | + * |
|
197 | + * @return string[] Comma separated categories |
|
198 | + * |
|
199 | + * @since 3.27.2 |
|
200 | + */ |
|
201 | + public function article_section() { |
|
202 | + $categories = get_the_category( $this->post_id ); |
|
203 | + |
|
204 | + return wp_list_pluck( $categories, 'cat_name' ); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Get comment count |
|
209 | + * |
|
210 | + * @return string|void Comment count |
|
211 | + * |
|
212 | + * @since 3.27.2 |
|
213 | + */ |
|
214 | + public function comment_count() { |
|
215 | + return get_comments_number( $this->post_id ); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Get Language |
|
220 | + * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
|
221 | + * |
|
222 | + * @return string|void Language code (locale) |
|
223 | + * |
|
224 | + * @since 3.27.2 |
|
225 | + */ |
|
226 | + public function locale() { |
|
227 | + $language = 'en-US'; |
|
228 | + |
|
229 | + if ( function_exists( 'wpml_get_language_information' ) ) { |
|
230 | + // WPML handling |
|
231 | + // WPML: Updated function signature. |
|
232 | + // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
|
233 | + $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | + if ( ! $post_language instanceof WP_Error ) { |
|
235 | + $language = $post_language['locale']; |
|
236 | + } |
|
237 | + } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
238 | + // Polylang handling |
|
239 | + $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
240 | + } else { |
|
241 | + $language = get_locale(); |
|
242 | + } |
|
243 | + |
|
244 | + return str_replace( '_', '-', $language ); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Add mentions to an article. |
|
249 | + * To add mentions we just push the referenced entities to mentions. |
|
250 | + * |
|
251 | + * @param $post_id int |
|
252 | + * @param $references int[] |
|
253 | + */ |
|
254 | + public function add_references( $post_id, &$references ) { |
|
255 | + $tags = get_the_tags( $post_id ); |
|
256 | + |
|
257 | + if ( $tags && ! is_wp_error( $tags ) ) { |
|
258 | + // Loop through the tags and push it to references. |
|
259 | + foreach ( $tags as $tag ) { |
|
260 | + /** |
|
261 | + * @var $tag WP_Term |
|
262 | + */ |
|
263 | + $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | + foreach ( $entity_uris as $uri ) { |
|
265 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | + if ( $referenced_entity instanceof WP_Post ) { |
|
267 | + // push the referenced entities to references. |
|
268 | + $references[] = $referenced_entity->ID; |
|
269 | + } |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + } |
|
275 | 275 | |
276 | 276 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @since 3.14.0 |
40 | 40 | */ |
41 | - public function __construct( $post_id ) { |
|
41 | + public function __construct($post_id) { |
|
42 | 42 | |
43 | 43 | $this->post_id = $post_id; |
44 | 44 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function word_count() { |
56 | 56 | |
57 | - $post = get_post( $this->post_id ); |
|
57 | + $post = get_post($this->post_id); |
|
58 | 58 | |
59 | 59 | /* |
60 | 60 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -62,9 +62,9 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @since 3.20.0 |
64 | 64 | */ |
65 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
65 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
66 | 66 | |
67 | - return self::str_word_count_utf8( wp_strip_all_tags( strip_shortcodes( $post_content ) ) ); |
|
67 | + return self::str_word_count_utf8(wp_strip_all_tags(strip_shortcodes($post_content))); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -78,11 +78,11 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return int The number of words. |
80 | 80 | */ |
81 | - private static function str_word_count_utf8( $str ) { |
|
81 | + private static function str_word_count_utf8($str) { |
|
82 | 82 | |
83 | - $words = preg_split( '~[^\p{L}\p{N}\']+~u', $str ); |
|
83 | + $words = preg_split('~[^\p{L}\p{N}\']+~u', $str); |
|
84 | 84 | |
85 | - return $words ? count( $words ) : 0; |
|
85 | + return $words ? count($words) : 0; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | * @return string The post permalink. |
94 | 94 | * @since 3.20.0 |
95 | 95 | */ |
96 | - public static function get_production_permalink( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
96 | + public static function get_production_permalink($post_id, $object_type = Object_Type_Enum::POST) { |
|
97 | 97 | |
98 | 98 | $object_link_service = Object_Link_Provider::get_instance(); |
99 | - $permalink = $object_link_service->get_permalink( $post_id, $object_type ); |
|
99 | + $permalink = $object_link_service->get_permalink($post_id, $object_type); |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here. |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | * post permalink if we find them. |
106 | 106 | */ |
107 | 107 | global $wp_version; |
108 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
109 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null ); |
|
108 | + if (version_compare($wp_version, '4.5', '<')) { |
|
109 | + return apply_filters('wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | |
131 | 131 | // Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled. |
132 | 132 | $terms = array(); |
133 | - if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) { |
|
133 | + if (defined('WL_ENABLE_TERM_LINKING') && WL_ENABLE_TERM_LINKING) { |
|
134 | 134 | // Try to find one term matching the entity. |
135 | 135 | $terms = get_terms( |
136 | 136 | array( |
@@ -147,9 +147,9 @@ discard block |
||
147 | 147 | $type = self::TYPE_ENTITY_LINK; |
148 | 148 | $term = null; |
149 | 149 | // If found use the term link, otherwise the permalink. |
150 | - if ( 1 === count( $terms ) ) { |
|
151 | - $term = current( $terms ); |
|
152 | - $permalink = get_term_link( $term ); |
|
150 | + if (1 === count($terms)) { |
|
151 | + $term = current($terms); |
|
152 | + $permalink = get_term_link($term); |
|
153 | 153 | $type = self::TYPE_TERM_LINK; |
154 | 154 | } |
155 | 155 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * |
164 | 164 | * @since 3.23.0 add the permalink type and term parameters. |
165 | 165 | */ |
166 | - return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term ); |
|
166 | + return apply_filters('wl_production_permalink', $permalink, $post_id, $type, $term); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -174,16 +174,16 @@ discard block |
||
174 | 174 | * @since 3.27.2 |
175 | 175 | */ |
176 | 176 | public function keywords() { |
177 | - $tags = get_the_tags( $this->post_id ); |
|
177 | + $tags = get_the_tags($this->post_id); |
|
178 | 178 | |
179 | - if ( empty( $tags ) ) { |
|
179 | + if (empty($tags)) { |
|
180 | 180 | return; |
181 | 181 | } |
182 | 182 | |
183 | 183 | return implode( |
184 | 184 | ',', |
185 | 185 | array_map( |
186 | - function ( $tag ) { |
|
186 | + function($tag) { |
|
187 | 187 | return $tag->name; |
188 | 188 | }, |
189 | 189 | $tags |
@@ -199,9 +199,9 @@ discard block |
||
199 | 199 | * @since 3.27.2 |
200 | 200 | */ |
201 | 201 | public function article_section() { |
202 | - $categories = get_the_category( $this->post_id ); |
|
202 | + $categories = get_the_category($this->post_id); |
|
203 | 203 | |
204 | - return wp_list_pluck( $categories, 'cat_name' ); |
|
204 | + return wp_list_pluck($categories, 'cat_name'); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | /** |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * @since 3.27.2 |
213 | 213 | */ |
214 | 214 | public function comment_count() { |
215 | - return get_comments_number( $this->post_id ); |
|
215 | + return get_comments_number($this->post_id); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -226,22 +226,22 @@ discard block |
||
226 | 226 | public function locale() { |
227 | 227 | $language = 'en-US'; |
228 | 228 | |
229 | - if ( function_exists( 'wpml_get_language_information' ) ) { |
|
229 | + if (function_exists('wpml_get_language_information')) { |
|
230 | 230 | // WPML handling |
231 | 231 | // WPML: Updated function signature. |
232 | 232 | // function wpml_get_language_information( $empty_value = null, $post_id = null ) |
233 | - $post_language = wpml_get_language_information( null, $this->post_id ); |
|
234 | - if ( ! $post_language instanceof WP_Error ) { |
|
233 | + $post_language = wpml_get_language_information(null, $this->post_id); |
|
234 | + if ( ! $post_language instanceof WP_Error) { |
|
235 | 235 | $language = $post_language['locale']; |
236 | 236 | } |
237 | - } elseif ( function_exists( 'pll_get_post_language' ) ) { |
|
237 | + } elseif (function_exists('pll_get_post_language')) { |
|
238 | 238 | // Polylang handling |
239 | - $language = pll_get_post_language( $this->post_id, 'locale' ); |
|
239 | + $language = pll_get_post_language($this->post_id, 'locale'); |
|
240 | 240 | } else { |
241 | 241 | $language = get_locale(); |
242 | 242 | } |
243 | 243 | |
244 | - return str_replace( '_', '-', $language ); |
|
244 | + return str_replace('_', '-', $language); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -251,19 +251,19 @@ discard block |
||
251 | 251 | * @param $post_id int |
252 | 252 | * @param $references int[] |
253 | 253 | */ |
254 | - public function add_references( $post_id, &$references ) { |
|
255 | - $tags = get_the_tags( $post_id ); |
|
254 | + public function add_references($post_id, &$references) { |
|
255 | + $tags = get_the_tags($post_id); |
|
256 | 256 | |
257 | - if ( $tags && ! is_wp_error( $tags ) ) { |
|
257 | + if ($tags && ! is_wp_error($tags)) { |
|
258 | 258 | // Loop through the tags and push it to references. |
259 | - foreach ( $tags as $tag ) { |
|
259 | + foreach ($tags as $tag) { |
|
260 | 260 | /** |
261 | 261 | * @var $tag WP_Term |
262 | 262 | */ |
263 | - $entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' ); |
|
264 | - foreach ( $entity_uris as $uri ) { |
|
265 | - $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri ); |
|
266 | - if ( $referenced_entity instanceof WP_Post ) { |
|
263 | + $entity_uris = get_term_meta($tag->term_id, '_wl_entity_id'); |
|
264 | + foreach ($entity_uris as $uri) { |
|
265 | + $referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity($uri); |
|
266 | + if ($referenced_entity instanceof WP_Post) { |
|
267 | 267 | // push the referenced entities to references. |
268 | 268 | $references[] = $referenced_entity->ID; |
269 | 269 | } |
@@ -20,100 +20,100 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class Wordlift_Entity_Uri_Service { |
22 | 22 | |
23 | - /** |
|
24 | - * A {@link Wordlift_Log_Service} instance. |
|
25 | - * |
|
26 | - * @since 3.16.3 |
|
27 | - * @access private |
|
28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | - */ |
|
30 | - private $log; |
|
31 | - |
|
32 | - /** |
|
33 | - * An array of URIs to post ID valid for the current request. |
|
34 | - * |
|
35 | - * @since 3.16.3 |
|
36 | - * @access private |
|
37 | - * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
38 | - */ |
|
39 | - protected $uri_to_post; |
|
40 | - |
|
41 | - /** |
|
42 | - * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
43 | - * |
|
44 | - * @since 3.16.3 |
|
45 | - */ |
|
46 | - protected function __construct() { |
|
47 | - |
|
48 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | - |
|
50 | - // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
51 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | - add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
53 | - |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
58 | - * |
|
59 | - * @since 3.21.5 |
|
60 | - * @access private |
|
61 | - * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
62 | - */ |
|
63 | - private static $instance = null; |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the singleton. |
|
67 | - * |
|
68 | - * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
69 | - * @since 3.21.5 |
|
70 | - */ |
|
71 | - public static function get_instance() { |
|
72 | - |
|
73 | - if ( ! isset( self::$instance ) ) { |
|
74 | - $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | - self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
76 | - |
|
77 | - } |
|
78 | - |
|
79 | - return self::$instance; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Try to find a post when the content service doesn't find it. |
|
84 | - * |
|
85 | - * @param WP_Post|null $post |
|
86 | - * @param string $uri |
|
87 | - * |
|
88 | - * @return false|int |
|
89 | - */ |
|
90 | - public function content_service__post__not_found( $post, $uri ) { |
|
91 | - return $this->get_post_id_from_url( $uri ); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Preload the provided URIs in the local cache. |
|
96 | - * |
|
97 | - * This function will populate the local `$uri_to_post` array by running a |
|
98 | - * single query with all the URIs and returning the mappings in the array. |
|
99 | - * |
|
100 | - * @param array $uris An array of URIs. |
|
101 | - * |
|
102 | - * @since 3.16.3 |
|
103 | - */ |
|
104 | - public function preload_uris( $uris ) { |
|
105 | - |
|
106 | - // Bail out if there are no URIs. |
|
107 | - if ( 0 === count( $uris ) ) { |
|
108 | - return; |
|
109 | - } |
|
110 | - |
|
111 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
112 | - |
|
113 | - global $wpdb; |
|
114 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
116 | - $sql = " |
|
23 | + /** |
|
24 | + * A {@link Wordlift_Log_Service} instance. |
|
25 | + * |
|
26 | + * @since 3.16.3 |
|
27 | + * @access private |
|
28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | + */ |
|
30 | + private $log; |
|
31 | + |
|
32 | + /** |
|
33 | + * An array of URIs to post ID valid for the current request. |
|
34 | + * |
|
35 | + * @since 3.16.3 |
|
36 | + * @access private |
|
37 | + * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
38 | + */ |
|
39 | + protected $uri_to_post; |
|
40 | + |
|
41 | + /** |
|
42 | + * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
43 | + * |
|
44 | + * @since 3.16.3 |
|
45 | + */ |
|
46 | + protected function __construct() { |
|
47 | + |
|
48 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | + |
|
50 | + // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
51 | + add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | + add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
53 | + |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
58 | + * |
|
59 | + * @since 3.21.5 |
|
60 | + * @access private |
|
61 | + * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
62 | + */ |
|
63 | + private static $instance = null; |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the singleton. |
|
67 | + * |
|
68 | + * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
69 | + * @since 3.21.5 |
|
70 | + */ |
|
71 | + public static function get_instance() { |
|
72 | + |
|
73 | + if ( ! isset( self::$instance ) ) { |
|
74 | + $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | + self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
76 | + |
|
77 | + } |
|
78 | + |
|
79 | + return self::$instance; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Try to find a post when the content service doesn't find it. |
|
84 | + * |
|
85 | + * @param WP_Post|null $post |
|
86 | + * @param string $uri |
|
87 | + * |
|
88 | + * @return false|int |
|
89 | + */ |
|
90 | + public function content_service__post__not_found( $post, $uri ) { |
|
91 | + return $this->get_post_id_from_url( $uri ); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Preload the provided URIs in the local cache. |
|
96 | + * |
|
97 | + * This function will populate the local `$uri_to_post` array by running a |
|
98 | + * single query with all the URIs and returning the mappings in the array. |
|
99 | + * |
|
100 | + * @param array $uris An array of URIs. |
|
101 | + * |
|
102 | + * @since 3.16.3 |
|
103 | + */ |
|
104 | + public function preload_uris( $uris ) { |
|
105 | + |
|
106 | + // Bail out if there are no URIs. |
|
107 | + if ( 0 === count( $uris ) ) { |
|
108 | + return; |
|
109 | + } |
|
110 | + |
|
111 | + $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
112 | + |
|
113 | + global $wpdb; |
|
114 | + $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | + $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
116 | + $sql = " |
|
117 | 117 | SELECT ID FROM $wpdb->posts p |
118 | 118 | INNER JOIN $wpdb->postmeta pm |
119 | 119 | ON pm.post_id = p.ID |
@@ -123,150 +123,150 @@ discard block |
||
123 | 123 | AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) |
124 | 124 | "; |
125 | 125 | |
126 | - // Get the posts. |
|
127 | - $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | - |
|
129 | - // Populate the array. We reinitialize the array on purpose because |
|
130 | - // we don't want these data to long live. |
|
131 | - $this->uri_to_post = array_reduce( |
|
132 | - $posts, |
|
133 | - function ( $carry, $item ) { |
|
134 | - $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
135 | - |
|
136 | - $uri = Wordpress_Content_Service::get_instance() |
|
137 | - ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
138 | - |
|
139 | - if ( isset( $uri ) ) { |
|
140 | - $uris[] = $uri; |
|
141 | - } |
|
142 | - |
|
143 | - return $carry |
|
144 | - // Get the URI related to the post and fill them with the item id. |
|
145 | - + array_fill_keys( $uris, $item ); |
|
146 | - }, |
|
147 | - array() |
|
148 | - ); |
|
149 | - |
|
150 | - // Add the not found URIs. |
|
151 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
152 | - |
|
153 | - $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
154 | - |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Reset the URI to post local cache. |
|
159 | - * |
|
160 | - * @since 3.16.3 |
|
161 | - */ |
|
162 | - public function reset_uris() { |
|
163 | - |
|
164 | - $this->uri_to_post = array(); |
|
165 | - |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
170 | - * |
|
171 | - * @param string $uri The entity URI. |
|
172 | - * |
|
173 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
174 | - * @since 3.2.0 |
|
175 | - */ |
|
176 | - public function get_entity( $uri ) { |
|
177 | - |
|
178 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
179 | - |
|
180 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
181 | - |
|
182 | - // Return null if the content isn't found or isn't a post. |
|
183 | - if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
184 | - return null; |
|
185 | - } |
|
186 | - |
|
187 | - return $content->get_bag(); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Determines whether a given uri is an internal uri or not. |
|
192 | - * |
|
193 | - * @param string $uri An uri. |
|
194 | - * |
|
195 | - * @return true if the uri internal to the current dataset otherwise false. |
|
196 | - * @since 3.16.3 |
|
197 | - */ |
|
198 | - public function is_internal( $uri ) { |
|
199 | - |
|
200 | - return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
205 | - * |
|
206 | - * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
207 | - * to provide full compatibility with WordPress 4.4+. |
|
208 | - * |
|
209 | - * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
210 | - * |
|
211 | - * @return WP_HTTP_Response The result to send to the client. |
|
212 | - * |
|
213 | - * @since 3.23.0 |
|
214 | - */ |
|
215 | - public function rest_post_dispatch( $result ) { |
|
216 | - |
|
217 | - // Get a reference to the actual data. |
|
218 | - $data = &$result->data; |
|
219 | - |
|
220 | - // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
221 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
223 | - return $result; |
|
224 | - } |
|
225 | - |
|
226 | - // Add the `wl:entity_url`. |
|
227 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
228 | - |
|
229 | - return $result; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Helper function to fetch post_id from a WordPress URL |
|
234 | - * Primarily used when dataset is not enabled |
|
235 | - * |
|
236 | - * @param $url |
|
237 | - * |
|
238 | - * @return int Post ID | bool false |
|
239 | - */ |
|
240 | - public function get_post_id_from_url( $url ) { |
|
241 | - |
|
242 | - // Try url_to_postid |
|
243 | - $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | - if ( 0 !== $post_id ) { |
|
245 | - return $post_id; |
|
246 | - } |
|
247 | - |
|
248 | - $parsed_url = wp_parse_url( $url ); |
|
249 | - |
|
250 | - if ( ! isset( $parsed_url['query'] ) ) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - parse_str( $parsed_url['query'], $parsed_query ); |
|
255 | - |
|
256 | - // Try to parse WooCommerce non-pretty product URL |
|
257 | - if ( $parsed_query['product'] ) { |
|
258 | - $posts = get_posts( |
|
259 | - array( |
|
260 | - 'name' => $parsed_query['product'], |
|
261 | - 'post_type' => 'product', |
|
262 | - ) |
|
263 | - ); |
|
264 | - if ( count( $posts ) > 0 ) { |
|
265 | - return $posts[0]->ID; |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - return false; |
|
270 | - } |
|
126 | + // Get the posts. |
|
127 | + $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | + |
|
129 | + // Populate the array. We reinitialize the array on purpose because |
|
130 | + // we don't want these data to long live. |
|
131 | + $this->uri_to_post = array_reduce( |
|
132 | + $posts, |
|
133 | + function ( $carry, $item ) { |
|
134 | + $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
135 | + |
|
136 | + $uri = Wordpress_Content_Service::get_instance() |
|
137 | + ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
138 | + |
|
139 | + if ( isset( $uri ) ) { |
|
140 | + $uris[] = $uri; |
|
141 | + } |
|
142 | + |
|
143 | + return $carry |
|
144 | + // Get the URI related to the post and fill them with the item id. |
|
145 | + + array_fill_keys( $uris, $item ); |
|
146 | + }, |
|
147 | + array() |
|
148 | + ); |
|
149 | + |
|
150 | + // Add the not found URIs. |
|
151 | + $this->uri_to_post += array_fill_keys( $uris, null ); |
|
152 | + |
|
153 | + $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
154 | + |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Reset the URI to post local cache. |
|
159 | + * |
|
160 | + * @since 3.16.3 |
|
161 | + */ |
|
162 | + public function reset_uris() { |
|
163 | + |
|
164 | + $this->uri_to_post = array(); |
|
165 | + |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
170 | + * |
|
171 | + * @param string $uri The entity URI. |
|
172 | + * |
|
173 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
174 | + * @since 3.2.0 |
|
175 | + */ |
|
176 | + public function get_entity( $uri ) { |
|
177 | + |
|
178 | + $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
179 | + |
|
180 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
181 | + |
|
182 | + // Return null if the content isn't found or isn't a post. |
|
183 | + if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
184 | + return null; |
|
185 | + } |
|
186 | + |
|
187 | + return $content->get_bag(); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Determines whether a given uri is an internal uri or not. |
|
192 | + * |
|
193 | + * @param string $uri An uri. |
|
194 | + * |
|
195 | + * @return true if the uri internal to the current dataset otherwise false. |
|
196 | + * @since 3.16.3 |
|
197 | + */ |
|
198 | + public function is_internal( $uri ) { |
|
199 | + |
|
200 | + return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
205 | + * |
|
206 | + * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
207 | + * to provide full compatibility with WordPress 4.4+. |
|
208 | + * |
|
209 | + * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
210 | + * |
|
211 | + * @return WP_HTTP_Response The result to send to the client. |
|
212 | + * |
|
213 | + * @since 3.23.0 |
|
214 | + */ |
|
215 | + public function rest_post_dispatch( $result ) { |
|
216 | + |
|
217 | + // Get a reference to the actual data. |
|
218 | + $data = &$result->data; |
|
219 | + |
|
220 | + // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
221 | + if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
223 | + return $result; |
|
224 | + } |
|
225 | + |
|
226 | + // Add the `wl:entity_url`. |
|
227 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
228 | + |
|
229 | + return $result; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Helper function to fetch post_id from a WordPress URL |
|
234 | + * Primarily used when dataset is not enabled |
|
235 | + * |
|
236 | + * @param $url |
|
237 | + * |
|
238 | + * @return int Post ID | bool false |
|
239 | + */ |
|
240 | + public function get_post_id_from_url( $url ) { |
|
241 | + |
|
242 | + // Try url_to_postid |
|
243 | + $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | + if ( 0 !== $post_id ) { |
|
245 | + return $post_id; |
|
246 | + } |
|
247 | + |
|
248 | + $parsed_url = wp_parse_url( $url ); |
|
249 | + |
|
250 | + if ( ! isset( $parsed_url['query'] ) ) { |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + parse_str( $parsed_url['query'], $parsed_query ); |
|
255 | + |
|
256 | + // Try to parse WooCommerce non-pretty product URL |
|
257 | + if ( $parsed_query['product'] ) { |
|
258 | + $posts = get_posts( |
|
259 | + array( |
|
260 | + 'name' => $parsed_query['product'], |
|
261 | + 'post_type' => 'product', |
|
262 | + ) |
|
263 | + ); |
|
264 | + if ( count( $posts ) > 0 ) { |
|
265 | + return $posts[0]->ID; |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + return false; |
|
270 | + } |
|
271 | 271 | |
272 | 272 | } |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | */ |
46 | 46 | protected function __construct() { |
47 | 47 | |
48 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
48 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
49 | 49 | |
50 | 50 | // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
51 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
52 | - add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 ); |
|
51 | + add_filter('rest_post_dispatch', array($this, 'rest_post_dispatch')); |
|
52 | + add_filter('wl_content_service__post__not_found', array($this, 'content_service__post__not_found'), 10, 2); |
|
53 | 53 | |
54 | 54 | } |
55 | 55 | |
@@ -70,9 +70,9 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public static function get_instance() { |
72 | 72 | |
73 | - if ( ! isset( self::$instance ) ) { |
|
74 | - $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' ); |
|
75 | - self::$instance = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service ); |
|
73 | + if ( ! isset(self::$instance)) { |
|
74 | + $entity_uri_cache_service = new Wordlift_File_Cache_Service(WL_TEMP_DIR.'entity_uri/'); |
|
75 | + self::$instance = new Wordlift_Cached_Entity_Uri_Service($entity_uri_cache_service); |
|
76 | 76 | |
77 | 77 | } |
78 | 78 | |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @return false|int |
89 | 89 | */ |
90 | - public function content_service__post__not_found( $post, $uri ) { |
|
91 | - return $this->get_post_id_from_url( $uri ); |
|
90 | + public function content_service__post__not_found($post, $uri) { |
|
91 | + return $this->get_post_id_from_url($uri); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -101,18 +101,18 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @since 3.16.3 |
103 | 103 | */ |
104 | - public function preload_uris( $uris ) { |
|
104 | + public function preload_uris($uris) { |
|
105 | 105 | |
106 | 106 | // Bail out if there are no URIs. |
107 | - if ( 0 === count( $uris ) ) { |
|
107 | + if (0 === count($uris)) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
111 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
111 | + $this->log->trace('Preloading '.count($uris).' URI(s)...'); |
|
112 | 112 | |
113 | 113 | global $wpdb; |
114 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
115 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
114 | + $in_post_types = implode("','", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types())); |
|
115 | + $in_entity_uris = implode("','", array_map('esc_sql', $uris)); |
|
116 | 116 | $sql = " |
117 | 117 | SELECT ID FROM $wpdb->posts p |
118 | 118 | INNER JOIN $wpdb->postmeta pm |
@@ -124,33 +124,33 @@ discard block |
||
124 | 124 | "; |
125 | 125 | |
126 | 126 | // Get the posts. |
127 | - $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
127 | + $posts = $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
128 | 128 | |
129 | 129 | // Populate the array. We reinitialize the array on purpose because |
130 | 130 | // we don't want these data to long live. |
131 | 131 | $this->uri_to_post = array_reduce( |
132 | 132 | $posts, |
133 | - function ( $carry, $item ) { |
|
134 | - $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
133 | + function($carry, $item) { |
|
134 | + $uris = get_post_meta($item, Wordlift_Schema_Service::FIELD_SAME_AS); |
|
135 | 135 | |
136 | 136 | $uri = Wordpress_Content_Service::get_instance() |
137 | - ->get_entity_id( Wordpress_Content_Id::create_post( $item ) ); |
|
137 | + ->get_entity_id(Wordpress_Content_Id::create_post($item)); |
|
138 | 138 | |
139 | - if ( isset( $uri ) ) { |
|
139 | + if (isset($uri)) { |
|
140 | 140 | $uris[] = $uri; |
141 | 141 | } |
142 | 142 | |
143 | 143 | return $carry |
144 | 144 | // Get the URI related to the post and fill them with the item id. |
145 | - + array_fill_keys( $uris, $item ); |
|
145 | + + array_fill_keys($uris, $item); |
|
146 | 146 | }, |
147 | 147 | array() |
148 | 148 | ); |
149 | 149 | |
150 | 150 | // Add the not found URIs. |
151 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
151 | + $this->uri_to_post += array_fill_keys($uris, null); |
|
152 | 152 | |
153 | - $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' ); |
|
153 | + $this->log->debug(count($this->uri_to_post).' URI(s) preloaded.'); |
|
154 | 154 | |
155 | 155 | } |
156 | 156 | |
@@ -173,14 +173,14 @@ discard block |
||
173 | 173 | * @return WP_Post|null A WP_Post instance or null if not found. |
174 | 174 | * @since 3.2.0 |
175 | 175 | */ |
176 | - public function get_entity( $uri ) { |
|
176 | + public function get_entity($uri) { |
|
177 | 177 | |
178 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
178 | + $this->log->trace("Getting an entity post for URI $uri..."); |
|
179 | 179 | |
180 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
180 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri); |
|
181 | 181 | |
182 | 182 | // Return null if the content isn't found or isn't a post. |
183 | - if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) { |
|
183 | + if ( ! isset($content) || ! is_a($content->get_bag(), '\WP_Post')) { |
|
184 | 184 | return null; |
185 | 185 | } |
186 | 186 | |
@@ -195,9 +195,9 @@ discard block |
||
195 | 195 | * @return true if the uri internal to the current dataset otherwise false. |
196 | 196 | * @since 3.16.3 |
197 | 197 | */ |
198 | - public function is_internal( $uri ) { |
|
198 | + public function is_internal($uri) { |
|
199 | 199 | |
200 | - return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) ); |
|
200 | + return (0 === strrpos($uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri())); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |
@@ -212,19 +212,19 @@ discard block |
||
212 | 212 | * |
213 | 213 | * @since 3.23.0 |
214 | 214 | */ |
215 | - public function rest_post_dispatch( $result ) { |
|
215 | + public function rest_post_dispatch($result) { |
|
216 | 216 | |
217 | 217 | // Get a reference to the actual data. |
218 | 218 | $data = &$result->data; |
219 | 219 | |
220 | 220 | // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
221 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
222 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
221 | + if ( ! is_array($data) || ! isset($data['id']) || ! isset($data['type']) |
|
222 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($data['type'])) { |
|
223 | 223 | return $result; |
224 | 224 | } |
225 | 225 | |
226 | 226 | // Add the `wl:entity_url`. |
227 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
227 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri($data['id']); |
|
228 | 228 | |
229 | 229 | return $result; |
230 | 230 | } |
@@ -237,31 +237,31 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return int Post ID | bool false |
239 | 239 | */ |
240 | - public function get_post_id_from_url( $url ) { |
|
240 | + public function get_post_id_from_url($url) { |
|
241 | 241 | |
242 | 242 | // Try url_to_postid |
243 | - $post_id = url_to_postid( htmlspecialchars_decode( $url ) ); |
|
244 | - if ( 0 !== $post_id ) { |
|
243 | + $post_id = url_to_postid(htmlspecialchars_decode($url)); |
|
244 | + if (0 !== $post_id) { |
|
245 | 245 | return $post_id; |
246 | 246 | } |
247 | 247 | |
248 | - $parsed_url = wp_parse_url( $url ); |
|
248 | + $parsed_url = wp_parse_url($url); |
|
249 | 249 | |
250 | - if ( ! isset( $parsed_url['query'] ) ) { |
|
250 | + if ( ! isset($parsed_url['query'])) { |
|
251 | 251 | return false; |
252 | 252 | } |
253 | 253 | |
254 | - parse_str( $parsed_url['query'], $parsed_query ); |
|
254 | + parse_str($parsed_url['query'], $parsed_query); |
|
255 | 255 | |
256 | 256 | // Try to parse WooCommerce non-pretty product URL |
257 | - if ( $parsed_query['product'] ) { |
|
257 | + if ($parsed_query['product']) { |
|
258 | 258 | $posts = get_posts( |
259 | 259 | array( |
260 | 260 | 'name' => $parsed_query['product'], |
261 | 261 | 'post_type' => 'product', |
262 | 262 | ) |
263 | 263 | ); |
264 | - if ( count( $posts ) > 0 ) { |
|
264 | + if (count($posts) > 0) { |
|
265 | 265 | return $posts[0]->ID; |
266 | 266 | } |
267 | 267 | } |
@@ -7,148 +7,148 @@ |
||
7 | 7 | */ |
8 | 8 | class Wordlift_Log_Service { |
9 | 9 | |
10 | - const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
11 | - |
|
12 | - const ERROR = 4; |
|
13 | - const WARN = 3; |
|
14 | - const INFO = 2; |
|
15 | - const DEBUG = 1; |
|
16 | - const TRACE = 0; |
|
17 | - |
|
18 | - /** |
|
19 | - * The class related to the logs. |
|
20 | - * |
|
21 | - * @since 1.0.0 |
|
22 | - * @access private |
|
23 | - * @var string $class_name The class related to the logs. |
|
24 | - */ |
|
25 | - private $class_name; |
|
26 | - |
|
27 | - /** |
|
28 | - * The log levels for printing in log lines. |
|
29 | - * |
|
30 | - * @var array $levels An array of log levels. |
|
31 | - */ |
|
32 | - private static $levels = array( |
|
33 | - self::TRACE => 'TRACE', |
|
34 | - self::DEBUG => 'DEBUG', |
|
35 | - self::INFO => 'INFO', |
|
36 | - self::WARN => 'WARN', |
|
37 | - self::ERROR => 'ERROR', |
|
38 | - ); |
|
39 | - |
|
40 | - /** |
|
41 | - * A singleton instance for legacy logging. |
|
42 | - * |
|
43 | - * @since 3.10.0 |
|
44 | - * @access private |
|
45 | - * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
46 | - */ |
|
47 | - private static $instance = null; |
|
10 | + const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
11 | + |
|
12 | + const ERROR = 4; |
|
13 | + const WARN = 3; |
|
14 | + const INFO = 2; |
|
15 | + const DEBUG = 1; |
|
16 | + const TRACE = 0; |
|
17 | + |
|
18 | + /** |
|
19 | + * The class related to the logs. |
|
20 | + * |
|
21 | + * @since 1.0.0 |
|
22 | + * @access private |
|
23 | + * @var string $class_name The class related to the logs. |
|
24 | + */ |
|
25 | + private $class_name; |
|
26 | + |
|
27 | + /** |
|
28 | + * The log levels for printing in log lines. |
|
29 | + * |
|
30 | + * @var array $levels An array of log levels. |
|
31 | + */ |
|
32 | + private static $levels = array( |
|
33 | + self::TRACE => 'TRACE', |
|
34 | + self::DEBUG => 'DEBUG', |
|
35 | + self::INFO => 'INFO', |
|
36 | + self::WARN => 'WARN', |
|
37 | + self::ERROR => 'ERROR', |
|
38 | + ); |
|
39 | + |
|
40 | + /** |
|
41 | + * A singleton instance for legacy logging. |
|
42 | + * |
|
43 | + * @since 3.10.0 |
|
44 | + * @access private |
|
45 | + * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
46 | + */ |
|
47 | + private static $instance = null; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Create an instance of the Log service. |
|
51 | - * |
|
52 | - * @param string $class_name The class related to the logs. |
|
53 | - * |
|
54 | - * @since 1.0.0 |
|
55 | - */ |
|
56 | - public function __construct( $class_name ) { |
|
49 | + /** |
|
50 | + * Create an instance of the Log service. |
|
51 | + * |
|
52 | + * @param string $class_name The class related to the logs. |
|
53 | + * |
|
54 | + * @since 1.0.0 |
|
55 | + */ |
|
56 | + public function __construct( $class_name ) { |
|
57 | 57 | |
58 | - $this->class_name = $class_name; |
|
58 | + $this->class_name = $class_name; |
|
59 | 59 | |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get the ROOT logger. |
|
64 | - * |
|
65 | - * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
66 | - * @since 3.10.0 |
|
67 | - */ |
|
68 | - public static function get_instance() { |
|
62 | + /** |
|
63 | + * Get the ROOT logger. |
|
64 | + * |
|
65 | + * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
66 | + * @since 3.10.0 |
|
67 | + */ |
|
68 | + public static function get_instance() { |
|
69 | 69 | |
70 | - if ( ! isset( self::$instance ) ) { |
|
71 | - self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
72 | - } |
|
70 | + if ( ! isset( self::$instance ) ) { |
|
71 | + self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
72 | + } |
|
73 | 73 | |
74 | - return self::$instance; |
|
75 | - } |
|
74 | + return self::$instance; |
|
75 | + } |
|
76 | 76 | |
77 | - public static function get_logger( $class_name ) { |
|
77 | + public static function get_logger( $class_name ) { |
|
78 | 78 | |
79 | - return new Wordlift_Log_Service( $class_name ); |
|
79 | + return new Wordlift_Log_Service( $class_name ); |
|
80 | 80 | |
81 | - } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Log a message. |
|
85 | - * |
|
86 | - * @param string $level The log level. |
|
87 | - * @param string $message The message to log. |
|
88 | - * |
|
89 | - * @since 1.0.0 |
|
90 | - */ |
|
91 | - public function log( $level, $message ) { |
|
83 | + /** |
|
84 | + * Log a message. |
|
85 | + * |
|
86 | + * @param string $level The log level. |
|
87 | + * @param string $message The message to log. |
|
88 | + * |
|
89 | + * @since 1.0.0 |
|
90 | + */ |
|
91 | + public function log( $level, $message ) { |
|
92 | 92 | |
93 | - // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
93 | + // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
94 | 94 | |
95 | - // Bail out if `WL_DEBUG` isn't defined or it's false. |
|
96 | - if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
97 | - return; |
|
98 | - } |
|
95 | + // Bail out if `WL_DEBUG` isn't defined or it's false. |
|
96 | + if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
97 | + return; |
|
98 | + } |
|
99 | 99 | |
100 | - // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
101 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
103 | - return; |
|
104 | - } |
|
100 | + // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
|
101 | + if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | + && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
103 | + return; |
|
104 | + } |
|
105 | 105 | |
106 | - // Bail out if the log message is below the minimum log level. |
|
107 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
108 | - return; |
|
109 | - } |
|
106 | + // Bail out if the log message is below the minimum log level. |
|
107 | + if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
108 | + return; |
|
109 | + } |
|
110 | 110 | |
111 | - // Bail out if there's a filter and we don't match it. |
|
112 | - $class_name = wp_slash( $this->class_name ); |
|
113 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
114 | - return; |
|
115 | - } |
|
111 | + // Bail out if there's a filter and we don't match it. |
|
112 | + $class_name = wp_slash( $this->class_name ); |
|
113 | + if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
114 | + return; |
|
115 | + } |
|
116 | 116 | |
117 | - // Finally log the message. |
|
118 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
119 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
117 | + // Finally log the message. |
|
118 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
119 | + error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
120 | 120 | |
121 | - } |
|
121 | + } |
|
122 | 122 | |
123 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
124 | - public function error( $message, $exception = null ) { |
|
123 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
124 | + public function error( $message, $exception = null ) { |
|
125 | 125 | |
126 | - $this->log( self::ERROR, $message ); |
|
126 | + $this->log( self::ERROR, $message ); |
|
127 | 127 | |
128 | - } |
|
128 | + } |
|
129 | 129 | |
130 | - public function warn( $message ) { |
|
130 | + public function warn( $message ) { |
|
131 | 131 | |
132 | - $this->log( self::WARN, $message ); |
|
132 | + $this->log( self::WARN, $message ); |
|
133 | 133 | |
134 | - } |
|
134 | + } |
|
135 | 135 | |
136 | - public function info( $message ) { |
|
136 | + public function info( $message ) { |
|
137 | 137 | |
138 | - $this->log( self::INFO, $message ); |
|
138 | + $this->log( self::INFO, $message ); |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - public function debug( $message ) { |
|
142 | + public function debug( $message ) { |
|
143 | 143 | |
144 | - $this->log( self::DEBUG, $message ); |
|
144 | + $this->log( self::DEBUG, $message ); |
|
145 | 145 | |
146 | - } |
|
146 | + } |
|
147 | 147 | |
148 | - public function trace( $message ) { |
|
148 | + public function trace( $message ) { |
|
149 | 149 | |
150 | - $this->log( self::TRACE, $message ); |
|
150 | + $this->log( self::TRACE, $message ); |
|
151 | 151 | |
152 | - } |
|
152 | + } |
|
153 | 153 | |
154 | 154 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @since 1.0.0 |
55 | 55 | */ |
56 | - public function __construct( $class_name ) { |
|
56 | + public function __construct($class_name) { |
|
57 | 57 | |
58 | 58 | $this->class_name = $class_name; |
59 | 59 | |
@@ -67,16 +67,16 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public static function get_instance() { |
69 | 69 | |
70 | - if ( ! isset( self::$instance ) ) { |
|
71 | - self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
70 | + if ( ! isset(self::$instance)) { |
|
71 | + self::$instance = new Wordlift_Log_Service('ROOT'); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | return self::$instance; |
75 | 75 | } |
76 | 76 | |
77 | - public static function get_logger( $class_name ) { |
|
77 | + public static function get_logger($class_name) { |
|
78 | 78 | |
79 | - return new Wordlift_Log_Service( $class_name ); |
|
79 | + return new Wordlift_Log_Service($class_name); |
|
80 | 80 | |
81 | 81 | } |
82 | 82 | |
@@ -88,66 +88,66 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @since 1.0.0 |
90 | 90 | */ |
91 | - public function log( $level, $message ) { |
|
91 | + public function log($level, $message) { |
|
92 | 92 | |
93 | 93 | // echo( sprintf( self::MESSAGE_TEMPLATE . "\n", self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
94 | 94 | |
95 | 95 | // Bail out if `WL_DEBUG` isn't defined or it's false. |
96 | - if ( ! defined( 'WL_DEBUG' ) || false === WL_DEBUG ) { |
|
96 | + if ( ! defined('WL_DEBUG') || false === WL_DEBUG) { |
|
97 | 97 | return; |
98 | 98 | } |
99 | 99 | |
100 | 100 | // Bail out if WordLift log level isn't defined, and WP debug is disabled. |
101 | - if ( ! defined( 'WL_LOG_LEVEL' ) && $level < self::ERROR |
|
102 | - && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
101 | + if ( ! defined('WL_LOG_LEVEL') && $level < self::ERROR |
|
102 | + && ( ! defined('WP_DEBUG') || false === WP_DEBUG)) { |
|
103 | 103 | return; |
104 | 104 | } |
105 | 105 | |
106 | 106 | // Bail out if the log message is below the minimum log level. |
107 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
107 | + if (defined('WL_LOG_LEVEL') && $level < intval(WL_LOG_LEVEL)) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
111 | 111 | // Bail out if there's a filter and we don't match it. |
112 | - $class_name = wp_slash( $this->class_name ); |
|
113 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$class_name($|,)/", WL_LOG_FILTER ) ) { |
|
112 | + $class_name = wp_slash($this->class_name); |
|
113 | + if (defined('WL_LOG_FILTER') && 1 !== preg_match("/(^|,)$class_name($|,)/", WL_LOG_FILTER)) { |
|
114 | 114 | return; |
115 | 115 | } |
116 | 116 | |
117 | 117 | // Finally log the message. |
118 | 118 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
119 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
119 | + error_log(sprintf(self::MESSAGE_TEMPLATE, self::$levels[$level], $this->class_name, is_array($message) ? implode(', ', $message) : $message)); |
|
120 | 120 | |
121 | 121 | } |
122 | 122 | |
123 | 123 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
124 | - public function error( $message, $exception = null ) { |
|
124 | + public function error($message, $exception = null) { |
|
125 | 125 | |
126 | - $this->log( self::ERROR, $message ); |
|
126 | + $this->log(self::ERROR, $message); |
|
127 | 127 | |
128 | 128 | } |
129 | 129 | |
130 | - public function warn( $message ) { |
|
130 | + public function warn($message) { |
|
131 | 131 | |
132 | - $this->log( self::WARN, $message ); |
|
132 | + $this->log(self::WARN, $message); |
|
133 | 133 | |
134 | 134 | } |
135 | 135 | |
136 | - public function info( $message ) { |
|
136 | + public function info($message) { |
|
137 | 137 | |
138 | - $this->log( self::INFO, $message ); |
|
138 | + $this->log(self::INFO, $message); |
|
139 | 139 | |
140 | 140 | } |
141 | 141 | |
142 | - public function debug( $message ) { |
|
142 | + public function debug($message) { |
|
143 | 143 | |
144 | - $this->log( self::DEBUG, $message ); |
|
144 | + $this->log(self::DEBUG, $message); |
|
145 | 145 | |
146 | 146 | } |
147 | 147 | |
148 | - public function trace( $message ) { |
|
148 | + public function trace($message) { |
|
149 | 149 | |
150 | - $this->log( self::TRACE, $message ); |
|
150 | + $this->log(self::TRACE, $message); |
|
151 | 151 | |
152 | 152 | } |
153 | 153 |
@@ -15,67 +15,67 @@ |
||
15 | 15 | */ |
16 | 16 | class Wordlift_Website_Jsonld_Converter extends Wordlift_Post_To_Jsonld_Converter { |
17 | 17 | |
18 | - /** |
|
19 | - * Convert the home/blog page to a JSON-LD array. |
|
20 | - * |
|
21 | - * @return array A JSON-LD array. |
|
22 | - * @since 3.14.0 |
|
23 | - */ |
|
24 | - public function create_schema() { |
|
18 | + /** |
|
19 | + * Convert the home/blog page to a JSON-LD array. |
|
20 | + * |
|
21 | + * @return array A JSON-LD array. |
|
22 | + * @since 3.14.0 |
|
23 | + */ |
|
24 | + public function create_schema() { |
|
25 | 25 | |
26 | - // Create new jsonld. |
|
27 | - $home_url = home_url( '/' ); |
|
26 | + // Create new jsonld. |
|
27 | + $home_url = home_url( '/' ); |
|
28 | 28 | |
29 | - $jsonld = array( |
|
30 | - '@context' => 'http://schema.org', |
|
31 | - '@type' => 'WebSite', |
|
32 | - '@id' => "$home_url#website", |
|
33 | - 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | - 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
35 | - 'url' => $home_url, |
|
36 | - ); |
|
29 | + $jsonld = array( |
|
30 | + '@context' => 'http://schema.org', |
|
31 | + '@type' => 'WebSite', |
|
32 | + '@id' => "$home_url#website", |
|
33 | + 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | + 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
35 | + 'url' => $home_url, |
|
36 | + ); |
|
37 | 37 | |
38 | - // Add publisher information. |
|
39 | - $this->set_publisher( $jsonld ); |
|
38 | + // Add publisher information. |
|
39 | + $this->set_publisher( $jsonld ); |
|
40 | 40 | |
41 | - // Add search action. |
|
42 | - $this->set_search_action( $jsonld ); |
|
41 | + // Add search action. |
|
42 | + $this->set_search_action( $jsonld ); |
|
43 | 43 | |
44 | - /** |
|
45 | - * Call the `wl_website_jsonld` filter. |
|
46 | - * |
|
47 | - * @param array $jsonld The JSON-LD structure. |
|
48 | - * |
|
49 | - * @since 3.14.0 |
|
50 | - * |
|
51 | - * @api |
|
52 | - */ |
|
53 | - return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
54 | - } |
|
44 | + /** |
|
45 | + * Call the `wl_website_jsonld` filter. |
|
46 | + * |
|
47 | + * @param array $jsonld The JSON-LD structure. |
|
48 | + * |
|
49 | + * @since 3.14.0 |
|
50 | + * |
|
51 | + * @api |
|
52 | + */ |
|
53 | + return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Add SearchAction part to the schema |
|
58 | - * |
|
59 | - * @param array $params The parameters array. |
|
60 | - * |
|
61 | - * @since 3.14.0 |
|
62 | - */ |
|
63 | - private function set_search_action( &$params ) { |
|
64 | - /** |
|
65 | - * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
|
66 | - * |
|
67 | - * @since 3.14.0 |
|
68 | - * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
|
69 | - */ |
|
70 | - $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
56 | + /** |
|
57 | + * Add SearchAction part to the schema |
|
58 | + * |
|
59 | + * @param array $params The parameters array. |
|
60 | + * |
|
61 | + * @since 3.14.0 |
|
62 | + */ |
|
63 | + private function set_search_action( &$params ) { |
|
64 | + /** |
|
65 | + * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
|
66 | + * |
|
67 | + * @since 3.14.0 |
|
68 | + * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
|
69 | + */ |
|
70 | + $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
71 | 71 | |
72 | - // Add search action |
|
73 | - $params['potentialAction'] = array( |
|
74 | - '@type' => 'SearchAction', |
|
75 | - 'target' => $search_url, |
|
76 | - 'query-input' => 'required name=search_term_string', |
|
77 | - ); |
|
72 | + // Add search action |
|
73 | + $params['potentialAction'] = array( |
|
74 | + '@type' => 'SearchAction', |
|
75 | + 'target' => $search_url, |
|
76 | + 'query-input' => 'required name=search_term_string', |
|
77 | + ); |
|
78 | 78 | |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | 81 | } |
@@ -24,22 +24,22 @@ discard block |
||
24 | 24 | public function create_schema() { |
25 | 25 | |
26 | 26 | // Create new jsonld. |
27 | - $home_url = home_url( '/' ); |
|
27 | + $home_url = home_url('/'); |
|
28 | 28 | |
29 | 29 | $jsonld = array( |
30 | 30 | '@context' => 'http://schema.org', |
31 | 31 | '@type' => 'WebSite', |
32 | 32 | '@id' => "$home_url#website", |
33 | - 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ), |
|
34 | - 'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ), |
|
33 | + 'name' => html_entity_decode(get_bloginfo('name'), ENT_QUOTES), |
|
34 | + 'alternateName' => html_entity_decode(get_bloginfo('description'), ENT_QUOTES), |
|
35 | 35 | 'url' => $home_url, |
36 | 36 | ); |
37 | 37 | |
38 | 38 | // Add publisher information. |
39 | - $this->set_publisher( $jsonld ); |
|
39 | + $this->set_publisher($jsonld); |
|
40 | 40 | |
41 | 41 | // Add search action. |
42 | - $this->set_search_action( $jsonld ); |
|
42 | + $this->set_search_action($jsonld); |
|
43 | 43 | |
44 | 44 | /** |
45 | 45 | * Call the `wl_website_jsonld` filter. |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @api |
52 | 52 | */ |
53 | - return apply_filters( 'wl_website_jsonld', $jsonld ); |
|
53 | + return apply_filters('wl_website_jsonld', $jsonld); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @since 3.14.0 |
62 | 62 | */ |
63 | - private function set_search_action( &$params ) { |
|
63 | + private function set_search_action(&$params) { |
|
64 | 64 | /** |
65 | 65 | * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL. |
66 | 66 | * |
67 | 67 | * @since 3.14.0 |
68 | 68 | * @api string $search_url The search URL for this site with a `{search_term_string}` variable. |
69 | 69 | */ |
70 | - $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' ); |
|
70 | + $search_url = apply_filters('wl_jsonld_search_url', home_url('/').'?s={search_term_string}'); |
|
71 | 71 | |
72 | 72 | // Add search action |
73 | 73 | $params['potentialAction'] = array( |
@@ -16,55 +16,55 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Publisher_Ajax_Adapter { |
18 | 18 | |
19 | - /** |
|
20 | - * The {@link Wordlift_Publisher_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.11.0 |
|
23 | - * @access private |
|
24 | - * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
25 | - */ |
|
26 | - private $publisher_service; |
|
19 | + /** |
|
20 | + * The {@link Wordlift_Publisher_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.11.0 |
|
23 | + * @access private |
|
24 | + * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
25 | + */ |
|
26 | + private $publisher_service; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
30 | - * |
|
31 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
32 | - * |
|
33 | - * @since 3.11.0 |
|
34 | - */ |
|
35 | - public function __construct( $publisher_service ) { |
|
28 | + /** |
|
29 | + * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
30 | + * |
|
31 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
32 | + * |
|
33 | + * @since 3.11.0 |
|
34 | + */ |
|
35 | + public function __construct( $publisher_service ) { |
|
36 | 36 | |
37 | - $this->publisher_service = $publisher_service; |
|
37 | + $this->publisher_service = $publisher_service; |
|
38 | 38 | |
39 | - } |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
43 | - * action. |
|
44 | - * |
|
45 | - * @since 3.11.0 |
|
46 | - */ |
|
47 | - public function publisher() { |
|
41 | + /** |
|
42 | + * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
43 | + * action. |
|
44 | + * |
|
45 | + * @since 3.11.0 |
|
46 | + */ |
|
47 | + public function publisher() { |
|
48 | 48 | |
49 | - // Ensure we don't have garbage before us. |
|
50 | - ob_clean(); |
|
49 | + // Ensure we don't have garbage before us. |
|
50 | + ob_clean(); |
|
51 | 51 | |
52 | - // Check if the current user can `manage_options`. |
|
53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | - wp_send_json_error( 'Access denied.' ); |
|
55 | - } |
|
52 | + // Check if the current user can `manage_options`. |
|
53 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | + wp_send_json_error( 'Access denied.' ); |
|
55 | + } |
|
56 | 56 | |
57 | - // No actual search parameter was passed, bail out. |
|
58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
60 | - } |
|
57 | + // No actual search parameter was passed, bail out. |
|
58 | + if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | + wp_send_json_error( 'The q parameter is required.' ); |
|
60 | + } |
|
61 | 61 | |
62 | - // Get the response. |
|
63 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
62 | + // Get the response. |
|
63 | + $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
64 | 64 | |
65 | - // Finally output the response. |
|
66 | - wp_send_json_success( $response ); |
|
65 | + // Finally output the response. |
|
66 | + wp_send_json_success( $response ); |
|
67 | 67 | |
68 | - } |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @since 3.11.0 |
34 | 34 | */ |
35 | - public function __construct( $publisher_service ) { |
|
35 | + public function __construct($publisher_service) { |
|
36 | 36 | |
37 | 37 | $this->publisher_service = $publisher_service; |
38 | 38 | |
@@ -50,20 +50,20 @@ discard block |
||
50 | 50 | ob_clean(); |
51 | 51 | |
52 | 52 | // Check if the current user can `manage_options`. |
53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
54 | - wp_send_json_error( 'Access denied.' ); |
|
53 | + if ( ! current_user_can('manage_options')) { |
|
54 | + wp_send_json_error('Access denied.'); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | // No actual search parameter was passed, bail out. |
58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
58 | + if ( ! isset($_POST['q']) || empty($_POST['q'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
59 | + wp_send_json_error('The q parameter is required.'); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | // Get the response. |
63 | - $response = $this->publisher_service->query( sanitize_text_field( wp_unslash( (string) $_POST['q'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
63 | + $response = $this->publisher_service->query(sanitize_text_field(wp_unslash((string) $_POST['q']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
64 | 64 | |
65 | 65 | // Finally output the response. |
66 | - wp_send_json_success( $response ); |
|
66 | + wp_send_json_success($response); |
|
67 | 67 | |
68 | 68 | } |
69 | 69 |
@@ -7,172 +7,172 @@ |
||
7 | 7 | */ |
8 | 8 | class Wordlift_Topic_Taxonomy_Service { |
9 | 9 | |
10 | - /** |
|
11 | - * The Log service. |
|
12 | - * |
|
13 | - * @since 3.5.0 |
|
14 | - * @access private |
|
15 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
16 | - */ |
|
17 | - private $log_service; |
|
18 | - |
|
19 | - /** |
|
20 | - * Taxonomy name. |
|
21 | - * |
|
22 | - * @since 3.5.0 |
|
23 | - */ |
|
24 | - const TAXONOMY_NAME = 'wl_topic'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Taxonomy object type. |
|
28 | - * |
|
29 | - * @since 3.5.0 |
|
30 | - */ |
|
31 | - const TAXONOMY_OBJECT_TYPE = 'post'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Taxonomy slug. |
|
35 | - * |
|
36 | - * @since 3.5.0 |
|
37 | - */ |
|
38 | - const TAXONOMY_SLUG = 'wl_topic'; |
|
39 | - |
|
40 | - /** |
|
41 | - * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
42 | - * |
|
43 | - * @since 3.5.0 |
|
44 | - * @access private |
|
45 | - * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
46 | - */ |
|
47 | - private static $instance; |
|
48 | - |
|
49 | - /** |
|
50 | - * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
51 | - * |
|
52 | - * @since 3.5.0 |
|
53 | - */ |
|
54 | - public function __construct() { |
|
55 | - |
|
56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
57 | - |
|
58 | - // Set the singleton instance. |
|
59 | - self::$instance = $this; |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get the singleton instance of the Entity service. |
|
65 | - * |
|
66 | - * @since 3.5.0 |
|
67 | - * @return Wordlift_Topic_Taxonomy_Service |
|
68 | - */ |
|
69 | - public static function get_instance() { |
|
70 | - |
|
71 | - return self::$instance; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Just register the topic taxonomy. |
|
76 | - * |
|
77 | - * @since 3.5.0 |
|
78 | - */ |
|
79 | - public function init() { |
|
80 | - |
|
81 | - // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
82 | - $labels = array( |
|
83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
94 | - ); |
|
95 | - |
|
96 | - $capabilities = array( |
|
97 | - 'manage_terms' => null, |
|
98 | - 'edit_terms' => null, |
|
99 | - 'delete_terms' => null, |
|
100 | - 'assign_terms' => 'edit_posts', |
|
101 | - ); |
|
102 | - |
|
103 | - $args = array( |
|
104 | - 'labels' => $labels, |
|
105 | - 'capabilities' => $capabilities, |
|
106 | - 'hierarchical' => true, |
|
107 | - 'show_admin_column' => false, |
|
108 | - 'show_ui' => false, |
|
109 | - 'rewrite' => array( |
|
110 | - 'slug' => self::TAXONOMY_SLUG, |
|
111 | - ), |
|
112 | - ); |
|
113 | - |
|
114 | - // Register taxonomy |
|
115 | - register_taxonomy( |
|
116 | - self::TAXONOMY_NAME, |
|
117 | - self::TAXONOMY_OBJECT_TYPE, |
|
118 | - $args |
|
119 | - ); |
|
120 | - |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Get or create a taxonomy term from a given entity topic. |
|
125 | - * |
|
126 | - * @since 3.5.0 |
|
127 | - */ |
|
128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
129 | - |
|
130 | - // Define taxonomy term slug |
|
131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
132 | - // Look for an existing taxonomy term with a given slug |
|
133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | - if ( $term ) { |
|
135 | - return (int) $term->term_id; |
|
136 | - } |
|
137 | - // Otherwise create a new term and return it |
|
138 | - $result = wp_insert_term( |
|
139 | - $topic->post_title, |
|
140 | - self::TAXONOMY_NAME, |
|
141 | - array( |
|
142 | - 'slug' => $term_slug, |
|
143 | - 'description' => $topic->post_content, |
|
144 | - ) |
|
145 | - ); |
|
146 | - |
|
147 | - return (int) $result['term_id']; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Set a topic for a given post. |
|
152 | - * |
|
153 | - * @since 3.5.0 |
|
154 | - */ |
|
155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
156 | - // Retrieve the topic entity post |
|
157 | - $topic_entity_post = get_post( $topic_id ); |
|
158 | - // If current topic does not exist in db false is returned |
|
159 | - if ( null === $topic_entity_post ) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - // Create the proper taxonomy term if needed |
|
163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
164 | - // Link the term to the current post |
|
165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
166 | - return true; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Unlink any topic for a given post. |
|
171 | - * |
|
172 | - * @since 3.5.0 |
|
173 | - */ |
|
174 | - public function unlink_topic_for( $post_id ) { |
|
175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
176 | - } |
|
10 | + /** |
|
11 | + * The Log service. |
|
12 | + * |
|
13 | + * @since 3.5.0 |
|
14 | + * @access private |
|
15 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
16 | + */ |
|
17 | + private $log_service; |
|
18 | + |
|
19 | + /** |
|
20 | + * Taxonomy name. |
|
21 | + * |
|
22 | + * @since 3.5.0 |
|
23 | + */ |
|
24 | + const TAXONOMY_NAME = 'wl_topic'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Taxonomy object type. |
|
28 | + * |
|
29 | + * @since 3.5.0 |
|
30 | + */ |
|
31 | + const TAXONOMY_OBJECT_TYPE = 'post'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Taxonomy slug. |
|
35 | + * |
|
36 | + * @since 3.5.0 |
|
37 | + */ |
|
38 | + const TAXONOMY_SLUG = 'wl_topic'; |
|
39 | + |
|
40 | + /** |
|
41 | + * A singleton instance of the Wordlift_Topic_Taxonomy_Service service. |
|
42 | + * |
|
43 | + * @since 3.5.0 |
|
44 | + * @access private |
|
45 | + * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service. |
|
46 | + */ |
|
47 | + private static $instance; |
|
48 | + |
|
49 | + /** |
|
50 | + * Create a Wordlift_Topic_Taxonomy_Service instance. |
|
51 | + * |
|
52 | + * @since 3.5.0 |
|
53 | + */ |
|
54 | + public function __construct() { |
|
55 | + |
|
56 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
57 | + |
|
58 | + // Set the singleton instance. |
|
59 | + self::$instance = $this; |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get the singleton instance of the Entity service. |
|
65 | + * |
|
66 | + * @since 3.5.0 |
|
67 | + * @return Wordlift_Topic_Taxonomy_Service |
|
68 | + */ |
|
69 | + public static function get_instance() { |
|
70 | + |
|
71 | + return self::$instance; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Just register the topic taxonomy. |
|
76 | + * |
|
77 | + * @since 3.5.0 |
|
78 | + */ |
|
79 | + public function init() { |
|
80 | + |
|
81 | + // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
|
82 | + $labels = array( |
|
83 | + 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | + 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | + 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | + 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | + 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | + 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | + 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | + 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | + 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | + 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | + 'menu_name' => __( 'Topics', 'wordlift' ), |
|
94 | + ); |
|
95 | + |
|
96 | + $capabilities = array( |
|
97 | + 'manage_terms' => null, |
|
98 | + 'edit_terms' => null, |
|
99 | + 'delete_terms' => null, |
|
100 | + 'assign_terms' => 'edit_posts', |
|
101 | + ); |
|
102 | + |
|
103 | + $args = array( |
|
104 | + 'labels' => $labels, |
|
105 | + 'capabilities' => $capabilities, |
|
106 | + 'hierarchical' => true, |
|
107 | + 'show_admin_column' => false, |
|
108 | + 'show_ui' => false, |
|
109 | + 'rewrite' => array( |
|
110 | + 'slug' => self::TAXONOMY_SLUG, |
|
111 | + ), |
|
112 | + ); |
|
113 | + |
|
114 | + // Register taxonomy |
|
115 | + register_taxonomy( |
|
116 | + self::TAXONOMY_NAME, |
|
117 | + self::TAXONOMY_OBJECT_TYPE, |
|
118 | + $args |
|
119 | + ); |
|
120 | + |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Get or create a taxonomy term from a given entity topic. |
|
125 | + * |
|
126 | + * @since 3.5.0 |
|
127 | + */ |
|
128 | + public function get_or_create_term_from_topic_entity( $topic ) { |
|
129 | + |
|
130 | + // Define taxonomy term slug |
|
131 | + $term_slug = sanitize_title( $topic->post_title ); |
|
132 | + // Look for an existing taxonomy term with a given slug |
|
133 | + $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | + if ( $term ) { |
|
135 | + return (int) $term->term_id; |
|
136 | + } |
|
137 | + // Otherwise create a new term and return it |
|
138 | + $result = wp_insert_term( |
|
139 | + $topic->post_title, |
|
140 | + self::TAXONOMY_NAME, |
|
141 | + array( |
|
142 | + 'slug' => $term_slug, |
|
143 | + 'description' => $topic->post_content, |
|
144 | + ) |
|
145 | + ); |
|
146 | + |
|
147 | + return (int) $result['term_id']; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Set a topic for a given post. |
|
152 | + * |
|
153 | + * @since 3.5.0 |
|
154 | + */ |
|
155 | + public function set_topic_for( $post_id, $topic_id ) { |
|
156 | + // Retrieve the topic entity post |
|
157 | + $topic_entity_post = get_post( $topic_id ); |
|
158 | + // If current topic does not exist in db false is returned |
|
159 | + if ( null === $topic_entity_post ) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + // Create the proper taxonomy term if needed |
|
163 | + $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
164 | + // Link the term to the current post |
|
165 | + wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
166 | + return true; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Unlink any topic for a given post. |
|
171 | + * |
|
172 | + * @since 3.5.0 |
|
173 | + */ |
|
174 | + public function unlink_topic_for( $post_id ) { |
|
175 | + wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
176 | + } |
|
177 | 177 | |
178 | 178 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function __construct() { |
55 | 55 | |
56 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
56 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
57 | 57 | |
58 | 58 | // Set the singleton instance. |
59 | 59 | self::$instance = $this; |
@@ -80,17 +80,17 @@ discard block |
||
80 | 80 | |
81 | 81 | // See https://codex.wordpress.org/Function_Reference/register_taxonomy |
82 | 82 | $labels = array( |
83 | - 'name' => _x( 'Topics', 'taxonomy general name', 'wordlift' ), |
|
84 | - 'singular_name' => _x( 'Topic', 'taxonomy singular name', 'wordlift' ), |
|
85 | - 'search_items' => __( 'Search Topics', 'wordlift' ), |
|
86 | - 'all_items' => __( 'All Topics', 'wordlift' ), |
|
87 | - 'parent_item' => __( 'Parent Topic', 'wordlift' ), |
|
88 | - 'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ), |
|
89 | - 'edit_item' => __( 'Edit Topic', 'wordlift' ), |
|
90 | - 'update_item' => __( 'Update Topic', 'wordlift' ), |
|
91 | - 'add_new_item' => __( 'Add New Topic', 'wordlift' ), |
|
92 | - 'new_item_name' => __( 'New Topic', 'wordlift' ), |
|
93 | - 'menu_name' => __( 'Topics', 'wordlift' ), |
|
83 | + 'name' => _x('Topics', 'taxonomy general name', 'wordlift'), |
|
84 | + 'singular_name' => _x('Topic', 'taxonomy singular name', 'wordlift'), |
|
85 | + 'search_items' => __('Search Topics', 'wordlift'), |
|
86 | + 'all_items' => __('All Topics', 'wordlift'), |
|
87 | + 'parent_item' => __('Parent Topic', 'wordlift'), |
|
88 | + 'parent_item_colon' => __('Parent Topic:', 'wordlift'), |
|
89 | + 'edit_item' => __('Edit Topic', 'wordlift'), |
|
90 | + 'update_item' => __('Update Topic', 'wordlift'), |
|
91 | + 'add_new_item' => __('Add New Topic', 'wordlift'), |
|
92 | + 'new_item_name' => __('New Topic', 'wordlift'), |
|
93 | + 'menu_name' => __('Topics', 'wordlift'), |
|
94 | 94 | ); |
95 | 95 | |
96 | 96 | $capabilities = array( |
@@ -125,13 +125,13 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @since 3.5.0 |
127 | 127 | */ |
128 | - public function get_or_create_term_from_topic_entity( $topic ) { |
|
128 | + public function get_or_create_term_from_topic_entity($topic) { |
|
129 | 129 | |
130 | 130 | // Define taxonomy term slug |
131 | - $term_slug = sanitize_title( $topic->post_title ); |
|
131 | + $term_slug = sanitize_title($topic->post_title); |
|
132 | 132 | // Look for an existing taxonomy term with a given slug |
133 | - $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ); |
|
134 | - if ( $term ) { |
|
133 | + $term = get_term_by('slug', $term_slug, self::TAXONOMY_NAME); |
|
134 | + if ($term) { |
|
135 | 135 | return (int) $term->term_id; |
136 | 136 | } |
137 | 137 | // Otherwise create a new term and return it |
@@ -152,17 +152,17 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @since 3.5.0 |
154 | 154 | */ |
155 | - public function set_topic_for( $post_id, $topic_id ) { |
|
155 | + public function set_topic_for($post_id, $topic_id) { |
|
156 | 156 | // Retrieve the topic entity post |
157 | - $topic_entity_post = get_post( $topic_id ); |
|
157 | + $topic_entity_post = get_post($topic_id); |
|
158 | 158 | // If current topic does not exist in db false is returned |
159 | - if ( null === $topic_entity_post ) { |
|
159 | + if (null === $topic_entity_post) { |
|
160 | 160 | return false; |
161 | 161 | } |
162 | 162 | // Create the proper taxonomy term if needed |
163 | - $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post ); |
|
163 | + $term_id = $this->get_or_create_term_from_topic_entity($topic_entity_post); |
|
164 | 164 | // Link the term to the current post |
165 | - wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false ); |
|
165 | + wp_set_post_terms($post_id, $term_id, self::TAXONOMY_NAME, false); |
|
166 | 166 | return true; |
167 | 167 | } |
168 | 168 | |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @since 3.5.0 |
173 | 173 | */ |
174 | - public function unlink_topic_for( $post_id ) { |
|
175 | - wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME ); |
|
174 | + public function unlink_topic_for($post_id) { |
|
175 | + wp_delete_object_term_relationships($post_id, self::TAXONOMY_NAME); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | } |