-
Notifications
You must be signed in to change notification settings - Fork 14
extend filter parameters and fix issues #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jon007
wants to merge
15
commits into
magicstickuk:master
Choose a base branch
from
Jon007:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b464d13
additional filter parameters and defensive code
Jon007 0a8d79d
allow non-array meta value
Jon007 b6f9b19
ensure current blog is correctly restored
Jon007 7e2affb
add action parameters, avoid duplicating posts already duplicated
Jon007 efd5a26
incorporating pull request fix error in mpd_get_image_alt_tags #54
Jon007 8ebb359
fix bad checkin
Jon007 185f449
implements #72
Jon007 6934585
fixes to handle simple copy
Jon007 eb0aad4
defensive code
Jon007 2c59341
remove deprecated parameter in get_terms call
Jon007 edf3bbb
suppress excess save hooks from other plugins
Jon007 935498c
handle existing terms
Jon007 67f0d9a
defensive code for missing media attachment
Jon007 428631f
correction to post media defensive code fix
Jon007 3635c1c
defensive coding for current_screen
Jon007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| <?php | ||
|
|
||
| // Load the posts-to-posts addon only if its network activated | ||
| if (!function_exists('is_plugin_active_for_network')) { | ||
| require_once(ABSPATH . '/wp-admin/includes/plugin.php'); | ||
| } | ||
|
|
||
| if (is_plugin_active_for_network('posts-to-posts/posts-to-posts.php')) { | ||
|
|
||
| $mpd_p2p_source_data = null; | ||
|
|
||
| function mpd_p2p_p2p_enrich(&$p2p_link) { | ||
| $tmp = (array) $p2p_link; | ||
| $tmp['meta'] = get_metadata('p2p', $p2p_link->p2p_id); | ||
| if($tmp['meta']){ | ||
| foreach ($tmp['meta'] as $key => &$val) { | ||
| // everything is singular | ||
| $val = $val[0]; | ||
| } | ||
| } | ||
|
|
||
| unset($val); | ||
| $p2p_link = (object) $tmp; | ||
| } | ||
|
|
||
| function mpd_p2p_prepare_source_data($source_post_id, $destination_blog_id) { | ||
| global $wpdb; | ||
| global $mpd_p2p_source_data; | ||
|
|
||
| $p2p_links = $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| "SELECT * FROM $wpdb->p2p WHERE p2p_from = %d OR p2p_to = %d", | ||
| $source_post_id, $source_post_id)); | ||
| if($p2p_links){ | ||
| foreach ($p2p_links as &$p2p_link) { | ||
| mpd_p2p_p2p_enrich($p2p_link); | ||
| } | ||
| } | ||
|
|
||
| unset($p2p_link); | ||
|
|
||
| $mpd_p2p_source_data = array( | ||
| 'source_post_id' => $source_post_id, | ||
| 'source_p2p_links' => $p2p_links, | ||
| 'destination_blog_id' => $destination_blog_id, | ||
| ); | ||
| } | ||
|
|
||
| function mpd_p2p_by_dest($mpd_links) { | ||
| $res = array(); | ||
| if($mpd_links){ | ||
| foreach ($mpd_links as $mpd_link) { | ||
| if (!array_key_exists($mpd_link->destination_id, $res)) { | ||
| $res[$mpd_link->destination_id] = array(); | ||
| } | ||
| array_push($res[$mpd_link->destination_id], $mpd_link->destination_post_id); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return $res; | ||
| } | ||
|
|
||
| add_action("mpd_during_core_in_source", "mpd_p2p_pre_copy_persist", 10, 5); | ||
| add_action("mpd_persist_during_core_in_source", "mpd_p2p_pre_copy_persist", 10, 5); | ||
| function mpd_p2p_pre_copy_persist($mpd_post, $attached_image, $meta_values, $source_post_id, $destination_blog_id) { | ||
| if (!function_exists('p2p_register_connection_type')) { | ||
| return; | ||
| } | ||
|
|
||
| mpd_p2p_prepare_source_data($source_post_id, $destination_blog_id); | ||
| } | ||
|
|
||
| add_action("mpd_end_of_core_before_return", "mpd_p2p_after_create_update", 10, 3); | ||
| add_action("mpd_persist_end_of_core_before_return", "mpd_p2p_after_create_update", 10, 3); | ||
| function mpd_p2p_after_create_update($dest_post_id, $mpd_post, $source_blog_id) { | ||
| if (!function_exists('p2p_register_connection_type')) { | ||
| return; | ||
| } | ||
|
|
||
| global $mpd_p2p_source_data; | ||
|
|
||
| $source_post_id = $mpd_p2p_source_data['source_post_id']; | ||
| $destination_blog_id = $mpd_p2p_source_data['destination_blog_id']; | ||
| if($mpd_p2p_source_data['source_p2p_links']){ | ||
| foreach ($mpd_p2p_source_data['source_p2p_links'] as $source_p2p_link) { | ||
| $source_p2p_other = ($source_post_id == $source_p2p_link->p2p_from ? | ||
| $source_p2p_link->p2p_to : $source_p2p_link->p2p_from); | ||
| $mpd_links_of_p2p_link = mpd_get_persists_for_post($source_blog_id, $source_p2p_other); | ||
|
|
||
| if($mpd_links_of_p2p_link){ | ||
| foreach ($mpd_links_of_p2p_link as $mpd_link_of_p2p_link) { | ||
| if ($mpd_link_of_p2p_link->destination_id != $destination_blog_id) { | ||
| continue; | ||
| } | ||
|
|
||
| _mpd_p2p_copy_link_raw( | ||
| true /* link */, | ||
| $source_p2p_link /* meta and dir */, | ||
| $source_post_id /* for dir */, | ||
| $dest_post_id, /* the two new ids */ | ||
| $mpd_link_of_p2p_link->destination_post_id); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| add_action('p2p_created_connection', 'mpd_p2p_created_connection', 10, 1); | ||
| function mpd_p2p_created_connection($p2p_id) { | ||
| _mpd_p2p_copy_link_by_id(true, $p2p_id); | ||
| } | ||
|
|
||
| add_action('p2p_delete_connections', 'mpd_p2p_delete_connections', 10, 1); | ||
| function mpd_p2p_delete_connections($p2p_ids) { | ||
| if($p2p_ids){ | ||
| foreach ($p2p_ids as $p2p_id) { | ||
| _mpd_p2p_copy_link_by_id(false, $p2p_id); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function _mpd_p2p_copy_link_by_id($is_create, $p2p_id) { | ||
| $p2p = p2p_get_connection($p2p_id); | ||
| mpd_p2p_p2p_enrich($p2p); | ||
| $from_post_id = $p2p->p2p_from; | ||
| $to_post_id = $p2p->p2p_to; | ||
|
|
||
| foreach (mpd_get_persists_for_post(null, $from_post_id) as $from_mpd_link) { | ||
| foreach (mpd_get_persists_for_post(null, $to_post_id) as $to_mpd_link) { | ||
| if ($from_mpd_link->destination_id != $to_mpd_link->destination_id) { | ||
| continue; | ||
| } | ||
|
|
||
| switch_to_blog($from_mpd_link->destination_id); | ||
|
|
||
| _mpd_p2p_copy_link_raw( | ||
| $is_create, | ||
| $p2p, | ||
| $from_post_id, | ||
| $from_mpd_link->destination_post_id, | ||
| $to_mpd_link->destination_post_id); | ||
|
|
||
| restore_current_blog(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function _mpd_p2p_copy_link_raw($is_create, $p2p, $old_post_id, $new_post_id, $other_post_id) { | ||
| global $wpdb; | ||
|
|
||
| $from = $p2p->p2p_from == $old_post_id ? $new_post_id : $other_post_id; | ||
| $to = $p2p->p2p_from == $old_post_id ? $other_post_id : $new_post_id; | ||
|
|
||
| // NOTE: This whole thing does not handle multiple p2p connections for | ||
| // a pair of posts | ||
| $existing_p2p_link = $wpdb->get_row( | ||
| $wpdb->prepare( | ||
| "SELECT * FROM $wpdb->p2p WHERE p2p_from = %d AND p2p_to = %d AND p2p_type = %s", | ||
| $from, $to, $p2p->p2p_type)); | ||
|
|
||
|
|
||
| if (!$is_create) { | ||
| if (!is_null($existing_p2p_link)) { | ||
| p2p_delete_connection($existing_p2p_link->p2p_id); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (is_null($existing_p2p_link)) { | ||
| p2p_create_connection($p2p->p2p_type, array( | ||
| 'from' => $from, | ||
| 'to' => $to, | ||
| 'meta' => $p2p->meta)); | ||
| } else { | ||
| // exists, most likely the meta is good | ||
| $p2p_meta = get_metadata('p2p', $existing_p2p_link->p2p_id); | ||
| $expected_p2p_meta = $p2p->meta; | ||
|
|
||
| $are_same = true; | ||
| if (count($p2p_meta) == count($expected_p2p_meta)) { | ||
| foreach ($expected_p2p_meta as $key => $val) { | ||
| if (!(array_key_exists($key, $p2p_meta) && $p2p_meta[$key] == $val)) { | ||
| $are_same = false; | ||
| break; | ||
| } | ||
| } | ||
| } else { | ||
| $are_same = false; | ||
| } | ||
|
|
||
| if (!$are_same) { | ||
| // well, meta differs, wipe all and re-add | ||
| $wpdb->delete($wpdb->p2pmeta, array('p2p_id' => $existing_p2p_link->p2p_id), array('%d')); | ||
| foreach ($expected_p2p_meta as $key => $val) { | ||
| p2p_add_meta($existing_p2p_link->p2p_id, $key, $val); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix is not sufficient. The
$master_idvar can be undefined within the if statement below due to this change.If statement should also check if the var exists or a better solution in my opinion is to set the var with a null value and check if there is a value in the if statement.