| 1 |
<?php |
| 2 |
/** |
| 3 |
* Job Instances. |
| 4 |
* |
| 5 |
* This is a mixture of traditional UD Utility "log" methods with job handling. |
| 6 |
* Jobs may utilize RaaS support. |
| 7 |
* |
| 8 |
* - Jobs and Batches both use same post type (_ud_job). However, batches will have a parent while jobs will not. |
| 9 |
* - Jobs have "job-ready" post_status once ready for processing. |
| 10 |
* - Batches have "job-batch-ready" post_status once ready for processing. |
| 11 |
* |
| 12 |
* @author team@UD |
| 13 |
* @version 0.2.4 |
| 14 |
* @namespace UsabilityDynamics |
| 15 |
* @module Utility |
| 16 |
* @author potanin@UD |
| 17 |
*/ |
| 18 |
namespace UsabilityDynamics { |
| 19 |
|
| 20 |
if( !class_exists( 'UsabilityDynamics\Job' ) ) { |
| 21 |
|
| 22 |
/** |
| 23 |
* Job Class |
| 24 |
* |
| 25 |
* Extends UsabilityDynamics\Utility to inherit version, text_domain, etc. |
| 26 |
* |
| 27 |
* @class Job |
| 28 |
* @author potanin@UD |
| 29 |
*/ |
| 30 |
class Job extends Utility { |
| 31 |
|
| 32 |
/** |
| 33 |
* Job Instance Defaults. |
| 34 |
* |
| 35 |
* self::$defaults |
| 36 |
* |
| 37 |
* @property $defaults |
| 38 |
* @public |
| 39 |
* @type {Array} |
| 40 |
*/ |
| 41 |
public static $defaults = array( |
| 42 |
'type' => '_default', |
| 43 |
'post_title' => null, |
| 44 |
'post_password' => null, |
| 45 |
'post_status' => 'job-ready', |
| 46 |
'post_type' => '_ud_job' |
| 47 |
); |
| 48 |
|
| 49 |
/** |
| 50 |
* Meta Keys. |
| 51 |
* |
| 52 |
* self::$_meta |
| 53 |
* |
| 54 |
* @property $_meta |
| 55 |
* @private |
| 56 |
* @type {Array} |
| 57 |
*/ |
| 58 |
public static $_meta = array( |
| 59 |
'type' => array( 'is_single' => true ), |
| 60 |
'status' => array( 'is_single' => true ), |
| 61 |
'callback::worker' => array( 'is_single' => true ), |
| 62 |
'callback::complete' => array( 'is_single' => true ) |
| 63 |
); |
| 64 |
|
| 65 |
/** |
| 66 |
* Job Instance ID. |
| 67 |
* |
| 68 |
* @property $id |
| 69 |
* @private |
| 70 |
* @type {Integer} |
| 71 |
*/ |
| 72 |
public $id = null; |
| 73 |
|
| 74 |
/** |
| 75 |
* Job Status. |
| 76 |
* |
| 77 |
* @property $status |
| 78 |
* @private |
| 79 |
* @type {Integer} |
| 80 |
*/ |
| 81 |
private $status = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* Job Type. |
| 85 |
* |
| 86 |
* @property $type |
| 87 |
* @private |
| 88 |
* @type {Integer} |
| 89 |
*/ |
| 90 |
private $type = null; |
| 91 |
|
| 92 |
/** |
| 93 |
* Job Batches. |
| 94 |
* |
| 95 |
* @property $batches |
| 96 |
* @private |
| 97 |
* @type {Array} |
| 98 |
*/ |
| 99 |
private $batches = array(); |
| 100 |
|
| 101 |
/** |
| 102 |
* Job Instance Settings. |
| 103 |
* |
| 104 |
* @property $_settings |
| 105 |
* @private |
| 106 |
* @type {Object} |
| 107 |
*/ |
| 108 |
private $_settings = stdClass; |
| 109 |
|
| 110 |
/** |
| 111 |
* Constructor for the Job class. |
| 112 |
* |
| 113 |
* |
| 114 |
* @todo Finish job loading. |
| 115 |
* |
| 116 |
* @method __construct |
| 117 |
* @for Job |
| 118 |
* @constructor |
| 119 |
* @param array|\UsabilityDynamics\object $settings array |
| 120 |
* @return \UsabilityDynamics\Job |
| 121 |
* @version 0.0.1 |
| 122 |
* @since 0.0.1 |
| 123 |
*/ |
| 124 |
public function __construct( $settings = array() ) { |
| 125 |
|
| 126 |
// Register Job Post Type, if needed. |
| 127 |
$this->_register_post_type(); |
| 128 |
|
| 129 |
// Save Settings to Instance, applying defaults, returns deeply-converted object. |
| 130 |
$this->_settings = self::defaults( $settings, self::$defaults ); |
| 131 |
|
| 132 |
// Load job if ID is set. |
| 133 |
if( $this->_settings->id ) { |
| 134 |
return Job::query( array( "id" => $this->_settings->id ) ); |
| 135 |
} |
| 136 |
|
| 137 |
// Generate Title. |
| 138 |
$this->_settings->post_title = sprintf( __( 'Job %s', self::$text_domain ), $this->_settings->type ); |
| 139 |
|
| 140 |
// Generate public job hash. |
| 141 |
$this->_settings->post_password = uniqid( $this->_settings->type . '-' ); |
| 142 |
|
| 143 |
// Encode payload. |
| 144 |
$this->_settings->post_content = json_encode( (array) $this->_settings->post_content ); |
| 145 |
|
| 146 |
// Insert Job, get job ID. |
| 147 |
$this->id = wp_insert_post( $this->_settings ); |
| 148 |
|
| 149 |
// Handle creation error. |
| 150 |
if( $this->id instanceof WP_Error ) { |
| 151 |
return $this->id; |
| 152 |
} |
| 153 |
|
| 154 |
// Commit Meta Key. |
| 155 |
foreach( (array) self::$_meta as $_key => $_options ) { |
| 156 |
|
| 157 |
$_value = $this->_settings->{$_key}; |
| 158 |
|
| 159 |
if( $_value ) { |
| 160 |
update_post_meta( $this->id, 'job::' . $_key, $_value ); |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
// Worker. |
| 167 |
return $this; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Load Job. |
| 173 |
* |
| 174 |
* Parses post object and adds declared meta. |
| 175 |
* |
| 176 |
* @param null $job |
| 177 |
* |
| 178 |
* @internal param null $id |
| 179 |
* |
| 180 |
* @return object |
| 181 |
*/ |
| 182 |
public function load_meta( $job = null ) { |
| 183 |
|
| 184 |
if( !$job ) { |
| 185 |
$_id = $this->id; |
| 186 |
} |
| 187 |
|
| 188 |
if( is_object( $job ) && $job->id ) { |
| 189 |
$_id = $job->id; |
| 190 |
} |
| 191 |
|
| 192 |
if( is_integer( $job ) ) { |
| 193 |
$_id = $job; |
| 194 |
} |
| 195 |
|
| 196 |
if( !$_id ) { |
| 197 |
return new WP_Error( __( 'Could not load job with unknown ID.', self::$text_domain ) ); |
| 198 |
} |
| 199 |
|
| 200 |
$_meta = array(); |
| 201 |
|
| 202 |
foreach( (array) self::$_meta as $_key => $_options ) { |
| 203 |
$_value = get_post_meta( $_id, 'job::' . $_key, $_options[ 'is_single' ] ? true : false ); |
| 204 |
|
| 205 |
if( $_value ) { |
| 206 |
$_meta[ $_key ] = $_value; |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
return $_meta; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Delete Job Instance. |
| 217 |
* |
| 218 |
*/ |
| 219 |
public function delete() { |
| 220 |
global $wpdb; |
| 221 |
|
| 222 |
// Kill Babies. |
| 223 |
foreach( (array) $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d;", $this->id ) ) as $_child ) { |
| 224 |
$_post = wp_delete_post( $_child, true ); |
| 225 |
} |
| 226 |
|
| 227 |
// Kill Parent. |
| 228 |
$_post = wp_delete_post( $this->id, true ); |
| 229 |
|
| 230 |
// Return object. |
| 231 |
return $_post; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Push Batch Cargo. |
| 237 |
* |
| 238 |
* @param array $data |
| 239 |
* @param array $args |
| 240 |
* |
| 241 |
* @return int|void|\WP_Error |
| 242 |
*/ |
| 243 |
public function push( $data = array(), $args = array() ) { |
| 244 |
|
| 245 |
// Convert to JSON String. |
| 246 |
$_data = json_encode( (array) $data ); |
| 247 |
|
| 248 |
// Generate public job hash. |
| 249 |
$_batchid = wp_insert_post( self::defaults( $args, array( |
| 250 |
'post_parent' => $this->id, |
| 251 |
'post_status' => 'job-ready', |
| 252 |
'post_title' => sprintf( __( 'Job Batch %s', self::$text_domain ), $this->_settings->type ), |
| 253 |
'post_password' => uniqid( $this->_settings->type . '-' ), |
| 254 |
'post_type' => $this->_settings->post_type, |
| 255 |
'post_content' => $_data |
| 256 |
)) ); |
| 257 |
|
| 258 |
// Add to batch list if not an error. |
| 259 |
if( !is_wp_error( $_batchid ) ) { |
| 260 |
$this->batches[ $_batchid ] = json_decode( $_data ); |
| 261 |
} |
| 262 |
|
| 263 |
return $_batchid; |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Process Mutiple Jobs. |
| 269 |
* |
| 270 |
* |
| 271 |
* @param string $type Job type. |
| 272 |
* @param array $args |
| 273 |
* |
| 274 |
* @return null |
| 275 |
*/ |
| 276 |
public static function process_jobs( $type = null, $args = array() ) { |
| 277 |
|
| 278 |
$_results = array(); |
| 279 |
|
| 280 |
foreach( self::query( $type ) as $_count => $_job ) { |
| 281 |
|
| 282 |
$_batches = self::query( $_job->type, array( |
| 283 |
"post_parent" => $_job->ID, |
| 284 |
"post_status" => 'job-ready', |
| 285 |
|
| 286 |
)); |
| 287 |
|
| 288 |
//die( '<pre>' . print_r( $_batches, true ) . '</pre>' ); |
| 289 |
// Get Batches |
| 290 |
|
| 291 |
// $_results[ $_count ] = apply_filters( 'job::' . $_job->type, $_job, $_results[ $_count ] = $_results[ $_count ] || array(), $_count ); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
return $_results; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Query Job Instance(s) |
| 301 |
* |
| 302 |
* @todo Should use instances of Job instead of WP_Post. |
| 303 |
* |
| 304 |
* @param null $type |
| 305 |
* @param array $args |
| 306 |
* |
| 307 |
* @return array |
| 308 |
*/ |
| 309 |
public static function query( $type = null, $args = array() ) { |
| 310 |
|
| 311 |
// Build get_posts query. |
| 312 |
$args = self::defaults( $args, array( |
| 313 |
'posts_per_page' => 100, |
| 314 |
'offset' => 0, |
| 315 |
'post_parent' => 0, |
| 316 |
'orderby' => 'post_date', |
| 317 |
'order' => 'DESC', |
| 318 |
'post_type' => '_ud_job', |
| 319 |
'post_status' => 'job-ready' |
| 320 |
)); |
| 321 |
|
| 322 |
// Query by ID. |
| 323 |
if( is_numeric( $type ) ) { |
| 324 |
$args->ID = $type; |
| 325 |
} |
| 326 |
|
| 327 |
// Query by type. |
| 328 |
if( is_string( $type ) ) { |
| 329 |
$args->meta_key = 'job::type'; |
| 330 |
$args->meta_value = $type; |
| 331 |
} |
| 332 |
|
| 333 |
// Get all top-level jobs. |
| 334 |
$_jobs = get_posts( (array) $args ); |
| 335 |
|
| 336 |
// Extend Job objects with meta. |
| 337 |
foreach( (array) $_jobs as $_count => $_job ) { |
| 338 |
|
| 339 |
// Remove unused keys. |
| 340 |
unset( $_jobs[ $_count ]->ping_status ); |
| 341 |
unset( $_jobs[ $_count ]->to_ping ); |
| 342 |
unset( $_jobs[ $_count ]->pinged ); |
| 343 |
unset( $_jobs[ $_count ]->pinged ); |
| 344 |
unset( $_jobs[ $_count ]->guid ); |
| 345 |
unset( $_jobs[ $_count ]->menu_order ); |
| 346 |
unset( $_jobs[ $_count ]->post_mime_type ); |
| 347 |
unset( $_jobs[ $_count ]->filter ); |
| 348 |
|
| 349 |
// Decode cargo. |
| 350 |
$_jobs[ $_count ]->post_content = json_decode( $_jobs[ $_count ]->post_content ? $_jobs[ $_count ]->post_content : array() ); |
| 351 |
$_jobs[ $_count ]->post_excerpt = json_decode( $_jobs[ $_count ]->post_excerpt ? $_jobs[ $_count ]->post_excerpt : array() ); |
| 352 |
|
| 353 |
// Load Meta into Object. |
| 354 |
foreach( self::load_meta( $_job->ID ) as $_key => $_value ) { |
| 355 |
$_jobs[ $_count ]->{$_key} = $_value; |
| 356 |
} |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
// Return formatted jobs result. |
| 361 |
return (object) $_jobs; |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Register Job Post Type |
| 367 |
* |
| 368 |
* @private |
| 369 |
* @uses get_post_type_object |
| 370 |
* @uses register_post_type |
| 371 |
*/ |
| 372 |
private function _register_post_type() { |
| 373 |
|
| 374 |
// Return if already registered. |
| 375 |
if( get_post_type_object( $this->_settings->post_type ) ) { |
| 376 |
return get_post_type_object( $this->_settings->post_type ); |
| 377 |
} |
| 378 |
|
| 379 |
// Register; |
| 380 |
register_post_type( $this->_settings->post_type, array( |
| 381 |
'labels' => array( |
| 382 |
'name' => __( 'Jobs', self::$text_domain ), |
| 383 |
'singular_name' => __( 'Jobs', self::$text_domain ), |
| 384 |
'new_item' => __( 'New Job', self::$text_domain ), |
| 385 |
'view_item' => __( 'View Job', self::$text_domain ), |
| 386 |
'all_items' => __( 'Jobs', self::$text_domain ) |
| 387 |
), |
| 388 |
'description' => __( 'UsabilityDynamics Jobs.', self::$text_domain ), |
| 389 |
'public' => false, |
| 390 |
'hierarchical' => true, |
| 391 |
'exclude_from_search' => true, |
| 392 |
'publicly_queryable' => false, |
| 393 |
'show_ui' => false, |
| 394 |
'show_in_menu' => false, |
| 395 |
'show_in_nav_menus' => false, |
| 396 |
'show_in_admin_bar' => false, |
| 397 |
'map_meta_cap' => false, |
| 398 |
'supports' => array( 'raas' ), |
| 399 |
'has_archive' => false, |
| 400 |
'rewrite' => false, |
| 401 |
'query_var' => false, |
| 402 |
'can_export' => true, |
| 403 |
'delete_with_user' => false, |
| 404 |
'_edit_link' => 'veneer/?job=%d', |
| 405 |
)); |
| 406 |
|
| 407 |
// Return post type object. |
| 408 |
return get_post_type_object( $this->_settings->post_type ); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
} |
| 417 |
|
| 418 |
|