public function step_start() { $o = new \stdClass(); $o->inputs = new \stdClass(); $o->form = $this->broadcast()->form2(); $o->r = ''; $o->inputs->user = $o->form->select('user')->description('The user or users for which to check blog access.')->label('User(s) to check')->multiple(); $all_users = get_users(); $users = []; foreach ($all_users as $user) { $o->inputs->user->option($user->data->user_login, $user->data->ID); $users[$user->data->ID] = $user; } $o->inputs->user->autosize(); $button = $o->form->primary_button('view_blog_access')->value('Display blog access for the selected user(s)'); if ($o->form->is_posting()) { $o->form->post()->use_post_value(); if ($button->pressed()) { $r = ''; $selected_users = $o->inputs->user->get_post_value(); if (is_array($selected_users)) { foreach ($selected_users as $selected_user) { $user = $users[$selected_user]; $filter = new get_user_writable_blogs($selected_user); $blogs = $filter->execute()->blogs; if (count($blogs) < 1) { $r .= $this->broadcast()->p_('%s does not have access to any blogs.', $user->data->user_login); } else { $blogs_ul = []; foreach ($blogs as $blog) { $blogs_ul[] = sprintf('<a href="%s/wp-admin">%s</a>', $blog->siteurl, $blog->get_name()); } $r .= $this->broadcast()->p_('%s has access to the following blogs: %s%s%s', $user->data->user_login, '<ul>', $this->broadcast()->implode_html($blogs_ul), '</ul>'); } } } if ($r != '') { $this->broadcast()->message($r); } } } $o->r .= $this->broadcast()->p("Use this tool to help diagnose why the Broadcast meta box isn't apprearing for some users. To be able to broadcast to a blog the user must be added as an author or above."); $o->r .= $o->form->open_tag(); $o->r .= $o->form->display_form_table(); $o->r .= $o->form->close_tag(); return $o->r; }
/** @brief Execute an action on a post. @since 2014-11-02 16:35:27 **/ public function threewp_broadcast_post_action($action) { $blog_id = get_current_blog_id(); $post_id = $action->post_id; // In order for this method to be usable for both single and bulk post actions, do some footwork here so that we can help the actions decide whether to work on a specific child or not. $on_child_blog_id = 0; if (isset($action->child_blog_id) && $action->child_blog_id > 0) { $on_child_blog_id = $action->child_blog_id; } switch ($action->action) { // Delete all children case 'delete': $broadcast_data = $this->get_post_broadcast_data($blog_id, $post_id); foreach ($broadcast_data->get_linked_children() as $child_blog_id => $child_post_id) { if ($on_child_blog_id > 0 && $child_blog_id != $on_child_blog_id) { continue; } switch_to_blog($child_blog_id); wp_delete_post($child_post_id, true); $broadcast_data->remove_linked_child($child_blog_id); restore_current_blog(); } $broadcast_data = $this->set_post_broadcast_data($blog_id, $post_id, $broadcast_data); break; case 'find_unlinked': $post = get_post($post_id); $broadcast_data = $this->get_post_broadcast_data($blog_id, $post_id); // Get a list of blogs that this user can link to. $filter = new actions\get_user_writable_blogs($this->user_id()); $blogs = $filter->execute()->blogs; foreach ($blogs as $blog) { if ($blog->id == $blog_id) { continue; } if ($broadcast_data->has_linked_child_on_this_blog($blog->id)) { continue; } $blog->switch_to(); $args = array('cache_results' => false, 'name' => $post->post_name, 'numberposts' => 2, 'post_type' => $post->post_type); $posts = get_posts($args); // An exact match was found. if (count($posts) == 1) { $unlinked = reset($posts); $child_broadcast_data = $this->get_post_broadcast_data($blog->id, $unlinked->ID); if ($child_broadcast_data->get_linked_parent() === false) { if (!$child_broadcast_data->has_linked_children()) { $broadcast_data->add_linked_child($blog->id, $unlinked->ID); // Add link info for the new child. $child_broadcast_data->set_linked_parent($blog_id, $post_id); $this->set_post_broadcast_data($blog->id, $unlinked->ID, $child_broadcast_data); } } } $blog->switch_from(); } $broadcast_data = $this->set_post_broadcast_data($blog_id, $post_id, $broadcast_data); break; // Restore children // Restore children case 'restore': $broadcast_data = $this->get_post_broadcast_data($blog_id, $post_id); foreach ($broadcast_data->get_linked_children() as $child_blog_id => $child_post_id) { if ($on_child_blog_id > 0 && $child_blog_id != $on_child_blog_id) { continue; } switch_to_blog($child_blog_id); wp_publish_post($child_post_id); restore_current_blog(); } break; // Trash children // Trash children case 'trash': $broadcast_data = $this->get_post_broadcast_data($blog_id, $post_id); foreach ($broadcast_data->get_linked_children() as $child_blog_id => $child_post_id) { if ($on_child_blog_id > 0 && $child_blog_id != $on_child_blog_id) { continue; } switch_to_blog($child_blog_id); wp_trash_post($child_post_id); restore_current_blog(); } break; // Unlink children // Unlink children case 'unlink': // TODO: Make this more flexible when we add parent / siblings. $broadcast_data = $this->get_post_broadcast_data($blog_id, $post_id); $parent = $broadcast_data->get_linked_parent(); if ($parent !== false) { // Remove the link to this child from the parent. $parent = (object) $parent; $parent_broadcast_data = $this->get_post_broadcast_data($parent->blog_id, $parent->post_id); $parent_broadcast_data->remove_linked_child($blog_id); $this->set_post_broadcast_data($parent->blog_id, $parent->post_id, $parent_broadcast_data); // And now we remove the link to the parent. $broadcast_data->remove_linked_parent(); } if ($broadcast_data->has_linked_children()) { $linked_children = $broadcast_data->get_linked_children(); foreach ($linked_children as $child_blog_id => $child_post_id) { if ($on_child_blog_id > 0 && $child_blog_id != $on_child_blog_id) { continue; } $broadcast_data->remove_linked_child($child_blog_id); $this->delete_post_broadcast_data($child_blog_id, $child_post_id); } } $this->set_post_broadcast_data($blog_id, $post_id, $broadcast_data); break; } }