/** * Control POST data for mass edit tags * * @param string $type */ function checkFormMassEdit() { if (!current_user_can('simple_tags')) { return false; } // Get GET data if (isset($_GET['post_type'])) { $type = stripslashes($_GET['post_type']); } if (isset($_POST['update_mass'])) { // origination and intention if (!wp_verify_nonce($_POST['secure_mass'], 'st_mass_terms')) { $this->message = __('Security problem. Try again. If this problem persist, contact <a href="mailto:amaury@wordpress-fr.net">plugin author</a>.', 'simpletags'); $this->status = 'error'; return false; } if (isset($_POST['tags'])) { $counter = 0; foreach ((array) $_POST['tags'] as $object_id => $tag_list) { // Trim data $tag_list = trim(stripslashes($tag_list)); // String to array $tags = explode(',', $tag_list); // Remove empty and trim tag $tags = array_filter($tags, '_delete_empty_element'); // Add new tag (no append ! replace !) wp_set_object_terms($object_id, $tags, $this->taxonomy); $counter++; // Clean cache if ($this->post_type == 'page') { clean_page_cache($object_id); } else { clean_post_cache($object_id); } } $this->message = sprintf(__('%1$s %2$s(s) terms updated with success !', 'simpletags'), (int) $counter, strtolower($this->post_type_name)); return true; } } return false; }
/** * Updates the comment count for the post. * * @since 2.5.0 * @uses $wpdb * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old * @uses do_action() Calls 'edit_posts' hook on $post_id and $post * * @param int $post_id Post ID * @return bool False on '0' $post_id or if post with ID does not exist. True on success. */ function wp_update_comment_count_now($post_id) { global $wpdb; $post_id = (int) $post_id; if (!$post_id) { return false; } if (!($post = get_post($post_id))) { return false; } $old = (int) $post->comment_count; $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET comment_count = %d WHERE ID = %d", $new, $post_id)); if ('page' == $post->post_type) { clean_page_cache($post_id); } else { clean_post_cache($post_id); } do_action('wp_update_comment_count', $post_id, $new, $old); do_action('edit_post', $post_id, $post); return true; }
function _save_post_hook($post_id, $post) { if ($post->post_type == 'page') { if (!empty($post->page_template)) { if (!update_post_meta($post_id, '_wp_page_template', $post->page_template)) { add_post_meta($post_id, '_wp_page_template', $post->page_template, true); } } clean_page_cache($post_id); global $wp_rewrite; $wp_rewrite->flush_rules(); } else { clean_post_cache($post_id); } }
/** * Hook used to prevent page/post cache from staying dirty when a post is saved. * * @since 2.3.0 * @access private * * @param int $post_id The ID in the database table for the $post * @param object $post Object type containing the post information */ function _save_post_hook($post_id, $post) { if ($post->post_type == 'page') { clean_page_cache($post_id); } else { clean_post_cache($post_id); } }
function page_rows( $pages ) { if ( ! $pages ) $pages = get_pages( 'sort_column=menu_order' ); if ( ! $pages ) return false; // splice pages into two parts: those without parent and those with parent $top_level_pages = array(); $children_pages = array(); foreach ( $pages as $page ) { // catch and repair bad pages if ( $page->post_parent == $page->ID ) { $page->post_parent = 0; $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) ); clean_page_cache( $page->ID ); } if ( 0 == $page->post_parent ) $top_level_pages[] = $page; else $children_pages[] = $page; } foreach ( $top_level_pages as $page ) display_page_row($page, $children_pages, 0); /* * display the remaining children_pages which are orphans * having orphan requires parental attention */ if ( count($children_pages) > 0 ) { $empty_array = array(); foreach ( $children_pages as $orphan_page ) { clean_page_cache( $orphan_page->ID); display_page_row( $orphan_page, $empty_array, 0 ); } } }
/** * Updates the comment count for the post. * * @since 2.5.0 * @uses $nxtdb * @uses do_action() Calls 'nxt_update_comment_count' hook on $post_id, $new, and $old * @uses do_action() Calls 'edit_posts' hook on $post_id and $post * * @param int $post_id Post ID * @return bool False on '0' $post_id or if post with ID does not exist. True on success. */ function nxt_update_comment_count_now($post_id) { global $nxtdb; $post_id = (int) $post_id; if (!$post_id) { return false; } if (!($post = get_post($post_id))) { return false; } $old = (int) $post->comment_count; $new = (int) $nxtdb->get_var($nxtdb->prepare("SELECT COUNT(*) FROM {$nxtdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id)); $nxtdb->update($nxtdb->posts, array('comment_count' => $new), array('ID' => $post_id)); if ('page' == $post->post_type) { clean_page_cache($post_id); } else { clean_post_cache($post_id); } do_action('nxt_update_comment_count', $post_id, $new, $old); do_action('edit_post', $post_id, $post); return true; }
/** * Automatically tag a post/page from the database terms for the taxonomy specified * * @param object $object * @param string $taxonomy * @param array $options * @param boolean $counter * @return boolean * @author Amaury Balmer */ function autoTermsPost($object, $taxonomy = 'post_tag', $options = array(), $counter = false) { global $wpdb; // Option exists ? if ($options == false || empty($options)) { return false; } if (get_the_terms($object->ID, $taxonomy) != false && $options['at_empty'] == 1) { return false; // Skip post with terms, if term only empty post option is checked } $terms_to_add = array(); // Merge title + content + excerpt to compare with terms $content = $object->post_content . ' ' . $object->post_title; if (isset($object->post_excerpt)) { $content .= ' ' . $object->post_excerpt; } $content = trim(strip_tags($content)); if (empty($content)) { return false; } // Auto term with specific auto terms list if (isset($options['auto_list'])) { $terms = (array) maybe_unserialize($options['auto_list']); foreach ($terms as $term) { if (!is_string($term) && empty($term)) { continue; } $term = trim($term); // Whole word ? if ((int) $options['only_full_word'] == 1) { if (preg_match("/\\b" . $term . "\\b/i", $content)) { $terms_to_add[] = $term; } } elseif (stristr($content, $term)) { $terms_to_add[] = $term; } } unset($terms, $term); } // Auto terms with all terms if (isset($options['at_all']) && $options['at_all'] == 1) { // Get all terms $terms = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT name\n\t\t\t\tFROM {$wpdb->terms} AS t\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id\n\t\t\t\tWHERE tt.taxonomy = %s", $taxonomy)); $terms = array_unique($terms); foreach ($terms as $term) { $term = stripslashes($term); if (!is_string($term) && empty($term)) { continue; } // Whole word ? if ((int) $options['only_full_word'] == 1) { $term = ' ' . $term . ' '; // Add space before and after ! } if (stristr($content, $term)) { $terms_to_add[] = $term; } } // Clean memory $terms = array(); unset($terms, $term); } // Append terms if terms to add if (!empty($terms_to_add)) { // Remove empty and duplicate elements $terms_to_add = array_filter($terms_to_add, '_delete_empty_element'); $terms_to_add = array_unique($terms_to_add); if ($counter == true) { // Increment counter $counter = (int) get_option('tmp_auto_terms_st') + count($terms_to_add); update_option('tmp_auto_terms_st', $counter); } // Add terms to posts wp_set_object_terms($object->ID, $terms_to_add, $taxonomy, true); // Clean cache if (isset($object->post_type) && ($object->post_type = 'page')) { clean_page_cache($object->ID); } else { clean_post_cache($object->ID); } return true; } return false; }
function wp_insert_post($postarr = array()) { global $wpdb, $wp_rewrite, $allowedtags, $user_ID; if ( is_object($postarr) ) $postarr = get_object_vars($postarr); // export array as variables extract($postarr); // Are we updating or creating? $update = false; if ( !empty($ID) ) { $update = true; $post = & get_post($ID); $previous_status = $post->post_status; } // Get the basics. $post_content = apply_filters('content_save_pre', $post_content); $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); $post_title = apply_filters('title_save_pre', $post_title); $post_category = apply_filters('category_save_pre', $post_category); $post_status = apply_filters('status_save_pre', $post_status); $post_name = apply_filters('name_save_pre', $post_name); $comment_status = apply_filters('comment_status_pre', $comment_status); $ping_status = apply_filters('ping_status_pre', $ping_status); // Make sure we set a valid category if (0 == count($post_category) || !is_array($post_category)) { $post_category = array(get_option('default_category')); } $post_cat = $post_category[0]; if ( empty($post_author) ) $post_author = $user_ID; if ( empty($post_status) ) $post_status = 'draft'; // Get the post ID. if ( $update ) $post_ID = $ID; // Create a valid post name. Drafts are allowed to have an empty // post name. if ( empty($post_name) ) { if ( 'draft' != $post_status ) $post_name = sanitize_title($post_title); } else { $post_name = sanitize_title($post_name); } // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now if (empty($post_date)) { if ( 'draft' != $post_status ) $post_date = current_time('mysql'); } if (empty($post_date_gmt)) { if ( 'draft' != $post_status ) $post_date_gmt = get_gmt_from_date($post_date); } if ( empty($comment_status) ) { if ( $update ) $comment_status = 'closed'; else $comment_status = get_settings('default_comment_status'); } if ( empty($ping_status) ) $ping_status = get_settings('default_ping_status'); if ( empty($post_pingback) ) $post_pingback = get_option('default_pingback_flag'); if ( isset($to_ping) ) $to_ping = preg_replace('|\s+|', "\n", $to_ping); else $to_ping = ''; if ( ! isset($pinged) ) $pinged = ''; 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 ( ('publish' == $post_status) || ('static' == $post_status) ) { $post_name_check = ('publish' == $post_status) ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1") : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); if ($post_name_check) { $suffix = 2; while ($post_name_check) { $alt_post_name = $post_name . "-$suffix"; $post_name_check = ('publish' == $post_status) ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1") : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1"); $suffix++; } $post_name = $alt_post_name; } } if ($update) { $wpdb->query( "UPDATE IGNORE $wpdb->posts SET post_author = '$post_author', post_date = '$post_date', post_date_gmt = '$post_date_gmt', post_content = '$post_content', post_content_filtered = '$post_content_filtered', post_title = '$post_title', post_excerpt = '$post_excerpt', post_status = '$post_status', comment_status = '$comment_status', ping_status = '$ping_status', post_password = '******', post_name = '$post_name', to_ping = '$to_ping', pinged = '$pinged', post_modified = '".current_time('mysql')."', post_modified_gmt = '".current_time('mysql',1)."', post_parent = '$post_parent', menu_order = '$menu_order' WHERE ID = $post_ID"); } else { $wpdb->query( "INSERT IGNORE INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type) VALUES ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')"); $post_ID = $wpdb->insert_id; } if ( empty($post_name) && 'draft' != $post_status ) { $post_name = sanitize_title($post_title, $post_ID); $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); } wp_set_post_cats('', $post_ID, $post_category); if ( 'static' == $post_status ) { clean_page_cache($post_ID); wp_cache_delete($post_ID, 'pages'); } else { clean_post_cache($post_ID); } // Set GUID if ( ! $update ) $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); if ( $update) { if ($previous_status != 'publish' && $post_status == 'publish') { // Reset GUID if transitioning to publish. $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'"); do_action('private_to_published', $post_ID); } do_action('edit_post', $post_ID); } if ($post_status == 'publish') { do_action('publish_post', $post_ID); if ( !defined('WP_IMPORTING') ) { if ( $post_pingback ) $result = $wpdb->query(" INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_ID','_pingme','1') "); $result = $wpdb->query(" INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_ID','_encloseme','1') "); spawn_pinger(); } } else if ($post_status == 'static') { wp_cache_delete('all_page_ids', 'pages'); $wp_rewrite->flush_rules(); if ( !empty($page_template) ) if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template)) add_post_meta($post_ID, '_wp_page_template', $page_template, true); } do_action('save_post', $post_ID); do_action('wp_insert_post', $post_ID); return $post_ID; }
/** * Save tags input for old field * * @param string $post_id * @param object $object * @return boolean * @author Amaury Balmer */ function saveAdvancedTagsInput($post_id = 0, $object = null) { if (isset($_POST['adv-tags-input'])) { // Trim/format data $tags = preg_replace("/[\n\r]/", ', ', stripslashes($_POST['adv-tags-input'])); $tags = trim($tags); // String to array $tags = explode(',', $tags); // Remove empty and trim tag $tags = array_filter($tags, '_delete_empty_element'); // Add new tag (no append ! replace !) wp_set_object_terms($post_id, $tags, 'post_tag'); // Clean cache if ('page' == $object->post_type) { clean_page_cache($post_id); } else { clean_post_cache($post_id); } return true; } return false; }
function wp_insert_post($postarr = array()) { global $wpdb, $wp_rewrite, $allowedtags, $user_ID; if (is_object($postarr)) { $postarr = get_object_vars($postarr); } // export array as variables extract($postarr, EXTR_SKIP); // Are we updating or creating? $update = false; if (!empty($ID)) { $update = true; $post =& get_post($ID); $previous_status = $post->post_status; } // Get the basics. if (empty($no_filter)) { $post_content = apply_filters('content_save_pre', $post_content); $post_content_filtered = apply_filters('content_filtered_save_pre', $post_content_filtered); $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt); $post_title = apply_filters('title_save_pre', $post_title); $post_category = apply_filters('category_save_pre', $post_category); $post_status = apply_filters('status_save_pre', $post_status); $post_name = apply_filters('name_save_pre', $post_name); $comment_status = apply_filters('comment_status_pre', $comment_status); $ping_status = apply_filters('ping_status_pre', $ping_status); } if ('' == $post_content && '' == $post_title && '' == $post_excerpt) { return 0; } // Make sure we set a valid category if (0 == count($post_category) || !is_array($post_category)) { $post_category = array(get_option('default_category')); } $post_cat = $post_category[0]; if (empty($post_author)) { $post_author = $user_ID; } if (empty($post_status)) { $post_status = 'draft'; } if (empty($post_type)) { $post_type = 'post'; } // Get the post ID. if ($update) { $post_ID = (int) $ID; } // Create a valid post name. Drafts are allowed to have an empty // post name. if (empty($post_name)) { if ('draft' != $post_status) { $post_name = sanitize_title($post_title); } } else { $post_name = sanitize_title($post_name); } // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now if (empty($post_date)) { if ('draft' != $post_status) { $post_date = current_time('mysql'); } } if (empty($post_date_gmt)) { if ('draft' != $post_status) { $post_date_gmt = get_gmt_from_date($post_date); } } if ('publish' == $post_status) { $now = gmdate('Y-m-d H:i:59'); if (mysql2date('U', $post_date_gmt) > mysql2date('U', $now)) { $post_status = 'future'; } } 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 (empty($post_pingback)) { $post_pingback = get_option('default_pingback_flag'); } if (isset($to_ping)) { $to_ping = preg_replace('|\\s+|', "\n", $to_ping); } else { $to_ping = ''; } if (!isset($pinged)) { $pinged = ''; } 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 ('draft' != $post_status) { $post_name_check = $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} WHERE post_name = '{$post_name}' AND post_type = '{$post_type}' AND ID != '{$post_ID}' AND post_parent = '{$post_parent}' LIMIT 1"); if ($post_name_check || in_array($post_name, $wp_rewrite->feeds)) { $suffix = 2; do { $alt_post_name = $post_name . "-{$suffix}"; $post_name_check = $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} WHERE post_name = '{$alt_post_name}' AND post_type = '{$post_type}' AND ID != '{$post_ID}' AND post_parent = '{$post_parent}' LIMIT 1"); $suffix++; } while ($post_name_check); $post_name = $alt_post_name; } } if ($update) { $wpdb->query("UPDATE IGNORE {$wpdb->posts} SET\n\t\t\tpost_author = '{$post_author}',\n\t\t\tpost_date = '{$post_date}',\n\t\t\tpost_date_gmt = '{$post_date_gmt}',\n\t\t\tpost_content = '{$post_content}',\n\t\t\tpost_content_filtered = '{$post_content_filtered}',\n\t\t\tpost_title = '{$post_title}',\n\t\t\tpost_excerpt = '{$post_excerpt}',\n\t\t\tpost_status = '{$post_status}',\n\t\t\tpost_type = '{$post_type}',\n\t\t\tcomment_status = '{$comment_status}',\n\t\t\tping_status = '{$ping_status}',\n\t\t\tpost_password = '******',\n\t\t\tpost_name = '{$post_name}',\n\t\t\tto_ping = '{$to_ping}',\n\t\t\tpinged = '{$pinged}',\n\t\t\tpost_modified = '" . current_time('mysql') . "',\n\t\t\tpost_modified_gmt = '" . current_time('mysql', 1) . "',\n\t\t\tpost_parent = '{$post_parent}',\n\t\t\tmenu_order = '{$menu_order}'\n\t\t\tWHERE ID = {$post_ID}"); } else { $wpdb->query("INSERT IGNORE INTO {$wpdb->posts}\n\t\t\t(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)\n\t\t\tVALUES\n\t\t\t('{$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_date}', '{$post_date_gmt}', '{$post_parent}', '{$menu_order}', '{$post_mime_type}')"); $post_ID = (int) $wpdb->insert_id; } if (empty($post_name) && 'draft' != $post_status) { $post_name = sanitize_title($post_title, $post_ID); $wpdb->query("UPDATE {$wpdb->posts} SET post_name = '{$post_name}' WHERE ID = '{$post_ID}'"); } wp_set_post_categories($post_ID, $post_category); if ('page' == $post_type) { clean_page_cache($post_ID); $wp_rewrite->flush_rules(); } else { clean_post_cache($post_ID); } // Set GUID if (!$update) { $wpdb->query("UPDATE {$wpdb->posts} SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '{$post_ID}'"); } if ($update) { if ($previous_status != 'publish' && $post_status == 'publish') { // Reset GUID if transitioning to publish. $wpdb->query("UPDATE {$wpdb->posts} SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '{$post_ID}'"); do_action('private_to_published', $post_ID); } do_action('edit_post', $post_ID); } if ($post_status == 'publish' && $post_type == 'post') { do_action('publish_post', $post_ID); if (defined('XMLRPC_REQUEST')) { do_action('xmlrpc_publish_post', $post_ID); } if (defined('APP_REQUEST')) { do_action('app_publish_post', $post_ID); } if (!defined('WP_IMPORTING')) { if ($post_pingback) { $result = $wpdb->query("\n\t\t\t\t\tINSERT INTO {$wpdb->postmeta}\n\t\t\t\t\t(post_id,meta_key,meta_value)\n\t\t\t\t\tVALUES ('{$post_ID}','_pingme','1')\n\t\t\t\t"); } $result = $wpdb->query("\n\t\t\t\tINSERT INTO {$wpdb->postmeta}\n\t\t\t\t(post_id,meta_key,meta_value)\n\t\t\t\tVALUES ('{$post_ID}','_encloseme','1')\n\t\t\t"); wp_schedule_single_event(time(), 'do_pings'); } } else { if ($post_type == 'page') { if (!empty($page_template)) { if (!update_post_meta($post_ID, '_wp_page_template', $page_template)) { add_post_meta($post_ID, '_wp_page_template', $page_template, true); } } if ($post_status == 'publish') { do_action('publish_page', $post_ID); } } } // Always clears the hook in case the post status bounced from future to draft. wp_clear_scheduled_hook('publish_future_post', $post_ID); // Schedule publication. if ('future' == $post_status) { wp_schedule_single_event(strtotime($post_date_gmt . ' GMT'), 'publish_future_post', array($post_ID)); } do_action('save_post', $post_ID); do_action('wp_insert_post', $post_ID); return $post_ID; }
private function page_rows($pagenum = 1, $per_page = 15) { global $wpdb, $wp_query; $level = 0; $pages =& $wp_query->posts; if (!$pages) { return false; } /* * arrange pages into two parts: top level pages and children_pages * children_pages is two dimensional array, eg. * children_pages[10][] contains all sub-pages whose parent is 10. * It only takes O(N) to arrange this and it takes O(1) for subsequent lookup operations * If searching, ignore hierarchy and treat everything as top level */ if (empty($this->search)) { $top_level_pages = array(); $children_pages = array(); foreach ($pages as $page) { // catch and repair bad pages if ($page->post_parent == $page->ID) { $page->post_parent = 0; $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = '0' WHERE ID = %d", $page->ID)); clean_page_cache($page->ID); } if (0 == $page->post_parent) { $top_level_pages[] = $page; } else { $children_pages[$page->post_parent][] = $page; } } $pages =& $top_level_pages; } $count = 0; $start = ($pagenum - 1) * $per_page; $end = $start + $per_page; ?> <dl><?php foreach ($pages as $page) { if ($count >= $end) { break; } if ($count >= $start) { echo $this->display_page_row($page, $level); } $count++; if (isset($children_pages)) { $this->_page_rows($children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page); } } // if it is the last pagenum and there are orphaned pages, display them with paging as well if (isset($children_pages) && $count < $end) { foreach ($children_pages as $orphans) { foreach ($orphans as $op) { if ($count >= $end) { break; } if ($count >= $start) { echo $this->display_page_row($op, 0); } $count++; } } } ?> </dl><?php }
function wp_update_comment_count($post_id) { global $wpdb, $comment_count_cache; $post_id = (int) $post_id; if (!$post_id) { return false; } $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post_id}' AND comment_approved = '1'"); $wpdb->query("UPDATE {$wpdb->posts} SET comment_count = {$count} WHERE ID = '{$post_id}'"); $comment_count_cache[$post_id] = $count; $post = get_post($post_id); if ('page' == $post->post_type) { clean_page_cache($post_id); } else { clean_post_cache($post_id); } do_action('edit_post', $post_id); return true; }
/** * Updates the comment count for the post. * * @since 2.5.0 * @uses $wpdb * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old * @uses do_action() Calls 'edit_posts' hook on $post_id and $post * * @param int $post_id Post ID * @return bool False on '0' $post_id or if post with ID does not exist. True on success. */ function wp_update_comment_count_now($post_id) { global $wpdb; $post_id = (int) $post_id; if ( !$post_id ) return false; if ( !$post = get_post($post_id) ) return false; $old = (int) $post->comment_count; $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) ); $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) ); if ( 'page' == $post->post_type ) clean_page_cache( $post_id ); else clean_post_cache( $post_id ); do_action('wp_update_comment_count', $post_id, $new, $old); do_action('edit_post', $post_id, $post); return true; }
function process_bulk_action() { global $wp_rewrite, $wpdb; if ('convert' === $this->current_action()) { $output = "<div id=\"viceversa-status\" class=\"updated\">\n <input type=\"button\" class=\"viceversa-close-icon button-secondary\" title=\"Close\" value=\"x\" />"; if (VICEVERSA_DEBUG) { $output .= "<strong>" . __('Debug Mode', 'vice-versa') . "</strong>\n"; } switch ($_POST['p_type']) { case 'page': $categories = array('ids' => array(), 'titles' => array()); $do_bulk = false; foreach ($_POST['parents'] as $category) { if ($category != "") { $data = explode("|", $category); $categories[$data[3]]['ids'] = array(); $categories[$data[3]]['titles'] = array(); if ($data[3] == 0) { $do_bulk = true; } } } foreach ($_POST['parents'] as $category) { if ($category != "") { $data = explode("|", $category); array_push($categories[$data[3]]['ids'], $data[0]); array_push($categories[$data[3]]['titles'], $data[2]); } } $output .= "<p>\n"; foreach ($_POST['post'] as $viceversa_page) { $viceversa_data = explode("|", $viceversa_page); $viceversa_url = site_url() . "/?p=" . $viceversa_data[0]; if ($do_bulk) { $ids = $categories[0]['ids']; $titles = $categories[0]['titles']; } else { $ids = $categories[$viceversa_data[0]]['ids']; $titles = $categories[$viceversa_data[0]]['titles']; } $catlist = ""; if ($titles) { foreach ($titles as $cat) { $catlist .= $cat . ", "; } } $catlist = trim($catlist, ', ') == "" ? get_cat_name(1) : trim($catlist, ', '); $cat_array = count($ids) < 1 ? array(1) : $ids; if (!VICEVERSA_DEBUG) { $wpdb->update($wpdb->posts, array('guid' => $viceversa_url, 'post_parent' => $cat_array[0]), array('ID' => intval($viceversa_data[0])), array('%s', '%d'), array('%d')); clean_page_cache(intval($viceversa_data[0])); set_post_type(intval($viceversa_data[0]), 'post'); wp_set_post_categories(intval($viceversa_data[0]), $cat_array); } $new_permalink = get_permalink(intval($viceversa_data[0])); $output .= sprintf(__('<strong>' . __('Page', 'vice-versa') . '</strong> #%s <code><a href="%s" target="_blank" title="' . __('New Permalink', 'vice-versa') . '">%s</a></code> ' . __('was successfully converted to a <strong>Post</strong> and assigned to category(s)', 'vice-versa') . ' <code>%s</code>. <a href="%s" target="_blank" title="' . __('New Permalink', 'vice-versa') . '">' . __('New Permalink', 'vice-versa') . '</a>', 'vice-versa'), $viceversa_data[0], $new_permalink, $viceversa_data[2], $catlist, $new_permalink) . "<br />\n"; } if (!VICEVERSA_DEBUG) { $wp_rewrite->flush_rules(); } break; default: $parents = array(); $do_bulk = false; foreach ($_POST['parents'] as $parent) { if ($parent != "") { $data = explode("|", $parent); if ($data[3] == 0) { $do_bulk = true; } $parents[intval($data[3])] = $data[0] . "|" . $data[2]; } } $output .= "<p>\n"; if (!$_POST['post']) { $output .= __('No items were selected. Please select items using the checkboxes.', 'vice-versa'); } else { foreach ($_POST['post'] as $viceversa_post) { $viceversa_data = explode("|", $viceversa_post); $viceversa_url = site_url() . "/?page_id=" . $viceversa_data[0]; if ($do_bulk) { $p = $parents[0]; } else { $p = $parents[$viceversa_data[0]]; } $parent = $p == "" ? "0|" . __('No Parent', 'vice-versa') . "" : $p; $parent = explode("|", $parent); if (!VICEVERSA_DEBUG) { $wpdb->update($wpdb->posts, array('guid' => $viceversa_url, 'post_parent' => intval($parent[0])), array('ID' => intval($viceversa_data[0])), array('%s', '%d'), array('%d')); clean_post_cache(intval($viceversa_data[0])); set_post_type(intval($viceversa_data[0]), 'page'); wp_set_post_categories(intval($viceversa_data[0]), array(intval($parent[0]))); } $permalink = get_permalink(intval($viceversa_data[0])); $output .= sprintf(__('<strong>' . __('Post', 'vice-versa') . '</strong> #%s <code><a href="%s" target="_blank" title="' . __('New Permalink', 'vice-versa') . '">%s</a></code> ' . __('was successfully converted to a <strong>Page</strong> and assigned to parent', 'vice-versa') . ' #%s <code>%s</code>. <a href="%s" target="_blank" title="' . __('New Permalink', 'vice-versa') . '">' . __('New Permalink', 'vice-versa') . '</a>', 'vice-versa'), $viceversa_data[0], $permalink, $viceversa_data[2], $parent[0], $parent[1], $permalink) . "<br />\n"; } if (!VICEVERSA_DEBUG) { $wp_rewrite->flush_rules(); } } } $output .= "</p></div>\n"; define("VICEVERSA_STATUS", $output); } // $this->current_action }
/** * _save_post_hook() - Hook used to prevent page/post cache and rewrite rules from staying dirty * * Does two things. If the post is a page and has a template then it will update/add that * template to the meta. For both pages and posts, it will clean the post cache to make sure * that the cache updates to the changes done recently. For pages, the rewrite rules of * WordPress are flushed to allow for any changes. * * The $post parameter, only uses 'post_type' property and 'page_template' property. * * @package WordPress * @subpackage Post * @since 2.3 * * @uses $wp_rewrite Flushes Rewrite Rules. * * @param int $post_id The ID in the database table for the $post * @param object $post Object type containing the post information */ function _save_post_hook($post_id, $post) { if ($post->post_type == 'page') { clean_page_cache($post_id); global $wp_rewrite; $wp_rewrite->flush_rules(); } else { clean_post_cache($post_id); } }
/** Save Changes */ function cmspo_ajax_save_tree() { if (!check_ajax_referer('cms-page-order', false, false) && (!isset($_REQUEST['open']) || empty($_REQUEST['order']))) { cmspo_do_err(); } if (isset($_REQUEST['open'])) { $user = wp_get_current_user(); update_user_option($user->ID, 'cmspo_tree_state', $_REQUEST['open']); } if (!empty($_REQUEST['order'])) { global $wpdb; unset($_REQUEST['order'][0]); $prev_depth = 1; $order = array(); $order[1] = 1; foreach ($_REQUEST['order'] as $page) { $post_id = (int) $page['item_id']; if ($page['parent_id'] == 'root') { $parent = 0; } else { $parent = (int) $page['parent_id']; } if ($page['depth'] > $prev_depth) { $order[$page['depth']] = 1; $menu_order = $order[$page['depth']]; } else { if ($page['depth'] < $prev_depth) { $menu_order = $order[$page['depth']]; } } $prev_depth = (int) $page['depth']; $data = array('menu_order' => $order[$page['depth']], 'post_parent' => $parent); $where = array('ID' => $post_id); $wpdb->update($wpdb->posts, $data, $where); clean_page_cache($post_id); $order[$page['depth']]++; } global $wp_rewrite; $wp_rewrite->flush_rules(false); } die; }
/** * Updates existing posts with tags * The $all_posts param specifies whether all posts are re-tagged or only those without tags * * @param bool $all_posts * @return int */ protected function ReTagPosts( $all_posts=false ) { set_time_limit(0); global $wpdb; $updated = 0; // in future rewrite this with a branch so that if we are looking at posts with no tags then // we only return from the DB those posts that have no tags $sql = "SELECT id FROM {$wpdb->posts} WHERE post_password='' AND post_status='publish' AND post_type NOT IN('page', 'attachment', 'revision') ORDER BY post_modified_gmt DESC;"; ShowDebugAutoTag($sql); $posts = $wpdb->get_results($sql); foreach($posts as $post){ // definitley a better way to do this but would involve a major rewrite! ShowDebugAutoTag("get post id " . $post->id); $object = get_post($post->id); if ( $object == false || $object == null ) { return false; } ShowDebugAutoTag("Do we need to clean any Strictly Goodness?"); $newcontent = $this->CheckAndCleanTags( $object->post_content ); // find tags for this post $posttags = $this->AutoTag( $object, $all_posts ); ShowDebugAutoTag("do we bold and deeplink"); if($this->boldtaggedwords || $this->taglinks){ ShowDebugAutoTag("call bold or deeplink tags"); if($this->boldtaggedwords && count($posttags) > 0){ ShowDebugAutoTag("Auto Bold this content"); // help SEO by bolding our tags $newcontent = $this->AutoBold($newcontent,$posttags); } if($this->taglinks && count($posttags) > 0){ ShowDebugAutoTag("Auto Link this content"); // help SEO by deeplinking our tags $newcontent = $this->AutoLink($newcontent,$posttags); } // now save the new deeplinked bolded content ShowDebugAutoTag("our new content is === " . $newcontent); $sql = $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = %s WHERE id = %d;", $newcontent,$object->ID); ShowDebugAutoTag("SQL is $sql"); $r = $wpdb->query($sql); ShowDebugAutoTag("should have been updated rows = " . $r); } if($posttags !== false){ $updated++; ShowDebugAutoTag("we have " . count($posttags) . " tags to add to this post"); // add tags to post // Append tags if tags to add if ( count($posttags) > 0) { // Add tags to posts wp_set_object_terms( $object->ID, $posttags, 'post_tag', true ); // Clean cache if ( 'page' == $object->post_type ) { clean_page_cache($object->ID); } else { clean_post_cache($object->ID); } } } unset($object,$posttags); } unset($posts); return $updated; }
function wp_update_comment_count($post_id) { global $wpdb, $comment_count_cache; $post_id = (int) $post_id; if ( !$post_id ) return false; $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'"); $wpdb->query("UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$post_id'"); $comment_count_cache[$post_id] = $count; $post = get_post($post_id); if ( 'page' == $post->post_type ) clean_page_cache( $post_id ); else clean_post_cache( $post_id ); do_action('edit_post', $post_id); return true; }
function _display_rows_hierarchical($pages, $pagenum = 1, $per_page = 20) { global $wpdb; $level = 0; if (!$pages) { $pages = get_pages(array('sort_column' => 'menu_order')); if (!$pages) { return false; } } /* * arrange pages into two parts: top level pages and children_pages * children_pages is two dimensional array, eg. * children_pages[10][] contains all sub-pages whose parent is 10. * It only takes O( N ) to arrange this and it takes O( 1 ) for subsequent lookup operations * If searching, ignore hierarchy and treat everything as top level */ if (empty($_REQUEST['s'])) { $top_level_pages = array(); $children_pages = array(); foreach ($pages as $page) { // catch and repair bad pages if ($page->post_parent == $page->ID) { $page->post_parent = 0; $wpdb->update($wpdb->posts, array('post_parent' => 0), array('ID' => $page->ID)); clean_page_cache($page->ID); } if (0 == $page->post_parent) { $top_level_pages[] = $page; } else { $children_pages[$page->post_parent][] = $page; } } $pages =& $top_level_pages; } $count = 0; $start = ($pagenum - 1) * $per_page; $end = $start + $per_page; foreach ($pages as $page) { if ($count >= $end) { break; } if ($count >= $start) { echo "\t" . $this->single_row($page, $level); } $count++; if (isset($children_pages)) { $this->_page_rows($children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page); } } // if it is the last pagenum and there are orphaned pages, display them with paging as well if (isset($children_pages) && $count < $end) { foreach ($children_pages as $orphans) { foreach ($orphans as $op) { if ($count >= $end) { break; } if ($count >= $start) { echo "\t" . $this->single_row($op, 0); } $count++; } } } }
public function import_deindex_post($post_ids) { // sets a post's status to draft so that it no longer appears in searches // TODO: need to find a better status to hide it from searches, // but not invalidate incoming links or remove comments global $wpdb; foreach ((array) $post_ids as $post_id) { $post_id = absint($post_id); if (!$post_id) { continue; } // set the post to draft (TODO: use a WP function instead of writing to DB) $wpdb->get_results("UPDATE {$wpdb->posts} SET post_status = 'draft' WHERE ID = {$post_id}"); // clear the post/page cache clean_page_cache($post_id); clean_post_cache($post_id); // do the post transition wp_transition_post_status('draft', 'publish', $post_id); } }
/** * Hook used to prevent page/post cache and rewrite rules from staying dirty. * * Does two things. If the post is a page and has a template then it will * update/add that template to the meta. For both pages and posts, it will clean * the post cache to make sure that the cache updates to the changes done * recently. For pages, the rewrite rules of WordPress are flushed to allow for * any changes. * * The $post parameter, only uses 'post_type' property and 'page_template' * property. * * @since 2.3.0 * @access private * @uses $wp_rewrite Flushes Rewrite Rules. * * @param int $post_id The ID in the database table for the $post * @param object $post Object type containing the post information */ function _save_post_hook($post_id, $post) { if ($post->post_type == 'page') { clean_page_cache($post_id); // Avoid flushing rules for every post during import. if (!defined('WP_IMPORTING')) { global $wp_rewrite; $wp_rewrite->flush_rules(false); } } else { clean_post_cache($post_id); } }
/** * Updates existing posts with tags * The $all_posts param specifies whether all posts are re-tagged or only those without tags * * @param bool $all_posts * @return int */ protected function ReTagPosts($all_posts = false) { set_time_limit(0); global $wpdb; $updated = 0; // in future rewrite this with a branch so that if we are looking at posts with no tags then // we only return from the DB those posts that have no tags $sql = "SELECT id \n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tWHERE post_password='' AND post_status='publish' AND post_type='post' \n\t\t\t\tORDER BY post_modified_gmt DESC;"; ShowDebugAutoTag($sql); $posts = $wpdb->get_results($sql); foreach ($posts as $post) { // definitley a better way to do this but would involve a major rewrite! ShowDebugAutoTag("get post id " . $post->id); $object = get_post($post->id); if ($object == false || $object == null) { return false; } $posttags = $this->AutoTag($object, $all_posts); if ($posttags !== false) { $updated++; ShowDebugAutoTag("we have " . count($posttags) . " tags to add to this post"); // add tags to post // Append tags if tags to add if (count($posttags) > 0) { // Add tags to posts wp_set_object_terms($object->ID, $posttags, 'post_tag', true); // Clean cache if ('page' == $object->post_type) { clean_page_cache($object->ID); } else { clean_post_cache($object->ID); } } } unset($object, $posttags); } unset($posts); return $updated; }