Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function sync_objects( $request ) {

$responses = array();

$objects_to_sync = in_array( $objects_to_sync, array( 'attachment', 'comment', 'user', 'option', 'taxonomy_term' ), true ) ? $objects_to_sync : 'post';
$objects_to_sync = in_array( $objects_to_sync, array( 'attachment', 'comment', 'user', 'option', 'taxonomy_term', 'meta' ), true ) ? $objects_to_sync : 'post';

foreach ( $objects as $object ) {
$sync_method = "sync_{$objects_to_sync}";
Expand Down Expand Up @@ -1247,5 +1247,56 @@ private function maybe_update_term_meta( $term_id, $term_meta ) {
*/
private function is_partial_term_sync( $terms ) {
return 2 == count( $terms );
}

/**
* Sync meta for a post.
*
* @since NEXT
* @param array $object The meta args from the request.
*
* @return array
*/
public function sync_meta( $object ) {
if ( ! $this->preserve_ids ) {
return array(
'debug' => array(
'message' => 'Meta syncing only works if you are using preserved IDs.',
),
);
}
if ( ! isset( $object['_import_id'] ) ) {
return array(
'debug' => array(
'message' => 'No _import_id sent with post meta.',
),
);
}

$post_id = absint( $object['_import_id'] );
unset( $object['_import_id'] );

$new_count = 0;
$update_count = 0;

foreach ( $object as $meta_key => $meta_value ) {
$meta_value = is_array( $meta_value ) ? current( $meta_value ) : $meta_value;
$meta_value = maybe_unserialize( $meta_value );
$update_result = update_post_meta( $post_id, $meta_key, $meta_value );

if ( is_numeric( $update_result ) ) {
$new_count++;
}

if ( true === $update_result ) {
$update_count++;
}
}

return array(
'debug' => array(
'message' => sprintf( 'Created %d meta, updated %d meta, for post %d', $new_count, $update_count, $post_id ),
),
);
}
}
79 changes: 78 additions & 1 deletion includes/class-press-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ public function get_objects_to_sync( $objects_to_sync, $next_page = 1, $taxonomi
$objects = $this->get_taxonomy_term_to_sync( $next_page );
break;

case 'meta':
$objects = $this->get_meta_to_sync( $next_page );
break;

default:
$objects = $this->get_posts_to_sync( $objects_to_sync, $next_page, $taxonomies, $where_clause );
break;
Expand Down Expand Up @@ -458,6 +462,10 @@ public function count_objects_to_sync( $objects_to_sync ) {
return $this->count_taxonomy_term_to_sync();
}

if ( 'meta' === $objects_to_sync ) {
return $this->count_meta_to_sync();
}

// If it's just one post return only 1.
if ( $testing_post_id = absint( get_option( 'ps_testing_post' ) ) ) {
return 1;
Expand Down Expand Up @@ -1145,6 +1153,7 @@ public function objects_to_sync( $exclude = array() ) {
'attachment' => __( 'Media', 'press-sync' ),
'page' => __( 'Pages', 'press-sync' ),
'post' => __( 'Posts', 'press-sync' ),
'meta' => __( 'Post Meta', 'press-sync' ),
'user' => __( 'Users', 'press-sync' ),
'option' => __( 'Options', 'press-sync' ),
);
Expand Down Expand Up @@ -1332,7 +1341,7 @@ public function get_remote_url( $url = '', $endpoint = 'status', $args = array()
* @return string $wp_object
*/
public function get_sync_function_name( $objects_to_sync = '' ) {
$wp_object = in_array( $objects_to_sync, array( 'attachment', 'comment', 'user', 'option', 'taxonomy_term' ), true ) ? $objects_to_sync : 'post';
$wp_object = in_array( $objects_to_sync, array( 'attachment', 'comment', 'user', 'option', 'taxonomy_term', 'meta' ), true ) ? $objects_to_sync : 'post';
return "prepare_{$wp_object}_args_to_sync";
}

Expand Down Expand Up @@ -1604,4 +1613,72 @@ protected function get_posts_delta( $objects_to_sync ) {

return $GLOBALS['wpdb']->prepare( ' AND post_modified >= %s ', $this->delta_date );
}

/**
* Gets a count of meta rows to sync.
*
* @since NEXT
* @return int
*/
public function count_meta_to_sync() {
$query = <<<SQL
SELECT DISTINCT
post_id
FROM
{$GLOBALS['wpdb']->postmeta} pm
JOIN
{$GLOBALS['wpdb']->posts} p ON ( p.ID = pm.post_id )
WHERE
p.post_status NOT IN ( 'auto-draft', 'trash' )
SQL;

$results = $GLOBALS['wpdb']->get_results( $query );
return count( $results );
}

/**
* Gets the meta data to sync.
*
* @since NEXT
* @param int $next_page The page to get meta for.
* @return array
*/
public function get_meta_to_sync( $next_page ) {
$offset = ( $next_page * $this->settings['ps_page_size'] ) - $this->settings['ps_page_size'];

$query = <<<SQL
SELECT DISTINCT
post_id
FROM
{$GLOBALS['wpdb']->postmeta} pm
JOIN
{$GLOBALS['wpdb']->posts} p ON ( p.ID = pm.post_id )
WHERE
p.post_status NOT IN ( 'auto-draft', 'trash' )
LIMIT
{$offset}, {$this->settings['ps_page_size']}
SQL;

$results = $GLOBALS['wpdb']->get_results( $query, ARRAY_A );

$meta = array();
foreach ( $results as $row ) {
$meta_row = get_post_meta( $row['post_id'] );
$meta_row['_import_id'] = $row['post_id'];
$meta[] = $meta_row;
}

return $meta;
}

/**
* Prepare meta to sync. Nothing to do here for now, just stubbed for compatibility.
*
* @since NEXT
* @param array $meta The incoming meta array.
* @return array
*/
public function prepare_meta_args_to_sync( $meta ) {
return $meta;
}
}