LLMS_Abstract_Processor
Background Processor abstract class
Source Source
File: includes/abstracts/llms.abstract.processor.php
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | abstract class LLMS_Abstract_Processor extends WP_Background_Process { /** * Prefix * * @var string */ protected $prefix = 'llms' ; /** * Unique identifier for the processor * * @var string */ protected $id ; /** * Initializer * * Acts as a constructor that extending processors should implement * at the very least should populate the $this->actions array. * * @since 3.15.0 * * @return void */ abstract protected function init(); /** * Array of actions that should be watched to trigger * the process(es) * * @var array */ protected $actions = array (); /** * Constructor * * @since 3.15.0 * * @return void */ public function __construct() { $this ->action .= '_' . $this ->id; parent::__construct(); // Setup. $this ->init(); // Add trigger actions. $this ->add_actions(); } /** * Add actions defined in $this->actions * * @since 3.15.0 * * @return void */ public function add_actions() { foreach ( $this ->get_actions() as $action => $data ) { $data = wp_parse_args( $data , array ( 'arguments' => 1, 'priority' => 10, ) ); add_action( $action , array ( $this , $data [ 'callback' ] ), $data [ 'priority' ], $data [ 'arguments' ] ); } } /** * Disable a processor * * Useful when bulk enrolling into a membership (for example) * so we don't trigger course data calculations a few hundred times. * * @since 3.15.0 * * @return void */ public function disable() { remove_action( $this ->cron_hook_identifier, array ( $this , 'handle_cron_healthcheck' ) ); foreach ( $this ->get_actions() as $action => $data ) { $data = wp_parse_args( $data , array ( 'arguments' => 1, 'priority' => 10, ) ); remove_action( $action , array ( $this , $data [ 'callback' ] ), $data [ 'priority' ], $data [ 'arguments' ] ); } } /** * Dispatch * * Overrides the parent method to reset the (saved) `$data` property and * prevent duplicate data being pushed into future batches. * * @since 4.21.0 * * @return array|WP_Error Result of wp_remote_post() */ public function dispatch() { // Perform the parent method. $ret = parent::dispatch(); /** * Empty the (saved) data to prevent duplicate data in future batches. * */ $this ->data = array (); return $ret ; } /** * Retrieve a filtered array of actions to be added by $this->add_actions * * @since 3.15.0 * * @return array */ private function get_actions() { return apply_filters( 'llms_data_processor_' . $this ->id . '_actions' , $this ->actions, $this ); } /** * Retrieve data for the current processor that can be used * in future processes * * @since 3.15.0 * * @param string $key If set, return a specific piece of data rather than the whole array. * @param string $default When returning a specific piece of data, allows a default value to be passed. * @return array|mixed */ public function get_data( $key = null, $default = '' ) { // Get the array of processor data. $all_data = get_option( 'llms_processor_data' , array () ); // Get data for current processor. $data = isset( $all_data [ $this ->id ] ) ? $all_data [ $this ->id ] : array (); // Get a specific piece of data. if ( $key ) { return isset( $data [ $key ] ) ? $data [ $key ] : $default ; } // Return all the data. return $data ; } /** * Returns the edit post link for a post. * * This is based on the WordPress {@see get_edit_post_link()} function, but does not check if the user can * edit the post or if the post's post type has an edit link defined. * * When the background processor is running, the current user ID is 0. This prevents {@see current_user_can()} * from ever returning true and also causes the post's post type edit link to be set to an empty string in * {@see WP_Post_Type::set_props()}. * * This method is useful when the processor has completed and creates an admin notice that contains an edit post link. * * @since 6.0.0 * * @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`. * @param string $context Optional. How to output the '&' character. Default '&'. * @return string|null The edit post link for the given post. Null if the post type does not exist * or does not allow an editing UI. */ protected function get_edit_post_link( $id = 0, $context = 'display' ) { $post = get_post( $id ); if ( ! $post ) { return null; } $post_type_object = get_post_type_object( $post ->post_type ); if ( ! $post_type_object ) { return null; } if ( 'revision' === $post ->post_type ) { $action = '' ; } elseif ( 'display' === $context ) { $action = '&action=edit' ; } else { $action = '&action=edit' ; } $link = admin_url( sprintf( 'post.php?post=%d%s' , $post ->ID, $action ) ); /** * Filters the post edit link. * * This is identical to the `get_edit_post_link` filter hook in {@see get_edit_post_link()}. * * @since 6.0.0 * * @param string $link The edit link. * @param int $post_id Post ID. * @param string $context The link context. If set to 'display' then ampersands are encoded. */ return apply_filters( 'get_edit_post_link' , $link , $post ->ID, $context ); } /** * Log data to the processors log when processors debugging is enabled * * @since 3.15.0 * * @param mixed $data Data to log. * @return void */ protected function log( $data ) { if ( defined( 'LLMS_PROCESSORS_DEBUG' ) && LLMS_PROCESSORS_DEBUG ) { llms_log( $data , 'processors' ); } } /** * Persist data to the database related to the processor * * @since 3.15.0 * * @param array $data Data to save. * @return void */ private function save_data( $data ) { // Merge the current data with all processor data. $all_data = wp_parse_args( array ( $this ->id => $data , ), get_option( 'llms_processor_data' , array () ) ); // Save it. update_option( 'llms_processor_data' , $all_data ); } /** * Update data to the database related to the processor * * @since 3.15.0 * * @param string $key Key name. * @param mixed $value Value. * @return void */ public function set_data( $key , $value ) { // Get the array of processor data. $data = $this ->get_data(); $data [ $key ] = $value ; $this ->save_data( $data ); } /** * Delete a piece of data from the database by key * * @since 3.15.0 * * @param string $key Key name to remove. * @return void */ public function unset_data( $key ) { $data = $this ->get_data(); if ( isset( $data [ $key ] ) ) { unset( $data [ $key ] ); } $this ->save_data( $data ); } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- __construct — Constructor
- add_actions — Add actions defined in $this->actions
- disable — Disable a processor
- dispatch — Dispatch
- get_actions — Retrieve a filtered array of actions to be added by $this->add_actions
- get_data — Retrieve data for the current processor that can be used in future processes
- get_edit_post_link — Returns the edit post link for a post.
- init — Initializer
- log — Log data to the processors log when processors debugging is enabled
- save_data — Persist data to the database related to the processor
- set_data — Update data to the database related to the processor
- unset_data — Delete a piece of data from the database by key
Changelog Changelog
Version | Description |
---|---|
3.15.0 | Introduced. |