*/ private $source = array(); /** @var array>> */ private $categories = array( 'attachments' => array(), 'references' => array(), 'post_parent' => array(), 'taxonomies' => array(), 'drift' => array(), 'locations' => array(), ); /** * @param int $blog_id * @param int $post_id * @param string $post_type */ public function set_source( $blog_id, $post_id, $post_type ) { $this->source = array( 'blog_id' => (int) $blog_id, 'post_id' => (int) $post_id, 'post_type' => (string) $post_type, ); } /** * @param string $field_label * @param string $field_key * @param int $source_id * @param string $source_title */ public function add_attachment_failure( $field_label, $field_key, $source_id, $source_title = '' ) { $this->categories['attachments'][] = array( 'field_label' => (string) $field_label, 'field_key' => (string) $field_key, 'source_id' => (int) $source_id, 'source_title' => (string) $source_title, 'kind' => 'single', ); } /** * @param string $field_label * @param string $field_key * @param int $source_id * @param string $source_title */ public function add_gallery_failure( $field_label, $field_key, $source_id, $source_title = '' ) { $this->categories['attachments'][] = array( 'field_label' => (string) $field_label, 'field_key' => (string) $field_key, 'source_id' => (int) $source_id, 'source_title' => (string) $source_title, 'kind' => 'gallery_item', ); } /** * @param string $field_label * @param string $field_key * @param string $status One of: unverified, ambiguous, dropped. * @param string $source_title * @param string $source_post_type */ public function add_reference( $field_label, $field_key, $status, $source_title, $source_post_type ) { $this->categories['references'][] = array( 'field_label' => (string) $field_label, 'field_key' => (string) $field_key, 'status' => (string) $status, 'source_title' => (string) $source_title, 'source_post_type' => (string) $source_post_type, ); } /** * @param string $source_title * @param string $source_post_type */ public function add_parent_dropped( $source_title, $source_post_type ) { $this->categories['post_parent'][] = array( 'source_title' => (string) $source_title, 'source_post_type' => (string) $source_post_type, ); } /** * @param string $taxonomy_label * @param string $term_name * @param string $term_slug */ public function add_term_created( $taxonomy_label, $term_name, $term_slug ) { $this->categories['taxonomies'][] = array( 'taxonomy_label' => (string) $taxonomy_label, 'term_name' => (string) $term_name, 'term_slug' => (string) $term_slug, ); } /** * @param string $field_label * @param string $field_name * @param string $field_key */ public function add_drift( $field_label, $field_name, $field_key ) { $this->categories['drift'][] = array( 'field_label' => (string) $field_label, 'field_name' => (string) $field_name, 'field_key' => (string) $field_key, ); } /** * @param string $group_title * @param string $param page|post * @param string $status added|manual */ public function add_location( $group_title, $param, $status ) { $this->categories['locations'][] = array( 'group_title' => (string) $group_title, 'param' => (string) $param, 'status' => (string) $status, ); } /** * @return bool */ public function is_empty() { foreach ( $this->categories as $entries ) { if ( ! empty( $entries ) ) { return false; } } return true; } /** * @return array */ public function to_array() { return array( 'version' => self::VERSION, 'created_at' => gmdate( 'c' ), 'source' => $this->source, 'categories' => $this->categories, 'counts' => array( 'attachments_failed' => count( $this->categories['attachments'] ), 'references_unmatched' => count( $this->categories['references'] ), 'post_parent_dropped' => count( $this->categories['post_parent'] ), 'terms_created' => count( $this->categories['taxonomies'] ), 'fields_drifted' => count( $this->categories['drift'] ), 'locations_to_check' => count( $this->categories['locations'] ), ), ); } /** * Persist to postmeta as JSON. No-op when empty (avoids creating empty * panel entries on clean copies). * * @param int $target_post_id */ public function persist( $target_post_id ) { if ( $this->is_empty() ) { return; } $json = wp_json_encode( $this->to_array() ); if ( false === $json ) { return; } update_post_meta( (int) $target_post_id, self::META_KEY, wp_slash( $json ) ); } /** * @param int $post_id * @return array|null */ public static function read( $post_id ) { $raw = get_post_meta( (int) $post_id, self::META_KEY, true ); if ( ! $raw ) { return null; } $decoded = json_decode( (string) $raw, true ); return is_array( $decoded ) ? $decoded : null; } /** * @param int $post_id */ public static function clear( $post_id ) { delete_post_meta( (int) $post_id, self::META_KEY ); } }