function _rs_mt_set_categories($args) { global $wp_xmlrpc_server; $wp_xmlrpc_server->escape($args); $post_ID = (int) $args[0]; $username = $args[1]; $password = $args[2]; $categories = $args[3]; if (!($user = $wp_xmlrpc_server->login($username, $password))) { return $wp_xmlrpc_server->error; } if (empty($categories)) { $categories = array(); } $catids = array(); foreach ($categories as $cat) { $catids[] = $cat['categoryId']; } $catids = apply_filters('pre_object_terms_rs', $catids, 'category'); do_action('xmlrpc_call', 'mt.setPostCategories'); if (!get_post($post_ID)) { return new IXR_Error(404, __('Invalid post ID.')); } if (!current_user_can('edit_post', $post_ID)) { return new IXR_Error(401, __('Sorry, you cannot edit this post.')); } wp_set_post_categories($post_ID, $catids); return true; }
/** * This function set post in default category. * * @param $post_ID * * @return mixed */ public function save_default_post_categories($post_ID) { if (wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) { return $post_ID; } $categories = wp_get_post_categories($post_ID); if (count($categories) === 1 && $categories[0] === (int) get_option('default_category')) { $cats = (array) $this->get_default_post_category_by_lang(get_post_meta($post_ID, 'translation_lang', true)); wp_set_post_categories($post_ID, $cats); } }
function testSetupMulticomicPartialPaths() { $cp = $this->getMock('ComicPress', array('_is_dir')); wp_set_post_categories(1, array('2', '3')); add_category('2', (object) array('slug' => 'test-one')); add_category('3', (object) array('slug' => 'test-two')); $cp->expects($this->at(0))->method('_is_dir')->with('/subthemes/test-one')->will($this->returnValue(true)); $cp->expects($this->at(1))->method('_is_dir')->with('/subthemes/test-two')->will($this->returnValue(false)); $cp->setup_multicomic_partial_paths(1); $this->assertEquals(array('/subthemes/test-one'), $cp->partial_paths); }
/** * @ticket 21167 */ public function test_dotted_formats() { global $wp_rewrite; // Create a sample post $cat_id = $this->factory->term->create(array('name' => 'permalink-test', 'taxonomy' => 'category')); $user_id = $this->factory->user->create(array('role' => 'editor', 'user_login' => 'permalink_user')); $post_id = $this->factory->post->create(array('post_title' => 'lorem-ipsum', 'post_date' => '2012-08-02 14:15:05', 'post_author' => $user_id, 'category' => $cat_id)); wp_set_post_categories($post_id, array($cat_id)); // Sample permalinks $tests = array('/%postname%.%post_id%/ ' => array('regex' => '([^/]+)\\.([0-9]+)(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&p=$2&page=$3'), '/%year%.%monthnum%.%postname%/' => array('regex' => '([0-9]{4})\\.([0-9]{1,2})\\.([^/]+)(/[0-9]+)?/?$', 'url' => 'index.php?year=$1&monthnum=$2&name=$3&page=$4'), '/%post_id%.%postname%/' => array('regex' => '([0-9]+)\\.([^/]+)(/[0-9]+)?/?$', 'url' => 'index.php?p=$1&name=$2&page=$3'), '/%postname%.%year%/' => array('regex' => '([^/]+)\\.([0-9]{4})(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&year=$2&page=$3'), '/$%postname%$/' => array('regex' => '\\$([^/]+)\\$(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&page=$2'), '%year%.+?%monthnum%.+?%day%.+?%hour%.+?%minute%.+?%second%.+?%post_id%.+?%postname%.+?%category%.+?%author%.+?' => array('regex' => '([0-9]{4})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]+)\\.\\+\\?([^/]+)\\.\\+\\?%category%\\.\\+\\?([^/]+)\\.\\+\\?(/[0-9]+)?/?$', 'url' => 'index.php?year=$1&monthnum=$2&day=$3&hour=$4&minute=$5&second=$6&p=$7&name=$8&%category%$9&author_name=$10&page=$11')); // Test permalinks foreach ($tests as $permalink_format => $expected) { update_option('permalink_structure', $permalink_format); // Get the rewrite rules $rules = $wp_rewrite->generate_rewrite_rules(get_option('permalink_structure'), EP_PERMALINK, false, false, false, false); // Filter out only the post rewrite rule foreach ($rules as $regex => $url) { if (false === strpos($url, 'attachment=$') && false === strpos($url, 'tb=') && false === strpos($url, 'cpage=$')) { break; } } // Test that expected === actual $this->assertEquals($regex, $expected['regex'], "Problem with permalink format: {$permalink_format}"); $this->assertEquals($url, $expected['url'], "Problem with permalink format: {$permalink_format}"); } }
function test_category__and_var() { $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) ); $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) ); $post_id = $this->factory->post->create(); wp_set_post_categories( $post_id, $term_id ); $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) ); $this->assertEmpty( $this->q->get( 'category__and' ) ); $this->assertCount( 0, $this->q->get( 'category__and' ) ); $this->assertNotEmpty( $this->q->get( 'category__in' ) ); $this->assertCount( 1, $this->q->get( 'category__in' ) ); $this->assertNotEmpty( $posts ); $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) ); $posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) ); $this->assertNotEmpty( $this->q->get( 'category__and' ) ); $this->assertCount( 2, $this->q->get( 'category__and' ) ); $this->assertEmpty( $this->q->get( 'category__in' ) ); $this->assertCount( 0, $this->q->get( 'category__in' ) ); $this->assertEmpty( $posts2 ); }
/** * @dataProvider providerTestGetValidPostCategory */ function testGetValidPostCategory($post_categories, $expected_result) { $css = $this->getMock('ComicPressStoryline', array('valid')); $css->expects($this->any())->method('valid')->will($this->returnValue(true)); wp_set_post_categories(1, $post_categories); $this->assertEquals($expected_result, $css->get_valid_post_category(1)); }
/** * Update the post categories based on all post category fields * * @since 1.17 * * @param array $form Gravity Forms form array * @param int $entry_id Numeric ID of the entry that was updated * * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. false if there are no post category fields or connected post. */ public function set_post_categories($form = array(), $entry_id = 0) { $entry = GFAPI::get_entry($entry_id); $post_id = rgar($entry, 'post_id'); if (empty($post_id)) { return false; } $return = false; $post_category_fields = GFAPI::get_fields_by_type($form, 'post_category'); if ($post_category_fields) { $updated_categories = array(); foreach ($post_category_fields as $field) { // Get the value of the field, including $_POSTed value $field_cats = RGFormsModel::get_field_value($field); $field_cats = is_array($field_cats) ? array_values($field_cats) : (array) $field_cats; $field_cats = gv_map_deep($field_cats, 'intval'); $updated_categories = array_merge($updated_categories, array_values($field_cats)); } // Remove `0` values from intval() $updated_categories = array_filter($updated_categories); /** * @filter `gravityview/edit_entry/post_categories/append` Should post categories be added to or replaced? * @since 1.17 * @param bool $append If `true`, don't delete existing categories, just add on. If `false`, replace the categories with the submitted categories. Default: `false` */ $append = apply_filters('gravityview/edit_entry/post_categories/append', false); $return = wp_set_post_categories($post_id, $updated_categories, $append); } return $return; }
function ajax_new() { check_ajax_referer('fee-new', 'nonce'); require_once ABSPATH . '/wp-admin/includes/post.php'; $post = get_default_post_to_edit(isset($_POST['post_type']) ? $_POST['post_type'] : 'post', true); wp_set_post_categories($post->ID, array(get_option('default_category'))); wp_send_json_success(get_permalink($post->ID)); }
/** * @dataProvider providerTestFindParents */ function testFindParents($post_categories, $expected_result) { add_category(1, (object) array('slug' => 'root', 'parent' => 0)); add_category(2, (object) array('slug' => 'comic', 'parent' => 1)); add_category(3, (object) array('slug' => 'part-1', 'parent' => 2)); add_category(4, (object) array('slug' => 'blog', 'parent' => 1)); wp_set_post_categories(1, $post_categories); $this->p->post = (object) array('ID' => 1); $this->assertEquals($expected_result, $this->p->find_parents()); }
function ahny_update_post_terms($post_id) { $post = get_post($post_id); if ('post' === $post->post_type) { $categories = wp_get_post_categories($post_id); $newcat = get_term_by('name', 'Living Modern', 'category'); array_push($categories, $newcat->term_id); wp_set_post_categories($post_id, $categories); } }
public static function wpSetUpBeforeClass($factory) { self::$posts[0] = $factory->post->create(array('post_name' => 'post0')); self::$posts[1] = $factory->post->create(array('post_name' => 'post1')); self::$cats[0] = $factory->category->create(array('slug' => 'cat0')); self::$cats[1] = $factory->category->create(array('slug' => 'cat1')); self::$cats[2] = $factory->category->create(array('slug' => 'cat2')); wp_set_post_categories(self::$posts[0], self::$cats[2]); wp_set_post_categories(self::$posts[0], self::$cats[0]); wp_set_post_categories(self::$posts[1], self::$cats[1]); }
/** * @ticket 22526 */ function test_category_name_change() { $term = self::factory()->category->create_and_get(array('name' => 'Foo')); $post_id = self::factory()->post->create(); wp_set_post_categories($post_id, $term->term_id); $post = get_post($post_id); $cats1 = get_the_category($post->ID); $this->assertEquals($term->name, reset($cats1)->name); wp_update_term($term->term_id, 'category', array('name' => 'Bar')); $cats2 = get_the_category($post->ID); $this->assertNotEquals($term->name, reset($cats2)->name); }
/** * @ticket 11003 */ public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects() { $post_id1 = self::factory()->post->create(); $post_id2 = self::factory()->post->create(); $cat_id = self::factory()->category->create(); $cat_id2 = self::factory()->category->create(); wp_set_post_categories($post_id1, array($cat_id, $cat_id2)); wp_set_post_categories($post_id2, $cat_id); $terms = wp_get_object_terms(array($post_id1, $post_id2), 'category'); $this->assertCount(2, $terms); $this->assertEquals(array($cat_id, $cat_id2), wp_list_pluck($terms, 'term_id')); $terms2 = wp_get_object_terms(array($post_id1, $post_id2), 'category', array('fields' => 'all_with_object_id')); $this->assertCount(3, $terms2); $this->assertEquals(array($cat_id, $cat_id, $cat_id2), wp_list_pluck($terms2, 'term_id')); }
function testBuildPostTable() { $this->markTestIncomplete(); $posts = array((object) array('ID' => 1, 'post_date' => '2009-01-01', 'post_title' => 'Post 1', 'guid' => 'post-1'), (object) array('ID' => 2, 'post_date' => '2009-01-02', 'post_title' => 'Post 2', 'guid' => 'post-2'), (object) array('ID' => 3, 'post_date' => '2009-01-03', 'post_title' => 'Post 3', 'guid' => 'post-3')); foreach ($posts as $post) { wp_insert_post($post); } $categories = array(1 => array(1), 2 => array(1), 3 => array(2)); foreach ($categories as $id => $cats) { wp_set_post_categories($id, $cats); } $output = '<div class="related_posts"><span>Title</span><table class="month-table"><tr><td class="archive-date" align="right">Jan 3, 2009</td><td class="archive-title"><a title="Post 3" href="post-3">Post 3</a></td></tr></table></div>'; $this->rp->related_categories = array(2); $this->assertEquals($output, $this->rp->build_post_table('Title', $posts)); }
/** * Create categories for the given post. * * @since 2.0.0 * * @param array $categories List of categories to create. * @param int $post_id Optional. The post ID. Default empty. * @return List of categories to create for the given post. */ function wp_create_categories($categories, $post_id = '') { $cat_ids = array(); foreach ($categories as $category) { if ($id = category_exists($category)) { $cat_ids[] = $id; } elseif ($id = wp_create_category($category)) { $cat_ids[] = $id; } } if ($post_id) { wp_set_post_categories($post_id, $cat_ids); } return $cat_ids; }
public static function attachCategories($sectionObject, $postUrl, $postId) { if (self::postIsInRootCategory($sectionObject->fullUrlPath)) { //Do single category $catId = self::createRootCategory($sectionObject->name, $sectionObject->slug); wp_set_post_categories($postId, $catId); } else { // get both categories $parentSlug = self::getParentSlug($sectionObject->fullUrlPath); $parentName = self::getCatfishCategoryNameBySlug($parentSlug, $postUrl); $parentId = self::createRootCategory($parentName, $parentSlug); $catId = self::createChildCategory($sectionObject->name, $sectionObject->slug, $parentId); wp_set_post_categories($postId, $catId); } }
/** * @dataProvider providerTestFindFile */ function testFindFile($files_to_setup, $search_path, $post_categories, $expected_path_result) { global $post; mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true); mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true); foreach ($files_to_setup as $path) { file_put_contents(vfsStream::url($path), "test"); } _set_template_directory(vfsStream::url('root/parent')); _set_stylesheet_directory(vfsStream::url('root/child')); $post = (object) array('ID' => 1); wp_set_post_categories(1, array(2)); add_category(1, (object) array('slug' => 'comic', 'parent' => 0)); add_category(2, (object) array('slug' => 'chapter-1', 'parent' => 1)); $this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', $search_path, $post_categories)); }
function testSkipEmptyCategories() { global $wp_query; $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_post', 'get_next_post', 'get_first_post', 'get_last_post')); $storyline = new ComicPressStoryline(); $storyline->_structure = array('1' => array('next' => 2), '2' => array('previous' => 1, 'next' => 3), '3' => array('previous' => 2)); wp_insert_post(array('ID' => 1)); $post = get_post(1); wp_set_post_categories(1, array(1)); $dbi->expects($this->any())->method('get_first_post')->will($this->returnCallback(array(&$this, 'callbackTestSkipEmptyCategories'))); $this->nav->_dbi = $dbi; $this->nav->_storyline = $storyline; $wp_query = (object) array('is_single' => true, 'in_the_loop' => true); $nav = $this->nav->get_post_nav($post); $this->assertEquals(10, $nav['storyline-chapter-next']->ID); }
function wpc_update_post_terms($post_id) { if ($parent = wp_is_post_revision($post_id)) { $post_id = $parent; } $post = get_post($post_id); if ($post->post_type != 'cpt-00-01') { return; } // add a category $categories = wp_get_post_categories($post_id); // make sure these category names already exists. They are not created automatically. $newcat1 = get_term_by('slug', 'cat-00-01', 'category'); array_push($categories, $newcat1->term_id); wp_set_post_categories($post_id, $categories); }
function quiz_import() { if (wp_verify_nonce($_POST['nonce'], "molie_admin_quiz")) { $post = get_post($_POST['course_post']); $categories = $this->create_categories($post); $quiz = $this->get_quiz($post); print_r($quiz); if (get_post_meta($post->ID, "canvasQuiz_" . $quiz->id, true) == "") { $quiz_post = wp_insert_post(array("post_type" => 'linkedcanvasquiz', "post_status" => 'publish', "post_title" => $quiz->title, "post_content" => $quiz->description, "post_author" => get_current_user_id())); wp_set_post_categories($quiz_post, $categories); update_post_meta($quiz_post, "CanvasCourse", get_post_meta($post->ID, "courseID", true), true); update_post_meta($quiz_post, "canvasQuizURL", $quiz->html_url, true); update_post_meta($post->ID, "canvasQuiz_" . $quiz->id, $quiz_post, true); } else { $quiz_post = get_post_meta($post->ID, "canvasQuiz_" . $quiz->id, true); } $questions = $this->get_quiz_questions($post); foreach ($questions as $question) { if (get_post_meta($quiz_post, "canvasQuizQuestion_" . $question->id, true) == "") { $question_post = wp_insert_post(array("post_type" => 'linkedcanvasqa', "post_status" => 'publish', "post_title" => $quiz->title . " " . $question->question_name, "post_content" => $question->question_text, "post_author" => get_current_user_id()), true); wp_set_post_categories($question_post, $categories); update_post_meta($question_post, "CanvasCourse", get_post_meta($post->ID, "courseID", true), true); update_post_meta($question_post, "canvasQuiz", $question->quiz_id, true); update_post_meta($question_post, "canvasQuizWPPost", $quiz_post, true); update_post_meta($question_post, "canvasQuestion_id", $question->id, true); update_post_meta($question_post, "canvasQuestion_position", $question->position, true); update_post_meta($quiz_post, "canvasQuizQuestion_" . $question->id, $question_post, true); if ($question->question_type == "multiple_choice_question") { $counter = 1; foreach ($question->answers as $answer) { print_r($answer); update_post_meta($question_post, "qa_id_" . $counter, $answer->id, true); update_post_meta($question_post, "qa_weight_" . $counter, $answer->weight, true); update_post_meta($question_post, "qa_answer_" . $counter, $answer->text, true); update_post_meta($question_post, "qa_feedback_" . $counter, $answer->comments, true); $counter++; } } } } echo __("Quiz linked"); } else { print_r($_POST); echo "Nonce failed"; } wp_die(); }
function testGetPostNav() { $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic')); $storyline = new ComicPressStoryline(); $storyline->root_category = 1; $storyline->_structure = array('1' => array('next' => 2), '2' => array('previous' => 1, 'next' => 3), '3' => array('previous' => 2)); wp_insert_post(array('ID' => 1)); $post = get_post(1); wp_set_post_categories(1, array(2)); $dbi->expects($this->at(0))->method('get_previous_comic')->with(null, $post); $dbi->expects($this->at(1))->method('get_next_comic')->with(null, $post); $dbi->expects($this->at(2))->method('get_first_comic')->with(1); $dbi->expects($this->at(3))->method('get_last_comic')->with(1); $dbi->expects($this->at(4))->method('get_previous_comic')->with(2, $post); $dbi->expects($this->at(5))->method('get_next_comic')->with(2, $post); $dbi->expects($this->at(6))->method('get_first_comic')->with(1); $dbi->expects($this->at(7))->method('get_first_comic')->with(3); $this->nav->_dbi = $dbi; $this->nav->_storyline = $storyline; $this->nav->get_post_nav($post); }
function discussion_import() { if (wp_verify_nonce($_POST['nonce'], "molie_admin_discussion")) { $post = get_post($_POST['course_post']); $categories = $this->create_categories($post); $discussion = $this->get_discussion($post); if (get_post_meta($post->ID, "canvasDiscussion_" . $discussion->id, true) == "") { $discussion_post = wp_insert_post(array("post_type" => 'linkedcanvasdis', "post_status" => 'publish', "post_title" => $discussion->title, "post_content" => $discussion->message, "post_author" => get_current_user_id())); wp_set_post_categories($discussion_post, $categories); update_post_meta($discussion_post, "CanvasCourse", get_post_meta($post->ID, "courseID", true), true); update_post_meta($discussion_post, "canvasDiscussionURL", $discussion->html_url, true); update_post_meta($post->ID, "canvasDiscussion_" . $discussion->id, $discussion_post, true); echo __("Discussion linked"); } else { echo __("Discussion already linked"); } } else { print_r($_POST); echo "Nonce failed"; } wp_die(); }
/** * @ticket 28099 * @group taxonomy */ function test_empty_category__in() { $cat_id = $this->factory->category->create(); $post_id = $this->factory->post->create(); wp_set_post_categories($post_id, $cat_id); $q1 = get_posts(array('category__in' => array($cat_id))); $this->assertNotEmpty($q1); $q2 = get_posts(array('category__in' => array())); $this->assertNotEmpty($q2); $tag = wp_insert_term('woo', 'post_tag'); $tag_id = $tag['term_id']; $slug = get_tag($tag_id)->slug; wp_set_post_tags($post_id, $slug); $q3 = get_posts(array('tag__in' => array($tag_id))); $this->assertNotEmpty($q3); $q4 = get_posts(array('tag__in' => array())); $this->assertNotEmpty($q4); $q5 = get_posts(array('tag_slug__in' => array($slug))); $this->assertNotEmpty($q5); $q6 = get_posts(array('tag_slug__in' => array())); $this->assertNotEmpty($q6); }
function assignment_import() { if (wp_verify_nonce($_POST['nonce'], "molie_admin_assignment")) { $post = get_post($_POST['course_post']); $categories = $this->create_categories($post); $quiz = $this->get_assignment($post); if (get_post_meta($post->ID, "canvasQuiz_" . $quiz->id, true) == "") { $quiz_post = wp_insert_post(array("post_type" => 'linkedcanvasamt', "post_status" => 'publish', "post_title" => $quiz->name, "post_content" => $quiz->description, "post_author" => get_current_user_id())); wp_set_post_categories($quiz_post, $categories); update_post_meta($quiz_post, "CanvasCourse", get_post_meta($post->ID, "courseID", true), true); update_post_meta($quiz_post, "canvasQuizURL", $quiz->html_url, true); update_post_meta($post->ID, "canvasQuiz_" . $quiz->id, $quiz_post, true); echo __("Assignment linked"); } else { echo __("Assignment already linked"); } } else { print_r($_POST); echo "Nonce failed"; } wp_die(); }
function roster_import() { if (wp_verify_nonce($_POST['nonce'], "molie_admin_roster")) { $post = get_post($_POST['course_post']); $categories = $this->create_categories($post); $student = $this->get_student($post, $_POST['item']); if (get_post_meta($post->ID, "canvasStudent_" . $_POST['item'], true) == "") { $student_post = wp_insert_post(array("post_type" => 'linkedcanvasuser', "post_status" => 'publish', "post_title" => $student->content->short_name, "post_content" => "<img src='" . $student->content->avatar_url . "'><p>" . $student->content->bio . "</p>", "post_author" => get_current_user_id())); wp_set_post_categories($student_post, $categories); update_post_meta($student_post, "CanvasUserID", $student->content->id, true); update_post_meta($student_post, "CanvasCourse", get_post_meta($post->ID, "courseID", true), true); update_post_meta($post->ID, "canvasStudent_" . $student->content->id, $student_post, true); echo __("Student Added"); } else { echo __("Student already added"); } } else { print_r($_POST); echo "Nonce failed"; } wp_die(); }
public function ct_update_categories($categories, $object, $field_name) { //wp_mail("*****@*****.**","debug info",serialize(array("object"=>$object,"categories"=>$categories))); if (empty($categories) || !$categories) { return; } if (is_string($categories)) { $categories = explode(",", $categories); } elseif (is_object($categories)) { $categories = (array) $categories; } $input_categories = array(); foreach ($categories as $category) { $cat_check = $this->category_id($category); if ($cat_check === false) { return; } else { $input_categories[] = $cat_check; } } return wp_set_post_categories($object->ID, $input_categories); }
/** * Insert an attachment. * * If you set the 'ID' in the $object parameter, it will mean that you are * updating and attempt to update the attachment. You can also set the * attachment name or title by setting the key 'post_name' or 'post_title'. * * You can set the dates for the attachment manually by setting the 'post_date' * and 'post_date_gmt' keys' values. * * By default, the comments will use the default settings for whether the * comments are allowed. You can close them manually or keep them open by * setting the value for the 'comment_status' key. * * The $object parameter can have the following: * 'post_status' - Default is 'draft'. Can not be overridden, set the same as parent post. * 'post_type' - Default is 'post', will be set to attachment. Can not override. * 'post_author' - Default is current user ID. The ID of the user, who added the attachment. * 'ping_status' - Default is the value in default ping status option. Whether the attachment * can accept pings. * 'post_parent' - Default is 0. Can use $parent parameter or set this for the post it belongs * to, if any. * 'menu_order' - Default is 0. The order it is displayed. * 'to_ping' - Whether to ping. * 'pinged' - Default is empty string. * 'post_password' - Default is empty string. The password to access the attachment. * 'guid' - Global Unique ID for referencing the attachment. * 'post_content_filtered' - Attachment post content filtered. * 'post_excerpt' - Attachment excerpt. * * @since 2.0.0 * @uses $wpdb * @uses $user_ID * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update. * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update. * * @param string|array $object Arguments to override defaults. * @param string $file Optional filename. * @param int $parent Parent post ID. * @return int Attachment ID. */ function wp_insert_attachment($object, $file = false, $parent = 0) { global $wpdb, $user_ID; $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0); $object = wp_parse_args($object, $defaults); if (!empty($parent)) { $object['post_parent'] = $parent; } $object = sanitize_post($object, 'db'); // export array as variables extract($object, EXTR_SKIP); if (empty($post_author)) { $post_author = $user_ID; } $post_type = 'attachment'; $post_status = 'inherit'; // Make sure we set a valid category. if (!isset($post_category) || 0 == count($post_category) || !is_array($post_category)) { // 'post' requires at least one category. if ('post' == $post_type) { $post_category = array(get_option('default_category')); } else { $post_category = array(); } } // Are we updating or creating? if (!empty($ID)) { $update = true; $post_ID = (int) $ID; } else { $update = false; $post_ID = 0; } // Create a valid post name. if (empty($post_name)) { $post_name = sanitize_title($post_title); } else { $post_name = sanitize_title($post_name); } // expected_slashed ($post_name) $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); if (empty($post_date)) { $post_date = current_time('mysql'); } if (empty($post_date_gmt)) { $post_date_gmt = current_time('mysql', 1); } if (empty($post_modified)) { $post_modified = $post_date; } if (empty($post_modified_gmt)) { $post_modified_gmt = $post_date_gmt; } if (empty($comment_status)) { if ($update) { $comment_status = 'closed'; } else { $comment_status = get_option('default_comment_status'); } } if (empty($ping_status)) { $ping_status = get_option('default_ping_status'); } if (isset($to_ping)) { $to_ping = preg_replace('|\\s+|', "\n", $to_ping); } else { $to_ping = ''; } if (isset($post_parent)) { $post_parent = (int) $post_parent; } else { $post_parent = 0; } if (isset($menu_order)) { $menu_order = (int) $menu_order; } else { $menu_order = 0; } if (!isset($post_password)) { $post_password = ''; } if (!isset($pinged)) { $pinged = ''; } // expected_slashed (everything!) $data = compact(array('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid')); $data = stripslashes_deep($data); if ($update) { $wpdb->update($wpdb->posts, $data, array('ID' => $post_ID)); } else { // If there is a suggested ID, use it if not already present if (!empty($import_id)) { $import_id = (int) $import_id; if (!$wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $import_id))) { $data['ID'] = $import_id; } } $wpdb->insert($wpdb->posts, $data); $post_ID = (int) $wpdb->insert_id; } if (empty($post_name)) { $post_name = sanitize_title($post_title, $post_ID); $wpdb->update($wpdb->posts, compact("post_name"), array('ID' => $post_ID)); } wp_set_post_categories($post_ID, $post_category); if ($file) { update_attached_file($post_ID, $file); } clean_post_cache($post_ID); if (isset($post_parent) && $post_parent < 0) { add_post_meta($post_ID, '_wp_attachment_temp_parent', $post_parent, true); } if ($update) { do_action('edit_attachment', $post_ID); } else { do_action('add_attachment', $post_ID); } return $post_ID; }
function mt_setPostCategories($args) { $this->escape($args); $post_ID = (int) $args[0]; $user_login = $args[1]; $user_pass = $args[2]; $categories = $args[3]; if (!$this->login_pass_ok($user_login, $user_pass)) { return $this->error; } set_current_user(0, $user_login); if (!current_user_can('edit_post', $post_ID)) { return new IXR_Error(401, __('Sorry, you can not edit this post.')); } foreach ($categories as $cat) { $catids[] = $cat['categoryId']; } wp_set_post_categories($post_ID, $catids); return true; }
/** * Sets the categories that the post id belongs to. * * @since 1.0.1 * @deprecated 2.1 * @deprecated Use wp_set_post_categories() * @see wp_set_post_categories() * * @param int $blogid Not used * @param int $post_ID * @param array $post_categories * @return unknown */ function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) { _deprecated_function(__FUNCTION__, '2.1', 'wp_set_post_categories()'); return wp_set_post_categories($post_ID, $post_categories); }
function test_adding_tutorial_text_to_single() { //creating admin user and set ut $user = new WP_User($this->factory->user->create(array('role' => 'administrator'))); wp_set_current_user($user->ID); //create category $cat_id = $this->factory->category->create(array('slug' => rand_str(), 'name' => rand_str(), 'description' => rand_str())); //create 3 posts, the $post_ids[1] is the middle one, suppose to have next and prev in the pretext $post_ids[] = $this->factory->post->create(array('post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'POST1', 'post_date' => date('Y-m-d H:i:s', time() - 300))); $post_ids[] = $this->factory->post->create(array('post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'POST2', 'post_date' => date('Y-m-d H:i:s', time() - 200))); $post_ids[] = $this->factory->post->create(array('post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'POST3', 'post_date' => date('Y-m-d H:i:s', time() - 100))); //adding them to the category foreach ($post_ids as $post_id) { $res = wp_set_post_categories($post_id, $cat_id); } //making the category a tutorial category $this->auxClass->set_up_post_data(); $this->plugin_admin->wp_tutorial_maker_option_update($cat_id); //go to tutorial post $this->go_to(get_permalink($post_ids[1])); $post_content_inside_tutorial = get_echo('the_content'); //making sure that the tutorial pretext that I want is there $this->assertRegExp('/<div class=\'wptm_prev\'><span>Some prev text<\\/span><a href="http:\\/\\/example\\.org\\/\\?p=' . $post_ids[0] . '" rel="prev">POST1<\\/a> <\\/div>/', $post_content_inside_tutorial); $this->assertRegExp('/<div class=\'wptm_next\'><span>Some next text<\\/span><a href="http:\\/\\/example\\.org\\/\\?p=' . $post_ids[2] . '" rel="next">POST3<\\/a> <\\/div>/', $post_content_inside_tutorial); $this->assertRegExp('/<div id=\'wptm_before_category_link_text\'>Some Category List Header<\\/div>/', $post_content_inside_tutorial); $this->assertRegExp('/<div id=\'wptm_before_category_link_text\'>Some Category List Header<\\/div>/', $post_content_inside_tutorial); $this->assertRegExp('/<div class=\'wptm_link_to_category\'><a href=\'http:\\/\\/example\\.org\\/\\?cat=' . $cat_id . '\'>Some Name to Category Link<\\/div>/', $post_content_inside_tutorial); //making sure that the tutorial pretext is not being added by mistake to some other non Tutorial post $non_tutorial_post_id = $this->factory->post->create(array('post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'POST2', 'post_date' => date('Y-m-d H:i:s', time() - 200))); $this->go_to(get_permalink($non_tutorial_post_id)); $post_content_inside_tutorial = get_echo('the_content'); $this->assertNotRegExp('/wptm/', $post_content_inside_tutorial); }