@@ -9,288 +9,288 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class Wordlift_Async_Task { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Constant identifier for a task that should be available to logged-in users |
|
| 14 | - * |
|
| 15 | - * See constructor documentation for more details. |
|
| 16 | - */ |
|
| 17 | - const LOGGED_IN = 1; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Constant identifier for a task that should be available to logged-out users |
|
| 21 | - * |
|
| 22 | - * See constructor documentation for more details. |
|
| 23 | - */ |
|
| 24 | - const LOGGED_OUT = 2; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constant identifier for a task that should be available to all users regardless of auth status |
|
| 28 | - * |
|
| 29 | - * See constructor documentation for more details. |
|
| 30 | - */ |
|
| 31 | - const BOTH = 3; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * This is the argument count for the main action set in the constructor. It |
|
| 35 | - * is set to an arbitrarily high value of twenty, but can be overridden if |
|
| 36 | - * necessary |
|
| 37 | - * |
|
| 38 | - * @var int |
|
| 39 | - */ |
|
| 40 | - protected $argument_count = 20; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Priority to fire intermediate action. |
|
| 44 | - * |
|
| 45 | - * @var int |
|
| 46 | - */ |
|
| 47 | - protected $priority = 10; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $action; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $_body_data; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * A {@link Wordlift_Log_Service} instance. |
|
| 61 | - * |
|
| 62 | - * @since 3.15.0 |
|
| 63 | - * @access private |
|
| 64 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 65 | - */ |
|
| 66 | - private $log; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Constructor to wire up the necessary actions |
|
| 70 | - * |
|
| 71 | - * Which hooks the asynchronous postback happens on can be set by the |
|
| 72 | - * $auth_level parameter. There are essentially three options: logged in users |
|
| 73 | - * only, logged out users only, or both. Set this when you instantiate an |
|
| 74 | - * object by using one of the three class constants to do so: |
|
| 75 | - * - LOGGED_IN |
|
| 76 | - * - LOGGED_OUT |
|
| 77 | - * - BOTH |
|
| 78 | - * $auth_level defaults to BOTH |
|
| 79 | - * |
|
| 80 | - * @throws Exception If the class' $action value hasn't been set |
|
| 81 | - * |
|
| 82 | - * @param int $auth_level The authentication level to use (see above) |
|
| 83 | - */ |
|
| 84 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 85 | - |
|
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | - |
|
| 88 | - if ( empty( $this->action ) ) { |
|
| 89 | - throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 90 | - } |
|
| 91 | - add_action( $this->action, array( |
|
| 92 | - $this, |
|
| 93 | - 'launch', |
|
| 94 | - ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | - if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | - add_action( "admin_post_wl_async_$this->action", array( |
|
| 97 | - $this, |
|
| 98 | - 'handle_postback', |
|
| 99 | - ) ); |
|
| 100 | - } |
|
| 101 | - if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | - add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | - $this, |
|
| 104 | - 'handle_postback', |
|
| 105 | - ) ); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Add the shutdown action for launching the real postback if we don't |
|
| 111 | - * get an exception thrown by prepare_data(). |
|
| 112 | - * |
|
| 113 | - * @uses func_get_args() To grab any arguments passed by the action |
|
| 114 | - */ |
|
| 115 | - public function launch() { |
|
| 116 | - $data = func_get_args(); |
|
| 117 | - try { |
|
| 118 | - $data = $this->prepare_data( $data ); |
|
| 119 | - } catch ( Exception $e ) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $data['action'] = "wl_async_$this->action"; |
|
| 124 | - $data['_nonce'] = $this->create_async_nonce(); |
|
| 125 | - |
|
| 126 | - $this->_body_data = $data; |
|
| 127 | - |
|
| 128 | - if ( ! has_action( 'shutdown', array( |
|
| 129 | - $this, |
|
| 130 | - 'launch_on_shutdown', |
|
| 131 | - ) ) |
|
| 132 | - ) { |
|
| 133 | - add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Launch the request on the WordPress shutdown hook |
|
| 139 | - * |
|
| 140 | - * On VIP we got into data races due to the postback sometimes completing |
|
| 141 | - * faster than the data could propogate to the database server cluster. |
|
| 142 | - * This made WordPress get empty data sets from the database without |
|
| 143 | - * failing. On their advice, we're moving the actual firing of the async |
|
| 144 | - * postback to the shutdown hook. Supposedly that will ensure that the |
|
| 145 | - * data at least has time to get into the object cache. |
|
| 146 | - * |
|
| 147 | - * @uses $_COOKIE To send a cookie header for async postback |
|
| 148 | - * @uses apply_filters() |
|
| 149 | - * @uses admin_url() |
|
| 150 | - * @uses wp_remote_post() |
|
| 151 | - */ |
|
| 152 | - public function launch_on_shutdown() { |
|
| 153 | - |
|
| 154 | - $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 155 | - |
|
| 156 | - if ( ! empty( $this->_body_data ) ) { |
|
| 157 | - $cookies = array(); |
|
| 158 | - foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | - $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $request_args = array( |
|
| 163 | - 'timeout' => 0.01, |
|
| 164 | - 'blocking' => false, |
|
| 165 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 166 | - 'body' => $this->_body_data, |
|
| 167 | - 'headers' => array( |
|
| 168 | - 'cookie' => implode( '; ', $cookies ), |
|
| 169 | - ), |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - $url = get_site_url( null, 'wl-api' ); |
|
| 173 | - |
|
| 174 | - $this->log->debug( "Posting URL $url..." ); |
|
| 175 | - |
|
| 176 | - $result = wp_remote_post( $url, $request_args ); |
|
| 177 | - |
|
| 178 | - if ( is_wp_error( $result ) ) { |
|
| 179 | - $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Verify the postback is valid, then fire any scheduled events. |
|
| 187 | - * |
|
| 188 | - * @uses $_POST['_nonce'] |
|
| 189 | - * @uses is_user_logged_in() |
|
| 190 | - * @uses add_filter() |
|
| 191 | - * @uses wp_die() |
|
| 192 | - */ |
|
| 193 | - public function handle_postback() { |
|
| 194 | - if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( (string) $_POST['_nonce'] ) ) { |
|
| 195 | - if ( ! is_user_logged_in() ) { |
|
| 196 | - $this->action = "nopriv_$this->action"; |
|
| 197 | - } |
|
| 198 | - $this->run_action(); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Create a random, one time use token. |
|
| 204 | - * |
|
| 205 | - * Based entirely on wp_create_nonce() but does not tie the nonce to the |
|
| 206 | - * current logged-in user. |
|
| 207 | - * |
|
| 208 | - * @uses wp_nonce_tick() |
|
| 209 | - * @uses wp_hash() |
|
| 210 | - * |
|
| 211 | - * @return string The one-time use token |
|
| 212 | - */ |
|
| 213 | - protected function create_async_nonce() { |
|
| 214 | - $action = $this->get_nonce_action(); |
|
| 215 | - $i = wp_nonce_tick(); |
|
| 216 | - |
|
| 217 | - return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Verify that the correct nonce was used within the time limit. |
|
| 222 | - * |
|
| 223 | - * @uses wp_nonce_tick() |
|
| 224 | - * @uses wp_hash() |
|
| 225 | - * |
|
| 226 | - * @param string $nonce Nonce to be verified |
|
| 227 | - * |
|
| 228 | - * @return bool Whether the nonce check passed or failed |
|
| 229 | - */ |
|
| 230 | - protected function verify_async_nonce( $nonce ) { |
|
| 231 | - $action = $this->get_nonce_action(); |
|
| 232 | - $i = wp_nonce_tick(); |
|
| 233 | - |
|
| 234 | - // Nonce generated 0-12 hours ago |
|
| 235 | - if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 236 | - return 1; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // Nonce generated 12-24 hours ago |
|
| 240 | - if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 241 | - return 2; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Invalid nonce |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Get a nonce action based on the $action property of the class |
|
| 250 | - * |
|
| 251 | - * @return string The nonce action for the current instance |
|
| 252 | - */ |
|
| 253 | - protected function get_nonce_action() { |
|
| 254 | - $action = $this->action; |
|
| 255 | - if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 256 | - $action = substr( $action, 7 ); |
|
| 257 | - } |
|
| 258 | - $action = "wl_async_$action"; |
|
| 259 | - |
|
| 260 | - return $action; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Prepare any data to be passed to the asynchronous postback |
|
| 265 | - * |
|
| 266 | - * The array this function receives will be a numerically keyed array from |
|
| 267 | - * func_get_args(). It is expected that you will return an associative array |
|
| 268 | - * so that the $_POST values used in the asynchronous call will make sense. |
|
| 269 | - * |
|
| 270 | - * The array you send back may or may not have anything to do with the data |
|
| 271 | - * passed into this method. It all depends on the implementation details and |
|
| 272 | - * what data is needed in the asynchronous postback. |
|
| 273 | - * |
|
| 274 | - * Do not set values for 'action' or '_nonce', as those will get overwritten |
|
| 275 | - * later in launch(). |
|
| 276 | - * |
|
| 277 | - * @throws Exception If the postback should not occur for any reason |
|
| 278 | - * |
|
| 279 | - * @param array $data The raw data received by the launch method |
|
| 280 | - * |
|
| 281 | - * @return array The prepared data |
|
| 282 | - */ |
|
| 283 | - abstract protected function prepare_data( $data ); |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Run the do_action function for the asynchronous postback. |
|
| 287 | - * |
|
| 288 | - * This method needs to fetch and sanitize any and all data from the $_POST |
|
| 289 | - * superglobal and provide them to the do_action call. |
|
| 290 | - * |
|
| 291 | - * The action should be constructed as "wl_async_task_$this->action" |
|
| 292 | - */ |
|
| 293 | - abstract protected function run_action(); |
|
| 12 | + /** |
|
| 13 | + * Constant identifier for a task that should be available to logged-in users |
|
| 14 | + * |
|
| 15 | + * See constructor documentation for more details. |
|
| 16 | + */ |
|
| 17 | + const LOGGED_IN = 1; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Constant identifier for a task that should be available to logged-out users |
|
| 21 | + * |
|
| 22 | + * See constructor documentation for more details. |
|
| 23 | + */ |
|
| 24 | + const LOGGED_OUT = 2; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constant identifier for a task that should be available to all users regardless of auth status |
|
| 28 | + * |
|
| 29 | + * See constructor documentation for more details. |
|
| 30 | + */ |
|
| 31 | + const BOTH = 3; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * This is the argument count for the main action set in the constructor. It |
|
| 35 | + * is set to an arbitrarily high value of twenty, but can be overridden if |
|
| 36 | + * necessary |
|
| 37 | + * |
|
| 38 | + * @var int |
|
| 39 | + */ |
|
| 40 | + protected $argument_count = 20; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Priority to fire intermediate action. |
|
| 44 | + * |
|
| 45 | + * @var int |
|
| 46 | + */ |
|
| 47 | + protected $priority = 10; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $action; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $_body_data; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * A {@link Wordlift_Log_Service} instance. |
|
| 61 | + * |
|
| 62 | + * @since 3.15.0 |
|
| 63 | + * @access private |
|
| 64 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 65 | + */ |
|
| 66 | + private $log; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Constructor to wire up the necessary actions |
|
| 70 | + * |
|
| 71 | + * Which hooks the asynchronous postback happens on can be set by the |
|
| 72 | + * $auth_level parameter. There are essentially three options: logged in users |
|
| 73 | + * only, logged out users only, or both. Set this when you instantiate an |
|
| 74 | + * object by using one of the three class constants to do so: |
|
| 75 | + * - LOGGED_IN |
|
| 76 | + * - LOGGED_OUT |
|
| 77 | + * - BOTH |
|
| 78 | + * $auth_level defaults to BOTH |
|
| 79 | + * |
|
| 80 | + * @throws Exception If the class' $action value hasn't been set |
|
| 81 | + * |
|
| 82 | + * @param int $auth_level The authentication level to use (see above) |
|
| 83 | + */ |
|
| 84 | + public function __construct( $auth_level = self::BOTH ) { |
|
| 85 | + |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | + |
|
| 88 | + if ( empty( $this->action ) ) { |
|
| 89 | + throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 90 | + } |
|
| 91 | + add_action( $this->action, array( |
|
| 92 | + $this, |
|
| 93 | + 'launch', |
|
| 94 | + ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | + if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | + add_action( "admin_post_wl_async_$this->action", array( |
|
| 97 | + $this, |
|
| 98 | + 'handle_postback', |
|
| 99 | + ) ); |
|
| 100 | + } |
|
| 101 | + if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | + add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | + $this, |
|
| 104 | + 'handle_postback', |
|
| 105 | + ) ); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Add the shutdown action for launching the real postback if we don't |
|
| 111 | + * get an exception thrown by prepare_data(). |
|
| 112 | + * |
|
| 113 | + * @uses func_get_args() To grab any arguments passed by the action |
|
| 114 | + */ |
|
| 115 | + public function launch() { |
|
| 116 | + $data = func_get_args(); |
|
| 117 | + try { |
|
| 118 | + $data = $this->prepare_data( $data ); |
|
| 119 | + } catch ( Exception $e ) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $data['action'] = "wl_async_$this->action"; |
|
| 124 | + $data['_nonce'] = $this->create_async_nonce(); |
|
| 125 | + |
|
| 126 | + $this->_body_data = $data; |
|
| 127 | + |
|
| 128 | + if ( ! has_action( 'shutdown', array( |
|
| 129 | + $this, |
|
| 130 | + 'launch_on_shutdown', |
|
| 131 | + ) ) |
|
| 132 | + ) { |
|
| 133 | + add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Launch the request on the WordPress shutdown hook |
|
| 139 | + * |
|
| 140 | + * On VIP we got into data races due to the postback sometimes completing |
|
| 141 | + * faster than the data could propogate to the database server cluster. |
|
| 142 | + * This made WordPress get empty data sets from the database without |
|
| 143 | + * failing. On their advice, we're moving the actual firing of the async |
|
| 144 | + * postback to the shutdown hook. Supposedly that will ensure that the |
|
| 145 | + * data at least has time to get into the object cache. |
|
| 146 | + * |
|
| 147 | + * @uses $_COOKIE To send a cookie header for async postback |
|
| 148 | + * @uses apply_filters() |
|
| 149 | + * @uses admin_url() |
|
| 150 | + * @uses wp_remote_post() |
|
| 151 | + */ |
|
| 152 | + public function launch_on_shutdown() { |
|
| 153 | + |
|
| 154 | + $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 155 | + |
|
| 156 | + if ( ! empty( $this->_body_data ) ) { |
|
| 157 | + $cookies = array(); |
|
| 158 | + foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | + $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $request_args = array( |
|
| 163 | + 'timeout' => 0.01, |
|
| 164 | + 'blocking' => false, |
|
| 165 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 166 | + 'body' => $this->_body_data, |
|
| 167 | + 'headers' => array( |
|
| 168 | + 'cookie' => implode( '; ', $cookies ), |
|
| 169 | + ), |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + $url = get_site_url( null, 'wl-api' ); |
|
| 173 | + |
|
| 174 | + $this->log->debug( "Posting URL $url..." ); |
|
| 175 | + |
|
| 176 | + $result = wp_remote_post( $url, $request_args ); |
|
| 177 | + |
|
| 178 | + if ( is_wp_error( $result ) ) { |
|
| 179 | + $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Verify the postback is valid, then fire any scheduled events. |
|
| 187 | + * |
|
| 188 | + * @uses $_POST['_nonce'] |
|
| 189 | + * @uses is_user_logged_in() |
|
| 190 | + * @uses add_filter() |
|
| 191 | + * @uses wp_die() |
|
| 192 | + */ |
|
| 193 | + public function handle_postback() { |
|
| 194 | + if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( (string) $_POST['_nonce'] ) ) { |
|
| 195 | + if ( ! is_user_logged_in() ) { |
|
| 196 | + $this->action = "nopriv_$this->action"; |
|
| 197 | + } |
|
| 198 | + $this->run_action(); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Create a random, one time use token. |
|
| 204 | + * |
|
| 205 | + * Based entirely on wp_create_nonce() but does not tie the nonce to the |
|
| 206 | + * current logged-in user. |
|
| 207 | + * |
|
| 208 | + * @uses wp_nonce_tick() |
|
| 209 | + * @uses wp_hash() |
|
| 210 | + * |
|
| 211 | + * @return string The one-time use token |
|
| 212 | + */ |
|
| 213 | + protected function create_async_nonce() { |
|
| 214 | + $action = $this->get_nonce_action(); |
|
| 215 | + $i = wp_nonce_tick(); |
|
| 216 | + |
|
| 217 | + return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Verify that the correct nonce was used within the time limit. |
|
| 222 | + * |
|
| 223 | + * @uses wp_nonce_tick() |
|
| 224 | + * @uses wp_hash() |
|
| 225 | + * |
|
| 226 | + * @param string $nonce Nonce to be verified |
|
| 227 | + * |
|
| 228 | + * @return bool Whether the nonce check passed or failed |
|
| 229 | + */ |
|
| 230 | + protected function verify_async_nonce( $nonce ) { |
|
| 231 | + $action = $this->get_nonce_action(); |
|
| 232 | + $i = wp_nonce_tick(); |
|
| 233 | + |
|
| 234 | + // Nonce generated 0-12 hours ago |
|
| 235 | + if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 236 | + return 1; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // Nonce generated 12-24 hours ago |
|
| 240 | + if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 241 | + return 2; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Invalid nonce |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Get a nonce action based on the $action property of the class |
|
| 250 | + * |
|
| 251 | + * @return string The nonce action for the current instance |
|
| 252 | + */ |
|
| 253 | + protected function get_nonce_action() { |
|
| 254 | + $action = $this->action; |
|
| 255 | + if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 256 | + $action = substr( $action, 7 ); |
|
| 257 | + } |
|
| 258 | + $action = "wl_async_$action"; |
|
| 259 | + |
|
| 260 | + return $action; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Prepare any data to be passed to the asynchronous postback |
|
| 265 | + * |
|
| 266 | + * The array this function receives will be a numerically keyed array from |
|
| 267 | + * func_get_args(). It is expected that you will return an associative array |
|
| 268 | + * so that the $_POST values used in the asynchronous call will make sense. |
|
| 269 | + * |
|
| 270 | + * The array you send back may or may not have anything to do with the data |
|
| 271 | + * passed into this method. It all depends on the implementation details and |
|
| 272 | + * what data is needed in the asynchronous postback. |
|
| 273 | + * |
|
| 274 | + * Do not set values for 'action' or '_nonce', as those will get overwritten |
|
| 275 | + * later in launch(). |
|
| 276 | + * |
|
| 277 | + * @throws Exception If the postback should not occur for any reason |
|
| 278 | + * |
|
| 279 | + * @param array $data The raw data received by the launch method |
|
| 280 | + * |
|
| 281 | + * @return array The prepared data |
|
| 282 | + */ |
|
| 283 | + abstract protected function prepare_data( $data ); |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Run the do_action function for the asynchronous postback. |
|
| 287 | + * |
|
| 288 | + * This method needs to fetch and sanitize any and all data from the $_POST |
|
| 289 | + * superglobal and provide them to the do_action call. |
|
| 290 | + * |
|
| 291 | + * The action should be constructed as "wl_async_task_$this->action" |
|
| 292 | + */ |
|
| 293 | + abstract protected function run_action(); |
|
| 294 | 294 | |
| 295 | 295 | } |
| 296 | 296 | |
@@ -81,28 +81,28 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @param int $auth_level The authentication level to use (see above) |
| 83 | 83 | */ |
| 84 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 84 | + public function __construct($auth_level = self::BOTH) { |
|
| 85 | 85 | |
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 87 | 87 | |
| 88 | - if ( empty( $this->action ) ) { |
|
| 89 | - throw new Exception( 'Action not defined for class ' . __CLASS__ ); |
|
| 88 | + if (empty($this->action)) { |
|
| 89 | + throw new Exception('Action not defined for class '.__CLASS__); |
|
| 90 | 90 | } |
| 91 | - add_action( $this->action, array( |
|
| 91 | + add_action($this->action, array( |
|
| 92 | 92 | $this, |
| 93 | 93 | 'launch', |
| 94 | - ), (int) $this->priority, (int) $this->argument_count ); |
|
| 95 | - if ( $auth_level & self::LOGGED_IN ) { |
|
| 96 | - add_action( "admin_post_wl_async_$this->action", array( |
|
| 94 | + ), (int) $this->priority, (int) $this->argument_count); |
|
| 95 | + if ($auth_level & self::LOGGED_IN) { |
|
| 96 | + add_action("admin_post_wl_async_$this->action", array( |
|
| 97 | 97 | $this, |
| 98 | 98 | 'handle_postback', |
| 99 | - ) ); |
|
| 99 | + )); |
|
| 100 | 100 | } |
| 101 | - if ( $auth_level & self::LOGGED_OUT ) { |
|
| 102 | - add_action( "admin_post_nopriv_wl_async_$this->action", array( |
|
| 101 | + if ($auth_level & self::LOGGED_OUT) { |
|
| 102 | + add_action("admin_post_nopriv_wl_async_$this->action", array( |
|
| 103 | 103 | $this, |
| 104 | 104 | 'handle_postback', |
| 105 | - ) ); |
|
| 105 | + )); |
|
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | |
@@ -115,8 +115,8 @@ discard block |
||
| 115 | 115 | public function launch() { |
| 116 | 116 | $data = func_get_args(); |
| 117 | 117 | try { |
| 118 | - $data = $this->prepare_data( $data ); |
|
| 119 | - } catch ( Exception $e ) { |
|
| 118 | + $data = $this->prepare_data($data); |
|
| 119 | + } catch (Exception $e) { |
|
| 120 | 120 | return; |
| 121 | 121 | } |
| 122 | 122 | |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | $this->_body_data = $data; |
| 127 | 127 | |
| 128 | - if ( ! has_action( 'shutdown', array( |
|
| 128 | + if ( ! has_action('shutdown', array( |
|
| 129 | 129 | $this, |
| 130 | 130 | 'launch_on_shutdown', |
| 131 | - ) ) |
|
| 131 | + )) |
|
| 132 | 132 | ) { |
| 133 | - add_action( 'shutdown', array( $this, 'launch_on_shutdown' ) ); |
|
| 133 | + add_action('shutdown', array($this, 'launch_on_shutdown')); |
|
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
@@ -151,32 +151,32 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function launch_on_shutdown() { |
| 153 | 153 | |
| 154 | - $this->log->debug( "Launching Async Task [ action :: $this->action ]..." ); |
|
| 154 | + $this->log->debug("Launching Async Task [ action :: $this->action ]..."); |
|
| 155 | 155 | |
| 156 | - if ( ! empty( $this->_body_data ) ) { |
|
| 156 | + if ( ! empty($this->_body_data)) { |
|
| 157 | 157 | $cookies = array(); |
| 158 | - foreach ( $_COOKIE as $name => $value ) { |
|
| 159 | - $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value ); |
|
| 158 | + foreach ($_COOKIE as $name => $value) { |
|
| 159 | + $cookies[] = "$name=".urlencode(is_array($value) ? serialize($value) : $value); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | $request_args = array( |
| 163 | 163 | 'timeout' => 0.01, |
| 164 | 164 | 'blocking' => false, |
| 165 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 165 | + 'sslverify' => apply_filters('https_local_ssl_verify', false), |
|
| 166 | 166 | 'body' => $this->_body_data, |
| 167 | 167 | 'headers' => array( |
| 168 | - 'cookie' => implode( '; ', $cookies ), |
|
| 168 | + 'cookie' => implode('; ', $cookies), |
|
| 169 | 169 | ), |
| 170 | 170 | ); |
| 171 | 171 | |
| 172 | - $url = get_site_url( null, 'wl-api' ); |
|
| 172 | + $url = get_site_url(null, 'wl-api'); |
|
| 173 | 173 | |
| 174 | - $this->log->debug( "Posting URL $url..." ); |
|
| 174 | + $this->log->debug("Posting URL $url..."); |
|
| 175 | 175 | |
| 176 | - $result = wp_remote_post( $url, $request_args ); |
|
| 176 | + $result = wp_remote_post($url, $request_args); |
|
| 177 | 177 | |
| 178 | - if ( is_wp_error( $result ) ) { |
|
| 179 | - $this->log->error( 'Posting URL returned an error: ' . $result->get_error_message() ); |
|
| 178 | + if (is_wp_error($result)) { |
|
| 179 | + $this->log->error('Posting URL returned an error: '.$result->get_error_message()); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | } |
@@ -191,8 +191,8 @@ discard block |
||
| 191 | 191 | * @uses wp_die() |
| 192 | 192 | */ |
| 193 | 193 | public function handle_postback() { |
| 194 | - if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( (string) $_POST['_nonce'] ) ) { |
|
| 195 | - if ( ! is_user_logged_in() ) { |
|
| 194 | + if (isset($_POST['_nonce']) && $this->verify_async_nonce((string) $_POST['_nonce'])) { |
|
| 195 | + if ( ! is_user_logged_in()) { |
|
| 196 | 196 | $this->action = "nopriv_$this->action"; |
| 197 | 197 | } |
| 198 | 198 | $this->run_action(); |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | $action = $this->get_nonce_action(); |
| 215 | 215 | $i = wp_nonce_tick(); |
| 216 | 216 | |
| 217 | - return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ); |
|
| 217 | + return substr(wp_hash($i.$action.get_class($this), 'nonce'), - 12, 10); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | /** |
@@ -227,17 +227,17 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return bool Whether the nonce check passed or failed |
| 229 | 229 | */ |
| 230 | - protected function verify_async_nonce( $nonce ) { |
|
| 230 | + protected function verify_async_nonce($nonce) { |
|
| 231 | 231 | $action = $this->get_nonce_action(); |
| 232 | 232 | $i = wp_nonce_tick(); |
| 233 | 233 | |
| 234 | 234 | // Nonce generated 0-12 hours ago |
| 235 | - if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 235 | + if (substr(wp_hash($i.$action.get_class($this), 'nonce'), - 12, 10) == $nonce) { |
|
| 236 | 236 | return 1; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | // Nonce generated 12-24 hours ago |
| 240 | - if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) == $nonce ) { |
|
| 240 | + if (substr(wp_hash(($i - 1).$action.get_class($this), 'nonce'), - 12, 10) == $nonce) { |
|
| 241 | 241 | return 2; |
| 242 | 242 | } |
| 243 | 243 | |
@@ -252,8 +252,8 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | protected function get_nonce_action() { |
| 254 | 254 | $action = $this->action; |
| 255 | - if ( substr( $action, 0, 7 ) === 'nopriv_' ) { |
|
| 256 | - $action = substr( $action, 7 ); |
|
| 255 | + if (substr($action, 0, 7) === 'nopriv_') { |
|
| 256 | + $action = substr($action, 7); |
|
| 257 | 257 | } |
| 258 | 258 | $action = "wl_async_$action"; |
| 259 | 259 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | * |
| 281 | 281 | * @return array The prepared data |
| 282 | 282 | */ |
| 283 | - abstract protected function prepare_data( $data ); |
|
| 283 | + abstract protected function prepare_data($data); |
|
| 284 | 284 | |
| 285 | 285 | /** |
| 286 | 286 | * Run the do_action function for the asynchronous postback. |
@@ -18,534 +18,534 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Entity_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The Log service. |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 27 | - */ |
|
| 28 | - private $log; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The UI service. |
|
| 32 | - * |
|
| 33 | - * @since 3.2.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 36 | - */ |
|
| 37 | - private $ui_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $relation_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.16.3 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 54 | - */ |
|
| 55 | - private $entity_uri_service; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The entity post type name. |
|
| 59 | - * |
|
| 60 | - * @since 3.1.0 |
|
| 61 | - */ |
|
| 62 | - const TYPE_NAME = 'entity'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The alternative label meta key. |
|
| 66 | - * |
|
| 67 | - * @since 3.2.0 |
|
| 68 | - */ |
|
| 69 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The alternative label input template. |
|
| 73 | - * |
|
| 74 | - * @since 3.2.0 |
|
| 75 | - */ |
|
| 76 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 77 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 21 | + /** |
|
| 22 | + * The Log service. |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 27 | + */ |
|
| 28 | + private $log; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The UI service. |
|
| 32 | + * |
|
| 33 | + * @since 3.2.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 36 | + */ |
|
| 37 | + private $ui_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $relation_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.16.3 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 54 | + */ |
|
| 55 | + private $entity_uri_service; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The entity post type name. |
|
| 59 | + * |
|
| 60 | + * @since 3.1.0 |
|
| 61 | + */ |
|
| 62 | + const TYPE_NAME = 'entity'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The alternative label meta key. |
|
| 66 | + * |
|
| 67 | + * @since 3.2.0 |
|
| 68 | + */ |
|
| 69 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The alternative label input template. |
|
| 73 | + * |
|
| 74 | + * @since 3.2.0 |
|
| 75 | + */ |
|
| 76 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 77 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 78 | 78 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 79 | 79 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 80 | 80 | <button class="button wl-delete-button">%s</button> |
| 81 | 81 | </div>'; |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * A singleton instance of the Entity service. |
|
| 85 | - * |
|
| 86 | - * @since 3.2.0 |
|
| 87 | - * @access private |
|
| 88 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 89 | - */ |
|
| 90 | - private static $instance; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Create a Wordlift_Entity_Service instance. |
|
| 94 | - * |
|
| 95 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 96 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 97 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 98 | - * |
|
| 99 | - * @since 3.2.0 |
|
| 100 | - * |
|
| 101 | - */ |
|
| 102 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 103 | - |
|
| 104 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 105 | - |
|
| 106 | - $this->ui_service = $ui_service; |
|
| 107 | - $this->relation_service = $relation_service; |
|
| 108 | - $this->entity_uri_service = $entity_uri_service; |
|
| 109 | - |
|
| 110 | - // Set the singleton instance. |
|
| 111 | - self::$instance = $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the singleton instance of the Entity service. |
|
| 116 | - * |
|
| 117 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 118 | - * @since 3.2.0 |
|
| 119 | - */ |
|
| 120 | - public static function get_instance() { |
|
| 121 | - |
|
| 122 | - return self::$instance; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 127 | - * something which is not an article. |
|
| 128 | - * |
|
| 129 | - * @param int $post_id A post id. |
|
| 130 | - * |
|
| 131 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 132 | - * @since 3.1.0 |
|
| 133 | - * |
|
| 134 | - */ |
|
| 135 | - public function is_entity( $post_id ) { |
|
| 136 | - |
|
| 137 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 138 | - |
|
| 139 | - if ( is_wp_error( $terms ) ) { |
|
| 140 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 141 | - |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ( empty( $terms ) ) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /* |
|
| 83 | + /** |
|
| 84 | + * A singleton instance of the Entity service. |
|
| 85 | + * |
|
| 86 | + * @since 3.2.0 |
|
| 87 | + * @access private |
|
| 88 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 89 | + */ |
|
| 90 | + private static $instance; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Create a Wordlift_Entity_Service instance. |
|
| 94 | + * |
|
| 95 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 96 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 97 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 98 | + * |
|
| 99 | + * @since 3.2.0 |
|
| 100 | + * |
|
| 101 | + */ |
|
| 102 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 103 | + |
|
| 104 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 105 | + |
|
| 106 | + $this->ui_service = $ui_service; |
|
| 107 | + $this->relation_service = $relation_service; |
|
| 108 | + $this->entity_uri_service = $entity_uri_service; |
|
| 109 | + |
|
| 110 | + // Set the singleton instance. |
|
| 111 | + self::$instance = $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the singleton instance of the Entity service. |
|
| 116 | + * |
|
| 117 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 118 | + * @since 3.2.0 |
|
| 119 | + */ |
|
| 120 | + public static function get_instance() { |
|
| 121 | + |
|
| 122 | + return self::$instance; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 127 | + * something which is not an article. |
|
| 128 | + * |
|
| 129 | + * @param int $post_id A post id. |
|
| 130 | + * |
|
| 131 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 132 | + * @since 3.1.0 |
|
| 133 | + * |
|
| 134 | + */ |
|
| 135 | + public function is_entity( $post_id ) { |
|
| 136 | + |
|
| 137 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 138 | + |
|
| 139 | + if ( is_wp_error( $terms ) ) { |
|
| 140 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 141 | + |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ( empty( $terms ) ) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /* |
|
| 150 | 150 | * We don't consider an `article` to be an entity. |
| 151 | 151 | * |
| 152 | 152 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 153 | 153 | * |
| 154 | 154 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 155 | 155 | */ |
| 156 | - foreach ( $terms as $term ) { |
|
| 157 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 158 | - return true; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return false; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the proper classification scope for a given entity post |
|
| 167 | - * |
|
| 168 | - * @param integer $post_id An entity post id. |
|
| 169 | - * |
|
| 170 | - * @param string $default The default classification scope, `what` if not |
|
| 171 | - * provided. |
|
| 172 | - * |
|
| 173 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 174 | - * @since 3.5.0 |
|
| 175 | - * |
|
| 176 | - */ |
|
| 177 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 178 | - |
|
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | - return $default; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Retrieve the entity type |
|
| 184 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 186 | - // Retrieve classification boxes configuration |
|
| 187 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | - foreach ( $classification_boxes as $cb ) { |
|
| 189 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 190 | - return $cb['id']; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $default; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Check whether a {@link WP_Post} is used. |
|
| 199 | - * |
|
| 200 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 201 | - * |
|
| 202 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 203 | - */ |
|
| 204 | - public function is_used( $post_id ) { |
|
| 205 | - |
|
| 206 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 207 | - return null; |
|
| 208 | - } |
|
| 209 | - // Retrieve the post |
|
| 210 | - $entity = get_post( $post_id ); |
|
| 211 | - |
|
| 212 | - global $wpdb; |
|
| 213 | - // Retrieve Wordlift relation instances table name |
|
| 214 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 215 | - |
|
| 216 | - // Check is it's referenced / related to another post / entity |
|
| 217 | - $stmt = $wpdb->prepare( |
|
| 218 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 219 | - $entity->ID |
|
| 220 | - ); |
|
| 221 | - |
|
| 222 | - // Perform the query |
|
| 223 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 224 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 225 | - if ( 0 < $relation_instances ) { |
|
| 226 | - return true; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Check if the entity uri is used as meta_value |
|
| 230 | - $stmt = $wpdb->prepare( |
|
| 231 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 232 | - $entity->ID, |
|
| 233 | - wl_get_entity_uri( $entity->ID ) |
|
| 234 | - ); |
|
| 235 | - // Perform the query |
|
| 236 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 237 | - |
|
| 238 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 239 | - if ( 0 < $meta_instances ) { |
|
| 240 | - return true; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // If we are here, it means the current entity is not used at the moment |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 249 | - * |
|
| 250 | - * @param string $uri The entity URI. |
|
| 251 | - * |
|
| 252 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 253 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | - * |
|
| 255 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 256 | - * @since 3.2.0 |
|
| 257 | - * |
|
| 258 | - */ |
|
| 259 | - public function get_entity_post_by_uri( $uri ) { |
|
| 260 | - |
|
| 261 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 266 | - * we check that the post we're saving is the current post. |
|
| 267 | - * |
|
| 268 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 269 | - * |
|
| 270 | - * @since 3.2.0 |
|
| 271 | - * |
|
| 272 | - * @param int $post_id Post ID. |
|
| 273 | - * @param WP_Post $post Post object. |
|
| 274 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 275 | - */ |
|
| 276 | - public function save_post( $post_id, $post, $update ) { |
|
| 277 | - |
|
| 278 | - // Avoid doing anything if post is autosave or a revision. |
|
| 279 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 280 | - return; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // We're setting the alternative label that have been provided via the UI |
|
| 284 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 285 | - // programmatically by some other function: we need to check therefore if |
|
| 286 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 287 | - // |
|
| 288 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 289 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 290 | - return; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // Get the alt labels from the request (or empty array). |
|
| 294 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | - |
|
| 296 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 297 | - // This is via classic editor, so set the alternative labels. |
|
| 298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Set the alternative labels. |
|
| 306 | - * |
|
| 307 | - * @param int $post_id The post id. |
|
| 308 | - * @param array $alt_labels An array of labels. |
|
| 309 | - * |
|
| 310 | - * @since 3.2.0 |
|
| 311 | - * |
|
| 312 | - */ |
|
| 313 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 314 | - |
|
| 315 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 316 | - // instead of post id. |
|
| 317 | - if ( ! is_numeric( $post_id ) ) { |
|
| 318 | - return; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - // Force $alt_labels to be an array |
|
| 322 | - if ( ! is_array( $alt_labels ) ) { |
|
| 323 | - $alt_labels = array( $alt_labels ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 327 | - |
|
| 328 | - // Delete all the existing alternate labels. |
|
| 329 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | - |
|
| 331 | - // Set the alternative labels. |
|
| 332 | - foreach ( $alt_labels as $alt_label ) { |
|
| 333 | - if ( ! empty( $alt_label ) ) { |
|
| 334 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Retrieve the alternate labels. |
|
| 342 | - * |
|
| 343 | - * @param int $post_id Post id. |
|
| 344 | - * |
|
| 345 | - * @return mixed An array of alternative labels. |
|
| 346 | - * @since 3.2.0 |
|
| 347 | - * |
|
| 348 | - */ |
|
| 349 | - public function get_alternative_labels( $post_id ) { |
|
| 350 | - |
|
| 351 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 356 | - * |
|
| 357 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 358 | - * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 359 | - * |
|
| 360 | - * @return array An array with the entity title and labels. |
|
| 361 | - * @since 3.12.0 |
|
| 362 | - */ |
|
| 363 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 364 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 365 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 366 | - } |
|
| 367 | - // Term Reference dont have synonyms yet. |
|
| 368 | - return array(); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 373 | - * |
|
| 374 | - * @param WP_Post $post Post object. |
|
| 375 | - * |
|
| 376 | - * @since 3.2.0 |
|
| 377 | - * |
|
| 378 | - */ |
|
| 379 | - public function edit_form_before_permalink( $post ) { |
|
| 380 | - |
|
| 381 | - // If it's not an entity, return. |
|
| 382 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 383 | - return; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // If disabled by filter, return. |
|
| 387 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 388 | - return; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // Print the input template. |
|
| 392 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 393 | - |
|
| 394 | - // Print all the currently set alternative labels. |
|
| 395 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 396 | - |
|
| 397 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 398 | - |
|
| 399 | - }; |
|
| 400 | - |
|
| 401 | - // Print the button. |
|
| 402 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 403 | - |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Get the URI for the entity with the specified post id. |
|
| 408 | - * |
|
| 409 | - * @param int $post_id The entity post id. |
|
| 410 | - * |
|
| 411 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 412 | - * @since 3.6.0 |
|
| 413 | - * |
|
| 414 | - */ |
|
| 415 | - private function get_uri_for_post( $post_id ) { |
|
| 416 | - |
|
| 417 | - $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 418 | - |
|
| 419 | - // If a null is given, nothing to do |
|
| 420 | - if ( is_null( $post_id ) ) { |
|
| 421 | - return null; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 425 | - |
|
| 426 | - if ( empty( $dataset_uri ) ) { |
|
| 427 | - // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
|
| 428 | - $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 432 | - |
|
| 433 | - /* |
|
| 156 | + foreach ( $terms as $term ) { |
|
| 157 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 158 | + return true; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return false; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the proper classification scope for a given entity post |
|
| 167 | + * |
|
| 168 | + * @param integer $post_id An entity post id. |
|
| 169 | + * |
|
| 170 | + * @param string $default The default classification scope, `what` if not |
|
| 171 | + * provided. |
|
| 172 | + * |
|
| 173 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 174 | + * @since 3.5.0 |
|
| 175 | + * |
|
| 176 | + */ |
|
| 177 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 178 | + |
|
| 179 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | + return $default; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Retrieve the entity type |
|
| 184 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 186 | + // Retrieve classification boxes configuration |
|
| 187 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | + foreach ( $classification_boxes as $cb ) { |
|
| 189 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 190 | + return $cb['id']; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $default; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Check whether a {@link WP_Post} is used. |
|
| 199 | + * |
|
| 200 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 201 | + * |
|
| 202 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 203 | + */ |
|
| 204 | + public function is_used( $post_id ) { |
|
| 205 | + |
|
| 206 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 207 | + return null; |
|
| 208 | + } |
|
| 209 | + // Retrieve the post |
|
| 210 | + $entity = get_post( $post_id ); |
|
| 211 | + |
|
| 212 | + global $wpdb; |
|
| 213 | + // Retrieve Wordlift relation instances table name |
|
| 214 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 215 | + |
|
| 216 | + // Check is it's referenced / related to another post / entity |
|
| 217 | + $stmt = $wpdb->prepare( |
|
| 218 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 219 | + $entity->ID |
|
| 220 | + ); |
|
| 221 | + |
|
| 222 | + // Perform the query |
|
| 223 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 224 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 225 | + if ( 0 < $relation_instances ) { |
|
| 226 | + return true; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Check if the entity uri is used as meta_value |
|
| 230 | + $stmt = $wpdb->prepare( |
|
| 231 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 232 | + $entity->ID, |
|
| 233 | + wl_get_entity_uri( $entity->ID ) |
|
| 234 | + ); |
|
| 235 | + // Perform the query |
|
| 236 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 237 | + |
|
| 238 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 239 | + if ( 0 < $meta_instances ) { |
|
| 240 | + return true; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // If we are here, it means the current entity is not used at the moment |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 249 | + * |
|
| 250 | + * @param string $uri The entity URI. |
|
| 251 | + * |
|
| 252 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 253 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | + * |
|
| 255 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 256 | + * @since 3.2.0 |
|
| 257 | + * |
|
| 258 | + */ |
|
| 259 | + public function get_entity_post_by_uri( $uri ) { |
|
| 260 | + |
|
| 261 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 266 | + * we check that the post we're saving is the current post. |
|
| 267 | + * |
|
| 268 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 269 | + * |
|
| 270 | + * @since 3.2.0 |
|
| 271 | + * |
|
| 272 | + * @param int $post_id Post ID. |
|
| 273 | + * @param WP_Post $post Post object. |
|
| 274 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 275 | + */ |
|
| 276 | + public function save_post( $post_id, $post, $update ) { |
|
| 277 | + |
|
| 278 | + // Avoid doing anything if post is autosave or a revision. |
|
| 279 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 280 | + return; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // We're setting the alternative label that have been provided via the UI |
|
| 284 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 285 | + // programmatically by some other function: we need to check therefore if |
|
| 286 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 287 | + // |
|
| 288 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 289 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 290 | + return; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // Get the alt labels from the request (or empty array). |
|
| 294 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | + |
|
| 296 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 297 | + // This is via classic editor, so set the alternative labels. |
|
| 298 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Set the alternative labels. |
|
| 306 | + * |
|
| 307 | + * @param int $post_id The post id. |
|
| 308 | + * @param array $alt_labels An array of labels. |
|
| 309 | + * |
|
| 310 | + * @since 3.2.0 |
|
| 311 | + * |
|
| 312 | + */ |
|
| 313 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 314 | + |
|
| 315 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 316 | + // instead of post id. |
|
| 317 | + if ( ! is_numeric( $post_id ) ) { |
|
| 318 | + return; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + // Force $alt_labels to be an array |
|
| 322 | + if ( ! is_array( $alt_labels ) ) { |
|
| 323 | + $alt_labels = array( $alt_labels ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 327 | + |
|
| 328 | + // Delete all the existing alternate labels. |
|
| 329 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | + |
|
| 331 | + // Set the alternative labels. |
|
| 332 | + foreach ( $alt_labels as $alt_label ) { |
|
| 333 | + if ( ! empty( $alt_label ) ) { |
|
| 334 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Retrieve the alternate labels. |
|
| 342 | + * |
|
| 343 | + * @param int $post_id Post id. |
|
| 344 | + * |
|
| 345 | + * @return mixed An array of alternative labels. |
|
| 346 | + * @since 3.2.0 |
|
| 347 | + * |
|
| 348 | + */ |
|
| 349 | + public function get_alternative_labels( $post_id ) { |
|
| 350 | + |
|
| 351 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 356 | + * |
|
| 357 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 358 | + * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 359 | + * |
|
| 360 | + * @return array An array with the entity title and labels. |
|
| 361 | + * @since 3.12.0 |
|
| 362 | + */ |
|
| 363 | + public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 364 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
| 365 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 366 | + } |
|
| 367 | + // Term Reference dont have synonyms yet. |
|
| 368 | + return array(); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 373 | + * |
|
| 374 | + * @param WP_Post $post Post object. |
|
| 375 | + * |
|
| 376 | + * @since 3.2.0 |
|
| 377 | + * |
|
| 378 | + */ |
|
| 379 | + public function edit_form_before_permalink( $post ) { |
|
| 380 | + |
|
| 381 | + // If it's not an entity, return. |
|
| 382 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 383 | + return; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // If disabled by filter, return. |
|
| 387 | + if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 388 | + return; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // Print the input template. |
|
| 392 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 393 | + |
|
| 394 | + // Print all the currently set alternative labels. |
|
| 395 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 396 | + |
|
| 397 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 398 | + |
|
| 399 | + }; |
|
| 400 | + |
|
| 401 | + // Print the button. |
|
| 402 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 403 | + |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Get the URI for the entity with the specified post id. |
|
| 408 | + * |
|
| 409 | + * @param int $post_id The entity post id. |
|
| 410 | + * |
|
| 411 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 412 | + * @since 3.6.0 |
|
| 413 | + * |
|
| 414 | + */ |
|
| 415 | + private function get_uri_for_post( $post_id ) { |
|
| 416 | + |
|
| 417 | + $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 418 | + |
|
| 419 | + // If a null is given, nothing to do |
|
| 420 | + if ( is_null( $post_id ) ) { |
|
| 421 | + return null; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 425 | + |
|
| 426 | + if ( empty( $dataset_uri ) ) { |
|
| 427 | + // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
|
| 428 | + $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 432 | + |
|
| 433 | + /* |
|
| 434 | 434 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 435 | 435 | * |
| 436 | 436 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 437 | 437 | */ |
| 438 | - if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 439 | - $uri = null; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - // Set the URI if it isn't set yet. |
|
| 443 | - $post_status = get_post_status( $post_id ); |
|
| 444 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) { |
|
| 445 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 446 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - return $uri; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 453 | - |
|
| 454 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 455 | - return $this->get_uri_for_post( $object_id ); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if ( Object_Type_Enum::USER === $type ) { |
|
| 459 | - $uri = Wordlift_User_Service::get_instance()->get_uri( $object_id ); |
|
| 460 | - |
|
| 461 | - return ( false === $uri ? null : $uri ); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - if ( Object_Type_Enum::TERM === $type ) { |
|
| 465 | - return wl_get_term_entity_uri( $object_id ); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - return null; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Get the alternative label input HTML code. |
|
| 473 | - * |
|
| 474 | - * @param string $value The input value. |
|
| 475 | - * |
|
| 476 | - * @return string The input HTML code. |
|
| 477 | - * @since 3.2.0 |
|
| 478 | - * |
|
| 479 | - */ |
|
| 480 | - private function get_alternative_label_input( $value = '' ) { |
|
| 481 | - |
|
| 482 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Get the number of entity posts published in this blog. |
|
| 487 | - * |
|
| 488 | - * @return int The number of published entity posts. |
|
| 489 | - * @since 3.6.0 |
|
| 490 | - * |
|
| 491 | - */ |
|
| 492 | - public function count() { |
|
| 493 | - global $wpdb; |
|
| 494 | - |
|
| 495 | - // Try to get the count from the transient. |
|
| 496 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | - if ( false !== $count ) { |
|
| 498 | - return $count; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - // Query the count. |
|
| 502 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 503 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 504 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 505 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 506 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 507 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 508 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 509 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | - 'article' |
|
| 511 | - ) ); |
|
| 512 | - |
|
| 513 | - // Store the count in cache. |
|
| 514 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 515 | - |
|
| 516 | - return $count; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 521 | - * call. |
|
| 522 | - * |
|
| 523 | - * @param array $args The arguments for a `get_posts` call. |
|
| 524 | - * |
|
| 525 | - * @return array The arguments for a `get_posts` call. |
|
| 526 | - * @since 3.15.0 |
|
| 527 | - * |
|
| 528 | - */ |
|
| 529 | - public static function add_criterias( $args ) { |
|
| 530 | - |
|
| 531 | - // Build an optimal tax-query. |
|
| 532 | - $tax_query = array( |
|
| 533 | - 'relation' => 'AND', |
|
| 534 | - array( |
|
| 535 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 536 | - 'operator' => 'EXISTS', |
|
| 537 | - ), |
|
| 538 | - array( |
|
| 539 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 540 | - 'field' => 'slug', |
|
| 541 | - 'terms' => 'article', |
|
| 542 | - 'operator' => 'NOT IN', |
|
| 543 | - ), |
|
| 544 | - ); |
|
| 545 | - |
|
| 546 | - return $args + array( |
|
| 547 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 548 | - /* |
|
| 438 | + if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 439 | + $uri = null; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + // Set the URI if it isn't set yet. |
|
| 443 | + $post_status = get_post_status( $post_id ); |
|
| 444 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) { |
|
| 445 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 446 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + return $uri; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 453 | + |
|
| 454 | + if ( Object_Type_Enum::POST === $type ) { |
|
| 455 | + return $this->get_uri_for_post( $object_id ); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if ( Object_Type_Enum::USER === $type ) { |
|
| 459 | + $uri = Wordlift_User_Service::get_instance()->get_uri( $object_id ); |
|
| 460 | + |
|
| 461 | + return ( false === $uri ? null : $uri ); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + if ( Object_Type_Enum::TERM === $type ) { |
|
| 465 | + return wl_get_term_entity_uri( $object_id ); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + return null; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Get the alternative label input HTML code. |
|
| 473 | + * |
|
| 474 | + * @param string $value The input value. |
|
| 475 | + * |
|
| 476 | + * @return string The input HTML code. |
|
| 477 | + * @since 3.2.0 |
|
| 478 | + * |
|
| 479 | + */ |
|
| 480 | + private function get_alternative_label_input( $value = '' ) { |
|
| 481 | + |
|
| 482 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Get the number of entity posts published in this blog. |
|
| 487 | + * |
|
| 488 | + * @return int The number of published entity posts. |
|
| 489 | + * @since 3.6.0 |
|
| 490 | + * |
|
| 491 | + */ |
|
| 492 | + public function count() { |
|
| 493 | + global $wpdb; |
|
| 494 | + |
|
| 495 | + // Try to get the count from the transient. |
|
| 496 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | + if ( false !== $count ) { |
|
| 498 | + return $count; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + // Query the count. |
|
| 502 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 503 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 504 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 505 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 506 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 507 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 508 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 509 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | + 'article' |
|
| 511 | + ) ); |
|
| 512 | + |
|
| 513 | + // Store the count in cache. |
|
| 514 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 515 | + |
|
| 516 | + return $count; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 521 | + * call. |
|
| 522 | + * |
|
| 523 | + * @param array $args The arguments for a `get_posts` call. |
|
| 524 | + * |
|
| 525 | + * @return array The arguments for a `get_posts` call. |
|
| 526 | + * @since 3.15.0 |
|
| 527 | + * |
|
| 528 | + */ |
|
| 529 | + public static function add_criterias( $args ) { |
|
| 530 | + |
|
| 531 | + // Build an optimal tax-query. |
|
| 532 | + $tax_query = array( |
|
| 533 | + 'relation' => 'AND', |
|
| 534 | + array( |
|
| 535 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 536 | + 'operator' => 'EXISTS', |
|
| 537 | + ), |
|
| 538 | + array( |
|
| 539 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 540 | + 'field' => 'slug', |
|
| 541 | + 'terms' => 'article', |
|
| 542 | + 'operator' => 'NOT IN', |
|
| 543 | + ), |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + return $args + array( |
|
| 547 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 548 | + /* |
|
| 549 | 549 | * Ensure compatibility with Polylang. |
| 550 | 550 | * |
| 551 | 551 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -553,102 +553,102 @@ discard block |
||
| 553 | 553 | * |
| 554 | 554 | * @since 3.19.5 |
| 555 | 555 | */ |
| 556 | - 'lang' => '', |
|
| 557 | - 'tax_query' => $tax_query, |
|
| 558 | - ); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Create a new entity. |
|
| 563 | - * |
|
| 564 | - * @param string $name The entity name. |
|
| 565 | - * @param string $type_uri The entity's type URI. |
|
| 566 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 567 | - * @param string $status The post status, by default 'publish'. |
|
| 568 | - * |
|
| 569 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 570 | - * @since 3.9.0 |
|
| 571 | - * |
|
| 572 | - */ |
|
| 573 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 574 | - |
|
| 575 | - // Create an entity for the publisher. |
|
| 576 | - $post_id = @wp_insert_post( array( |
|
| 577 | - 'post_type' => self::TYPE_NAME, |
|
| 578 | - 'post_title' => $name, |
|
| 579 | - 'post_status' => $status, |
|
| 580 | - 'post_content' => '', |
|
| 581 | - ) ); |
|
| 582 | - |
|
| 583 | - // Return the error if any. |
|
| 584 | - if ( is_wp_error( $post_id ) ) { |
|
| 585 | - return $post_id; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // Set the entity logo. |
|
| 589 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | - set_post_thumbnail( $post_id, $logo ); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - // Set the entity type. |
|
| 594 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 595 | - |
|
| 596 | - return $post_id; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Get the entities related to the one with the specified id. By default only |
|
| 601 | - * published entities will be returned. |
|
| 602 | - * |
|
| 603 | - * @param int $id The post id. |
|
| 604 | - * @param string $post_status The target post status (default = publish). |
|
| 605 | - * |
|
| 606 | - * @return array An array of post ids. |
|
| 607 | - * @since 3.10.0 |
|
| 608 | - * |
|
| 609 | - */ |
|
| 610 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 611 | - |
|
| 612 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Get the list of entities. |
|
| 617 | - * |
|
| 618 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 619 | - * |
|
| 620 | - * @return array An array of entity posts. |
|
| 621 | - * @since 3.12.2 |
|
| 622 | - * |
|
| 623 | - */ |
|
| 624 | - public function get( $params = array() ) { |
|
| 625 | - |
|
| 626 | - // Set the defaults. |
|
| 627 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 628 | - |
|
| 629 | - // Merge the defaults with the provided parameters. |
|
| 630 | - $args = wp_parse_args( $params, $defaults ); |
|
| 631 | - |
|
| 632 | - // Call the `get_posts` function. |
|
| 633 | - return get_posts( $args ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * The list of post type names which can be used for entities |
|
| 638 | - * |
|
| 639 | - * Criteria is that the post type is public. The list of valid post types |
|
| 640 | - * can be overridden with a filter. |
|
| 641 | - * |
|
| 642 | - * @return array Array containing the names of the valid post types. |
|
| 643 | - * @since 3.15.0 |
|
| 644 | - * |
|
| 645 | - */ |
|
| 646 | - static function valid_entity_post_types() { |
|
| 647 | - |
|
| 648 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 649 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 650 | - |
|
| 651 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 652 | - } |
|
| 556 | + 'lang' => '', |
|
| 557 | + 'tax_query' => $tax_query, |
|
| 558 | + ); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Create a new entity. |
|
| 563 | + * |
|
| 564 | + * @param string $name The entity name. |
|
| 565 | + * @param string $type_uri The entity's type URI. |
|
| 566 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 567 | + * @param string $status The post status, by default 'publish'. |
|
| 568 | + * |
|
| 569 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 570 | + * @since 3.9.0 |
|
| 571 | + * |
|
| 572 | + */ |
|
| 573 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 574 | + |
|
| 575 | + // Create an entity for the publisher. |
|
| 576 | + $post_id = @wp_insert_post( array( |
|
| 577 | + 'post_type' => self::TYPE_NAME, |
|
| 578 | + 'post_title' => $name, |
|
| 579 | + 'post_status' => $status, |
|
| 580 | + 'post_content' => '', |
|
| 581 | + ) ); |
|
| 582 | + |
|
| 583 | + // Return the error if any. |
|
| 584 | + if ( is_wp_error( $post_id ) ) { |
|
| 585 | + return $post_id; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // Set the entity logo. |
|
| 589 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | + set_post_thumbnail( $post_id, $logo ); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + // Set the entity type. |
|
| 594 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 595 | + |
|
| 596 | + return $post_id; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Get the entities related to the one with the specified id. By default only |
|
| 601 | + * published entities will be returned. |
|
| 602 | + * |
|
| 603 | + * @param int $id The post id. |
|
| 604 | + * @param string $post_status The target post status (default = publish). |
|
| 605 | + * |
|
| 606 | + * @return array An array of post ids. |
|
| 607 | + * @since 3.10.0 |
|
| 608 | + * |
|
| 609 | + */ |
|
| 610 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 611 | + |
|
| 612 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Get the list of entities. |
|
| 617 | + * |
|
| 618 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 619 | + * |
|
| 620 | + * @return array An array of entity posts. |
|
| 621 | + * @since 3.12.2 |
|
| 622 | + * |
|
| 623 | + */ |
|
| 624 | + public function get( $params = array() ) { |
|
| 625 | + |
|
| 626 | + // Set the defaults. |
|
| 627 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 628 | + |
|
| 629 | + // Merge the defaults with the provided parameters. |
|
| 630 | + $args = wp_parse_args( $params, $defaults ); |
|
| 631 | + |
|
| 632 | + // Call the `get_posts` function. |
|
| 633 | + return get_posts( $args ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * The list of post type names which can be used for entities |
|
| 638 | + * |
|
| 639 | + * Criteria is that the post type is public. The list of valid post types |
|
| 640 | + * can be overridden with a filter. |
|
| 641 | + * |
|
| 642 | + * @return array Array containing the names of the valid post types. |
|
| 643 | + * @since 3.15.0 |
|
| 644 | + * |
|
| 645 | + */ |
|
| 646 | + static function valid_entity_post_types() { |
|
| 647 | + |
|
| 648 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 649 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 650 | + |
|
| 651 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | 654 | } |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * @since 3.2.0 |
| 100 | 100 | * |
| 101 | 101 | */ |
| 102 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 102 | + public function __construct($ui_service, $relation_service, $entity_uri_service) { |
|
| 103 | 103 | |
| 104 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 104 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 105 | 105 | |
| 106 | 106 | $this->ui_service = $ui_service; |
| 107 | 107 | $this->relation_service = $relation_service; |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | * @since 3.1.0 |
| 133 | 133 | * |
| 134 | 134 | */ |
| 135 | - public function is_entity( $post_id ) { |
|
| 135 | + public function is_entity($post_id) { |
|
| 136 | 136 | |
| 137 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 137 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 138 | 138 | |
| 139 | - if ( is_wp_error( $terms ) ) { |
|
| 140 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | + if (is_wp_error($terms)) { |
|
| 140 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 141 | 141 | |
| 142 | 142 | return false; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if ( empty( $terms ) ) { |
|
| 145 | + if (empty($terms)) { |
|
| 146 | 146 | return false; |
| 147 | 147 | } |
| 148 | 148 | |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 155 | 155 | */ |
| 156 | - foreach ( $terms as $term ) { |
|
| 157 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 156 | + foreach ($terms as $term) { |
|
| 157 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
| 158 | 158 | return true; |
| 159 | 159 | } |
| 160 | 160 | } |
@@ -174,19 +174,19 @@ discard block |
||
| 174 | 174 | * @since 3.5.0 |
| 175 | 175 | * |
| 176 | 176 | */ |
| 177 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 177 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 178 | 178 | |
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 179 | + if (false === $this->is_entity($post_id)) { |
|
| 180 | 180 | return $default; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // Retrieve the entity type |
| 184 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 185 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 186 | 186 | // Retrieve classification boxes configuration |
| 187 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | - foreach ( $classification_boxes as $cb ) { |
|
| 189 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 187 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 188 | + foreach ($classification_boxes as $cb) { |
|
| 189 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 190 | 190 | return $cb['id']; |
| 191 | 191 | } |
| 192 | 192 | } |
@@ -201,13 +201,13 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 203 | 203 | */ |
| 204 | - public function is_used( $post_id ) { |
|
| 204 | + public function is_used($post_id) { |
|
| 205 | 205 | |
| 206 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 206 | + if (false === $this->is_entity($post_id)) { |
|
| 207 | 207 | return null; |
| 208 | 208 | } |
| 209 | 209 | // Retrieve the post |
| 210 | - $entity = get_post( $post_id ); |
|
| 210 | + $entity = get_post($post_id); |
|
| 211 | 211 | |
| 212 | 212 | global $wpdb; |
| 213 | 213 | // Retrieve Wordlift relation instances table name |
@@ -220,9 +220,9 @@ discard block |
||
| 220 | 220 | ); |
| 221 | 221 | |
| 222 | 222 | // Perform the query |
| 223 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 223 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 224 | 224 | // If there is at least one relation instance for the current entity, then it's used |
| 225 | - if ( 0 < $relation_instances ) { |
|
| 225 | + if (0 < $relation_instances) { |
|
| 226 | 226 | return true; |
| 227 | 227 | } |
| 228 | 228 | |
@@ -230,13 +230,13 @@ discard block |
||
| 230 | 230 | $stmt = $wpdb->prepare( |
| 231 | 231 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 232 | 232 | $entity->ID, |
| 233 | - wl_get_entity_uri( $entity->ID ) |
|
| 233 | + wl_get_entity_uri($entity->ID) |
|
| 234 | 234 | ); |
| 235 | 235 | // Perform the query |
| 236 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 236 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 237 | 237 | |
| 238 | 238 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 239 | - if ( 0 < $meta_instances ) { |
|
| 239 | + if (0 < $meta_instances) { |
|
| 240 | 240 | return true; |
| 241 | 241 | } |
| 242 | 242 | |
@@ -256,9 +256,9 @@ discard block |
||
| 256 | 256 | * @since 3.2.0 |
| 257 | 257 | * |
| 258 | 258 | */ |
| 259 | - public function get_entity_post_by_uri( $uri ) { |
|
| 259 | + public function get_entity_post_by_uri($uri) { |
|
| 260 | 260 | |
| 261 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 261 | + return $this->entity_uri_service->get_entity($uri); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -273,10 +273,10 @@ discard block |
||
| 273 | 273 | * @param WP_Post $post Post object. |
| 274 | 274 | * @param bool $update Whether this is an existing post being updated or not. |
| 275 | 275 | */ |
| 276 | - public function save_post( $post_id, $post, $update ) { |
|
| 276 | + public function save_post($post_id, $post, $update) { |
|
| 277 | 277 | |
| 278 | 278 | // Avoid doing anything if post is autosave or a revision. |
| 279 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 279 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 280 | 280 | return; |
| 281 | 281 | } |
| 282 | 282 | |
@@ -286,16 +286,16 @@ discard block |
||
| 286 | 286 | // the $post_id in the save_post call matches the post id set in the request. |
| 287 | 287 | // |
| 288 | 288 | // If this is not the current post being saved or if it's not an entity, return. |
| 289 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 289 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 290 | 290 | return; |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // Get the alt labels from the request (or empty array). |
| 294 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 294 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | 295 | |
| 296 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 296 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 297 | 297 | // This is via classic editor, so set the alternative labels. |
| 298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 298 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
@@ -310,28 +310,28 @@ discard block |
||
| 310 | 310 | * @since 3.2.0 |
| 311 | 311 | * |
| 312 | 312 | */ |
| 313 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 313 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 314 | 314 | |
| 315 | 315 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 316 | 316 | // instead of post id. |
| 317 | - if ( ! is_numeric( $post_id ) ) { |
|
| 317 | + if ( ! is_numeric($post_id)) { |
|
| 318 | 318 | return; |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | // Force $alt_labels to be an array |
| 322 | - if ( ! is_array( $alt_labels ) ) { |
|
| 323 | - $alt_labels = array( $alt_labels ); |
|
| 322 | + if ( ! is_array($alt_labels)) { |
|
| 323 | + $alt_labels = array($alt_labels); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 326 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 327 | 327 | |
| 328 | 328 | // Delete all the existing alternate labels. |
| 329 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 329 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 330 | 330 | |
| 331 | 331 | // Set the alternative labels. |
| 332 | - foreach ( $alt_labels as $alt_label ) { |
|
| 333 | - if ( ! empty( $alt_label ) ) { |
|
| 334 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 332 | + foreach ($alt_labels as $alt_label) { |
|
| 333 | + if ( ! empty($alt_label)) { |
|
| 334 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label); |
|
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
@@ -346,9 +346,9 @@ discard block |
||
| 346 | 346 | * @since 3.2.0 |
| 347 | 347 | * |
| 348 | 348 | */ |
| 349 | - public function get_alternative_labels( $post_id ) { |
|
| 349 | + public function get_alternative_labels($post_id) { |
|
| 350 | 350 | |
| 351 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 351 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -360,9 +360,9 @@ discard block |
||
| 360 | 360 | * @return array An array with the entity title and labels. |
| 361 | 361 | * @since 3.12.0 |
| 362 | 362 | */ |
| 363 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 364 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 365 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 363 | + public function get_labels($post_id, $object_type = Object_Type_Enum::POST) { |
|
| 364 | + if ($object_type === Object_Type_Enum::POST) { |
|
| 365 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 366 | 366 | } |
| 367 | 367 | // Term Reference dont have synonyms yet. |
| 368 | 368 | return array(); |
@@ -376,30 +376,30 @@ discard block |
||
| 376 | 376 | * @since 3.2.0 |
| 377 | 377 | * |
| 378 | 378 | */ |
| 379 | - public function edit_form_before_permalink( $post ) { |
|
| 379 | + public function edit_form_before_permalink($post) { |
|
| 380 | 380 | |
| 381 | 381 | // If it's not an entity, return. |
| 382 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 382 | + if ( ! $this->is_entity($post->ID)) { |
|
| 383 | 383 | return; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | // If disabled by filter, return. |
| 387 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 387 | + if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) { |
|
| 388 | 388 | return; |
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | // Print the input template. |
| 392 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 392 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 393 | 393 | |
| 394 | 394 | // Print all the currently set alternative labels. |
| 395 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 395 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 396 | 396 | |
| 397 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 397 | + echo $this->get_alternative_label_input($alt_label); |
|
| 398 | 398 | |
| 399 | 399 | }; |
| 400 | 400 | |
| 401 | 401 | // Print the button. |
| 402 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 402 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 403 | 403 | |
| 404 | 404 | } |
| 405 | 405 | |
@@ -412,57 +412,57 @@ discard block |
||
| 412 | 412 | * @since 3.6.0 |
| 413 | 413 | * |
| 414 | 414 | */ |
| 415 | - private function get_uri_for_post( $post_id ) { |
|
| 415 | + private function get_uri_for_post($post_id) { |
|
| 416 | 416 | |
| 417 | - $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 417 | + $log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 418 | 418 | |
| 419 | 419 | // If a null is given, nothing to do |
| 420 | - if ( is_null( $post_id ) ) { |
|
| 420 | + if (is_null($post_id)) { |
|
| 421 | 421 | return null; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
| 425 | 425 | |
| 426 | - if ( empty( $dataset_uri ) ) { |
|
| 426 | + if (empty($dataset_uri)) { |
|
| 427 | 427 | // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
| 428 | - $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 428 | + $log->debug('Continuing, dataset uri not configured...'); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 431 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 432 | 432 | |
| 433 | 433 | /* |
| 434 | 434 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 435 | 435 | * |
| 436 | 436 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 437 | 437 | */ |
| 438 | - if ( empty( $dataset_uri ) || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 438 | + if (empty($dataset_uri) || 0 !== strpos($uri, $dataset_uri)) { |
|
| 439 | 439 | $uri = null; |
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | // Set the URI if it isn't set yet. |
| 443 | - $post_status = get_post_status( $post_id ); |
|
| 444 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'inherit' !== $post_status ) { |
|
| 445 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 446 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 443 | + $post_status = get_post_status($post_id); |
|
| 444 | + if (empty($uri) && 'auto-draft' !== $post_status && 'inherit' !== $post_status) { |
|
| 445 | + $uri = wl_build_entity_uri($post_id); |
|
| 446 | + wl_set_entity_uri($post_id, $uri); |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | 449 | return $uri; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 452 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
| 453 | 453 | |
| 454 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 455 | - return $this->get_uri_for_post( $object_id ); |
|
| 454 | + if (Object_Type_Enum::POST === $type) { |
|
| 455 | + return $this->get_uri_for_post($object_id); |
|
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - if ( Object_Type_Enum::USER === $type ) { |
|
| 459 | - $uri = Wordlift_User_Service::get_instance()->get_uri( $object_id ); |
|
| 458 | + if (Object_Type_Enum::USER === $type) { |
|
| 459 | + $uri = Wordlift_User_Service::get_instance()->get_uri($object_id); |
|
| 460 | 460 | |
| 461 | - return ( false === $uri ? null : $uri ); |
|
| 461 | + return (false === $uri ? null : $uri); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | - if ( Object_Type_Enum::TERM === $type ) { |
|
| 465 | - return wl_get_term_entity_uri( $object_id ); |
|
| 464 | + if (Object_Type_Enum::TERM === $type) { |
|
| 465 | + return wl_get_term_entity_uri($object_id); |
|
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | return null; |
@@ -477,9 +477,9 @@ discard block |
||
| 477 | 477 | * @since 3.2.0 |
| 478 | 478 | * |
| 479 | 479 | */ |
| 480 | - private function get_alternative_label_input( $value = '' ) { |
|
| 480 | + private function get_alternative_label_input($value = '') { |
|
| 481 | 481 | |
| 482 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 482 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | /** |
@@ -493,13 +493,13 @@ discard block |
||
| 493 | 493 | global $wpdb; |
| 494 | 494 | |
| 495 | 495 | // Try to get the count from the transient. |
| 496 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | - if ( false !== $count ) { |
|
| 496 | + $count = get_transient('_wl_entity_service__count'); |
|
| 497 | + if (false !== $count) { |
|
| 498 | 498 | return $count; |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | // Query the count. |
| 502 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 502 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 503 | 503 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 504 | 504 | . " FROM {$wpdb->term_relationships} tr" |
| 505 | 505 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -508,10 +508,10 @@ discard block |
||
| 508 | 508 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 509 | 509 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 510 | 510 | 'article' |
| 511 | - ) ); |
|
| 511 | + )); |
|
| 512 | 512 | |
| 513 | 513 | // Store the count in cache. |
| 514 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 514 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 515 | 515 | |
| 516 | 516 | return $count; |
| 517 | 517 | } |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | * @since 3.15.0 |
| 527 | 527 | * |
| 528 | 528 | */ |
| 529 | - public static function add_criterias( $args ) { |
|
| 529 | + public static function add_criterias($args) { |
|
| 530 | 530 | |
| 531 | 531 | // Build an optimal tax-query. |
| 532 | 532 | $tax_query = array( |
@@ -570,28 +570,28 @@ discard block |
||
| 570 | 570 | * @since 3.9.0 |
| 571 | 571 | * |
| 572 | 572 | */ |
| 573 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 573 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 574 | 574 | |
| 575 | 575 | // Create an entity for the publisher. |
| 576 | - $post_id = @wp_insert_post( array( |
|
| 576 | + $post_id = @wp_insert_post(array( |
|
| 577 | 577 | 'post_type' => self::TYPE_NAME, |
| 578 | 578 | 'post_title' => $name, |
| 579 | 579 | 'post_status' => $status, |
| 580 | 580 | 'post_content' => '', |
| 581 | - ) ); |
|
| 581 | + )); |
|
| 582 | 582 | |
| 583 | 583 | // Return the error if any. |
| 584 | - if ( is_wp_error( $post_id ) ) { |
|
| 584 | + if (is_wp_error($post_id)) { |
|
| 585 | 585 | return $post_id; |
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | // Set the entity logo. |
| 589 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | - set_post_thumbnail( $post_id, $logo ); |
|
| 589 | + if ($logo && is_numeric($logo)) { |
|
| 590 | + set_post_thumbnail($post_id, $logo); |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | // Set the entity type. |
| 594 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 594 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 595 | 595 | |
| 596 | 596 | return $post_id; |
| 597 | 597 | } |
@@ -607,9 +607,9 @@ discard block |
||
| 607 | 607 | * @since 3.10.0 |
| 608 | 608 | * |
| 609 | 609 | */ |
| 610 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 610 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 611 | 611 | |
| 612 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 612 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -621,16 +621,16 @@ discard block |
||
| 621 | 621 | * @since 3.12.2 |
| 622 | 622 | * |
| 623 | 623 | */ |
| 624 | - public function get( $params = array() ) { |
|
| 624 | + public function get($params = array()) { |
|
| 625 | 625 | |
| 626 | 626 | // Set the defaults. |
| 627 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 627 | + $defaults = array('post_type' => 'entity'); |
|
| 628 | 628 | |
| 629 | 629 | // Merge the defaults with the provided parameters. |
| 630 | - $args = wp_parse_args( $params, $defaults ); |
|
| 630 | + $args = wp_parse_args($params, $defaults); |
|
| 631 | 631 | |
| 632 | 632 | // Call the `get_posts` function. |
| 633 | - return get_posts( $args ); |
|
| 633 | + return get_posts($args); |
|
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | /** |
@@ -646,9 +646,9 @@ discard block |
||
| 646 | 646 | static function valid_entity_post_types() { |
| 647 | 647 | |
| 648 | 648 | // Ignore builtins in the call to avoid getting attachments. |
| 649 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 649 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 650 | 650 | |
| 651 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 651 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | } |
@@ -15,45 +15,45 @@ discard block |
||
| 15 | 15 | * @return mixed |
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | - // Get the last 20 articles by post date. |
|
| 19 | - // For each article get the entities they reference. |
|
| 20 | - $post_ids = get_posts( array( |
|
| 21 | - 'numberposts' => 20, |
|
| 22 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | - 'fields' => 'ids', // Only get post IDs. |
|
| 24 | - 'post_status' => 'publish', |
|
| 25 | - 'tax_query' => array( |
|
| 26 | - 'relation' => 'OR', |
|
| 27 | - array( |
|
| 28 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | - 'operator' => 'NOT EXISTS', |
|
| 30 | - ), |
|
| 31 | - array( |
|
| 32 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | - 'field' => 'slug', |
|
| 34 | - 'terms' => 'article', |
|
| 35 | - ), |
|
| 36 | - ), |
|
| 37 | - 'orderby' => 'post_date', |
|
| 38 | - 'order' => 'DESC', |
|
| 39 | - ) ); |
|
| 40 | - |
|
| 41 | - if ( empty( $post_ids ) ) { |
|
| 42 | - return null; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $entities = array(); |
|
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 54 | - } else { |
|
| 55 | - return $post_ids[0]; |
|
| 56 | - } |
|
| 18 | + // Get the last 20 articles by post date. |
|
| 19 | + // For each article get the entities they reference. |
|
| 20 | + $post_ids = get_posts( array( |
|
| 21 | + 'numberposts' => 20, |
|
| 22 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | + 'fields' => 'ids', // Only get post IDs. |
|
| 24 | + 'post_status' => 'publish', |
|
| 25 | + 'tax_query' => array( |
|
| 26 | + 'relation' => 'OR', |
|
| 27 | + array( |
|
| 28 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | + 'operator' => 'NOT EXISTS', |
|
| 30 | + ), |
|
| 31 | + array( |
|
| 32 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | + 'field' => 'slug', |
|
| 34 | + 'terms' => 'article', |
|
| 35 | + ), |
|
| 36 | + ), |
|
| 37 | + 'orderby' => 'post_date', |
|
| 38 | + 'order' => 'DESC', |
|
| 39 | + ) ); |
|
| 40 | + |
|
| 41 | + if ( empty( $post_ids ) ) { |
|
| 42 | + return null; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $entities = array(); |
|
| 46 | + foreach ( $post_ids as $id ) { |
|
| 47 | + $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $famous_entities = array_count_values( $entities ); |
|
| 51 | + arsort( $famous_entities ); |
|
| 52 | + if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | + return key( $famous_entities ); |
|
| 54 | + } else { |
|
| 55 | + return $post_ids[0]; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -71,60 +71,60 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 76 | - return $related; |
|
| 77 | - } |
|
| 78 | - } |
|
| 74 | + if ( ! is_null( $related ) ) { |
|
| 75 | + if ( 0 === $depth ) { |
|
| 76 | + return $related; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 81 | 81 | |
| 82 | - // Create a related array which will hold entities and relations. |
|
| 83 | - if ( is_null( $related ) ) { |
|
| 84 | - $related = array( |
|
| 85 | - 'entities' => array( $entity_id ), |
|
| 86 | - 'relations' => array(), |
|
| 87 | - ); |
|
| 88 | - } |
|
| 82 | + // Create a related array which will hold entities and relations. |
|
| 83 | + if ( is_null( $related ) ) { |
|
| 84 | + $related = array( |
|
| 85 | + 'entities' => array( $entity_id ), |
|
| 86 | + 'relations' => array(), |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - // Get related entities |
|
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | - 'status' => 'publish', |
|
| 93 | - ) ); |
|
| 90 | + // Get related entities |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | + 'status' => 'publish', |
|
| 93 | + ) ); |
|
| 94 | 94 | |
| 95 | - // If the current node is an entity, add related posts too |
|
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | - 'status' => 'publish', |
|
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 95 | + // If the current node is an entity, add related posts too |
|
| 96 | + $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity( $entity_id ) ) ? |
|
| 98 | + wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | + 'status' => 'publish', |
|
| 100 | + ) ) : |
|
| 101 | + array(); |
|
| 102 | 102 | |
| 103 | - // Merge results and remove duplicated entries |
|
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + // Merge results and remove duplicated entries |
|
| 104 | + $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 105 | 105 | |
| 106 | - // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | - shuffle( $related_ids ); |
|
| 106 | + // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | + shuffle( $related_ids ); |
|
| 108 | 108 | |
| 109 | - // Now we have all the related IDs. |
|
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + // Now we have all the related IDs. |
|
| 110 | + foreach ( $related_ids as $related_id ) { |
|
| 111 | 111 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | - return $related; |
|
| 114 | - } |
|
| 112 | + if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | + return $related; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 116 | + $related['relations'][] = array( $entity_id, $related_id ); |
|
| 117 | 117 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | - // Found new related entity! |
|
| 120 | - $related['entities'][] = $related_id; |
|
| 118 | + if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | + // Found new related entity! |
|
| 120 | + $related['entities'][] = $related_id; |
|
| 121 | 121 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | - } |
|
| 124 | - } |
|
| 122 | + $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // End condition 2: no more entities to search for. |
|
| 127 | - return $related; |
|
| 126 | + // End condition 2: no more entities to search for. |
|
| 127 | + return $related; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -138,57 +138,57 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | function wl_shortcode_chord_get_graph( $data ) { |
| 140 | 140 | |
| 141 | - // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 144 | - |
|
| 145 | - // Skip non-existing posts. |
|
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | - |
|
| 149 | - return $item; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | - |
|
| 155 | - // The following log may create a circular loop. |
|
| 156 | - // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | - |
|
| 158 | - // TODO: get all images |
|
| 159 | - $thumbnail = null; |
|
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $entity = array( |
|
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 171 | - 'label' => $post->post_title, |
|
| 172 | - 'type' => $post->post_type, |
|
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - $item = $entity; |
|
| 178 | - } ); |
|
| 179 | - |
|
| 180 | - // Refactor the relations. |
|
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | - $relation = array( |
|
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - $item = $relation; |
|
| 188 | - } ); |
|
| 189 | - |
|
| 190 | - // Return the JSON representation. |
|
| 191 | - return $data; |
|
| 141 | + // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | + array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | + $post = get_post( $item ); |
|
| 144 | + |
|
| 145 | + // Skip non-existing posts. |
|
| 146 | + if ( is_null( $post ) ) { |
|
| 147 | + wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | + |
|
| 149 | + return $item; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | + $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | + |
|
| 155 | + // The following log may create a circular loop. |
|
| 156 | + // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | + |
|
| 158 | + // TODO: get all images |
|
| 159 | + $thumbnail = null; |
|
| 160 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | + $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | + if ( false !== $attachment ) { |
|
| 164 | + $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $entity = array( |
|
| 169 | + 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | + 'url' => get_permalink( $item ), |
|
| 171 | + 'label' => $post->post_title, |
|
| 172 | + 'type' => $post->post_type, |
|
| 173 | + 'thumbnails' => array( $thumbnail ), |
|
| 174 | + 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + $item = $entity; |
|
| 178 | + } ); |
|
| 179 | + |
|
| 180 | + // Refactor the relations. |
|
| 181 | + array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | + $relation = array( |
|
| 183 | + 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | + 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + $item = $relation; |
|
| 188 | + } ); |
|
| 189 | + |
|
| 190 | + // Return the JSON representation. |
|
| 191 | + return $data; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | function wl_shortcode_chord_ajax() { |
| 201 | 201 | |
| 202 | - $post_id = isset( $_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0 ; |
|
| 203 | - $depth = isset( $_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2; |
|
| 202 | + $post_id = isset( $_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0 ; |
|
| 203 | + $depth = isset( $_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2; |
|
| 204 | 204 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 205 | + $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | + $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 207 | 207 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 208 | + wl_core_send_json( $graph ); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | 18 | // Get the last 20 articles by post date. |
| 19 | 19 | // For each article get the entities they reference. |
| 20 | - $post_ids = get_posts( array( |
|
| 20 | + $post_ids = get_posts(array( |
|
| 21 | 21 | 'numberposts' => 20, |
| 22 | 22 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 23 | 23 | 'fields' => 'ids', // Only get post IDs. |
@@ -36,21 +36,21 @@ discard block |
||
| 36 | 36 | ), |
| 37 | 37 | 'orderby' => 'post_date', |
| 38 | 38 | 'order' => 'DESC', |
| 39 | - ) ); |
|
| 39 | + )); |
|
| 40 | 40 | |
| 41 | - if ( empty( $post_ids ) ) { |
|
| 41 | + if (empty($post_ids)) { |
|
| 42 | 42 | return null; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $entities = array(); |
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 46 | + foreach ($post_ids as $id) { |
|
| 47 | + $entities = array_merge($entities, wl_core_get_related_entity_ids($id)); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 50 | + $famous_entities = array_count_values($entities); |
|
| 51 | + arsort($famous_entities); |
|
| 52 | + if (sizeof($famous_entities) >= 1) { |
|
| 53 | + return key($famous_entities); |
|
| 54 | 54 | } else { |
| 55 | 55 | return $post_ids[0]; |
| 56 | 56 | } |
@@ -69,57 +69,56 @@ discard block |
||
| 69 | 69 | * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity. |
| 70 | 70 | * |
| 71 | 71 | */ |
| 72 | -function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
|
| 72 | +function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) { |
|
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 74 | + if ( ! is_null($related)) { |
|
| 75 | + if (0 === $depth) { |
|
| 76 | 76 | return $related; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".(is_null($related) ? 'yes' : 'no')." ]"); |
|
| 81 | 81 | |
| 82 | 82 | // Create a related array which will hold entities and relations. |
| 83 | - if ( is_null( $related ) ) { |
|
| 83 | + if (is_null($related)) { |
|
| 84 | 84 | $related = array( |
| 85 | - 'entities' => array( $entity_id ), |
|
| 85 | + 'entities' => array($entity_id), |
|
| 86 | 86 | 'relations' => array(), |
| 87 | 87 | ); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | // Get related entities |
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids($entity_id, array( |
|
| 92 | 92 | 'status' => 'publish', |
| 93 | - ) ); |
|
| 93 | + )); |
|
| 94 | 94 | |
| 95 | 95 | // If the current node is an entity, add related posts too |
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 96 | + $related_post_ids = (Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity($entity_id)) ? |
|
| 98 | + wl_core_get_related_post_ids($entity_id, array( |
|
| 99 | 99 | 'status' => 'publish', |
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 100 | + )) : array(); |
|
| 102 | 101 | |
| 103 | 102 | // Merge results and remove duplicated entries |
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + $related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids)); |
|
| 105 | 104 | |
| 106 | 105 | // TODO: List of entities ($rel) should be ordered by interest factors. |
| 107 | - shuffle( $related_ids ); |
|
| 106 | + shuffle($related_ids); |
|
| 108 | 107 | |
| 109 | 108 | // Now we have all the related IDs. |
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + foreach ($related_ids as $related_id) { |
|
| 111 | 110 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 111 | + if (count($related['entities']) >= $max_size) { |
|
| 113 | 112 | return $related; |
| 114 | 113 | } |
| 115 | 114 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 115 | + $related['relations'][] = array($entity_id, $related_id); |
|
| 117 | 116 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 117 | + if ( ! in_array($related_id, $related['entities'])) { |
|
| 119 | 118 | // Found new related entity! |
| 120 | 119 | $related['entities'][] = $related_id; |
| 121 | 120 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 121 | + $related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size); |
|
| 123 | 122 | } |
| 124 | 123 | } |
| 125 | 124 | |
@@ -136,52 +135,52 @@ discard block |
||
| 136 | 135 | * |
| 137 | 136 | * @return mixed|string |
| 138 | 137 | */ |
| 139 | -function wl_shortcode_chord_get_graph( $data ) { |
|
| 138 | +function wl_shortcode_chord_get_graph($data) { |
|
| 140 | 139 | |
| 141 | 140 | // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 141 | + array_walk($data['entities'], function(&$item) { |
|
| 142 | + $post = get_post($item); |
|
| 144 | 143 | |
| 145 | 144 | // Skip non-existing posts. |
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 145 | + if (is_null($post)) { |
|
| 146 | + wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]"); |
|
| 148 | 147 | |
| 149 | 148 | return $item; |
| 150 | 149 | } |
| 151 | 150 | |
| 152 | 151 | // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 152 | + $term = Wordlift_Entity_Type_Service::get_instance()->get($item); |
|
| 154 | 153 | |
| 155 | 154 | // The following log may create a circular loop. |
| 156 | 155 | // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
| 157 | 156 | |
| 158 | 157 | // TODO: get all images |
| 159 | 158 | $thumbnail = null; |
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 159 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 160 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 161 | + $attachment = wp_get_attachment_image_src($thumbnail_id); |
|
| 162 | + if (false !== $attachment) { |
|
| 163 | + $thumbnail = esc_attr($attachment[0]); |
|
| 165 | 164 | } |
| 166 | 165 | } |
| 167 | 166 | |
| 168 | 167 | $entity = array( |
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 168 | + 'uri' => wl_get_entity_uri($item), |
|
| 169 | + 'url' => get_permalink($item), |
|
| 171 | 170 | 'label' => $post->post_title, |
| 172 | 171 | 'type' => $post->post_type, |
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 172 | + 'thumbnails' => array($thumbnail), |
|
| 173 | + 'css_class' => (isset($term['css_class']) ? $term['css_class'] : ''), |
|
| 175 | 174 | ); |
| 176 | 175 | |
| 177 | 176 | $item = $entity; |
| 178 | 177 | } ); |
| 179 | 178 | |
| 180 | 179 | // Refactor the relations. |
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 180 | + array_walk($data['relations'], function(&$item) { |
|
| 182 | 181 | $relation = array( |
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 182 | + 's' => wl_get_entity_uri($item[0]), |
|
| 183 | + 'o' => wl_get_entity_uri($item[1]), |
|
| 185 | 184 | ); |
| 186 | 185 | |
| 187 | 186 | $item = $relation; |
@@ -199,14 +198,14 @@ discard block |
||
| 199 | 198 | */ |
| 200 | 199 | function wl_shortcode_chord_ajax() { |
| 201 | 200 | |
| 202 | - $post_id = isset( $_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0 ; |
|
| 203 | - $depth = isset( $_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2; |
|
| 201 | + $post_id = isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0; |
|
| 202 | + $depth = isset($_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2; |
|
| 204 | 203 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 204 | + $relations = wl_shortcode_chord_get_relations($post_id, $depth); |
|
| 205 | + $graph = wl_shortcode_chord_get_graph($relations); |
|
| 207 | 206 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 207 | + wl_core_send_json($graph); |
|
| 209 | 208 | } |
| 210 | 209 | |
| 211 | -add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 212 | -add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 210 | +add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 211 | +add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax'); |
|
@@ -2,110 +2,110 @@ |
||
| 2 | 2 | |
| 3 | 3 | class Wordlift_Reference_Rebuild_Service extends Wordlift_Rebuild_Service { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 7 | - * |
|
| 8 | - * @since 3.18.0 |
|
| 9 | - * @access private |
|
| 10 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 11 | - */ |
|
| 12 | - private $entity_service; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * A {@link Wordlift_Log_Service} instance. |
|
| 16 | - * |
|
| 17 | - * @since 3.18.0 |
|
| 18 | - * @access private |
|
| 19 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 20 | - */ |
|
| 21 | - private $log; |
|
| 22 | - |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Wordlift_Reference_Rebuild_Service constructor. |
|
| 26 | - * |
|
| 27 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 28 | - */ |
|
| 29 | - public function __construct( $entity_service ) { |
|
| 30 | - |
|
| 31 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Reference_Rebuild_Service' ); |
|
| 32 | - |
|
| 33 | - $this->entity_service = $entity_service; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function rebuild() { |
|
| 37 | - set_time_limit( 21600 ); // 6 hours |
|
| 38 | - |
|
| 39 | - // Send textual output. |
|
| 40 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 41 | - |
|
| 42 | - // We start at 0 by default and get to max. |
|
| 43 | - $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 44 | - $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 45 | - $max = $offset + $limit; |
|
| 46 | - |
|
| 47 | - $this->log->debug( 'Processing references...' ); |
|
| 48 | - |
|
| 49 | - // Go through the list of published entities and posts and call the (legacy) |
|
| 50 | - // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 51 | - // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 52 | - // and will cycle through all the posts w/ a very small memory footprint |
|
| 53 | - // in order to avoid memory errors. |
|
| 54 | - |
|
| 55 | - $count = 0; |
|
| 56 | - $log = $this->log; |
|
| 57 | - $entity_service = $this->entity_service; |
|
| 58 | - |
|
| 59 | - $this->process( |
|
| 60 | - function ( $post_id ) use ( &$count, $log, $entity_service ) { |
|
| 61 | - $count ++; |
|
| 62 | - |
|
| 63 | - if ( $entity_service->is_entity( $post_id ) ) { |
|
| 64 | - $log->trace( "Post $post_id is an entity, skipping..." ); |
|
| 65 | - |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $log->trace( "Going to save post $count, ID $post_id..." ); |
|
| 70 | - do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 71 | - }, |
|
| 72 | - array(), |
|
| 73 | - $offset, |
|
| 74 | - $max |
|
| 75 | - ); |
|
| 76 | - |
|
| 77 | - // Redirect to the next chunk. |
|
| 78 | - if ( $count == $limit ) { |
|
| 79 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 80 | - $url = admin_url( 'admin-ajax.php?action=wl_rebuild_references&offset=' . ( $offset + $limit ) . '&limit=' . $limit ); |
|
| 81 | - $this->redirect( $url ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $this->log->info( "Rebuild complete" ); |
|
| 85 | - echo( "Rebuild complete" ); |
|
| 86 | - |
|
| 87 | - // If we're being called as AJAX, die here. |
|
| 88 | - if ( DOING_AJAX ) { |
|
| 89 | - wp_die(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @inheritdoc |
|
| 95 | - */ |
|
| 96 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 97 | - global $wpdb; |
|
| 98 | - |
|
| 99 | - return $wpdb->get_col( $wpdb->prepare( |
|
| 100 | - " |
|
| 5 | + /** |
|
| 6 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 7 | + * |
|
| 8 | + * @since 3.18.0 |
|
| 9 | + * @access private |
|
| 10 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 11 | + */ |
|
| 12 | + private $entity_service; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * A {@link Wordlift_Log_Service} instance. |
|
| 16 | + * |
|
| 17 | + * @since 3.18.0 |
|
| 18 | + * @access private |
|
| 19 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 20 | + */ |
|
| 21 | + private $log; |
|
| 22 | + |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Wordlift_Reference_Rebuild_Service constructor. |
|
| 26 | + * |
|
| 27 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 28 | + */ |
|
| 29 | + public function __construct( $entity_service ) { |
|
| 30 | + |
|
| 31 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Reference_Rebuild_Service' ); |
|
| 32 | + |
|
| 33 | + $this->entity_service = $entity_service; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function rebuild() { |
|
| 37 | + set_time_limit( 21600 ); // 6 hours |
|
| 38 | + |
|
| 39 | + // Send textual output. |
|
| 40 | + header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 41 | + |
|
| 42 | + // We start at 0 by default and get to max. |
|
| 43 | + $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 44 | + $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 45 | + $max = $offset + $limit; |
|
| 46 | + |
|
| 47 | + $this->log->debug( 'Processing references...' ); |
|
| 48 | + |
|
| 49 | + // Go through the list of published entities and posts and call the (legacy) |
|
| 50 | + // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 51 | + // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 52 | + // and will cycle through all the posts w/ a very small memory footprint |
|
| 53 | + // in order to avoid memory errors. |
|
| 54 | + |
|
| 55 | + $count = 0; |
|
| 56 | + $log = $this->log; |
|
| 57 | + $entity_service = $this->entity_service; |
|
| 58 | + |
|
| 59 | + $this->process( |
|
| 60 | + function ( $post_id ) use ( &$count, $log, $entity_service ) { |
|
| 61 | + $count ++; |
|
| 62 | + |
|
| 63 | + if ( $entity_service->is_entity( $post_id ) ) { |
|
| 64 | + $log->trace( "Post $post_id is an entity, skipping..." ); |
|
| 65 | + |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $log->trace( "Going to save post $count, ID $post_id..." ); |
|
| 70 | + do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 71 | + }, |
|
| 72 | + array(), |
|
| 73 | + $offset, |
|
| 74 | + $max |
|
| 75 | + ); |
|
| 76 | + |
|
| 77 | + // Redirect to the next chunk. |
|
| 78 | + if ( $count == $limit ) { |
|
| 79 | + $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 80 | + $url = admin_url( 'admin-ajax.php?action=wl_rebuild_references&offset=' . ( $offset + $limit ) . '&limit=' . $limit ); |
|
| 81 | + $this->redirect( $url ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $this->log->info( "Rebuild complete" ); |
|
| 85 | + echo( "Rebuild complete" ); |
|
| 86 | + |
|
| 87 | + // If we're being called as AJAX, die here. |
|
| 88 | + if ( DOING_AJAX ) { |
|
| 89 | + wp_die(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @inheritdoc |
|
| 95 | + */ |
|
| 96 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 97 | + global $wpdb; |
|
| 98 | + |
|
| 99 | + return $wpdb->get_col( $wpdb->prepare( |
|
| 100 | + " |
|
| 101 | 101 | SELECT DISTINCT subject_id AS id |
| 102 | 102 | FROM {$wpdb->prefix}wl_relation_instances |
| 103 | 103 | LIMIT %d OFFSET %d |
| 104 | 104 | ", |
| 105 | - $limit, |
|
| 106 | - $offset |
|
| 107 | - ) ); |
|
| 105 | + $limit, |
|
| 106 | + $offset |
|
| 107 | + ) ); |
|
| 108 | 108 | |
| 109 | - } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | } |
@@ -26,25 +26,25 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 28 | 28 | */ |
| 29 | - public function __construct( $entity_service ) { |
|
| 29 | + public function __construct($entity_service) { |
|
| 30 | 30 | |
| 31 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Reference_Rebuild_Service' ); |
|
| 31 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Reference_Rebuild_Service'); |
|
| 32 | 32 | |
| 33 | 33 | $this->entity_service = $entity_service; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | public function rebuild() { |
| 37 | - set_time_limit( 21600 ); // 6 hours |
|
| 37 | + set_time_limit(21600); // 6 hours |
|
| 38 | 38 | |
| 39 | 39 | // Send textual output. |
| 40 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 40 | + header('Content-type: text/plain; charset=utf-8'); |
|
| 41 | 41 | |
| 42 | 42 | // We start at 0 by default and get to max. |
| 43 | - $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 44 | - $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 43 | + $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0; |
|
| 44 | + $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 1; |
|
| 45 | 45 | $max = $offset + $limit; |
| 46 | 46 | |
| 47 | - $this->log->debug( 'Processing references...' ); |
|
| 47 | + $this->log->debug('Processing references...'); |
|
| 48 | 48 | |
| 49 | 49 | // Go through the list of published entities and posts and call the (legacy) |
| 50 | 50 | // `wl_linked_data_save_post` function for each one. We're using the `process` |
@@ -57,17 +57,17 @@ discard block |
||
| 57 | 57 | $entity_service = $this->entity_service; |
| 58 | 58 | |
| 59 | 59 | $this->process( |
| 60 | - function ( $post_id ) use ( &$count, $log, $entity_service ) { |
|
| 61 | - $count ++; |
|
| 60 | + function($post_id) use (&$count, $log, $entity_service) { |
|
| 61 | + $count++; |
|
| 62 | 62 | |
| 63 | - if ( $entity_service->is_entity( $post_id ) ) { |
|
| 64 | - $log->trace( "Post $post_id is an entity, skipping..." ); |
|
| 63 | + if ($entity_service->is_entity($post_id)) { |
|
| 64 | + $log->trace("Post $post_id is an entity, skipping..."); |
|
| 65 | 65 | |
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - $log->trace( "Going to save post $count, ID $post_id..." ); |
|
| 70 | - do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 69 | + $log->trace("Going to save post $count, ID $post_id..."); |
|
| 70 | + do_action('wl_legacy_linked_data__push', $post_id); |
|
| 71 | 71 | }, |
| 72 | 72 | array(), |
| 73 | 73 | $offset, |
@@ -75,17 +75,17 @@ discard block |
||
| 75 | 75 | ); |
| 76 | 76 | |
| 77 | 77 | // Redirect to the next chunk. |
| 78 | - if ( $count == $limit ) { |
|
| 79 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 80 | - $url = admin_url( 'admin-ajax.php?action=wl_rebuild_references&offset=' . ( $offset + $limit ) . '&limit=' . $limit ); |
|
| 81 | - $this->redirect( $url ); |
|
| 78 | + if ($count == $limit) { |
|
| 79 | + $log->trace('Redirecting to post #'.($offset + 1).'...'); |
|
| 80 | + $url = admin_url('admin-ajax.php?action=wl_rebuild_references&offset='.($offset + $limit).'&limit='.$limit); |
|
| 81 | + $this->redirect($url); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - $this->log->info( "Rebuild complete" ); |
|
| 85 | - echo( "Rebuild complete" ); |
|
| 84 | + $this->log->info("Rebuild complete"); |
|
| 85 | + echo("Rebuild complete"); |
|
| 86 | 86 | |
| 87 | 87 | // If we're being called as AJAX, die here. |
| 88 | - if ( DOING_AJAX ) { |
|
| 88 | + if (DOING_AJAX) { |
|
| 89 | 89 | wp_die(); |
| 90 | 90 | } |
| 91 | 91 | } |
@@ -93,10 +93,10 @@ discard block |
||
| 93 | 93 | /** |
| 94 | 94 | * @inheritdoc |
| 95 | 95 | */ |
| 96 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 96 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 97 | 97 | global $wpdb; |
| 98 | 98 | |
| 99 | - return $wpdb->get_col( $wpdb->prepare( |
|
| 99 | + return $wpdb->get_col($wpdb->prepare( |
|
| 100 | 100 | " |
| 101 | 101 | SELECT DISTINCT subject_id AS id |
| 102 | 102 | FROM {$wpdb->prefix}wl_relation_instances |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | ", |
| 105 | 105 | $limit, |
| 106 | 106 | $offset |
| 107 | - ) ); |
|
| 107 | + )); |
|
| 108 | 108 | |
| 109 | 109 | } |
| 110 | 110 | |
@@ -20,132 +20,132 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Rebuild_Service extends Wordlift_Listable { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.6.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.6.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $sparql_service; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | - * |
|
| 44 | - * @since 3.15.0 |
|
| 45 | - * @access private |
|
| 46 | - * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | - */ |
|
| 48 | - private $uri_service; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | - * |
|
| 53 | - * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 54 | - * @param \Wordlift_Uri_Service $uri_service |
|
| 55 | - * |
|
| 56 | - * @since 3.6.0 |
|
| 57 | - * |
|
| 58 | - */ |
|
| 59 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 60 | - |
|
| 61 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 62 | - |
|
| 63 | - $this->sparql_service = $sparql_service; |
|
| 64 | - $this->uri_service = $uri_service; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 69 | - * it with local data. |
|
| 70 | - * |
|
| 71 | - * @since 3.6.0 |
|
| 72 | - */ |
|
| 73 | - public function rebuild() { |
|
| 74 | - |
|
| 75 | - ob_clean(); |
|
| 76 | - |
|
| 77 | - // Give ourselves some time to process the data. |
|
| 78 | - set_time_limit( 21600 ); // 6 hours |
|
| 79 | - |
|
| 80 | - // Send textual output. |
|
| 81 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 82 | - |
|
| 83 | - // We start at 0 by default and get to max. |
|
| 84 | - $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 85 | - $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 86 | - $max = $offset + $limit; |
|
| 87 | - |
|
| 88 | - // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 89 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 90 | - |
|
| 91 | - // If we're starting at offset 0, then delete existing URIs and data from |
|
| 92 | - // the remote dataset. |
|
| 93 | - if ( 0 === $offset ) { |
|
| 94 | - |
|
| 95 | - // Clear out all generated URIs, since the dataset URI might have changed |
|
| 96 | - // in the process. |
|
| 97 | - $this->uri_service->delete_all(); |
|
| 98 | - |
|
| 99 | - // Delete all the triples in the remote dataset. |
|
| 100 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 101 | - |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // Go through the list of published entities and posts and call the (legacy) |
|
| 105 | - // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 106 | - // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 107 | - // and will cycle through all the posts w/ a very small memory footprint |
|
| 108 | - // in order to avoid memory errors. |
|
| 109 | - |
|
| 110 | - $count = 0; |
|
| 111 | - $log = $this->log; |
|
| 112 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 113 | - $count ++; |
|
| 114 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 115 | - if ( function_exists( 'wl_linked_data_save_post' ) ) { |
|
| 116 | - wl_linked_data_save_post( $post->ID ); |
|
| 117 | - } |
|
| 118 | - }, array( |
|
| 119 | - 'post_status' => 'publish', |
|
| 120 | - ), $offset, $max ); |
|
| 121 | - |
|
| 122 | - // Redirect to the next chunk. |
|
| 123 | - if ( $count == $limit ) { |
|
| 124 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 125 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Flush the cache. |
|
| 129 | - Wordlift_File_Cache_Service::flush_all(); |
|
| 130 | - |
|
| 131 | - // Rebuild also the references. |
|
| 132 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 137 | - * |
|
| 138 | - * @param string $url The URL to redirect to. |
|
| 139 | - * |
|
| 140 | - * @since 3.9.8 |
|
| 141 | - * |
|
| 142 | - */ |
|
| 143 | - public function redirect( $url ) { |
|
| 144 | - |
|
| 145 | - ob_clean(); |
|
| 146 | - |
|
| 147 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 148 | - ?> |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.6.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.6.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $sparql_service; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | + * |
|
| 44 | + * @since 3.15.0 |
|
| 45 | + * @access private |
|
| 46 | + * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | + */ |
|
| 48 | + private $uri_service; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | + * |
|
| 53 | + * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 54 | + * @param \Wordlift_Uri_Service $uri_service |
|
| 55 | + * |
|
| 56 | + * @since 3.6.0 |
|
| 57 | + * |
|
| 58 | + */ |
|
| 59 | + public function __construct( $sparql_service, $uri_service ) { |
|
| 60 | + |
|
| 61 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 62 | + |
|
| 63 | + $this->sparql_service = $sparql_service; |
|
| 64 | + $this->uri_service = $uri_service; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 69 | + * it with local data. |
|
| 70 | + * |
|
| 71 | + * @since 3.6.0 |
|
| 72 | + */ |
|
| 73 | + public function rebuild() { |
|
| 74 | + |
|
| 75 | + ob_clean(); |
|
| 76 | + |
|
| 77 | + // Give ourselves some time to process the data. |
|
| 78 | + set_time_limit( 21600 ); // 6 hours |
|
| 79 | + |
|
| 80 | + // Send textual output. |
|
| 81 | + header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 82 | + |
|
| 83 | + // We start at 0 by default and get to max. |
|
| 84 | + $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 85 | + $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 86 | + $max = $offset + $limit; |
|
| 87 | + |
|
| 88 | + // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 89 | + $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 90 | + |
|
| 91 | + // If we're starting at offset 0, then delete existing URIs and data from |
|
| 92 | + // the remote dataset. |
|
| 93 | + if ( 0 === $offset ) { |
|
| 94 | + |
|
| 95 | + // Clear out all generated URIs, since the dataset URI might have changed |
|
| 96 | + // in the process. |
|
| 97 | + $this->uri_service->delete_all(); |
|
| 98 | + |
|
| 99 | + // Delete all the triples in the remote dataset. |
|
| 100 | + $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 101 | + |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // Go through the list of published entities and posts and call the (legacy) |
|
| 105 | + // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 106 | + // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 107 | + // and will cycle through all the posts w/ a very small memory footprint |
|
| 108 | + // in order to avoid memory errors. |
|
| 109 | + |
|
| 110 | + $count = 0; |
|
| 111 | + $log = $this->log; |
|
| 112 | + $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 113 | + $count ++; |
|
| 114 | + $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 115 | + if ( function_exists( 'wl_linked_data_save_post' ) ) { |
|
| 116 | + wl_linked_data_save_post( $post->ID ); |
|
| 117 | + } |
|
| 118 | + }, array( |
|
| 119 | + 'post_status' => 'publish', |
|
| 120 | + ), $offset, $max ); |
|
| 121 | + |
|
| 122 | + // Redirect to the next chunk. |
|
| 123 | + if ( $count == $limit ) { |
|
| 124 | + $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 125 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Flush the cache. |
|
| 129 | + Wordlift_File_Cache_Service::flush_all(); |
|
| 130 | + |
|
| 131 | + // Rebuild also the references. |
|
| 132 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 137 | + * |
|
| 138 | + * @param string $url The URL to redirect to. |
|
| 139 | + * |
|
| 140 | + * @since 3.9.8 |
|
| 141 | + * |
|
| 142 | + */ |
|
| 143 | + public function redirect( $url ) { |
|
| 144 | + |
|
| 145 | + ob_clean(); |
|
| 146 | + |
|
| 147 | + @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 148 | + ?> |
|
| 149 | 149 | <html> |
| 150 | 150 | <head> |
| 151 | 151 | <meta http-equiv="refresh" |
@@ -157,37 +157,37 @@ discard block |
||
| 157 | 157 | </html> |
| 158 | 158 | <?php |
| 159 | 159 | |
| 160 | - exit; |
|
| 161 | - |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 166 | - * |
|
| 167 | - * @param int $offset The start offset. |
|
| 168 | - * @param int $limit The maximum number of items to return. |
|
| 169 | - * @param array $args Additional arguments. |
|
| 170 | - * |
|
| 171 | - * @return array A array of items (or an empty array if no items are found). |
|
| 172 | - * @since 3.19.5 remove Polylang hooks. |
|
| 173 | - * @since 3.6.0 |
|
| 174 | - * |
|
| 175 | - */ |
|
| 176 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 177 | - |
|
| 178 | - $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 179 | - 'offset' => $offset, |
|
| 180 | - 'numberposts' => $limit, |
|
| 181 | - 'fields' => 'all', |
|
| 182 | - 'orderby' => 'ID', |
|
| 183 | - 'order' => 'ASC', |
|
| 184 | - 'post_status' => 'any', |
|
| 185 | - 'cache_results' => false, |
|
| 186 | - ) ) ); |
|
| 187 | - |
|
| 188 | - $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 189 | - |
|
| 190 | - return get_posts( $actual_args ); |
|
| 191 | - } |
|
| 160 | + exit; |
|
| 161 | + |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 166 | + * |
|
| 167 | + * @param int $offset The start offset. |
|
| 168 | + * @param int $limit The maximum number of items to return. |
|
| 169 | + * @param array $args Additional arguments. |
|
| 170 | + * |
|
| 171 | + * @return array A array of items (or an empty array if no items are found). |
|
| 172 | + * @since 3.19.5 remove Polylang hooks. |
|
| 173 | + * @since 3.6.0 |
|
| 174 | + * |
|
| 175 | + */ |
|
| 176 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 177 | + |
|
| 178 | + $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 179 | + 'offset' => $offset, |
|
| 180 | + 'numberposts' => $limit, |
|
| 181 | + 'fields' => 'all', |
|
| 182 | + 'orderby' => 'ID', |
|
| 183 | + 'order' => 'ASC', |
|
| 184 | + 'post_status' => 'any', |
|
| 185 | + 'cache_results' => false, |
|
| 186 | + ) ) ); |
|
| 187 | + |
|
| 188 | + $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 189 | + |
|
| 190 | + return get_posts( $actual_args ); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | * @since 3.6.0 |
| 57 | 57 | * |
| 58 | 58 | */ |
| 59 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 59 | + public function __construct($sparql_service, $uri_service) { |
|
| 60 | 60 | |
| 61 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 61 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rebuild_Service'); |
|
| 62 | 62 | |
| 63 | 63 | $this->sparql_service = $sparql_service; |
| 64 | 64 | $this->uri_service = $uri_service; |
@@ -75,29 +75,29 @@ discard block |
||
| 75 | 75 | ob_clean(); |
| 76 | 76 | |
| 77 | 77 | // Give ourselves some time to process the data. |
| 78 | - set_time_limit( 21600 ); // 6 hours |
|
| 78 | + set_time_limit(21600); // 6 hours |
|
| 79 | 79 | |
| 80 | 80 | // Send textual output. |
| 81 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 81 | + header('Content-type: text/plain; charset=utf-8'); |
|
| 82 | 82 | |
| 83 | 83 | // We start at 0 by default and get to max. |
| 84 | - $offset = isset( $_GET['offset'] ) ? (int) $_GET['offset'] : 0; |
|
| 85 | - $limit = isset( $_GET['limit'] ) ? (int) $_GET['limit'] : 1; |
|
| 84 | + $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0; |
|
| 85 | + $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 1; |
|
| 86 | 86 | $max = $offset + $limit; |
| 87 | 87 | |
| 88 | 88 | // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
| 89 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 89 | + $asynchronous = isset($_GET['wl-async']) && 'true' === $_GET['wl-async']; |
|
| 90 | 90 | |
| 91 | 91 | // If we're starting at offset 0, then delete existing URIs and data from |
| 92 | 92 | // the remote dataset. |
| 93 | - if ( 0 === $offset ) { |
|
| 93 | + if (0 === $offset) { |
|
| 94 | 94 | |
| 95 | 95 | // Clear out all generated URIs, since the dataset URI might have changed |
| 96 | 96 | // in the process. |
| 97 | 97 | $this->uri_service->delete_all(); |
| 98 | 98 | |
| 99 | 99 | // Delete all the triples in the remote dataset. |
| 100 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 100 | + $this->sparql_service->execute('DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };'); |
|
| 101 | 101 | |
| 102 | 102 | } |
| 103 | 103 | |
@@ -109,27 +109,27 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | $count = 0; |
| 111 | 111 | $log = $this->log; |
| 112 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 113 | - $count ++; |
|
| 114 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 115 | - if ( function_exists( 'wl_linked_data_save_post' ) ) { |
|
| 116 | - wl_linked_data_save_post( $post->ID ); |
|
| 112 | + $this->process(function($post) use (&$count, $log) { |
|
| 113 | + $count++; |
|
| 114 | + $log->trace("Going to save post $count, ID $post->ID..."); |
|
| 115 | + if (function_exists('wl_linked_data_save_post')) { |
|
| 116 | + wl_linked_data_save_post($post->ID); |
|
| 117 | 117 | } |
| 118 | 118 | }, array( |
| 119 | 119 | 'post_status' => 'publish', |
| 120 | - ), $offset, $max ); |
|
| 120 | + ), $offset, $max); |
|
| 121 | 121 | |
| 122 | 122 | // Redirect to the next chunk. |
| 123 | - if ( $count == $limit ) { |
|
| 124 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 125 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 123 | + if ($count == $limit) { |
|
| 124 | + $log->trace('Redirecting to post #'.($offset + 1).'...'); |
|
| 125 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild&offset='.($offset + $limit).'&limit='.$limit.'&wl-async='.($asynchronous ? 'true' : 'false'))); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // Flush the cache. |
| 129 | 129 | Wordlift_File_Cache_Service::flush_all(); |
| 130 | 130 | |
| 131 | 131 | // Rebuild also the references. |
| 132 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references' ) ); |
|
| 132 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild_references')); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -140,16 +140,16 @@ discard block |
||
| 140 | 140 | * @since 3.9.8 |
| 141 | 141 | * |
| 142 | 142 | */ |
| 143 | - public function redirect( $url ) { |
|
| 143 | + public function redirect($url) { |
|
| 144 | 144 | |
| 145 | 145 | ob_clean(); |
| 146 | 146 | |
| 147 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 147 | + @header('Content-Type: text/html; charset='.get_option('blog_charset')); |
|
| 148 | 148 | ?> |
| 149 | 149 | <html> |
| 150 | 150 | <head> |
| 151 | 151 | <meta http-equiv="refresh" |
| 152 | - content="0; <?php echo esc_attr( $url ); ?>"> |
|
| 152 | + content="0; <?php echo esc_attr($url); ?>"> |
|
| 153 | 153 | </head> |
| 154 | 154 | <body> |
| 155 | 155 | Rebuilding, please wait... |
@@ -173,9 +173,9 @@ discard block |
||
| 173 | 173 | * @since 3.6.0 |
| 174 | 174 | * |
| 175 | 175 | */ |
| 176 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 176 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 177 | 177 | |
| 178 | - $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 178 | + $actual_args = wp_parse_args($args, Wordlift_Entity_Service::add_criterias(array( |
|
| 179 | 179 | 'offset' => $offset, |
| 180 | 180 | 'numberposts' => $limit, |
| 181 | 181 | 'fields' => 'all', |
@@ -183,11 +183,11 @@ discard block |
||
| 183 | 183 | 'order' => 'ASC', |
| 184 | 184 | 'post_status' => 'any', |
| 185 | 185 | 'cache_results' => false, |
| 186 | - ) ) ); |
|
| 186 | + ))); |
|
| 187 | 187 | |
| 188 | - $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 188 | + $this->log->trace('Using '.var_export($actual_args, true)); |
|
| 189 | 189 | |
| 190 | - return get_posts( $actual_args ); |
|
| 190 | + return get_posts($actual_args); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | } |
@@ -14,135 +14,135 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Batch_Operation_Ajax_Adapter { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The access levels. |
|
| 19 | - * |
|
| 20 | - * @since 3.20.0 |
|
| 21 | - */ |
|
| 22 | - const ACCESS_ANONYMOUS = 1; |
|
| 23 | - const ACCESS_ADMIN = 2; |
|
| 24 | - const ACCESS_ALL = 3; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.20.0 |
|
| 30 | - * @access private |
|
| 31 | - * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 32 | - */ |
|
| 33 | - private $operation; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The ajax action name. |
|
| 37 | - * |
|
| 38 | - * @since 3.20.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var string $action The ajax action name. |
|
| 41 | - */ |
|
| 42 | - private $action; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Wordlift_Batch_Operation_Ajax_Adapter constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Wordlift_Batch_Operation_Interface $operation The batch operation. |
|
| 48 | - * @param string $action The action name. |
|
| 49 | - * @param int $access The access level. |
|
| 50 | - */ |
|
| 51 | - public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 52 | - |
|
| 53 | - $this->operation = $operation; |
|
| 54 | - |
|
| 55 | - if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | - add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | - add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 58 | - |
|
| 59 | - // Add the nonce for the `schemaorg_sync` action. |
|
| 60 | - add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | - add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | - add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $this->action = $action; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Hook to `wl_admin_settings`, adds the nonce. |
|
| 73 | - * |
|
| 74 | - * @param array $params An array of settings. |
|
| 75 | - * |
|
| 76 | - * @return array The updated array of settings. |
|
| 77 | - * @since 3.20.0 |
|
| 78 | - * |
|
| 79 | - */ |
|
| 80 | - public function add_nonce( $params ) { |
|
| 81 | - |
|
| 82 | - return array_merge( $params, array( |
|
| 83 | - "{$this->action}_nonce" => $this->create_nonce(), |
|
| 84 | - ) ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Process the requested operation. |
|
| 89 | - * |
|
| 90 | - * @since 3.20.0 |
|
| 91 | - */ |
|
| 92 | - public function process() { |
|
| 93 | - $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 94 | - // Validate the nonce. |
|
| 95 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | - $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 101 | - |
|
| 102 | - // Run the batch operation. |
|
| 103 | - $result = $this->operation->process( $offset, $limit ); |
|
| 104 | - |
|
| 105 | - // Send the results along with a potentially updated nonce. |
|
| 106 | - wp_send_json_success( array_merge( $result, array( |
|
| 107 | - '_nonce' => $this->create_nonce(), |
|
| 108 | - ) ) ); |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Count the number of elements that would be affected by the operation. |
|
| 114 | - * |
|
| 115 | - * @since 3.20.0 |
|
| 116 | - */ |
|
| 117 | - public function count() { |
|
| 118 | - |
|
| 119 | - // Validate the nonce. |
|
| 120 | - $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 121 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Run the batch operation. |
|
| 126 | - $result = $this->operation->count(); |
|
| 127 | - |
|
| 128 | - // Send the results along with a potentially updated nonce. |
|
| 129 | - wp_send_json_success( array( |
|
| 130 | - 'count' => $result, |
|
| 131 | - '_nonce' => $this->create_nonce(), |
|
| 132 | - ) ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Create a nonce for the ajax operation. |
|
| 138 | - * |
|
| 139 | - * @return string The nonce. |
|
| 140 | - * @since 3.20.0 |
|
| 141 | - * |
|
| 142 | - */ |
|
| 143 | - public function create_nonce() { |
|
| 144 | - |
|
| 145 | - return wp_create_nonce( $this->action ); |
|
| 146 | - } |
|
| 17 | + /** |
|
| 18 | + * The access levels. |
|
| 19 | + * |
|
| 20 | + * @since 3.20.0 |
|
| 21 | + */ |
|
| 22 | + const ACCESS_ANONYMOUS = 1; |
|
| 23 | + const ACCESS_ADMIN = 2; |
|
| 24 | + const ACCESS_ALL = 3; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.20.0 |
|
| 30 | + * @access private |
|
| 31 | + * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance. |
|
| 32 | + */ |
|
| 33 | + private $operation; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The ajax action name. |
|
| 37 | + * |
|
| 38 | + * @since 3.20.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var string $action The ajax action name. |
|
| 41 | + */ |
|
| 42 | + private $action; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Wordlift_Batch_Operation_Ajax_Adapter constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Wordlift_Batch_Operation_Interface $operation The batch operation. |
|
| 48 | + * @param string $action The action name. |
|
| 49 | + * @param int $access The access level. |
|
| 50 | + */ |
|
| 51 | + public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 52 | + |
|
| 53 | + $this->operation = $operation; |
|
| 54 | + |
|
| 55 | + if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | + add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | + add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 58 | + |
|
| 59 | + // Add the nonce for the `schemaorg_sync` action. |
|
| 60 | + add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | + add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | + add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $this->action = $action; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Hook to `wl_admin_settings`, adds the nonce. |
|
| 73 | + * |
|
| 74 | + * @param array $params An array of settings. |
|
| 75 | + * |
|
| 76 | + * @return array The updated array of settings. |
|
| 77 | + * @since 3.20.0 |
|
| 78 | + * |
|
| 79 | + */ |
|
| 80 | + public function add_nonce( $params ) { |
|
| 81 | + |
|
| 82 | + return array_merge( $params, array( |
|
| 83 | + "{$this->action}_nonce" => $this->create_nonce(), |
|
| 84 | + ) ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Process the requested operation. |
|
| 89 | + * |
|
| 90 | + * @since 3.20.0 |
|
| 91 | + */ |
|
| 92 | + public function process() { |
|
| 93 | + $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 94 | + // Validate the nonce. |
|
| 95 | + if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | + wp_send_json_error( 'Invalid nonce.' ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | + $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 101 | + |
|
| 102 | + // Run the batch operation. |
|
| 103 | + $result = $this->operation->process( $offset, $limit ); |
|
| 104 | + |
|
| 105 | + // Send the results along with a potentially updated nonce. |
|
| 106 | + wp_send_json_success( array_merge( $result, array( |
|
| 107 | + '_nonce' => $this->create_nonce(), |
|
| 108 | + ) ) ); |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Count the number of elements that would be affected by the operation. |
|
| 114 | + * |
|
| 115 | + * @since 3.20.0 |
|
| 116 | + */ |
|
| 117 | + public function count() { |
|
| 118 | + |
|
| 119 | + // Validate the nonce. |
|
| 120 | + $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 121 | + if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | + wp_send_json_error( 'Invalid nonce.' ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Run the batch operation. |
|
| 126 | + $result = $this->operation->count(); |
|
| 127 | + |
|
| 128 | + // Send the results along with a potentially updated nonce. |
|
| 129 | + wp_send_json_success( array( |
|
| 130 | + 'count' => $result, |
|
| 131 | + '_nonce' => $this->create_nonce(), |
|
| 132 | + ) ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Create a nonce for the ajax operation. |
|
| 138 | + * |
|
| 139 | + * @return string The nonce. |
|
| 140 | + * @since 3.20.0 |
|
| 141 | + * |
|
| 142 | + */ |
|
| 143 | + public function create_nonce() { |
|
| 144 | + |
|
| 145 | + return wp_create_nonce( $this->action ); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | } |
@@ -48,21 +48,21 @@ discard block |
||
| 48 | 48 | * @param string $action The action name. |
| 49 | 49 | * @param int $access The access level. |
| 50 | 50 | */ |
| 51 | - public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) { |
|
| 51 | + public function __construct($operation, $action, $access = self::ACCESS_ADMIN) { |
|
| 52 | 52 | |
| 53 | 53 | $this->operation = $operation; |
| 54 | 54 | |
| 55 | - if ( $access & self::ACCESS_ADMIN ) { |
|
| 56 | - add_action( "wp_ajax_$action", array( $this, 'process' ) ); |
|
| 57 | - add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) ); |
|
| 55 | + if ($access & self::ACCESS_ADMIN) { |
|
| 56 | + add_action("wp_ajax_$action", array($this, 'process')); |
|
| 57 | + add_action("wp_ajax_{$action}_count", array($this, 'count')); |
|
| 58 | 58 | |
| 59 | 59 | // Add the nonce for the `schemaorg_sync` action. |
| 60 | - add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) ); |
|
| 60 | + add_filter('wl_admin_settings', array($this, 'add_nonce')); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if ( $access & self::ACCESS_ANONYMOUS ) { |
|
| 64 | - add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) ); |
|
| 65 | - add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) ); |
|
| 63 | + if ($access & self::ACCESS_ANONYMOUS) { |
|
| 64 | + add_action("wp_ajax_nopriv_$action", array($this, 'process')); |
|
| 65 | + add_action("wp_ajax_nopriv_{$action}_count", array($this, 'count')); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | $this->action = $action; |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | * @since 3.20.0 |
| 78 | 78 | * |
| 79 | 79 | */ |
| 80 | - public function add_nonce( $params ) { |
|
| 80 | + public function add_nonce($params) { |
|
| 81 | 81 | |
| 82 | - return array_merge( $params, array( |
|
| 82 | + return array_merge($params, array( |
|
| 83 | 83 | "{$this->action}_nonce" => $this->create_nonce(), |
| 84 | - ) ); |
|
| 84 | + )); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -90,22 +90,22 @@ discard block |
||
| 90 | 90 | * @since 3.20.0 |
| 91 | 91 | */ |
| 92 | 92 | public function process() { |
| 93 | - $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 93 | + $nonce = isset($_POST['_nonce']) ? (string) $_POST['_nonce'] : ''; |
|
| 94 | 94 | // Validate the nonce. |
| 95 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 96 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 95 | + if ( ! wp_verify_nonce($nonce, $this->action)) { |
|
| 96 | + wp_send_json_error('Invalid nonce.'); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0; |
|
| 100 | - $limit = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10; |
|
| 99 | + $offset = isset($_POST['offset']) ? (int) $_POST['offset'] : 0; |
|
| 100 | + $limit = isset($_POST['limit']) ? (int) $_POST['limit'] : 10; |
|
| 101 | 101 | |
| 102 | 102 | // Run the batch operation. |
| 103 | - $result = $this->operation->process( $offset, $limit ); |
|
| 103 | + $result = $this->operation->process($offset, $limit); |
|
| 104 | 104 | |
| 105 | 105 | // Send the results along with a potentially updated nonce. |
| 106 | - wp_send_json_success( array_merge( $result, array( |
|
| 106 | + wp_send_json_success(array_merge($result, array( |
|
| 107 | 107 | '_nonce' => $this->create_nonce(), |
| 108 | - ) ) ); |
|
| 108 | + ))); |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
@@ -117,19 +117,19 @@ discard block |
||
| 117 | 117 | public function count() { |
| 118 | 118 | |
| 119 | 119 | // Validate the nonce. |
| 120 | - $nonce = isset( $_POST['_nonce'] ) ? (string) $_POST['_nonce'] : ''; |
|
| 121 | - if ( ! wp_verify_nonce( $nonce, $this->action ) ) { |
|
| 122 | - wp_send_json_error( 'Invalid nonce.' ); |
|
| 120 | + $nonce = isset($_POST['_nonce']) ? (string) $_POST['_nonce'] : ''; |
|
| 121 | + if ( ! wp_verify_nonce($nonce, $this->action)) { |
|
| 122 | + wp_send_json_error('Invalid nonce.'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Run the batch operation. |
| 126 | 126 | $result = $this->operation->count(); |
| 127 | 127 | |
| 128 | 128 | // Send the results along with a potentially updated nonce. |
| 129 | - wp_send_json_success( array( |
|
| 129 | + wp_send_json_success(array( |
|
| 130 | 130 | 'count' => $result, |
| 131 | 131 | '_nonce' => $this->create_nonce(), |
| 132 | - ) ); |
|
| 132 | + )); |
|
| 133 | 133 | |
| 134 | 134 | } |
| 135 | 135 | |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | public function create_nonce() { |
| 144 | 144 | |
| 145 | - return wp_create_nonce( $this->action ); |
|
| 145 | + return wp_create_nonce($this->action); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | } |
@@ -8,79 +8,79 @@ |
||
| 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 | - /** |
|
| 37 | - * |
|
| 38 | - */ |
|
| 39 | - public function set_entity_types_for_post_type() { |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + */ |
|
| 39 | + public function set_entity_types_for_post_type() { |
|
| 40 | 40 | |
| 41 | - $post_type = sanitize_text_field( $_REQUEST['post_type'] ); |
|
| 42 | - $entity_types = (array) $_REQUEST['entity_types']; |
|
| 41 | + $post_type = sanitize_text_field( $_REQUEST['post_type'] ); |
|
| 42 | + $entity_types = (array) $_REQUEST['entity_types']; |
|
| 43 | 43 | |
| 44 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 44 | + $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 45 | 45 | |
| 46 | - wp_send_json_success(); |
|
| 46 | + wp_send_json_success(); |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function update_post_type_entity_types() { |
|
| 50 | + public function update_post_type_entity_types() { |
|
| 51 | 51 | |
| 52 | - // If the nonce is invalid, return an error. |
|
| 53 | - $nonce = isset( $_REQUEST['_nonce'] ) ? $_REQUEST['_nonce'] : ''; |
|
| 54 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 55 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 56 | - } |
|
| 52 | + // If the nonce is invalid, return an error. |
|
| 53 | + $nonce = isset( $_REQUEST['_nonce'] ) ? $_REQUEST['_nonce'] : ''; |
|
| 54 | + if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 55 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 59 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 60 | - } |
|
| 58 | + if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 59 | + wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 63 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 64 | - } |
|
| 62 | + if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 63 | + wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // Get the post type. |
|
| 67 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : ''; |
|
| 66 | + // Get the post type. |
|
| 67 | + $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : ''; |
|
| 68 | 68 | |
| 69 | - // Get the entity types URIs. |
|
| 70 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? (array) $_REQUEST['entity_types'] : array(); |
|
| 69 | + // Get the entity types URIs. |
|
| 70 | + $entity_types = isset( $_REQUEST['entity_types'] ) ? (array) $_REQUEST['entity_types'] : array(); |
|
| 71 | 71 | |
| 72 | - // Get the offset. |
|
| 73 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 72 | + // Get the offset. |
|
| 73 | + $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 74 | 74 | |
| 75 | - // Update and get the results. |
|
| 76 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 75 | + // Update and get the results. |
|
| 76 | + $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 77 | 77 | |
| 78 | - // Add our nonce to the result. |
|
| 79 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 78 | + // Add our nonce to the result. |
|
| 79 | + $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 80 | 80 | |
| 81 | - // Finally send the results. |
|
| 82 | - wp_send_json_success( $result ); |
|
| 81 | + // Finally send the results. |
|
| 82 | + wp_send_json_success( $result ); |
|
| 83 | 83 | |
| 84 | - } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | 86 | } |
@@ -24,12 +24,12 @@ 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 | |
@@ -38,10 +38,10 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function set_entity_types_for_post_type() { |
| 40 | 40 | |
| 41 | - $post_type = sanitize_text_field( $_REQUEST['post_type'] ); |
|
| 41 | + $post_type = sanitize_text_field($_REQUEST['post_type']); |
|
| 42 | 42 | $entity_types = (array) $_REQUEST['entity_types']; |
| 43 | 43 | |
| 44 | - $this->mapping_service->set_entity_types_for_post_type( $post_type, $entity_types ); |
|
| 44 | + $this->mapping_service->set_entity_types_for_post_type($post_type, $entity_types); |
|
| 45 | 45 | |
| 46 | 46 | wp_send_json_success(); |
| 47 | 47 | |
@@ -50,36 +50,36 @@ discard block |
||
| 50 | 50 | public function update_post_type_entity_types() { |
| 51 | 51 | |
| 52 | 52 | // If the nonce is invalid, return an error. |
| 53 | - $nonce = isset( $_REQUEST['_nonce'] ) ? $_REQUEST['_nonce'] : ''; |
|
| 54 | - if ( ! wp_verify_nonce( $nonce, 'update_post_type_entity_types' ) ) { |
|
| 55 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
| 53 | + $nonce = isset($_REQUEST['_nonce']) ? $_REQUEST['_nonce'] : ''; |
|
| 54 | + if ( ! wp_verify_nonce($nonce, 'update_post_type_entity_types')) { |
|
| 55 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - if ( empty( $_REQUEST['post_type'] ) ) { |
|
| 59 | - wp_send_json_error( __( '`post_type` is required', 'wordlift' ) ); |
|
| 58 | + if (empty($_REQUEST['post_type'])) { |
|
| 59 | + wp_send_json_error(__('`post_type` is required', 'wordlift')); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ( empty( $_REQUEST['entity_types'] ) ) { |
|
| 63 | - wp_send_json_error( __( '`entity_types` is required', 'wordlift' ) ); |
|
| 62 | + if (empty($_REQUEST['entity_types'])) { |
|
| 63 | + wp_send_json_error(__('`entity_types` is required', 'wordlift')); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Get the post type. |
| 67 | - $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : ''; |
|
| 67 | + $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field($_REQUEST['post_type']) : ''; |
|
| 68 | 68 | |
| 69 | 69 | // Get the entity types URIs. |
| 70 | - $entity_types = isset( $_REQUEST['entity_types'] ) ? (array) $_REQUEST['entity_types'] : array(); |
|
| 70 | + $entity_types = isset($_REQUEST['entity_types']) ? (array) $_REQUEST['entity_types'] : array(); |
|
| 71 | 71 | |
| 72 | 72 | // Get the offset. |
| 73 | - $offset = isset( $_REQUEST['offset'] ) ? intval( $_REQUEST['offset'] ) : 0; |
|
| 73 | + $offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : 0; |
|
| 74 | 74 | |
| 75 | 75 | // Update and get the results. |
| 76 | - $result = $this->mapping_service->update( $post_type, $entity_types, $offset ); |
|
| 76 | + $result = $this->mapping_service->update($post_type, $entity_types, $offset); |
|
| 77 | 77 | |
| 78 | 78 | // Add our nonce to the result. |
| 79 | - $result['_nonce'] = wp_create_nonce( 'update_post_type_entity_types' ); |
|
| 79 | + $result['_nonce'] = wp_create_nonce('update_post_type_entity_types'); |
|
| 80 | 80 | |
| 81 | 81 | // Finally send the results. |
| 82 | - wp_send_json_success( $result ); |
|
| 82 | + wp_send_json_success($result); |
|
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | |
@@ -24,391 +24,391 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class Edit_Mappings_Page extends Wordlift_Admin_Page { |
| 26 | 26 | |
| 27 | - /** Instance to store the registry class. |
|
| 28 | - * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
| 29 | - */ |
|
| 30 | - public $transform_function_registry; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Edit_Mappings_Page constructor. |
|
| 34 | - * |
|
| 35 | - * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
| 36 | - */ |
|
| 37 | - public function __construct( $transform_function_registry ) { |
|
| 38 | - parent::__construct(); |
|
| 39 | - $this->transform_function_registry = $transform_function_registry; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function render() { |
|
| 43 | - // Render all the settings when this method is called, because the partial page is loaded after |
|
| 44 | - // this method. |
|
| 45 | - // Load the UI dependencies. |
|
| 46 | - $edit_mapping_settings = $this->get_ui_settings_array(); |
|
| 47 | - // Supply the settings to js client. |
|
| 48 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 49 | - |
|
| 50 | - parent::render(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Load the text settings needed for the edit_mappings_page. |
|
| 55 | - * |
|
| 56 | - * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
| 57 | - * |
|
| 58 | - * @return array Adding text settings to the main settings array. |
|
| 59 | - */ |
|
| 60 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 61 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 62 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 63 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 64 | - $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
| 65 | - |
|
| 66 | - return $edit_mapping_settings; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
| 71 | - * |
|
| 72 | - * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
| 73 | - * show it there. |
|
| 74 | - * |
|
| 75 | - * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
| 76 | - */ |
|
| 77 | - protected function get_parent_slug() { |
|
| 78 | - return null; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * {@inheritdoc} |
|
| 83 | - */ |
|
| 84 | - public function get_page_title() { |
|
| 85 | - |
|
| 86 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * {@inheritdoc} |
|
| 91 | - */ |
|
| 92 | - public function get_menu_title() { |
|
| 93 | - |
|
| 94 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * {@inheritdoc} |
|
| 99 | - */ |
|
| 100 | - public function get_menu_slug() { |
|
| 101 | - |
|
| 102 | - return 'wl_edit_mapping'; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * {@inheritdoc} |
|
| 107 | - */ |
|
| 108 | - public function get_partial_name() { |
|
| 109 | - return 'wordlift-admin-mappings-edit.php'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - */ |
|
| 115 | - public function enqueue_scripts() { |
|
| 116 | - |
|
| 117 | - // Enqueue the script. |
|
| 118 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 119 | - 'wl-mappings-edit', |
|
| 120 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 121 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 122 | - true |
|
| 123 | - ); |
|
| 124 | - |
|
| 125 | - // Enqueue the style. |
|
| 126 | - wp_enqueue_style( |
|
| 127 | - 'wl-mappings-edit', |
|
| 128 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 129 | - Wordlift::get_instance()->get_version() |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Returns field name options based on the chosen field type. |
|
| 135 | - * if string is returned a text field would be shown to user, if an array of options is returned |
|
| 136 | - * then the select box would be shown to user. |
|
| 137 | - * |
|
| 138 | - * @return array Array of the options. |
|
| 139 | - */ |
|
| 140 | - public static function get_all_field_name_options() { |
|
| 141 | - |
|
| 142 | - $options = array( |
|
| 143 | - array( |
|
| 144 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
| 145 | - 'value' => '', |
|
| 146 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 147 | - ), |
|
| 148 | - // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
| 149 | - array( |
|
| 150 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
| 151 | - 'value' => '', |
|
| 152 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 153 | - ), |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Allow 3rd parties to add field types. |
|
| 158 | - * |
|
| 159 | - * @param array An array of Field Types. |
|
| 160 | - * |
|
| 161 | - * @return array An array of Field Types. |
|
| 162 | - * |
|
| 163 | - * @since 3.25.0 |
|
| 164 | - */ |
|
| 165 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @return array An Array containing key value pairs of settings. |
|
| 170 | - * @since 3.25.0 |
|
| 171 | - * Load dependencies required for js client. |
|
| 172 | - */ |
|
| 173 | - public function get_ui_settings_array() { |
|
| 174 | - // Create ui settings array to be used by js client. |
|
| 175 | - $edit_mapping_settings = array(); |
|
| 176 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 177 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 178 | - $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
| 179 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 180 | - // Load logic field options. |
|
| 181 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 182 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 183 | - |
|
| 184 | - return $edit_mapping_settings; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Returns post type, post category, or any other post taxonomies |
|
| 189 | - * @return array An array of select options |
|
| 190 | - */ |
|
| 191 | - private static function get_post_taxonomies_and_terms() { |
|
| 192 | - $taxonomy_options = array(); |
|
| 193 | - $term_options = array(); |
|
| 194 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 195 | - |
|
| 196 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 197 | - array_push( |
|
| 198 | - $taxonomy_options, |
|
| 199 | - array( |
|
| 200 | - 'label' => $taxonomy->label, |
|
| 201 | - 'value' => $taxonomy->name, |
|
| 202 | - 'api_source' => 'taxonomy' |
|
| 203 | - ) |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
| 207 | - $post_type_array = self::get_post_type_key_and_value(); |
|
| 208 | - $post_type_option = $post_type_array['post_type_option_name']; |
|
| 209 | - // Get also the list of post types from the post_type_array. |
|
| 210 | - $post_type_option_values = $post_type_array['post_type_option_values']; |
|
| 211 | - |
|
| 212 | - $post_taxonomy_array = self::get_post_taxonomy_key_and_value(); |
|
| 213 | - $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
|
| 214 | - |
|
| 215 | - // Merge the post type option and post types in the taxonomy options |
|
| 216 | - array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
| 217 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 218 | - |
|
| 219 | - return array( |
|
| 220 | - 'taxonomy_options' => $taxonomy_options, |
|
| 221 | - 'term_options' => $term_options |
|
| 222 | - ); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Return post type option and post type option values. |
|
| 227 | - * |
|
| 228 | - * @return array Array of post_type_option and post_type_option_values. |
|
| 229 | - */ |
|
| 230 | - private static function get_post_type_key_and_value() { |
|
| 231 | - $post_type_option_name = array( |
|
| 232 | - 'label' => __( 'Post type', 'wordlift' ), |
|
| 233 | - 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
| 234 | - // Left empty since post types are provided locally. |
|
| 235 | - 'api_source' => '', |
|
| 236 | - ); |
|
| 237 | - $post_type_option_values = array(); |
|
| 238 | - $post_types = get_post_types( |
|
| 239 | - array(), |
|
| 240 | - 'objects' |
|
| 241 | - ); |
|
| 242 | - |
|
| 243 | - foreach ( $post_types as $post_type ) { |
|
| 244 | - array_push( |
|
| 245 | - $post_type_option_values, |
|
| 246 | - array( |
|
| 247 | - 'label' => $post_type->label, |
|
| 248 | - 'value' => $post_type->name, |
|
| 249 | - 'parent_value' => 'post_type', |
|
| 250 | - ) |
|
| 251 | - ); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return array( |
|
| 255 | - 'post_type_option_name' => $post_type_option_name, |
|
| 256 | - 'post_type_option_values' => $post_type_option_values |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Return post type option and post type option values. |
|
| 262 | - * |
|
| 263 | - * @return array Array of post_type_option and post_type_option_values. |
|
| 264 | - */ |
|
| 265 | - private static function get_post_taxonomy_key_and_value() { |
|
| 266 | - |
|
| 267 | - $post_taxonomy_option_name = array( |
|
| 268 | - 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
| 269 | - 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
|
| 270 | - 'api_source' => 'post_taxonomy' |
|
| 271 | - ); |
|
| 272 | - $post_taxonomy_option_values = array(); |
|
| 273 | - |
|
| 274 | - return array( |
|
| 275 | - 'post_taxonomy_option_name' => $post_taxonomy_option_name, |
|
| 276 | - 'post_taxonomy_option_values' => $post_taxonomy_option_values |
|
| 277 | - ); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
| 282 | - * |
|
| 283 | - * @param array $edit_mapping_settings |
|
| 284 | - * |
|
| 285 | - * @return array Loads the logic field options to the $edit_mapping_settings. |
|
| 286 | - */ |
|
| 287 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 288 | - $edit_mapping_settings['wl_logic_field_options'] = array( |
|
| 289 | - array( |
|
| 290 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
| 291 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
| 292 | - ), |
|
| 293 | - array( |
|
| 294 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 295 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
| 296 | - ), |
|
| 297 | - ); |
|
| 298 | - |
|
| 299 | - return $edit_mapping_settings; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
| 304 | - * |
|
| 305 | - * @param array $edit_mapping_settings |
|
| 306 | - * |
|
| 307 | - * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
| 308 | - */ |
|
| 309 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 310 | - // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
|
| 311 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 312 | - && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 313 | - // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
| 314 | - $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
| 315 | - (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ) : 0; |
|
| 316 | - $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return $edit_mapping_settings; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Load the rest settings required for the edit_mappings js client. |
|
| 324 | - * |
|
| 325 | - * @param array $edit_mapping_settings |
|
| 326 | - * |
|
| 327 | - * @return array |
|
| 328 | - */ |
|
| 329 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 330 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 331 | - null, |
|
| 332 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 333 | - ); |
|
| 334 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 335 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 336 | - |
|
| 337 | - return $edit_mapping_settings; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Load the rule field options in to the settings. |
|
| 342 | - * |
|
| 343 | - * @param array $edit_mapping_settings |
|
| 344 | - * |
|
| 345 | - * @return array Return the settings. |
|
| 346 | - */ |
|
| 347 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 348 | - // Load the rule field options. |
|
| 349 | - $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
| 350 | - $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
| 351 | - $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Allow 3rd parties to add ui options. |
|
| 355 | - * |
|
| 356 | - * @param array Array of Rule field one options where each item is in format |
|
| 357 | - * |
|
| 358 | - * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
| 359 | - * |
|
| 360 | - * Leave api_source empty string to ensure didnt fetch rule field two options |
|
| 361 | - * from api. |
|
| 362 | - * |
|
| 363 | - * @return array Array of Rule field one options |
|
| 364 | - * |
|
| 365 | - * @since 3.27.0 |
|
| 366 | - */ |
|
| 367 | - $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
| 368 | - 'wl_mappings_rule_field_one_options', |
|
| 369 | - $edit_mapping_settings['wl_rule_field_one_options'] |
|
| 370 | - ); |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Allow 3rd parties to add rule field two options. |
|
| 374 | - * |
|
| 375 | - * @param array Array of Rule field two option where each item is in format |
|
| 376 | - * |
|
| 377 | - * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
| 378 | - * |
|
| 379 | - * where parent_value is the value of the parent option in the rule_field_one_option. |
|
| 380 | - * |
|
| 381 | - * @since 3.27.0 |
|
| 382 | - */ |
|
| 383 | - $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
| 384 | - 'wl_mappings_rule_field_two_options', |
|
| 385 | - $edit_mapping_settings['wl_rule_field_two_options'] |
|
| 386 | - ); |
|
| 387 | - |
|
| 388 | - return $edit_mapping_settings; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Load field type and field name options to the settings array. |
|
| 393 | - * |
|
| 394 | - * @param array $edit_mapping_settings |
|
| 395 | - * |
|
| 396 | - * @return array |
|
| 397 | - */ |
|
| 398 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 399 | - $all_field_name_options = self::get_all_field_name_options(); |
|
| 400 | - $all_field_types_options = array_map( function ( $item ) { |
|
| 401 | - return array( |
|
| 402 | - 'label' => $item['label'], |
|
| 403 | - 'value' => $item['field_type'], |
|
| 404 | - ); |
|
| 405 | - }, $all_field_name_options ); |
|
| 406 | - |
|
| 407 | - $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
| 408 | - // Add wl_edit_field_name_options. |
|
| 409 | - $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
| 410 | - |
|
| 411 | - return $edit_mapping_settings; |
|
| 412 | - } |
|
| 27 | + /** Instance to store the registry class. |
|
| 28 | + * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
| 29 | + */ |
|
| 30 | + public $transform_function_registry; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Edit_Mappings_Page constructor. |
|
| 34 | + * |
|
| 35 | + * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
| 36 | + */ |
|
| 37 | + public function __construct( $transform_function_registry ) { |
|
| 38 | + parent::__construct(); |
|
| 39 | + $this->transform_function_registry = $transform_function_registry; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function render() { |
|
| 43 | + // Render all the settings when this method is called, because the partial page is loaded after |
|
| 44 | + // this method. |
|
| 45 | + // Load the UI dependencies. |
|
| 46 | + $edit_mapping_settings = $this->get_ui_settings_array(); |
|
| 47 | + // Supply the settings to js client. |
|
| 48 | + wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 49 | + |
|
| 50 | + parent::render(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Load the text settings needed for the edit_mappings_page. |
|
| 55 | + * |
|
| 56 | + * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
| 57 | + * |
|
| 58 | + * @return array Adding text settings to the main settings array. |
|
| 59 | + */ |
|
| 60 | + private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 61 | + $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 62 | + $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 63 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 64 | + $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
| 65 | + |
|
| 66 | + return $edit_mapping_settings; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
| 71 | + * |
|
| 72 | + * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
| 73 | + * show it there. |
|
| 74 | + * |
|
| 75 | + * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
| 76 | + */ |
|
| 77 | + protected function get_parent_slug() { |
|
| 78 | + return null; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * {@inheritdoc} |
|
| 83 | + */ |
|
| 84 | + public function get_page_title() { |
|
| 85 | + |
|
| 86 | + return __( 'Edit Mappings', 'wordlift' ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * {@inheritdoc} |
|
| 91 | + */ |
|
| 92 | + public function get_menu_title() { |
|
| 93 | + |
|
| 94 | + return __( 'Edit Mappings', 'wordlift' ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * {@inheritdoc} |
|
| 99 | + */ |
|
| 100 | + public function get_menu_slug() { |
|
| 101 | + |
|
| 102 | + return 'wl_edit_mapping'; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * {@inheritdoc} |
|
| 107 | + */ |
|
| 108 | + public function get_partial_name() { |
|
| 109 | + return 'wordlift-admin-mappings-edit.php'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + */ |
|
| 115 | + public function enqueue_scripts() { |
|
| 116 | + |
|
| 117 | + // Enqueue the script. |
|
| 118 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 119 | + 'wl-mappings-edit', |
|
| 120 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 121 | + array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 122 | + true |
|
| 123 | + ); |
|
| 124 | + |
|
| 125 | + // Enqueue the style. |
|
| 126 | + wp_enqueue_style( |
|
| 127 | + 'wl-mappings-edit', |
|
| 128 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 129 | + Wordlift::get_instance()->get_version() |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Returns field name options based on the chosen field type. |
|
| 135 | + * if string is returned a text field would be shown to user, if an array of options is returned |
|
| 136 | + * then the select box would be shown to user. |
|
| 137 | + * |
|
| 138 | + * @return array Array of the options. |
|
| 139 | + */ |
|
| 140 | + public static function get_all_field_name_options() { |
|
| 141 | + |
|
| 142 | + $options = array( |
|
| 143 | + array( |
|
| 144 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
| 145 | + 'value' => '', |
|
| 146 | + 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 147 | + ), |
|
| 148 | + // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
| 149 | + array( |
|
| 150 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
| 151 | + 'value' => '', |
|
| 152 | + 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 153 | + ), |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Allow 3rd parties to add field types. |
|
| 158 | + * |
|
| 159 | + * @param array An array of Field Types. |
|
| 160 | + * |
|
| 161 | + * @return array An array of Field Types. |
|
| 162 | + * |
|
| 163 | + * @since 3.25.0 |
|
| 164 | + */ |
|
| 165 | + return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @return array An Array containing key value pairs of settings. |
|
| 170 | + * @since 3.25.0 |
|
| 171 | + * Load dependencies required for js client. |
|
| 172 | + */ |
|
| 173 | + public function get_ui_settings_array() { |
|
| 174 | + // Create ui settings array to be used by js client. |
|
| 175 | + $edit_mapping_settings = array(); |
|
| 176 | + $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 177 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 178 | + $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
| 179 | + $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 180 | + // Load logic field options. |
|
| 181 | + $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 182 | + $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 183 | + |
|
| 184 | + return $edit_mapping_settings; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Returns post type, post category, or any other post taxonomies |
|
| 189 | + * @return array An array of select options |
|
| 190 | + */ |
|
| 191 | + private static function get_post_taxonomies_and_terms() { |
|
| 192 | + $taxonomy_options = array(); |
|
| 193 | + $term_options = array(); |
|
| 194 | + $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 195 | + |
|
| 196 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 197 | + array_push( |
|
| 198 | + $taxonomy_options, |
|
| 199 | + array( |
|
| 200 | + 'label' => $taxonomy->label, |
|
| 201 | + 'value' => $taxonomy->name, |
|
| 202 | + 'api_source' => 'taxonomy' |
|
| 203 | + ) |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
| 207 | + $post_type_array = self::get_post_type_key_and_value(); |
|
| 208 | + $post_type_option = $post_type_array['post_type_option_name']; |
|
| 209 | + // Get also the list of post types from the post_type_array. |
|
| 210 | + $post_type_option_values = $post_type_array['post_type_option_values']; |
|
| 211 | + |
|
| 212 | + $post_taxonomy_array = self::get_post_taxonomy_key_and_value(); |
|
| 213 | + $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
|
| 214 | + |
|
| 215 | + // Merge the post type option and post types in the taxonomy options |
|
| 216 | + array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
| 217 | + $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 218 | + |
|
| 219 | + return array( |
|
| 220 | + 'taxonomy_options' => $taxonomy_options, |
|
| 221 | + 'term_options' => $term_options |
|
| 222 | + ); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Return post type option and post type option values. |
|
| 227 | + * |
|
| 228 | + * @return array Array of post_type_option and post_type_option_values. |
|
| 229 | + */ |
|
| 230 | + private static function get_post_type_key_and_value() { |
|
| 231 | + $post_type_option_name = array( |
|
| 232 | + 'label' => __( 'Post type', 'wordlift' ), |
|
| 233 | + 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
| 234 | + // Left empty since post types are provided locally. |
|
| 235 | + 'api_source' => '', |
|
| 236 | + ); |
|
| 237 | + $post_type_option_values = array(); |
|
| 238 | + $post_types = get_post_types( |
|
| 239 | + array(), |
|
| 240 | + 'objects' |
|
| 241 | + ); |
|
| 242 | + |
|
| 243 | + foreach ( $post_types as $post_type ) { |
|
| 244 | + array_push( |
|
| 245 | + $post_type_option_values, |
|
| 246 | + array( |
|
| 247 | + 'label' => $post_type->label, |
|
| 248 | + 'value' => $post_type->name, |
|
| 249 | + 'parent_value' => 'post_type', |
|
| 250 | + ) |
|
| 251 | + ); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return array( |
|
| 255 | + 'post_type_option_name' => $post_type_option_name, |
|
| 256 | + 'post_type_option_values' => $post_type_option_values |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Return post type option and post type option values. |
|
| 262 | + * |
|
| 263 | + * @return array Array of post_type_option and post_type_option_values. |
|
| 264 | + */ |
|
| 265 | + private static function get_post_taxonomy_key_and_value() { |
|
| 266 | + |
|
| 267 | + $post_taxonomy_option_name = array( |
|
| 268 | + 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
| 269 | + 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
|
| 270 | + 'api_source' => 'post_taxonomy' |
|
| 271 | + ); |
|
| 272 | + $post_taxonomy_option_values = array(); |
|
| 273 | + |
|
| 274 | + return array( |
|
| 275 | + 'post_taxonomy_option_name' => $post_taxonomy_option_name, |
|
| 276 | + 'post_taxonomy_option_values' => $post_taxonomy_option_values |
|
| 277 | + ); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
| 282 | + * |
|
| 283 | + * @param array $edit_mapping_settings |
|
| 284 | + * |
|
| 285 | + * @return array Loads the logic field options to the $edit_mapping_settings. |
|
| 286 | + */ |
|
| 287 | + private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 288 | + $edit_mapping_settings['wl_logic_field_options'] = array( |
|
| 289 | + array( |
|
| 290 | + 'label' => __( 'is equal to', 'wordlift' ), |
|
| 291 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
| 292 | + ), |
|
| 293 | + array( |
|
| 294 | + 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 295 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
| 296 | + ), |
|
| 297 | + ); |
|
| 298 | + |
|
| 299 | + return $edit_mapping_settings; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
| 304 | + * |
|
| 305 | + * @param array $edit_mapping_settings |
|
| 306 | + * |
|
| 307 | + * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
| 308 | + */ |
|
| 309 | + private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 310 | + // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
|
| 311 | + if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 312 | + && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 313 | + // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
| 314 | + $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
| 315 | + (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ) : 0; |
|
| 316 | + $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return $edit_mapping_settings; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Load the rest settings required for the edit_mappings js client. |
|
| 324 | + * |
|
| 325 | + * @param array $edit_mapping_settings |
|
| 326 | + * |
|
| 327 | + * @return array |
|
| 328 | + */ |
|
| 329 | + private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 330 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 331 | + null, |
|
| 332 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 333 | + ); |
|
| 334 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 335 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 336 | + |
|
| 337 | + return $edit_mapping_settings; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Load the rule field options in to the settings. |
|
| 342 | + * |
|
| 343 | + * @param array $edit_mapping_settings |
|
| 344 | + * |
|
| 345 | + * @return array Return the settings. |
|
| 346 | + */ |
|
| 347 | + private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 348 | + // Load the rule field options. |
|
| 349 | + $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
| 350 | + $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
| 351 | + $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Allow 3rd parties to add ui options. |
|
| 355 | + * |
|
| 356 | + * @param array Array of Rule field one options where each item is in format |
|
| 357 | + * |
|
| 358 | + * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
| 359 | + * |
|
| 360 | + * Leave api_source empty string to ensure didnt fetch rule field two options |
|
| 361 | + * from api. |
|
| 362 | + * |
|
| 363 | + * @return array Array of Rule field one options |
|
| 364 | + * |
|
| 365 | + * @since 3.27.0 |
|
| 366 | + */ |
|
| 367 | + $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
| 368 | + 'wl_mappings_rule_field_one_options', |
|
| 369 | + $edit_mapping_settings['wl_rule_field_one_options'] |
|
| 370 | + ); |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Allow 3rd parties to add rule field two options. |
|
| 374 | + * |
|
| 375 | + * @param array Array of Rule field two option where each item is in format |
|
| 376 | + * |
|
| 377 | + * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
| 378 | + * |
|
| 379 | + * where parent_value is the value of the parent option in the rule_field_one_option. |
|
| 380 | + * |
|
| 381 | + * @since 3.27.0 |
|
| 382 | + */ |
|
| 383 | + $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
| 384 | + 'wl_mappings_rule_field_two_options', |
|
| 385 | + $edit_mapping_settings['wl_rule_field_two_options'] |
|
| 386 | + ); |
|
| 387 | + |
|
| 388 | + return $edit_mapping_settings; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Load field type and field name options to the settings array. |
|
| 393 | + * |
|
| 394 | + * @param array $edit_mapping_settings |
|
| 395 | + * |
|
| 396 | + * @return array |
|
| 397 | + */ |
|
| 398 | + private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 399 | + $all_field_name_options = self::get_all_field_name_options(); |
|
| 400 | + $all_field_types_options = array_map( function ( $item ) { |
|
| 401 | + return array( |
|
| 402 | + 'label' => $item['label'], |
|
| 403 | + 'value' => $item['field_type'], |
|
| 404 | + ); |
|
| 405 | + }, $all_field_name_options ); |
|
| 406 | + |
|
| 407 | + $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
| 408 | + // Add wl_edit_field_name_options. |
|
| 409 | + $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
| 410 | + |
|
| 411 | + return $edit_mapping_settings; |
|
| 412 | + } |
|
| 413 | 413 | |
| 414 | 414 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
| 36 | 36 | */ |
| 37 | - public function __construct( $transform_function_registry ) { |
|
| 37 | + public function __construct($transform_function_registry) { |
|
| 38 | 38 | parent::__construct(); |
| 39 | 39 | $this->transform_function_registry = $transform_function_registry; |
| 40 | 40 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | // Load the UI dependencies. |
| 46 | 46 | $edit_mapping_settings = $this->get_ui_settings_array(); |
| 47 | 47 | // Supply the settings to js client. |
| 48 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 48 | + wp_localize_script('wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings); |
|
| 49 | 49 | |
| 50 | 50 | parent::render(); |
| 51 | 51 | } |
@@ -57,10 +57,10 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return array Adding text settings to the main settings array. |
| 59 | 59 | */ |
| 60 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 61 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 62 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 63 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 60 | + private function load_text_settings_for_edit_mapping_page(array $edit_mapping_settings) { |
|
| 61 | + $edit_mapping_settings['wl_add_mapping_text'] = __('Add Mapping', 'wordlift'); |
|
| 62 | + $edit_mapping_settings['wl_edit_mapping_text'] = __('Edit Mapping', 'wordlift'); |
|
| 63 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __('Unable to find the mapping item', 'wordlift'); |
|
| 64 | 64 | $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
| 65 | 65 | |
| 66 | 66 | return $edit_mapping_settings; |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | public function get_page_title() { |
| 85 | 85 | |
| 86 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 86 | + return __('Edit Mappings', 'wordlift'); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function get_menu_title() { |
| 93 | 93 | |
| 94 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 94 | + return __('Edit Mappings', 'wordlift'); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -117,15 +117,15 @@ discard block |
||
| 117 | 117 | // Enqueue the script. |
| 118 | 118 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 119 | 119 | 'wl-mappings-edit', |
| 120 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 121 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 120 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit', |
|
| 121 | + array('react', 'react-dom', 'wp-polyfill'), |
|
| 122 | 122 | true |
| 123 | 123 | ); |
| 124 | 124 | |
| 125 | 125 | // Enqueue the style. |
| 126 | 126 | wp_enqueue_style( |
| 127 | 127 | 'wl-mappings-edit', |
| 128 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 128 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit.css', |
|
| 129 | 129 | Wordlift::get_instance()->get_version() |
| 130 | 130 | ); |
| 131 | 131 | } |
@@ -143,13 +143,13 @@ discard block |
||
| 143 | 143 | array( |
| 144 | 144 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
| 145 | 145 | 'value' => '', |
| 146 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 146 | + 'label' => __('Fixed Text', 'wordlift'), |
|
| 147 | 147 | ), |
| 148 | 148 | // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
| 149 | 149 | array( |
| 150 | 150 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
| 151 | 151 | 'value' => '', |
| 152 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 152 | + 'label' => __('Custom Field', 'wordlift'), |
|
| 153 | 153 | ), |
| 154 | 154 | ); |
| 155 | 155 | |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | * |
| 163 | 163 | * @since 3.25.0 |
| 164 | 164 | */ |
| 165 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 165 | + return apply_filters('wl_mappings_field_types', $options); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -173,13 +173,13 @@ discard block |
||
| 173 | 173 | public function get_ui_settings_array() { |
| 174 | 174 | // Create ui settings array to be used by js client. |
| 175 | 175 | $edit_mapping_settings = array(); |
| 176 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 177 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 176 | + $edit_mapping_settings = $this->load_rest_settings($edit_mapping_settings); |
|
| 177 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page($edit_mapping_settings); |
|
| 178 | 178 | $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
| 179 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 179 | + $edit_mapping_settings = $this->load_field_type_and_name_options($edit_mapping_settings); |
|
| 180 | 180 | // Load logic field options. |
| 181 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 182 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 181 | + $edit_mapping_settings = $this->load_logic_field_options($edit_mapping_settings); |
|
| 182 | + $edit_mapping_settings = $this->load_rule_field_options($edit_mapping_settings); |
|
| 183 | 183 | |
| 184 | 184 | return $edit_mapping_settings; |
| 185 | 185 | } |
@@ -191,9 +191,9 @@ discard block |
||
| 191 | 191 | private static function get_post_taxonomies_and_terms() { |
| 192 | 192 | $taxonomy_options = array(); |
| 193 | 193 | $term_options = array(); |
| 194 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 194 | + $taxonomies = get_object_taxonomies('post', 'objects'); |
|
| 195 | 195 | |
| 196 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 196 | + foreach ($taxonomies as $taxonomy) { |
|
| 197 | 197 | array_push( |
| 198 | 198 | $taxonomy_options, |
| 199 | 199 | array( |
@@ -213,8 +213,8 @@ discard block |
||
| 213 | 213 | $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
| 214 | 214 | |
| 215 | 215 | // Merge the post type option and post types in the taxonomy options |
| 216 | - array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
| 217 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 216 | + array_push($taxonomy_options, $post_type_option, $post_taxonomy_option); |
|
| 217 | + $term_options = array_merge($term_options, $post_type_option_values); |
|
| 218 | 218 | |
| 219 | 219 | return array( |
| 220 | 220 | 'taxonomy_options' => $taxonomy_options, |
@@ -228,8 +228,8 @@ discard block |
||
| 228 | 228 | * @return array Array of post_type_option and post_type_option_values. |
| 229 | 229 | */ |
| 230 | 230 | private static function get_post_type_key_and_value() { |
| 231 | - $post_type_option_name = array( |
|
| 232 | - 'label' => __( 'Post type', 'wordlift' ), |
|
| 231 | + $post_type_option_name = array( |
|
| 232 | + 'label' => __('Post type', 'wordlift'), |
|
| 233 | 233 | 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
| 234 | 234 | // Left empty since post types are provided locally. |
| 235 | 235 | 'api_source' => '', |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | 'objects' |
| 241 | 241 | ); |
| 242 | 242 | |
| 243 | - foreach ( $post_types as $post_type ) { |
|
| 243 | + foreach ($post_types as $post_type) { |
|
| 244 | 244 | array_push( |
| 245 | 245 | $post_type_option_values, |
| 246 | 246 | array( |
@@ -264,8 +264,8 @@ discard block |
||
| 264 | 264 | */ |
| 265 | 265 | private static function get_post_taxonomy_key_and_value() { |
| 266 | 266 | |
| 267 | - $post_taxonomy_option_name = array( |
|
| 268 | - 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
| 267 | + $post_taxonomy_option_name = array( |
|
| 268 | + 'label' => __('Post Taxonomy', 'wordlift'), |
|
| 269 | 269 | 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
| 270 | 270 | 'api_source' => 'post_taxonomy' |
| 271 | 271 | ); |
@@ -284,14 +284,14 @@ discard block |
||
| 284 | 284 | * |
| 285 | 285 | * @return array Loads the logic field options to the $edit_mapping_settings. |
| 286 | 286 | */ |
| 287 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 287 | + private function load_logic_field_options(array $edit_mapping_settings) { |
|
| 288 | 288 | $edit_mapping_settings['wl_logic_field_options'] = array( |
| 289 | 289 | array( |
| 290 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
| 290 | + 'label' => __('is equal to', 'wordlift'), |
|
| 291 | 291 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
| 292 | 292 | ), |
| 293 | 293 | array( |
| 294 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 294 | + 'label' => __('is not equal to', 'wordlift'), |
|
| 295 | 295 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
| 296 | 296 | ), |
| 297 | 297 | ); |
@@ -306,13 +306,13 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
| 308 | 308 | */ |
| 309 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 309 | + private function validate_nonce_and_assign_mapping_id(array $edit_mapping_settings) { |
|
| 310 | 310 | // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
| 311 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 312 | - && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 311 | + if (isset($_REQUEST['_wl_edit_mapping_nonce']) |
|
| 312 | + && wp_verify_nonce($_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce')) { |
|
| 313 | 313 | // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
| 314 | - $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
| 315 | - (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ) : 0; |
|
| 314 | + $mapping_id = isset($_REQUEST['wl_edit_mapping_id']) ? |
|
| 315 | + (int) filter_var($_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT) : 0; |
|
| 316 | 316 | $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
| 317 | 317 | } |
| 318 | 318 | |
@@ -326,13 +326,13 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @return array |
| 328 | 328 | */ |
| 329 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 330 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 329 | + private function load_rest_settings(array $edit_mapping_settings) { |
|
| 330 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 331 | 331 | null, |
| 332 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 332 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 333 | 333 | ); |
| 334 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 335 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 334 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce('wp_rest'); |
|
| 335 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id($edit_mapping_settings); |
|
| 336 | 336 | |
| 337 | 337 | return $edit_mapping_settings; |
| 338 | 338 | } |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | * |
| 345 | 345 | * @return array Return the settings. |
| 346 | 346 | */ |
| 347 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 347 | + private function load_rule_field_options(array $edit_mapping_settings) { |
|
| 348 | 348 | // Load the rule field options. |
| 349 | 349 | $rule_field_data = self::get_post_taxonomies_and_terms(); |
| 350 | 350 | $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
@@ -395,14 +395,14 @@ discard block |
||
| 395 | 395 | * |
| 396 | 396 | * @return array |
| 397 | 397 | */ |
| 398 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 398 | + private function load_field_type_and_name_options(array $edit_mapping_settings) { |
|
| 399 | 399 | $all_field_name_options = self::get_all_field_name_options(); |
| 400 | - $all_field_types_options = array_map( function ( $item ) { |
|
| 400 | + $all_field_types_options = array_map(function($item) { |
|
| 401 | 401 | return array( |
| 402 | 402 | 'label' => $item['label'], |
| 403 | 403 | 'value' => $item['field_type'], |
| 404 | 404 | ); |
| 405 | - }, $all_field_name_options ); |
|
| 405 | + }, $all_field_name_options); |
|
| 406 | 406 | |
| 407 | 407 | $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
| 408 | 408 | // Add wl_edit_field_name_options. |
@@ -4,192 +4,192 @@ |
||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | class Config { |
| 7 | - /** |
|
| 8 | - * @var \Wordlift_Admin_Setup |
|
| 9 | - */ |
|
| 10 | - private $admin_setup; |
|
| 11 | - /** |
|
| 12 | - * @var \Wordlift_Key_Validation_Service |
|
| 13 | - */ |
|
| 14 | - private $key_validation_service; |
|
| 15 | - /** |
|
| 16 | - * @var \Wordlift_Configuration_Service |
|
| 17 | - */ |
|
| 18 | - private $configuration_service; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Config constructor. |
|
| 22 | - * |
|
| 23 | - * @param $admin_setup \Wordlift_Admin_Setup |
|
| 24 | - * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 25 | - * @param $configuration_service \Wordlift_Configuration_Service |
|
| 26 | - */ |
|
| 27 | - public function __construct( $admin_setup, $key_validation_service, $configuration_service ) { |
|
| 28 | - |
|
| 29 | - $this->admin_setup = $admin_setup; |
|
| 30 | - $this->key_validation_service = $key_validation_service; |
|
| 31 | - $this->configuration_service = $configuration_service; |
|
| 32 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Check if the key is valid and also not bound to any domain. |
|
| 38 | - * |
|
| 39 | - * @param $key string |
|
| 40 | - * |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 44 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * we need to check if the key is not associated with any account |
|
| 48 | - * before setting it, we should check if the url is null. |
|
| 49 | - */ |
|
| 50 | - if ( is_wp_error( $account_info ) |
|
| 51 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $account_info_json = $account_info['body']; |
|
| 56 | - |
|
| 57 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 58 | - |
|
| 59 | - if ( ! $account_info_data ) { |
|
| 60 | - // Invalid json returned by api. |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - if ( $account_info_data['url'] === null ) { |
|
| 68 | - return true; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Check if the key belongs to same site. |
|
| 72 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 73 | - // key already associated with another account. |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Return true if the key domain and site domain are the same. |
|
| 78 | - return true; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - public function config() { |
|
| 83 | - |
|
| 84 | - // Perform validation check for all the parameters. |
|
| 85 | - $required_fields = array( |
|
| 86 | - 'diagnostic', |
|
| 87 | - 'vocabulary', |
|
| 88 | - 'language', |
|
| 89 | - 'country', |
|
| 90 | - 'publisherName', |
|
| 91 | - 'publisher', |
|
| 92 | - 'license' |
|
| 93 | - ); |
|
| 94 | - |
|
| 95 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 96 | - |
|
| 97 | - // validate all the fields before processing |
|
| 98 | - foreach ( $required_fields as $field ) { |
|
| 99 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 100 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 101 | - |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $key = (string) $_POST['license']; |
|
| 107 | - |
|
| 108 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 109 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 110 | - |
|
| 111 | - // exit if not valid. |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - |
|
| 7 | + /** |
|
| 8 | + * @var \Wordlift_Admin_Setup |
|
| 9 | + */ |
|
| 10 | + private $admin_setup; |
|
| 11 | + /** |
|
| 12 | + * @var \Wordlift_Key_Validation_Service |
|
| 13 | + */ |
|
| 14 | + private $key_validation_service; |
|
| 15 | + /** |
|
| 16 | + * @var \Wordlift_Configuration_Service |
|
| 17 | + */ |
|
| 18 | + private $configuration_service; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Config constructor. |
|
| 22 | + * |
|
| 23 | + * @param $admin_setup \Wordlift_Admin_Setup |
|
| 24 | + * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 25 | + * @param $configuration_service \Wordlift_Configuration_Service |
|
| 26 | + */ |
|
| 27 | + public function __construct( $admin_setup, $key_validation_service, $configuration_service ) { |
|
| 28 | + |
|
| 29 | + $this->admin_setup = $admin_setup; |
|
| 30 | + $this->key_validation_service = $key_validation_service; |
|
| 31 | + $this->configuration_service = $configuration_service; |
|
| 32 | + add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Check if the key is valid and also not bound to any domain. |
|
| 38 | + * |
|
| 39 | + * @param $key string |
|
| 40 | + * |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 44 | + $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * we need to check if the key is not associated with any account |
|
| 48 | + * before setting it, we should check if the url is null. |
|
| 49 | + */ |
|
| 50 | + if ( is_wp_error( $account_info ) |
|
| 51 | + || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $account_info_json = $account_info['body']; |
|
| 56 | + |
|
| 57 | + $account_info_data = json_decode( $account_info_json, true ); |
|
| 58 | + |
|
| 59 | + if ( ! $account_info_data ) { |
|
| 60 | + // Invalid json returned by api. |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + if ( $account_info_data['url'] === null ) { |
|
| 68 | + return true; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Check if the key belongs to same site. |
|
| 72 | + if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 73 | + // key already associated with another account. |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Return true if the key domain and site domain are the same. |
|
| 78 | + return true; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + public function config() { |
|
| 83 | + |
|
| 84 | + // Perform validation check for all the parameters. |
|
| 85 | + $required_fields = array( |
|
| 86 | + 'diagnostic', |
|
| 87 | + 'vocabulary', |
|
| 88 | + 'language', |
|
| 89 | + 'country', |
|
| 90 | + 'publisherName', |
|
| 91 | + 'publisher', |
|
| 92 | + 'license' |
|
| 93 | + ); |
|
| 94 | + |
|
| 95 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 96 | + |
|
| 97 | + // validate all the fields before processing |
|
| 98 | + foreach ( $required_fields as $field ) { |
|
| 99 | + if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 100 | + wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 101 | + |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $key = (string) $_POST['license']; |
|
| 107 | + |
|
| 108 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 109 | + wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 110 | + |
|
| 111 | + // exit if not valid. |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | 115 | |
| 116 | - // check if key is already configured, if yes then dont save settings. |
|
| 117 | - if ( $this->configuration_service->get_key() ) { |
|
| 118 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 116 | + // check if key is already configured, if yes then dont save settings. |
|
| 117 | + if ( $this->configuration_service->get_key() ) { |
|
| 118 | + wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 119 | 119 | |
| 120 | - // key already configured |
|
| 121 | - return; |
|
| 122 | - } |
|
| 120 | + // key already configured |
|
| 121 | + return; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 124 | + $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 125 | 125 | |
| 126 | 126 | |
| 127 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 128 | - } |
|
| 127 | + wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * |
|
| 132 | - * @return array |
|
| 133 | - */ |
|
| 134 | - private function get_params() { |
|
| 130 | + /** |
|
| 131 | + * |
|
| 132 | + * @return array |
|
| 133 | + */ |
|
| 134 | + private function get_params() { |
|
| 135 | 135 | |
| 136 | - $attachment_id = $this->may_be_get_attachment_id(); |
|
| 136 | + $attachment_id = $this->may_be_get_attachment_id(); |
|
| 137 | 137 | |
| 138 | - $params = array( |
|
| 139 | - 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 140 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 141 | - 'wl-site-language' => isset( $_POST['language'] ) ? (string) $_POST['language'] : '', |
|
| 142 | - 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 143 | - 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 144 | - 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 145 | - 'logo' => $attachment_id |
|
| 146 | - ); |
|
| 138 | + $params = array( |
|
| 139 | + 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 140 | + 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 141 | + 'wl-site-language' => isset( $_POST['language'] ) ? (string) $_POST['language'] : '', |
|
| 142 | + 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 143 | + 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 144 | + 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 145 | + 'logo' => $attachment_id |
|
| 146 | + ); |
|
| 147 | 147 | |
| 148 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 149 | - if ( $diagnostic ) { |
|
| 150 | - $params['share-diagnostic'] = 'on'; |
|
| 151 | - } |
|
| 148 | + $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 149 | + if ( $diagnostic ) { |
|
| 150 | + $params['share-diagnostic'] = 'on'; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - return $params; |
|
| 154 | - } |
|
| 153 | + return $params; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @return int | bool |
|
| 158 | - */ |
|
| 159 | - private function may_be_get_attachment_id() { |
|
| 156 | + /** |
|
| 157 | + * @return int | bool |
|
| 158 | + */ |
|
| 159 | + private function may_be_get_attachment_id() { |
|
| 160 | 160 | |
| 161 | - // if image or image extension not posted then return false. |
|
| 162 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 161 | + // if image or image extension not posted then return false. |
|
| 162 | + if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 167 | - $image_string = (string) $_POST['image']; |
|
| 168 | - $image_ext = (string) $_POST['imageExtension']; |
|
| 166 | + $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 167 | + $image_string = (string) $_POST['image']; |
|
| 168 | + $image_ext = (string) $_POST['imageExtension']; |
|
| 169 | 169 | |
| 170 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 171 | - return false; |
|
| 172 | - } |
|
| 170 | + if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 174 | + $image_decoded_string = base64_decode( $image_string ); |
|
| 175 | 175 | |
| 176 | - $upload_dir = wp_upload_dir(); |
|
| 176 | + $upload_dir = wp_upload_dir(); |
|
| 177 | 177 | |
| 178 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 178 | + $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 179 | 179 | |
| 180 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 180 | + file_put_contents( $file_path, $image_decoded_string ); |
|
| 181 | 181 | |
| 182 | - $attachment_id = wp_insert_attachment( array( |
|
| 183 | - 'post_status' => 'inherit', |
|
| 184 | - 'post_mime_type' => "image/$image_ext" |
|
| 185 | - ), $file_path ); |
|
| 182 | + $attachment_id = wp_insert_attachment( array( |
|
| 183 | + 'post_status' => 'inherit', |
|
| 184 | + 'post_mime_type' => "image/$image_ext" |
|
| 185 | + ), $file_path ); |
|
| 186 | 186 | |
| 187 | - // Generate the metadata for the attachment, and update the database record. |
|
| 188 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 189 | - // Update the attachment metadata. |
|
| 190 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 187 | + // Generate the metadata for the attachment, and update the database record. |
|
| 188 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 189 | + // Update the attachment metadata. |
|
| 190 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 191 | 191 | |
| 192 | - return $attachment_id; |
|
| 193 | - } |
|
| 192 | + return $attachment_id; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | 195 | } |
@@ -24,12 +24,12 @@ discard block |
||
| 24 | 24 | * @param $key_validation_service \Wordlift_Key_Validation_Service |
| 25 | 25 | * @param $configuration_service \Wordlift_Configuration_Service |
| 26 | 26 | */ |
| 27 | - public function __construct( $admin_setup, $key_validation_service, $configuration_service ) { |
|
| 27 | + public function __construct($admin_setup, $key_validation_service, $configuration_service) { |
|
| 28 | 28 | |
| 29 | 29 | $this->admin_setup = $admin_setup; |
| 30 | 30 | $this->key_validation_service = $key_validation_service; |
| 31 | 31 | $this->configuration_service = $configuration_service; |
| 32 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 32 | + add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config')); |
|
| 33 | 33 | |
| 34 | 34 | } |
| 35 | 35 | |
@@ -40,36 +40,36 @@ discard block |
||
| 40 | 40 | * |
| 41 | 41 | * @return bool |
| 42 | 42 | */ |
| 43 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 44 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 43 | + private function is_key_valid_and_not_bound_to_any_domain($key) { |
|
| 44 | + $account_info = $this->key_validation_service->get_account_info($key); |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * we need to check if the key is not associated with any account |
| 48 | 48 | * before setting it, we should check if the url is null. |
| 49 | 49 | */ |
| 50 | - if ( is_wp_error( $account_info ) |
|
| 51 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 50 | + if (is_wp_error($account_info) |
|
| 51 | + || wp_remote_retrieve_response_code($account_info) !== 200) { |
|
| 52 | 52 | return false; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | $account_info_json = $account_info['body']; |
| 56 | 56 | |
| 57 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 57 | + $account_info_data = json_decode($account_info_json, true); |
|
| 58 | 58 | |
| 59 | - if ( ! $account_info_data ) { |
|
| 59 | + if ( ! $account_info_data) { |
|
| 60 | 60 | // Invalid json returned by api. |
| 61 | 61 | return false; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 64 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))); |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - if ( $account_info_data['url'] === null ) { |
|
| 67 | + if ($account_info_data['url'] === null) { |
|
| 68 | 68 | return true; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Check if the key belongs to same site. |
| 72 | - if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) { |
|
| 72 | + if ($site_url !== untrailingslashit($account_info_data['url'])) { |
|
| 73 | 73 | // key already associated with another account. |
| 74 | 74 | return false; |
| 75 | 75 | } |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | 'license' |
| 93 | 93 | ); |
| 94 | 94 | |
| 95 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 95 | + header('Access-Control-Allow-Origin: *'); |
|
| 96 | 96 | |
| 97 | 97 | // validate all the fields before processing |
| 98 | - foreach ( $required_fields as $field ) { |
|
| 99 | - if ( ! array_key_exists( $field, $_POST ) ) { |
|
| 100 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 98 | + foreach ($required_fields as $field) { |
|
| 99 | + if ( ! array_key_exists($field, $_POST)) { |
|
| 100 | + wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422); |
|
| 101 | 101 | |
| 102 | 102 | return; |
| 103 | 103 | } |
@@ -105,8 +105,8 @@ discard block |
||
| 105 | 105 | |
| 106 | 106 | $key = (string) $_POST['license']; |
| 107 | 107 | |
| 108 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 109 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 108 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) { |
|
| 109 | + wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403); |
|
| 110 | 110 | |
| 111 | 111 | // exit if not valid. |
| 112 | 112 | return; |
@@ -114,17 +114,17 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | |
| 116 | 116 | // check if key is already configured, if yes then dont save settings. |
| 117 | - if ( $this->configuration_service->get_key() ) { |
|
| 118 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 117 | + if ($this->configuration_service->get_key()) { |
|
| 118 | + wp_send_json_error(__('Key already configured.', 'wordlift'), 403); |
|
| 119 | 119 | |
| 120 | 120 | // key already configured |
| 121 | 121 | return; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 124 | + $this->admin_setup->save_configuration($this->get_params()); |
|
| 125 | 125 | |
| 126 | 126 | |
| 127 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 127 | + wp_send_json_success(__('Configuration Saved', 'wordlift')); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -136,17 +136,17 @@ discard block |
||
| 136 | 136 | $attachment_id = $this->may_be_get_attachment_id(); |
| 137 | 137 | |
| 138 | 138 | $params = array( |
| 139 | - 'key' => isset( $_POST['license'] ) ? (string) $_POST['license'] : '', |
|
| 140 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '', |
|
| 141 | - 'wl-site-language' => isset( $_POST['language'] ) ? (string) $_POST['language'] : '', |
|
| 142 | - 'wl-country-code' => isset( $_POST['country'] ) ? (string) $_POST['country'] : '', |
|
| 143 | - 'name' => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '', |
|
| 144 | - 'user_type' => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '', |
|
| 139 | + 'key' => isset($_POST['license']) ? (string) $_POST['license'] : '', |
|
| 140 | + 'vocabulary' => isset($_POST['vocabulary']) ? (string) $_POST['vocabulary'] : '', |
|
| 141 | + 'wl-site-language' => isset($_POST['language']) ? (string) $_POST['language'] : '', |
|
| 142 | + 'wl-country-code' => isset($_POST['country']) ? (string) $_POST['country'] : '', |
|
| 143 | + 'name' => isset($_POST['publisherName']) ? (string) $_POST['publisherName'] : '', |
|
| 144 | + 'user_type' => isset($_POST['publisher']) ? (string) $_POST['publisher'] : '', |
|
| 145 | 145 | 'logo' => $attachment_id |
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; |
|
| 149 | - if ( $diagnostic ) { |
|
| 148 | + $diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false; |
|
| 149 | + if ($diagnostic) { |
|
| 150 | 150 | $params['share-diagnostic'] = 'on'; |
| 151 | 151 | } |
| 152 | 152 | |
@@ -159,35 +159,35 @@ discard block |
||
| 159 | 159 | private function may_be_get_attachment_id() { |
| 160 | 160 | |
| 161 | 161 | // if image or image extension not posted then return false. |
| 162 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { |
|
| 162 | + if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) { |
|
| 163 | 163 | return false; |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 166 | + $allowed_extensions = array('png', 'jpeg', 'jpg'); |
|
| 167 | 167 | $image_string = (string) $_POST['image']; |
| 168 | 168 | $image_ext = (string) $_POST['imageExtension']; |
| 169 | 169 | |
| 170 | - if ( ! in_array( $image_ext, $allowed_extensions ) ) { |
|
| 170 | + if ( ! in_array($image_ext, $allowed_extensions)) { |
|
| 171 | 171 | return false; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 174 | + $image_decoded_string = base64_decode($image_string); |
|
| 175 | 175 | |
| 176 | 176 | $upload_dir = wp_upload_dir(); |
| 177 | 177 | |
| 178 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext; |
|
| 178 | + $file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).".".$image_ext; |
|
| 179 | 179 | |
| 180 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 180 | + file_put_contents($file_path, $image_decoded_string); |
|
| 181 | 181 | |
| 182 | - $attachment_id = wp_insert_attachment( array( |
|
| 182 | + $attachment_id = wp_insert_attachment(array( |
|
| 183 | 183 | 'post_status' => 'inherit', |
| 184 | 184 | 'post_mime_type' => "image/$image_ext" |
| 185 | - ), $file_path ); |
|
| 185 | + ), $file_path); |
|
| 186 | 186 | |
| 187 | 187 | // Generate the metadata for the attachment, and update the database record. |
| 188 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 188 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); |
|
| 189 | 189 | // Update the attachment metadata. |
| 190 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 190 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 191 | 191 | |
| 192 | 192 | return $attachment_id; |
| 193 | 193 | } |