public function update_count() { wp_update_term_count($this->term_taxonomy_id, 'language'); // posts count wp_update_term_count($this->tl_term_taxonomy_id, 'term_language'); // terms count }
public function update_for_post($post_id) { $taxonomies = get_taxonomies(); foreach ($taxonomies as $taxonomy) { $terms_for_post = wp_get_post_terms($post_id, $taxonomy); foreach ($terms_for_post as $term) { if (isset($term->term_taxonomy_id)) { wp_update_term_count($term->term_taxonomy_id, $taxonomy); } } } }
/** * @param int $post_id * @param string $taxonomy * @param array $changed_ttids * @param bool $bulk * Running this function will remove certain issues arising out of bulk adding of terms to posts of various languages. * This case can result in situations in which the WP Core functionality adds a term to a post, before the language assignment * operations of WPML are triggered. This leads to states in which terms can be assigned to a post even though their language * differs from that of the post. * This function behaves between hierarchical and flag taxonomies. Hierarchical terms from the wrong taxonomy are simply removed * from the post. Flat terms are added with the same name but in the correct language. * For flat terms this implies either the use of the existing term or the creation of a new one. * This function uses wpdb queries instead of the WordPress API, it is therefore save to be run out of * any language setting. */ public static function quick_edited_post_terms($post_id, $taxonomy, $changed_ttids = array(), $bulk = false) { global $wpdb, $sitepress, $wpml_post_translations; if (!$sitepress->is_translated_taxonomy($taxonomy) || !($post_lang = $wpml_post_translations->get_element_lang_code($post_id))) { return; } $query_for_allowed_ttids = $wpdb->prepare("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code = %s AND element_type = %s", $post_lang, 'tax_' . $taxonomy); $allowed_ttids = $wpdb->get_col($query_for_allowed_ttids); $new_ttids = array(); foreach ($changed_ttids as $ttid) { if (!in_array($ttid, $allowed_ttids)) { $wrong_term_where = array('object_id' => $post_id, 'term_taxonomy_id' => $ttid); if (is_taxonomy_hierarchical($taxonomy)) { // Hierarchical terms are simply deleted if they land on the wrong language $wpdb->delete($wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $ttid)); } else { /* Flat taxonomy terms could also be given via their names and not their ttids * In this case we append the ttids resulting from these names to the $changed_ttids array, * we do this only in the case of these terms actually being present in another but the * posts' language. */ $query_for_term_name = $wpdb->prepare("SELECT t.name FROM {$wpdb->terms} AS t JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.term_taxonomy_id=%d", $ttid); $term_name = $wpdb->get_var($query_for_term_name); $ttid_in_correct_lang = false; if (!empty($allowed_ttids)) { $in = wpml_prepare_in($allowed_ttids, "%d"); // Try to get the ttid of a term in the correct language, that has the same $ttid_in_correct_lang = $wpdb->get_var($wpdb->prepare("SELECT tt.term_taxonomy_id\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t{$wpdb->terms} AS t\n\t\t\t\t\t\t\t\tJOIN {$wpdb->term_taxonomy} AS tt\n\t\t\t\t\t\t\t\t\tON t.term_id = tt.term_id\n\t\t\t\t\t\t\tWHERE t.name=%s AND tt.taxonomy=%s AND tt.term_taxonomy_id IN ({$in})", $term_name, $taxonomy)); } if (!$ttid_in_correct_lang) { /* If we do not have a term by this name in the given taxonomy and language we have to create it. * In doing so we must avoid interactions with filtering by wpml on this functionality and ensure uniqueness for the slug of the newly created term. */ $new_term = wp_insert_term($term_name, $taxonomy, array('slug' => self::term_unique_slug(sanitize_title($term_name), $taxonomy, $post_lang))); if (isset($new_term['term_taxonomy_id'])) { $ttid_in_correct_lang = $new_term['term_taxonomy_id']; $trid = $bulk ? $sitepress->get_element_trid($ttid, 'tax_' . $taxonomy) : false; $sitepress->set_element_language_details($ttid_in_correct_lang, 'tax_' . $taxonomy, $trid, $post_lang); } } if (!in_array($ttid_in_correct_lang, $changed_ttids)) { $wpdb->update($wpdb->term_relationships, array('term_taxonomy_id' => $ttid_in_correct_lang), $wrong_term_where); $new_ttids[] = $ttid_in_correct_lang; } else { $wpdb->delete($wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $ttid)); } } } } // Update term counts manually here, since using sql, will not trigger the updating of term counts automatically. wp_update_term_count(array_merge($changed_ttids, $new_ttids), $taxonomy); }
/** * Remove term(s) associated with a given object(s). Core doesn't have this as of 3.2 * @see http://core.trac.wordpress.org/ticket/15475 * * @author ericmann * @compat 3.3? * * @param int|array $object_ids The ID(s) of the object(s) to retrieve. * @param int|array $terms The ids of the terms to remove. * @param string|array $taxonomies The taxonomies to retrieve terms from. * @return bool|WP_Error Affected Term IDs */ function remove_object_terms($object_id, $terms, $taxonomy) { global $wpdb; if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); } if (!is_array($object_id)) { $object_id = array($object_id); } if (!is_array($terms)) { $terms = array($terms); } $delete_objects = array_map('intval', $object_id); $delete_terms = array_map('intval', $terms); if ($delete_terms) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; $in_delete_objects = "'" . implode("', '", $delete_objects) . "'"; $return = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id IN ({$in_delete_objects}) AND term_taxonomy_id IN ({$in_delete_terms})", $object_id)); wp_update_term_count($delete_terms, $taxonomy); return true; } return false; }
/** * save simplyhired job */ function save_job($job, $import_author) { $prefix = 'et_'; $result = wp_insert_post(array('post_author' => $import_author != '' ? $import_author : 1, 'post_date' => date('Y-m-d 00:00:00', strtotime($job['date'])), 'post_status' => 'publish', 'post_content' => apply_filters('et_job_content', $job['description']), 'post_type' => 'job', 'post_title' => $job['title'], 'tax_input' => array('job_category' => array($job['job_category']), 'job_type' => array($job['job_type'])))); // if insert fail, return false if (!$result) { return false; } // otherwise, insert meta data and terms wp_set_object_terms($result, $job['job_category'], 'job_category'); wp_set_object_terms($result, $job['job_type'], 'job_type'); if (defined('ALTERNATE_WP_CRON')) { global $wpdb; if ($term_info = term_exists($job['job_category'], 'job_category')) { $tt_id = $term_info['term_taxonomy_id']; $wpdb->insert($wpdb->term_relationships, array('object_id' => $result, 'term_taxonomy_id' => $tt_id)); wp_update_term_count(array($tt_id), 'job_category'); } if ($term_info = term_exists($job['job_type'], 'job_type')) { $tt_id = $term_info['term_taxonomy_id']; $wpdb->insert($wpdb->term_relationships, array('object_id' => $result, 'term_taxonomy_id' => $tt_id)); wp_update_term_count(array($tt_id), 'job_type'); } } update_post_meta($result, $prefix . 'simplyhired_url', $job['url']); update_post_meta($result, $prefix . 'template_id', 'simplyhired'); update_post_meta($result, $prefix . 'simplyhired_creator', $job['company']); update_post_meta($result, $prefix . 'simplyhired_jobkey', $job['jobkey']); update_post_meta($result, $prefix . 'location', $job['location']); update_post_meta($result, $prefix . 'full_location', $job['location']); update_post_meta($result, $prefix . 'job_paid', 2); update_post_meta($result, $prefix . 'simplyhired_add_time', date('Y-m-d 00:00:00', time())); return true; }
function edcal_changedate() { if (!$this->edcal_checknonce()) { die; } header("Content-Type: application/json"); $this->edcal_addNoCacheHeaders(); $edcal_postid = isset($_GET['postid']) ? $_GET['postid'] : null; $edcal_newDate = isset($_GET['newdate']) ? $_GET['newdate'] : null; $edcal_oldDate = isset($_GET['olddate']) ? $_GET['olddate'] : null; $edcal_postStatus = isset($_GET['postStatus']) ? $_GET['postStatus'] : null; $move_to_drawer = $edcal_newDate == '0000-00-00'; $move_from_drawer = $edcal_oldDate == '00000000'; global $post; $args = array('posts_id' => $edcal_postid); $post = get_post($edcal_postid); setup_postdata($post); /* * Posts in WordPress have more than one date. There is the GMT date, * the date in the local time zone, the modified date in GMT and the * modified date in the local time zone. We update all of them. */ if ($move_from_drawer) { /* * Set the date to 'unscheduled' [ie. 0]. We use this date * further down in the concurrency check, and this will make the dates * technically off by 10 hours, but it's still the same day. We only do * this for posts that were created as drafts. Works for now, but * we would have to revamp this if we use an actual timestamp check. */ $post->post_date = '0000-00-00 ' . date('H:i:s', strtotime($post->post_date)); } else { if ($move_to_drawer) { // echo ( "\r\npost->post_date_gmt=".$post->post_date_gmt); $post->post_date_gmt = $post->post_date; } else { // set the scheduled time as our original time $post->post_date_gmt = $post->post_date; } } // echo ( "\r\npost->post_date_gmt = $post->post_date_gmt \r\npost->post_date = $post->post_date"); /* * Error-checking: */ $error = false; if (!current_user_can('edit_post', $edcal_postid)) { /* * This is just a sanity check to make sure that the current * user has permission to edit posts. Most of the time this * will never be run because you can't see the calendar unless * you are at least an editor. */ $error = EDCAL_PERMISSION_ERROR; } else { if (date('Y-m-d', strtotime($post->post_date)) != date('Y-m-d', strtotime($edcal_oldDate))) { /* * We are doing optimistic concurrency checking on the dates. If * the user tries to move a post we want to make sure nobody else * has moved that post since the page was last updated. If the * old date in the database doesn't match the old date from the * browser then we return an error to the browser along with the * updated post data. */ $error = EDCAL_CONCURRENCY_ERROR; } } if ($error) { // die('error= '.$error); ?> { "error": <?php echo $error; ?> , "post" : <?php $this->edcal_postJSON($post, false, true); ?> } <?php die; } /* * No errors, so let's go create our new post parameters to update */ $updated_post = array(); $updated_post['ID'] = $edcal_postid; if (!$move_to_drawer) { $updated_post['post_date'] = $edcal_newDate . substr($post->post_date, strlen($edcal_newDate)); } /* * When a user creates a draft and never sets a date or publishes it * then the GMT date will have a timestamp of 00:00:00 to indicate * that the date hasn't been set. In that case we need to specify * an edit date or the wp_update_post function will strip our new * date out and leave the post as publish immediately. */ $needsEditDate = preg_match('/^0000/', $post->post_date_gmt); if ($needsEditDate) { // echo "\r\nneeds edit date\r\n"; $updated_post['edit_date'] = $edcal_newDate . substr($post->post_date, strlen($edcal_newDate)); } if ($move_to_drawer) { $updated_post['post_date_gmt'] = "0000-00-00 00:00:00"; $updated_post['edit_date'] = $post->post_date; } else { if ($move_from_drawer) { $updated_post['post_date_gmt'] = get_gmt_from_date($post->post_date); $updated_post['post_modified_gmt'] = get_gmt_from_date($post->post_date); } } /* * We need to make sure to use the GMT formatting for the date. */ if (!$move_to_drawer) { $updated_post['post_date_gmt'] = get_gmt_from_date($updated_post['post_date']); $updated_post['post_modified'] = $edcal_newDate . substr($post->post_modified, strlen($edcal_newDate)); $updated_post['post_modified_gmt'] = get_gmt_from_date($updated_post['post_date']); } if ($edcal_postStatus != $post->post_status) { /* * We only want to update the post status if it has changed. * If the post status has changed that takes a few more steps */ wp_transition_post_status($edcal_postStatus, $post->post_status, $post); $updated_post['post_status'] = $edcal_postStatus; // Update counts for the post's terms. foreach ((array) get_object_taxonomies('post') as $taxonomy) { $tt_ids = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids'); wp_update_term_count($tt_ids, $taxonomy); } do_action('edit_post', $edcal_postid, $post); do_action('save_post', $edcal_postid, $post); do_action('wp_insert_post', $edcal_postid, $post); } // die(var_dump($updated_post).'success!'); /* * Now we finally update the post into the database */ wp_update_post($updated_post); /* * We finish by returning the latest data for the post in the JSON */ global $post; $args = array('posts_id' => $edcal_postid); $post = get_post($edcal_postid); ?> { "post" : <?php $this->edcal_postJSON($post, false, true); ?> } <?php die; }
/** * wp_set_object_terms() - * * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). * * @global $wpdb Database Object * @param int $object_id The object to relate to. * @param array|int|string $term The slug or id of the term. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append If false will delete difference of terms. */ function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { global $wpdb; $object_id = (int) $object_id; if ( ! is_taxonomy($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); if ( !is_array($terms) ) $terms = array($terms); if ( ! $append ) $old_terms = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); $tt_ids = array(); $term_ids = array(); foreach ($terms as $term) { if ( !$id = is_term($term, $taxonomy) ) $id = wp_insert_term($term, $taxonomy); $term_ids[] = $id['term_id']; $id = $id['term_taxonomy_id']; $tt_ids[] = $id; if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) continue; $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); } wp_update_term_count($tt_ids, $taxonomy); if ( ! $append ) { $delete_terms = array_diff($old_terms, $tt_ids); if ( $delete_terms ) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_delete_terms)"); wp_update_term_count($delete_terms, $taxonomy); } } return $tt_ids; }
function languages_page() { global $wp_rewrite; $options = get_option('polylang'); // for nav menus form $locations = get_registered_nav_menus(); $menus = wp_get_nav_menus(); $menu_lang = get_option('polylang_nav_menus'); // for widgets $widget_lang = get_option('polylang_widgets'); $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; switch ($action) { case 'add': check_admin_referer('add-lang', '_wpnonce_add-lang'); $error = $this->validate_lang(); if ($error == 0) { $r = wp_insert_term($_POST['name'], 'language', array('slug' => $_POST['slug'], 'description' => $_POST['description'])); wp_update_term($r['term_id'], 'language', array('term_group' => $_POST['term_group'])); // can't set the term group directly in wp_insert_term update_metadata('term', $r['term_id'], '_rtl', $_POST['rtl']); if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language $options['default_lang'] = $_POST['slug']; update_option('polylang', $options); } $wp_rewrite->flush_rules(); // refresh rewrite rules if (!$this->download_mo($_POST['description'])) { $error = 5; } } wp_redirect('admin.php?page=mlang' . ($error ? '&error=' . $error : '')); // to refresh the page (possible thanks to the $_GET['noheader']=true) exit; break; case 'delete': check_admin_referer('delete-lang'); if (isset($_GET['lang']) && $_GET['lang']) { $lang_id = (int) $_GET['lang']; $lang = $this->get_language($lang_id); $lang_slug = $lang->slug; // update the language slug in posts meta $posts = get_posts(array('numberposts' => -1, 'fields' => 'ids', 'meta_key' => '_translations', 'post_type' => 'any', 'post_status' => 'any')); $this->delete_translations('post', $posts, $lang_slug); // update the language slug in categories & post tags meta $terms = get_terms($this->taxonomies, array('get' => 'all', 'fields' => 'ids')); $this->delete_translations('term', $terms, $lang_slug); // FIXME should find something more efficient (with a sql query ?) foreach ($terms as $id) { if (($lg = $this->get_term_language($id)) && $lg->term_id == $lang_id) { $this->delete_term_language($id); } // delete language of this term } // delete menus locations foreach ($locations as $location => $description) { unset($menu_lang[$location][$lang_slug]); } update_option('polylang_nav_menus', $menu_lang); // delete language option in widgets foreach ($widget_lang as $key => $lang) { if ($lang == $lang_slug) { unset($widget_lang[$key]); } } update_option('polylang_widgets', $widget_lang); // delete the string translations delete_option('polylang_mo' . $lang_id); // delete the language itself delete_metadata('term', $lang_id, '_rtl'); wp_delete_term($lang_id, 'language'); // oops ! we deleted the default language... if ($options['default_lang'] == $lang_slug) { if ($listlanguages = $this->get_languages_list()) { $options['default_lang'] = $listlanguages[0]->slug; } else { unset($options['default_lang']); } update_option('polylang', $options); $wp_rewrite->flush_rules(); // refresh rewrite rules } } wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true) exit; break; case 'edit': if (isset($_GET['lang']) && $_GET['lang']) { $edit_lang = $this->get_language((int) $_GET['lang']); $rtl = get_metadata('term', $edit_lang->term_id, '_rtl', true); } break; case 'update': check_admin_referer('add-lang', '_wpnonce_add-lang'); $lang_id = (int) $_POST['lang_id']; $lang = $this->get_language($lang_id); $error = $this->validate_lang($lang); if ($error == 0) { // Update links to this language in posts and terms in case the slug has been modified $old_slug = $lang->slug; if ($old_slug != $_POST['slug']) { // update the language slug in posts meta $posts = get_posts(array('numberposts' => -1, 'fields' => 'ids', 'meta_key' => '_translations', 'post_type' => 'any', 'post_status' => 'any')); $this->update_translations('post', $posts, $old_slug); // update the language slug in categories & post tags meta $terms = get_terms($this->taxonomies, array('get' => 'all', 'fields' => 'ids')); $this->update_translations('term', $terms, $old_slug); // update menus locations foreach ($locations as $location => $description) { if (isset($menu_lang[$location][$old_slug])) { $menu_lang[$location][$_POST['slug']] = $menu_lang[$location][$old_slug]; unset($menu_lang[$location][$old_slug]); } } update_option('polylang_nav_menus', $menu_lang); // update language option in widgets foreach ($widget_lang as $key => $lang) { if ($lang == $old_slug) { $widget_lang[$key] = $_POST['slug']; } } update_option('polylang_widgets', $widget_lang); // update the default language option if necessary if ($options['default_lang'] == $old_slug) { $options['default_lang'] = $_POST['slug']; update_option('polylang', $options); } } // and finally update the language itself $args = array('name' => $_POST['name'], 'slug' => $_POST['slug'], 'description' => $_POST['description'], 'term_group' => $_POST['term_group']); wp_update_term($lang_id, 'language', $args); update_metadata('term', $lang_id, '_rtl', $_POST['rtl']); $wp_rewrite->flush_rules(); // refresh rewrite rules } wp_redirect('admin.php?page=mlang' . ($error ? '&error=' . $error : '')); // to refresh the page (possible thanks to the $_GET['noheader']=true) exit; break; case 'nav-menus': check_admin_referer('nav-menus-lang', '_wpnonce_nav-menus-lang'); $menu_lang = $_POST['menu-lang']; foreach ($locations as $location => $description) { foreach ($this->get_switcher_options('menu') as $key => $str) { $menu_lang[$location][$key] = isset($menu_lang[$location][$key]) ? 1 : 0; } } update_option('polylang_nav_menus', $menu_lang); break; case 'string-translation': check_admin_referer('string-translation', '_wpnonce_string-translation'); $strings = $this->get_strings(); foreach ($this->get_languages_list() as $language) { $mo = $this->mo_import($language); foreach ($_POST['translation'][$language->name] as $key => $translation) { $mo->add_entry($mo->make_entry($strings[$key]['string'], stripslashes($translation))); } // FIXME should I clean the mo object to remove unused strings ? $this->mo_export($mo, $language); } $paged = isset($_GET['paged']) ? '&paged=' . $_GET['paged'] : ''; wp_redirect('admin.php?page=mlang&tab=strings' . $paged); // to refresh the page (possible thanks to the $_GET['noheader']=true) exit; break; case 'options': check_admin_referer('options-lang', '_wpnonce_options-lang'); $options['default_lang'] = $_POST['default_lang']; $options['rewrite'] = $_POST['rewrite']; foreach (array('browser', 'hide_default', 'force_lang', 'redirect_lang') as $key) { $options[$key] = isset($_POST[$key]) ? 1 : 0; } update_option('polylang', $options); // refresh rewrite rules in case rewrite or hide_default options have been modified // it seems useless to refresh permastruct here $wp_rewrite->flush_rules(); // fills existing posts & terms with default language if (isset($_POST['fill_languages'])) { global $wpdb; $untranslated = $this->get_untranslated(); $lang = $this->get_language($options['default_lang']); $values = array(); foreach ($untranslated['posts'] as $post_id) { $values[] = $wpdb->prepare("(%d, %d)", $post_id, $lang->term_taxonomy_id); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id) VALUES " . implode(',', $values)); wp_update_term_count($lang->term_taxonomy_id, 'language'); // updating term count is mandatory (thanks to AndyDeGroo) } $values = array(); foreach ($untranslated['terms'] as $term_id) { $values[] = $wpdb->prepare("(%d, %s, %d)", $term_id, '_language', $lang->term_id); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->termmeta} (term_id, meta_key, meta_value) VALUES " . implode(',', $values)); } } break; default: break; } // prepare the list of tabs $tabs = array('lang' => __('Languages', 'polylang')); // only if at least one language has been created if ($listlanguages = $this->get_languages_list()) { if (current_theme_supports('menus')) { $tabs['menus'] = __('Menus', 'polylang'); } // don't display the menu tab if the active theme does not support nav menus $tabs['strings'] = __('Strings translation', 'polylang'); $tabs['settings'] = __('Settings', 'polylang'); } $active_tab = isset($_GET['tab']) && $_GET['tab'] ? $_GET['tab'] : 'lang'; switch ($active_tab) { case 'lang': // prepare the list table of languages $data = array(); foreach ($listlanguages as $lang) { $data[] = array_merge((array) $lang, array('flag' => $this->get_flag($lang))); } $list_table = new Polylang_List_Table(); $list_table->prepare_items($data); $rtl = 0; // error messages for data validation $errors[1] = __('Enter a valid WorPress locale', 'polylang'); $errors[2] = __('The language code must be 2 characters long', 'polylang'); $errors[3] = __('The language code must be unique', 'polylang'); $errors[4] = __('The language must have a name', 'polylang'); $errors[5] = __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang'); break; case 'menus': // default values foreach ($locations as $key => $location) { if (isset($menu_lang[$key])) { $menu_lang[$key] = wp_parse_args($menu_lang[$key], $this->get_switcher_options('menu', 'default')); } } break; case 'strings': // get the strings to translate $data = $this->get_strings(); // load translations foreach ($listlanguages as $language) { $mo = $this->mo_import($language); foreach ($data as $key => $row) { $data[$key]['translations'][$language->name] = $mo->translate($data[$key]['string']); $data[$key]['row'] = $key; // store the row number for convenience } } $string_table = new Polylang_String_Table(); $string_table->prepare_items($data); break; case 'settings': // detects posts & pages without language set $untranslated = $this->get_untranslated(); break; default: break; } // displays the page include PLL_INC . '/languages-form.php'; }
/** * Save sending job */ function save_indeed_jobs($job, $author = 1) { $prefix = 'et_'; $_POST += array('post_type' => 'job'); $result = wp_insert_post(array('post_author' => $author, 'post_date' => date('Y-m-d h:i:s', time()), 'post_status' => 'publish', 'post_content' => $job['content'], 'post_type' => 'job', 'post_title' => $job['jobtitle'], 'tax_input' => array('job_category' => array($job['job_category']), 'job_type' => array($job['job_type'])))); // if insert fail, return false if (!$result) { return false; } // otherwise, insert meta data and terms wp_set_object_terms($result, $job['job_category'], 'job_category'); wp_set_object_terms($result, $job['job_type'], 'job_type'); if (defined('ALTERNATE_WP_CRON')) { global $wpdb; if ($term_info = term_exists($job['job_category'], 'job_category')) { $tt_id = $term_info['term_taxonomy_id']; $wpdb->insert($wpdb->term_relationships, array('object_id' => $result, 'term_taxonomy_id' => $tt_id)); wp_update_term_count(array($tt_id), 'job_category'); } if ($term_info = term_exists($job['job_type'], 'job_type')) { $tt_id = $term_info['term_taxonomy_id']; $wpdb->insert($wpdb->term_relationships, array('object_id' => $result, 'term_taxonomy_id' => $tt_id)); wp_update_term_count(array($tt_id), 'job_type'); } } $meta_maps = array('location' => 'formattedLocationFull', 'full_location' => 'formattedLocationFull', 'indeed_company' => 'company', 'indeed_url' => 'url', 'indeed_city' => 'city', 'indeed_state' => 'city', 'indeed_country' => 'country', 'indeed_id' => 'jobkey'); foreach ($meta_maps as $key => $value) { update_post_meta($result, $prefix . $key, $job[$value]); } update_post_meta($result, $prefix . 'template_id', 'indeed'); return true; }
/** * Count terms. These are done at this point so all product props are set in advance. * * @param WC_Product * @since 2.7.0 */ protected function update_term_counts(&$product) { if (!wp_defer_term_counting()) { global $wc_allow_term_recount; $wc_allow_term_recount = true; $post_type = $product->is_type('variation') ? 'product_variation' : 'product'; // Update counts for the post's terms. foreach ((array) get_object_taxonomies($post_type) as $taxonomy) { $tt_ids = wc_get_object_terms($product->get_id(), $taxonomy, 'term_taxonomy_id'); wp_update_term_count($tt_ids, $taxonomy); } } }
/** * Recalculate number of posts assigned to each term. * * In instances where manual updates are made to the terms assigned to * posts in the database, the number of posts associated with a term * can become out-of-sync with the actual number of posts. * * This command runs wp_update_term_count() on the taxonomy's terms * to bring the count back to the correct value. * * ## OPTIONS * * <taxonomy>... * : One or more taxonomies to recalculate. * * ## EXAMPLES * * wp term recount category post_tag * * wp taxonomy list --field=name | xargs wp term recount */ public function recount($args) { foreach ($args as $taxonomy) { if (!taxonomy_exists($taxonomy)) { WP_CLI::warning(sprintf("Taxonomy %s does not exist.", $taxonomy)); } else { $terms = get_terms($taxonomy, array('hide_empty' => false)); $term_taxonomy_ids = wp_list_pluck($terms, 'term_taxonomy_id'); wp_update_term_count($term_taxonomy_ids, $taxonomy); WP_CLI::success(sprintf("Updated %s term count", $taxonomy)); } } }
/** * Remove term(s) associated with a given object. * * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $object_id The ID of the object from which the terms will be removed. * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove. * @param array|string $taxonomy Taxonomy name. * @return bool|WP_Error True on success, false or WP_Error on failure. */ private static function wp_remove_object_terms($object_id, $terms, $taxonomy) { global $wpdb; // Remove notices in below 3.6 and support backwards compatibility if (function_exists('wp_remove_object_terms')) { return wp_remove_object_terms($object_id, $terms, $taxonomy); } $object_id = (int) $object_id; if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); } if (!is_array($terms)) { $terms = array($terms); } $tt_ids = array(); foreach ((array) $terms as $term) { if (!strlen(trim($term))) { continue; } if (!($term_info = term_exists($term, $taxonomy))) { // Skip if a non-existent term ID is passed. if (is_int($term)) { continue; } } if (is_wp_error($term_info)) { return $term_info; } $tt_ids[] = $term_info['term_taxonomy_id']; } if ($tt_ids) { $in_tt_ids = "'" . implode("', '", $tt_ids) . "'"; /** * Fires immediately before an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action('delete_term_relationships', $object_id, $tt_ids); $deleted = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_tt_ids})", $object_id)); /** * Fires immediately after an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action('deleted_term_relationships', $object_id, $tt_ids); wp_update_term_count($tt_ids, $taxonomy); return (bool) $deleted; } return false; }
function jr_check_expired_cron() { global $wpdb; $action = get_option('jr_expired_action'); // Get list of expired posts that are published $postids = $wpdb->get_col($wpdb->prepare("\n\t\tSELECT postmeta.post_id\n\t\tFROM {$wpdb->postmeta} postmeta\n\t\tLEFT JOIN\t{$wpdb->posts} posts ON postmeta.post_id = posts.ID\n\t\tWHERE postmeta.meta_key = '_expires' \n\t\t AND postmeta.meta_value < '%s'\n\t\t AND post_status = 'publish'\n\t\t AND post_type = 'job_listing'\n\t", strtotime('NOW'))); if ($action == 'hide') { if ($postids) { foreach ($postids as $id) { // Captains log supplemental, we have detected a job which is out of date // Activate Cloak $post = get_post($id); if (empty($post)) { return; } if ('private' == $post->post_status) { return; } $old_status = $post->post_status; $job_post = array(); $job_post['ID'] = $id; $job_post['post_status'] = 'private'; wp_update_post($job_post); $post->post_status = 'private'; wp_transition_post_status('private', $old_status, $post); // Update counts for the post's terms. foreach ((array) get_object_taxonomies('job_listing') as $taxonomy) { $tt_ids = wp_get_object_terms($id, $taxonomy, array('fields' => 'tt_ids')); wp_update_term_count($tt_ids, $taxonomy); } do_action('edit_post', $id, $post); do_action('save_post', $id, $post); do_action('wp_insert_post', $id, $post); } } } if (get_option('jr_expired_job_email_owner') == 'yes') { $notify_ids = array(); // Get list of expiring posts that are published $postids = $wpdb->get_col($wpdb->prepare("\n\t\t\tSELECT DISTINCT postmeta.post_id\n\t\t\tFROM {$wpdb->postmeta} postmeta\n\t\t\tLEFT JOIN\t{$wpdb->posts} posts ON postmeta.post_id = posts.ID\n\t\t\tWHERE postmeta.meta_key = '_expires' \n\t\t\t AND postmeta.meta_value > '%s'\n\t\t\t AND postmeta.meta_value < '%s'\n\t\t\t AND post_status = 'publish'\n\t\t\t AND post_type = 'job_listing'\n\t\t", strtotime('NOW'), strtotime('+5 day'))); if (sizeof($postids) > 0) { // of those, get ids of posts that have already been notified $jobs_notified = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT postmeta.post_id\n\t\t\t\tFROM {$wpdb->postmeta} postmeta\n\t\t\t\tWHERE postmeta.meta_key = 'reminder_email_sent' \n\t\t\t\t AND postmeta.meta_value IN ('5','1')\n\t\t\t")); // Now only send to those who need sending to $notify_ids = array_diff($postids, $jobs_notified); if ($notify_ids && sizeof($notify_ids) > 0) { foreach ($notify_ids as $id) { update_post_meta($id, 'reminder_email_sent', '5'); jr_owner_job_expiring_soon($id, 5); } } } // Get list of expiring posts (1 day left) that are published $postids = $wpdb->get_col($wpdb->prepare("\n\t\t\tSELECT postmeta.post_id\n\t\t\tFROM {$wpdb->postmeta} postmeta\n\t\t\tLEFT JOIN\t{$wpdb->posts} posts ON postmeta.post_id = posts.ID\n\t\t\tWHERE postmeta.meta_key = '_expires' \n\t\t\t AND postmeta.meta_value > '%s'\n\t\t\t AND postmeta.meta_value < '%s'\n\t\t\t AND post_status = 'publish'\n\t\t\t AND post_type = 'job_listing'\n\t\t", strtotime('NOW'), strtotime('+1 day'))); if (sizeof($postids) > 0) { // of those, get ids of posts that have already been notified $jobs_notified = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT postmeta.post_id\n\t\t\t\tFROM {$wpdb->postmeta} postmeta\n\t\t\t\tWHERE postmeta.meta_key = 'reminder_email_sent' \n\t\t\t\t AND postmeta.meta_value IN ('1')\n\t\t\t", implode(',', $postids))); // Now only send to those who need sending to $notify_ids_2 = array_diff($postids, $jobs_notified, $notify_ids); if ($notify_ids_2 && sizeof($notify_ids_2) > 0) { foreach ($notify_ids_2 as $id) { update_post_meta($id, 'reminder_email_sent', '1'); jr_owner_job_expiring_soon($id, 1); } } } } }
function delete_series_object_relationship($object_id, $terms) { global $wpdb; $object_id = (int) $object_id; $t_ids = array(); if (!is_array($terms)) { $terms = array($terms); } foreach ($terms as $term) { $t_obj = term_exists($term, 'series'); if (is_object($t_obj)) { $t_ids[] = $t_obj->term_taxonomy_id; } } if (!empty($t_ids)) { $in_tt_ids = "'" . implode("', '", $t_ids) . "'"; $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_tt_ids})", $object_id)); wp_update_term_count($t_ids, 'series'); } }
function edcal_changedate() { if (!edcal_checknonce()) { die; } header("Content-Type: application/json"); edcal_addNoCacheHeaders(); global $edcal_startDate, $edcal_endDate; $edcal_postid = isset($_GET['postid']) ? $_GET['postid'] : null; $edcal_newDate = isset($_GET['newdate']) ? $_GET['newdate'] : null; $edcal_oldDate = isset($_GET['olddate']) ? $_GET['olddate'] : null; $edcal_postStatus = isset($_GET['postStatus']) ? $_GET['postStatus'] : null; if (!current_user_can('edit_post', $edcal_postid)) { global $EDCAL_PERMISSION_ERROR; /* * This is just a sanity check to make sure that the current * user has permission to edit posts. Most of the time this * will never be run because you can't see the calendar unless * you are at least an editor */ ?> { "error": <?php echo $EDCAL_PERMISSION_ERROR; ?> , <?php global $post; $args = array('posts_id' => $edcal_postid); $post = get_post($edcal_postid); ?> "post" : <?php edcal_postJSON($post, false, true); ?> } <?php die; } $post = get_post($edcal_postid, ARRAY_A); setup_postdata($post); /* * We are doing optimistic concurrency checking on the dates. If * the user tries to move a post we want to make sure nobody else * has moved that post since the page was last updated. If the * old date in the database doesn't match the old date from the * browser then we return an error to the browser along with the * updated post data. */ if (date('Y-m-d', strtotime($post['post_date'])) != date('Y-m-d', strtotime($edcal_oldDate))) { global $EDCAL_CONCURRENCY_ERROR; ?> { "error": <?php echo $EDCAL_CONCURRENCY_ERROR; ?> , <?php global $post; $args = array('posts_id' => $edcal_postid); $post = get_post($edcal_postid); ?> "post" : <?php edcal_postJSON($post, false, true); ?> } <?php die; } /* * Posts in WordPress have more than one date. There is the GMT date, * the date in the local time zone, the modified date in GMT and the * modified date in the local time zone. We update all of them. */ $post['post_date_gmt'] = $post['post_date']; /* * When a user creates a draft and never sets a date or publishes it * then the GMT date will have a timestamp of 00:00:00 to indicate * that the date hasn't been set. In that case we need to specify * an edit date or the wp_update_post function will strip our new * date out and leave the post as publish immediately. */ $needsEditDate = strpos($post['post_date_gmt'], "0000-00-00 00:00:00") === 0; $updated_post = array(); $updated_post['ID'] = $edcal_postid; $updated_post['post_date'] = $edcal_newDate . substr($post['post_date'], strlen($edcal_newDate)); if ($needsEditDate != -1) { $updated_post['edit_date'] = $edcal_newDate . substr($post['post_date'], strlen($edcal_newDate)); } /* * We need to make sure to use the GMT formatting for the date. */ $updated_post['post_date_gmt'] = get_gmt_from_date($updated_post['post_date']); $updated_post['post_modified'] = $edcal_newDate . substr($post['post_modified'], strlen($edcal_newDate)); $updated_post['post_modified_gmt'] = get_gmt_from_date($updated_post['post_date']); if ($edcal_postStatus != $post['post_status']) { /* * We only want to update the post status if it has changed. * If the post status has changed that takes a few more steps */ wp_transition_post_status($edcal_postStatus, $post['post_status'], $post); $updated_post['post_status'] = $edcal_postStatus; // Update counts for the post's terms. foreach ((array) get_object_taxonomies('post') as $taxonomy) { $tt_ids = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids'); wp_update_term_count($tt_ids, $taxonomy); } do_action('edit_post', $edcal_postid, $post); do_action('save_post', $edcal_postid, $post); do_action('wp_insert_post', $edcal_postid, $post); } /* * Now we finally update the post into the database */ wp_update_post($updated_post); /* * We finish by returning the latest data for the post in the JSON */ global $post; $args = array('posts_id' => $edcal_postid); $post = get_post($edcal_postid); ?> { "post" : <?php edcal_postJSON($post, false, true); ?> } <?php die; }
/** * Performs an SQL query assigning all terms to their correct language equivalent if it exists. * This should only be run after the previous functionality in here has finished. * Afterwards the term counts are recalculated globally, since term assignments bypassing the WordPress Core, * will not trigger any sort of update on those. */ private function reassign_terms() { global $wpdb; $update_query = $wpdb->prepare("UPDATE {$wpdb->term_relationships} AS o,\r\n\t\t\t\t\t{$wpdb->prefix}icl_translations AS ic,\r\n\t\t\t\t\t{$wpdb->prefix}icl_translations AS iw,\r\n\t\t\t\t\t{$wpdb->prefix}icl_translations AS ip,\r\n\t\t\t\t\t{$wpdb->posts} AS p\r\n\t\t\t\t\t\tSET o.term_taxonomy_id = ic.element_id\r\n\t\t\t\t\t\tWHERE\r\n\t\t\t\t\t\tic.trid = iw.trid\r\n\t\t\t\t\t\tAND ic.element_type = iw.element_type\r\n\t\t\t\t\t\tAND iw.element_id = o.term_taxonomy_id\r\n\t\t\t\t\t\tAND ic.language_code = ip.language_code\r\n\t\t\t\t\t\tAND ip.element_type = CONCAT('post_', p.post_type)\r\n\t\t\t\t\t\tAND ip.element_id = p.ID\r\n\t\t\t\t\t\tAND o.object_id = p.ID\r\n\t\t\t\t\t\tAND o.term_taxonomy_id != ic.element_id\r\n\t\t\t\t\t\tAND iw.element_type = %s", 'tax_' . $this->taxonomy); $rows_affected = $wpdb->query($update_query); if ($rows_affected) { $term_ids = $wpdb->get_col($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = %s", $this->taxonomy)); // Do not run the count update on taxonomies that are not actually registered as proper taxonomy objects, e.g. WooCommerce Product Attributes. $taxonomy_object = get_taxonomy($this->taxonomy); if ($taxonomy_object && isset($taxonomy_object->object_type)) { wp_update_term_count($term_ids, $this->taxonomy); } } }
function duplicate_taxonomies($master_post_id, $lang) { global $wpdb, $sitepress; $post_type = get_post_field('post_type', $master_post_id); $taxonomies = get_object_taxonomies($post_type); $trid = $sitepress->get_element_trid($master_post_id, 'post_' . $post_type); $duplicate_post_id = false; if ($trid) { $translations = $sitepress->get_element_translations($trid, 'post_' . $post_type, false, false, true); if (isset($translations[$lang])) { $duplicate_post_id = $translations[$lang]->element_id; } else { return false; // translation not found! } } remove_filter('get_terms_args', array($sitepress, 'get_terms_args_filter')); remove_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1); // AVOID filtering to current language remove_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10, 4); foreach ($taxonomies as $taxonomy) { $terms = wp_get_post_terms($master_post_id, $taxonomy); usort($terms, create_function('$a,$b', 'return $a->term_taxonomy_id > $b->term_taxonomy_id;')); $is_translated_taxonomy = $sitepress->is_translated_taxonomy($taxonomy); $terms_array = array(); $is_taxonomy_hierarchical = is_taxonomy_hierarchical($taxonomy); foreach ($terms as $term) { if ($is_translated_taxonomy) { $tr_id = icl_object_id($term->term_id, $taxonomy, false, $lang); //If it has a parent if ($is_taxonomy_hierarchical && $term->parent) { //Get its translation $translated_parent = icl_object_id($term->parent, $taxonomy, false, $lang); //If translation does not exists, create one if (!$translated_parent && $sitepress->get_option('sync_taxonomy_parents')) { $parent_term = get_term($term->parent, $taxonomy); if (is_object($parent_term) && !is_wp_error($parent_term)) { $parent_term->name = apply_filters('icl_duplicate_generic_string', $parent_term->name, $lang, array('context' => 'taxonomy', 'attribute' => $taxonomy, 'key' => $parent_term->ID)); $parent_term->slug = apply_filters('icl_duplicate_generic_string', $parent_term->slug, $lang, array('context' => 'taxonomy_slug', 'attribute' => $taxonomy, 'key' => $parent_term->ID)); } $this->create_translated_term($lang, $taxonomy, $parent_term, true); } } if ($tr_id) { // not using get_term - unfiltered get_term $translated_term = $wpdb->get_row($wpdb->prepare("\n SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id = t.term_id WHERE t.term_id = %d AND x.taxonomy = %s", $tr_id, $taxonomy)); if ($is_taxonomy_hierarchical) { $terms_array[] = $translated_term->term_id; } else { $terms_array[] = $translated_term->name; } $tt_id = $sitepress->get_element_trid($translated_term->term_taxonomy_id, 'tax_' . $taxonomy); $sitepress->set_element_language_details($translated_term->term_taxonomy_id, 'tax_' . $taxonomy, $tt_id, $lang, null, false); } else { //Create translated term and it parents, if missing from translations $term->name = apply_filters('icl_duplicate_generic_string', $term->name, $lang, array('context' => 'taxonomy', 'attribute' => $taxonomy, 'key' => $term->term_id)); $term->slug = apply_filters('icl_duplicate_generic_string', $term->slug, $lang, array('context' => 'taxonomy_slug', 'attribute' => $taxonomy, 'key' => $term->term_id)); $translated_term = $this->create_translated_term($lang, $taxonomy, $term, true); if ($translated_term) { if ($is_taxonomy_hierarchical) { $terms_array[] = $translated_term->term_id; } else { $terms_array[] = $translated_term->name; } } } } else { if ($is_taxonomy_hierarchical) { $terms_array[] = $term->term_id; } else { $terms_array[] = $term->name; } } } if ($duplicate_post_id) { wp_set_post_terms($duplicate_post_id, $terms_array, $taxonomy); //Update terms count for terms removed from the duplicated post $all_terms = get_terms($taxonomy); $all_terms_array = false; foreach ($all_terms as $all_term) { if (!in_array($all_term->term_id, $terms_array)) { $all_terms_array[] = $all_term->term_id; } } if ($all_terms_array) { wp_update_term_count($all_terms_array, $taxonomy, false); $sitepress->update_terms_relationship_cache($all_terms_array, $taxonomy); } } } add_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10, 4); add_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1); // Add back the get_term_filter add_filter('get_terms_args', array($sitepress, 'get_terms_args_filter')); return true; }
function check_for_valid_dates_taxo($post_id) { global $wp_locale, $wpdb; $taxonomy = get_object_taxonomies('post'); if (!in_array('calendar', $taxonomy)) { return; } $tt_calendar = array(); foreach ((array) wp_get_object_terms($post_id, 'calendar') as $item) { // term déjà OK if (preg_match('`^[0-9]{4}-[0-9]{2}-[0-9]{2}$`', $item->slug) && !empty($item->description)) { $tt_calendar[] = $item->term_id; continue; } // c'est pas un format normal if (!preg_match('`^[0-9]{2}-[0-9]{2}-[0-9]{4}$`', $item->slug)) { wp_delete_term($item->term_id, 'calendar'); continue; } // reste les nouveaux $new_slug = implode('-', array_reverse(explode('-', $item->slug))); // on vérifi que ça cree pas un doublon $id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} WHERE slug = %s", $new_slug)); if ($id) { wp_delete_term($item->term_id, 'calendar'); $existing_term = get_term($id, 'calendar'); $tt_calendar[] = $existing_term->term_id; continue; } // c'est un vrai nouveau $date = explode('-', $item->slug); $description = gmdate('Y-m-d H:i:s', mktime(0, 0, 0, $date[1], $date[0], $date[2])); $name = absint($date[0]) . ' ' . ucfirst($wp_locale->get_month($date[1])) . ' ' . $date[2]; $return = wp_update_term($item->term_id, 'calendar', array('slug' => $new_slug, 'description' => $description, 'name' => $name)); if (is_wp_error($return)) { die('Erreur :' . $return->get_error_message()); } else { $tt_calendar[] = $return['term_id']; } } wp_set_object_terms($post_id, null, 'calendar'); $tt_calendar = array_unique(array_map('intval', $tt_calendar)); wp_set_object_terms($post_id, $tt_calendar, 'calendar'); $tt_ids = wp_get_object_terms($post_id, 'calendar', array('fields' => 'tt_ids')); wp_update_term_count($tt_ids, 'calendar'); widget_calendar_clear_cache(); }
/** * Enable or disable term counting. * * @since 2.5.0 * * @staticvar bool $_defer * * @param bool $defer Optional. Enable if true, disable if false. * @return bool Whether term counting is enabled or disabled. */ function wp_defer_term_counting($defer = null) { static $_defer = false; if (is_bool($defer)) { $_defer = $defer; // flush any deferred counts if (!$defer) { wp_update_term_count(null, null, true); } } return $_defer; }
/** * Force terms recount for imported taxonomy * * @param $tt_ids array Terms ids * @param $ids array Post ids * @param $tax string Taxonomy name * * @return void * @since 1.0 * @author Antonio La Rocca <*****@*****.**> */ public function recount_terms_post( $tt_ids, $ids, $tax ){ wp_update_term_count( $tt_ids, $tax ); }
case 'link_post_type': $wpdb->update($wpdb->prefix . 'icl_translations', array('element_type' => 'post_' . sanitize_key(filter_input(INPUT_GET, 'new_value'))), array('element_type' => 'post_' . sanitize_key(filter_input(INPUT_GET, 'old_value')))); exit; case 'link_taxonomy': $wpdb->update($wpdb->prefix . 'icl_translations', array('element_type' => 'tax_' . $_GET['new_value']), array('element_type' => 'tax_' . $_GET['old_value'])); exit; case 'icl_fix_terms_count': global $sitepress; remove_filter('get_terms_args', array($sitepress, 'get_terms_args_filter')); remove_filter('get_term', array($sitepress, 'get_term_adjust_id')); remove_filter('terms_clauses', array($sitepress, 'terms_clauses')); foreach (get_taxonomies(array(), 'names') as $taxonomy) { $terms_objects = get_terms($taxonomy, 'hide_empty=0'); if ($terms_objects) { $term_taxonomy_ids = array_map('get_term_taxonomy_id_from_term_object', $terms_objects); wp_update_term_count($term_taxonomy_ids, $taxonomy, true); } } add_filter('terms_clauses', array($sitepress, 'terms_clauses')); add_filter('get_term', array($sitepress, 'get_term_adjust_id')); add_filter('get_terms_args', array($sitepress, 'get_terms_args_filter'), 10, 2); exit; } } /* DEBUG ACTION */ global $sitepress; if (wp_verify_nonce((string) filter_input(INPUT_POST, 'icl_reset_allnonce'), 'icl_reset_all')) { if ($_POST['icl-reset-all'] == 'on') { icl_reset_wpml(); echo '<script type="text/javascript">location.href=\'' . admin_url('plugins.php?deactivate=true') . '\'</script>'; exit;
function wp_defer_term_counting($defer=NULL) { static $_defer = false; if ( is_bool($defer) ) { $_defer = $defer; // flush any deferred counts if ( !$defer ) wp_update_term_count( NULL, NULL, true ); } return $_defer; }
/** * 'admin_init' hook. * * @package webcomic * @since 3 */ public function hook_admin_init() { $this->admin_ajax(); $this->domain(); add_action('admin_footer-post.php', array($this, 'admin_footer_files')); add_action('admin_footer-post-new.php', array($this, 'admin_footer_files')); if (!current_user_can('manage_categories') && (isset($_REQUEST['subpage']) && 'edit_webcomic_collection' == $_REQUEST['subpage'] || !empty($_REQUEST['bulk']) && ($action = !empty($_REQUEST['submit-1']) ? $_REQUEST['action-1'] : $_REQUEST['action-2']) && 'batch_file' == $action)) { wp_die(__('You do not have sufficient permissions to access this page.', 'webcomic')); } if (isset($_REQUEST['post_type'], $_REQUEST['trashed']) && 'webcomic_post' == $_REQUEST['post_type']) { $wc = current(wp_get_object_terms($_REQUEST['ids'], 'webcomic_collection', array('fields' => 'ids'))); wp_redirect(admin_url('admin.php?page=webcomic_files&webcomic_post_trashed=1&webcomic_collection=' . $wc)); die; } $dc = $this->option('default_collection'); if (empty($dc)) { $this->errors['no_default_collection'] = sprintf(__("It looks like you don't have a default webcomic collection set. Please <a href='%s'>set a default webcomic collection</a> now."), 'admin.php?page=webcomic_collection'); } if (isset($_REQUEST['webcomic_post_trashed'])) { $this->update['deleted_post'] = __('Post moved to the trash', 'webcomic'); } if (empty($_REQUEST['action'])) { return false; } if ('add_webcomic_term' == $_REQUEST['action']) { check_admin_referer('add_webcomic_term'); $args = array('slug' => $_REQUEST['webcomic_nicename'], 'parent' => $_REQUEST['webcomic_parent'], 'description' => $_REQUEST['webcomic_description']); if ('webcomic_collection' == $_REQUEST['page']) { unset($args['parent']); } if (is_wp_error($new_term = wp_insert_term(trim($_REQUEST['webcomic_name']), $_REQUEST['page'], $args))) { $this->errors = $new_term->get_error_messages(); } else { $this->update['added_term'] = sprintf(__('Added %s', 'webcomic'), trim($_REQUEST['webcomic_name'])); } } if ('update_webcomic_term' == $_REQUEST['action']) { check_admin_referer('update_webcomic_term'); $args = array('name' => trim($_REQUEST['webcomic_name']), 'slug' => $_REQUEST['webcomic_nicename'], 'description' => $_REQUEST['webcomic_description']); if ('webcomic_collection' != $_REQUEST['page']) { $args['parent'] = $_REQUEST['webcomic_parent']; } else { $args['term_group'] = $_REQUEST['webcomic_group']; } if (is_wp_error($update_term = wp_update_term($_REQUEST['webcomic_term'], $_REQUEST['page'], $args))) { $this->errors = $update_term->get_error_messages(); } else { $this->update['updated_term'] = sprintf(__('Updated %s', 'webcomic'), trim($_REQUEST['webcomic_name'])); } } if ('delete_webcomic_term' == $_REQUEST['action'] && ($old_term = get_term($_REQUEST['webcomic_term'], $_REQUEST['page']))) { check_admin_referer('delete_webcomic_term'); global $wpdb; if ('webcomic_collection' == $old_term->taxonomy) { if ($old_term->webcomic_default) { $this->errors['default_term'] = __('You cannot delete the default collection.', 'webcomic'); return false; } $posts = get_objects_in_term($old_term->term_id, 'webcomic_collection'); } if (is_wp_error($deleted = wp_delete_term($_REQUEST['webcomic_term'], $_REQUEST['page']))) { $this->errors = $deleted->get_error_messages(); } else { if (!empty($posts)) { $cat = (int) get_option('default_category'); $wpdb->query("UPDATE {$wpdb->posts} SET post_type = 'post' WHERE ID IN (" . implode(',', $posts) . ")"); $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN (" . implode(',', $posts) . ") AND meta_key IN ( 'webcomic' ) "); foreach ($posts as $p) { wp_set_object_terms($p, $cat, 'category'); } } $this->update['deleted_term'] = sprintf(__('Deleted %s', 'webcomic'), $old_term->name); } } if ('default_webcomic_term' == $_REQUEST['action'] && ('on' == $_REQUEST['switch'] || 'off' == $_REQUEST['switch']) && ($term = get_term($_REQUEST['webcomic_term'], $_REQUEST['page']))) { check_admin_referer('default_webcomic_term'); $type = end(explode('_', $_REQUEST['page'])); if ('collection' == $type) { $a = __('set as default', 'webcomic'); $this->option('default_collection', $_REQUEST['webcomic_term']); } else { $a = 'on' == $_REQUEST['switch'] ? __('added to defaults', 'webcomic') : __('removed from defaults', 'webcomic'); $term_meta = $this->option('term_meta'); $term_meta[$type][$_REQUEST['webcomic_term']]['default'] = 'on' == $_REQUEST['switch'] ? true : false; $this->option('term_meta', $term_meta); } $this->update['default_term'] = sprintf(__('%s %s', 'webcomic'), $term->name, $a); } if ('move_webcomic_term' == $_REQUEST['action'] && ('up' == $_REQUEST['direction'] || 'dn' == $_REQUEST['direction'])) { check_admin_referer('move_webcomic_term'); $term = get_term($_REQUEST['webcomic_term'], $_REQUEST['page']); $sibs = get_terms($_REQUEST['page'], 'hide_empty=0&webcomic_order=1&term_group=' . $_REQUEST['webcomic_collection'] . '&parent=' . $term->parent); $last = end($sibs); $first = reset($sibs); echo $term->term_id . ' = ' . $last->term_id; if ('up' == $_REQUEST['direction'] && $term->term_id == $first->term_id || 'dn' == $_REQUEST['direction'] && $term->term_id == $last->term_id) { $d = 'up' == $_REQUEST['direction'] ? __('first', 'webcomic') : __('last', 'webcomic'); $this->errors['bad_move'] = sprintf(__('“%s” is already %s in this storyline.', 'webcomic'), $term->name, $d); } elseif ($new_order = 'up' == $_REQUEST['direction'] ? $term->webcomic_order - 1 : $term->webcomic_order + 1) { $type = end(explode('_', $_REQUEST['page'])); $term_meta = $this->option('term_meta'); foreach ($sibs as $sib) { if ($new_order == $sib->webcomic_order) { $term_meta[$type][$sib->term_id]['order'] = 'up' == $_REQUEST['direction'] ? $sib->webcomic_order + 1 : $sib->webcomic_order - 1; $term_meta[$type][$term->term_id]['order'] = $new_order; break; } } $this->option('term_meta', $term_meta); $a = 'up' == $_REQUEST['direction'] ? __('up', 'webcomic') : __('down', 'webcomic'); $this->update['moved_term'] = sprintf(__('%s moved %s', 'webcomic'), $term->name, $a); } } if ('bulk_webcomic_term' == $_REQUEST['action']) { check_admin_referer('bulk_webcomic_term'); if ($_REQUEST['bulk'] && ($action = !empty($_REQUEST['submit-1']) ? $_REQUEST['action-1'] : $_REQUEST['action-2'])) { $type = end(explode('_', $_REQUEST['page'])); if ('regen' == $action) { $i = 0; $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); foreach ($_REQUEST['bulk'] as $bulk) { if ($this->regen($bulk, $type, $wc->slug)) { $i++; } } if ($i) { $this->update['regen_thumbs'] = sprintf(_n('Thumbnails for %d file regenerated', 'Thumbnails for %d files regenerated', $i, 'webcomic'), $i); } } elseif ('add_default' == $action || 'remove_default' == $action) { $i = 0; $a = 'add_default' == $action ? __('added to', 'webcomic') : __('removed from', 'webcomic'); $term_meta = $this->option('term_meta'); foreach ($_REQUEST['bulk'] as $bulk) { if ('add_default' == $action && !$term_meta[$type][$bulk]['default'] || 'remove_default' == $action && $term_meta[$type][$bulk]['default']) { $i++; } $term_meta[$type][$bulk]['default'] = 'add_default' == $action ? true : false; } $this->option('term_meta', $term_meta); if ($i) { $this->update['default_term'] = sprintf(_n('%d term %s defaults', '%d terms %s defaults', $i, 'webcomic'), $i, $a); } } elseif ('delete' == $action) { $i = 0; foreach ($_REQUEST['bulk'] as $bulk) { if ($old_term = get_term($bulk, "webcomic_{$type}")) { if ($old_term->webcomic_default) { continue; } else { if (!is_wp_error($deleted = wp_delete_term($bulk, $_REQUEST['page']))) { $i++; } } } } if ($i) { $this->update['deleted_term'] = sprintf(_n('%d term deleted', '%d terms deleted', $i, 'webcomic'), $i); } else { $this->errors['no_terms'] = sprintf(__('None of the selected terms could be deleted', 'webcomic')); } } } } if ('bind_webcomic_file' == $_REQUEST['action'] || 'unbind_webcomic_file' == $_REQUEST['action'] && isset($_REQUEST['webcomic_key'])) { check_admin_referer('bind_webcomic_file'); $a = 'bind_webcomic_file' == $_REQUEST['action'] ? __('bound to', 'webcomic') : __('unbound from', 'webcomic'); $m = 'bind_webcomic_file' == $_REQUEST['action'] ? true : false; $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); if (!($files = $this->fetch($_REQUEST['post'], 'post', $wc->slug, $m))) { return false; } if ('unbind_webcomic_file' == $_REQUEST['action']) { foreach ($files as $s => $f) { foreach ($f as $k => $v) { if ($k == $_REQUEST['webcomic_key']) { unset($files[$s][$k]); } } } } if ($this->bind($_REQUEST['post'], 'post', $files)) { $this->update['bound_file'] = sprintf(__('Files %s %s', 'webcomic'), $a, get_the_title($_REQUEST['post'])); } } if ('regen_webcomic_file' == $_REQUEST['action'] && isset($_REQUEST['webcomic_key'])) { check_admin_referer('regen_webcomic_file'); $wc = $_REQUEST['webcomic_collection'] ? get_term($_REQUEST['webcomic_collection'], 'webcomic_collection') : get_term($_REQUEST['webcomic_term'], 'webcomic_collection'); if ($_REQUEST['post']) { $id = $_REQUEST['post']; $type = 'post'; $match = true; } elseif ($_REQUEST['orphan']) { $id = $_REQUEST['orphan']; $type = 'orphan'; } if ($files = $this->regen($id, $type, $wc->slug, $_REQUEST['webcomic_key'])) { $this->update['regen_thumbs'] = sprintf(__('Thumbnails regenerated for %s', 'webcomic'), implode(', ', $files)); } } if ('delete_webcomic_file' == $_REQUEST['action'] && isset($_REQUEST['webcomic_key'])) { check_admin_referer('delete_webcomic_file'); $wc = $_REQUEST['webcomic_collection'] ? get_term($_REQUEST['webcomic_collection'], 'webcomic_collection') : get_term($_REQUEST['webcomic_term'], 'webcomic_collection'); if ($_REQUEST['post']) { $id = $_REQUEST['post']; $type = 'post'; } elseif ($_REQUEST['orphan']) { $id = $_REQUEST['orphan']; $type = 'orphan'; } if (is_array($files = $this->delete($id, $type, $wc->slug, $_REQUEST['webcomic_key']))) { $this->errors["no_delete"] = sprintf(__('The following files could not be deleted:<br><br>%s', 'webcomic'), implode('<br>', $files)); } else { $this->update['deleted_file'] = sprintf(__('Deleted %s', 'webcomic'), $files); } } if ('delete_webcomic_post' == $_REQUEST['action']) { check_admin_referer('delete_webcomic_post'); $s = get_post_status($_REQUEST['post']); $a = 'trash' == $s ? __('permanently deleted', 'webcomic') : __('moved to the trash', 'webcomic'); if ('trash' == $s && wp_delete_post($_REQUEST['post'])) { $this->update['deleted_post'] = __('Post permanently deleted', 'webcomic'); } elseif (wp_trash_post($_REQUEST['post'])) { $this->update['deleted_post'] = __('Post moved to the trash', 'webcomic'); } else { $this->errors['no_delete'] = sprintf(__('The post could not be %a', 'webcomic'), $a); } } if ('undelete_webcomic_post' == $_REQUEST['action']) { check_admin_referer('undelete_webcomic_post'); if (wp_untrash_post($_REQUEST['post'])) { $this->update['restored_post'] = __('Post restored', 'webcomic'); } else { $this->errors['no_undelete'] = __('The post could not be restored', 'webcomic'); } } if ('bulk_webcomic_file' == $_REQUEST['action']) { check_admin_referer('bulk_webcomic_file'); $type = $_REQUEST['webcomic_type']; if (!empty($_REQUEST['bulk']) && ($action = !empty($_REQUEST['submit-1']) ? $_REQUEST['action-1'] : $_REQUEST['action-2'])) { if ('regen' == $action) { $i = 0; $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); foreach ($_REQUEST['bulk'] as $bulk) { if ($files = $this->regen($bulk, $type, $wc->slug)) { $i += count($files); } } if ($i) { $this->update['regen_thumbs'] = sprintf(_n('Thumbnails for %d file regenerated', 'Thumbnails for %d files regenerated', $i, 'webcomic'), $i); } } elseif ('bind' == $action || 'unbind' == $action) { $i = 0; $a = 'bind' == $action ? __('bound to') : __('unbound from'); $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); foreach ($_REQUEST['bulk'] as $bulk) { $files = 'bind' == $action ? $this->fetch($bulk, 'post', $wc->slug, true) : array(); if ($this->bind($bulk, $type, $files)) { $i++; } } if ($i) { $this->update['bound_file'] = sprintf(_n('Files %s %d post', 'Files %s %d posts', $i, 'webcomic'), $a, $i); } } elseif ('delete_file' == $action || 'delete_post' == $action || 'delete_filepost' == $action) { $i = $x = 0; $a = 'trash' == get_post_status(current($_REQUEST['bulk'])) ? __('permanently deleted', 'webcomic') : __('moved to the trash', 'webcomic'); $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); $no_delete = array(); foreach ($_REQUEST['bulk'] as $bulk) { if ('delete_file' == $action || 'delete_filepost' == $action) { if (is_array($files = $this->delete($bulk, $type, $wc->slug))) { foreach ($files as $file) { array_push($no_delete, $file); } } elseif ($files) { $i += count(explode(',', $files)); } } if ('delete_post' == $action || 'delete_filepost' == $action) { if ('trash' == get_post_status($bulk)) { if (wp_delete_post($bulk)) { $x++; } } elseif (wp_trash_post($bulk)) { $x++; } } } $output = array(); $output['files'] = $i ? sprintf(_n('%d file deleted', '%d files deleted', $i, 'webcomic'), $i) : sprintf(__('No files deleted', 'webcomic')); $output['posts'] = $x ? sprintf(_n('%d post %s', '%d posts %s', $x, 'webcomic'), $x, $a) : ($output['posts'] = sprintf(__('no posts %s', 'webcomic'), $a)); if ('delete_file' == $action && $i) { $this->update['deleted_files'] = $output['files']; } elseif ('delete_file' == $action) { $this->errors['no_files'] = $output['files']; } elseif ('delete_post' == $action && $x) { $this->update['deleted_posts'] = $output['posts']; } elseif ('delete_post' == $action) { $this->errors['no_posts'] = ucfirst($output['posts']); } elseif (!$i && !$x) { $this->errors['no_filepost'] = implode(' and ', $output); } else { $this->update['deteled_fileposts'] = implode(' and ', $output); } } elseif ('undelete' == $action) { $i = 0; foreach ($_REQUEST['bulk'] as $bulk) { if (wp_untrash_post($bulk)) { $i++; } } if ($i) { $this->update['restored_posts'] = sprintf(_n('%d post restored', '%d posts restored', $i, 'webcomic'), $i); } else { $this->errors['no_posts'] = sprintf(__('No posts restored', 'webcomic')); } } } elseif ('orphan' == $type) { $i = 0; $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); $no_rename = array(); foreach ($_REQUEST['webcomic_filename'] as $k => $v) { if (!$v || $v == $_REQUEST['webcomic_oldname'][$k]) { continue; } if (is_array($names = $this->rename($_REQUEST['webcomic_oldname'][$k] . $_REQUEST['webcomic_extension'][$k], $type, $wc->slug, $v, 0))) { foreach ($names as $name) { array_push($no_rename, $name); } } else { $i++; } } if ($i) { $this->update['renamed_files'] = sprintf(_n('%d file renamed', '%d files renamed', $i, 'webcomic'), $i); } if (!empty($no_rename)) { $this->errors["no_rename"] = sprintf(__('The following files could not be renamed:<br><br>%s', 'webcomic'), implode('<br>', $no_rename)); } } if (isset($_REQUEST['delete_all']) && 'Empty Trash' == $_REQUEST['delete_all']) { global $wpdb; $p = $wpdb->get_col("SELECT * FROM {$wpdb->posts} AS p WHERE p.post_type = 'webcomic_post' AND p.post_status = 'trash'"); foreach ($p as $k) { wp_delete_post($k); } $this->update['empty_trash'] = sprintf(_n('%d post permanently deleted', '%d posts permanently deleted', count($p), 'webcomic'), count($p)); } } if (!empty($_REQUEST['bulk']) && 'batch_webcomic_posts' == $_REQUEST['action']) { check_admin_referer('batch_webcomic_posts'); global $wpdb; $i = 0; $st = 'add' == $_REQUEST['webcomic_post_storylines_action'] ? true : false; $ct = 'add' == $_REQUEST['webcomic_post_characters_action'] ? true : false; $ps = 'publish' == $_REQUEST['webcomic_post_status'] || 'private' == $_REQUEST['webcomic_post_status'] || 'pending' == $_REQUEST['webcomic_post_status'] || 'draft' == $_REQUEST['webcomic_post_status'] ? $_REQUEST['webcomic_post_status'] : false; $ss = 'on' == $_REQUEST['webcomic_post_sticky'] || 'off' == $_REQUEST['webcomic_post_sticky'] ? $_REQUEST['webcomic_post_sticky'] : false; $cs = 'open' == $_REQUEST['webcomic_post_comments'] || 'closed' == $_REQUEST['webcomic_post_comments'] ? $_REQUEST['webcomic_post_comments'] : false; $gs = 'open' == $_REQUEST['webcomic_post_pings'] || 'closed' == $_REQUEST['webcomic_post_pings'] ? $_REQUEST['webcomic_post_pings'] : false; $ts = 'open' == $_REQUEST['webcomic_post_transcripts'] || 'closed' == $_REQUEST['webcomic_post_transcripts'] ? $_REQUEST['webcomic_post_transcripts'] : false; $xs = 'open' == $_REQUEST['webcomic_post_prints'] || 'closed' == $_REQUEST['webcomic_post_prints'] ? $_REQUEST['webcomic_post_prints'] : false; $os = 'open' == $_REQUEST['webcomic_post_originals'] || 'closed' == $_REQUEST['webcomic_post_originals'] ? $_REQUEST['webcomic_post_originals'] : false; if (!empty($_REQUEST['webcomic_post_storylines'])) { foreach ((array) $_REQUEST['webcomic_post_storylines'] as $k => $v) { $_REQUEST['webcomic_post_storylines'][$k] = (int) $v; } } if (!empty($_REQUEST['webcomic_post_characters'])) { foreach ((array) $_REQUEST['webcomic_post_characters'] as $k => $v) { $_REQUEST['webcomic_post_characters'][$k] = (int) $v; } } foreach ($_REQUEST['bulk'] as $bulk) { $i++; if ($ss) { if ('on' == $ss) { stick_post($bulk); } else { unstick_post($bulk); } } if ($ps) { $wpdb->update($wpdb->posts, array('post_status' => $ps), array('ID' => $bulk)); } if ($cs) { $wpdb->update($wpdb->posts, array('comment_status' => $cs), array('ID' => $bulk)); } if ($gs) { $wpdb->update($wpdb->posts, array('ping_status' => $gs), array('ID' => $bulk)); } if ($ts || $xs || $os) { $post_meta = current(get_post_meta($bulk, 'webcomic')); if ($ts) { $post_meta['transcribe_toggle'] = 'open' == $_REQUEST['webcomic_post_transcripts'] ? true : false; } if ($xs) { $post_meta['paypal']['prints'] = 'open' == $_REQUEST['webcomic_post_prints'] ? true : false; } if ($os) { $post_meta['paypal']['original'] = 'open' == $_REQUEST['webcomic_post_originals'] ? true : false; } update_post_meta($bulk, 'webcomic', $post_meta); } $storylines = wp_get_object_terms($bulk, 'webcomic_storyline', array('fields' => 'ids')); $characters = wp_get_object_terms($bulk, 'webcomic_character', array('fields' => 'ids')); wp_update_term_count($storylines, 'webcomic_storyline'); wp_update_term_count($characters, 'webcomic_character'); if ($_REQUEST['webcomic_collection'] != $_REQUEST['webcomic_post_collection']) { $this->bind($bulk, 'post'); wp_delete_object_term_relationships($bulk, array('webcomic_collection', 'webcomic_storyline', 'webcomic_character')); wp_set_object_terms($bulk, (int) $_REQUEST['webcomic_post_collection'], 'webcomic_collection', false); } if (!empty($_REQUEST['webcomic_post_storylines'])) { wp_set_object_terms($bulk, $_REQUEST['webcomic_post_storylines'], 'webcomic_storyline', $st); } elseif (!$st) { wp_delete_object_term_relationships($bulk, array('webcomic_storyline')); } if (!empty($_REQUEST['webcomic_post_characters'])) { wp_set_object_terms($bulk, $_REQUEST['webcomic_post_characters'], 'webcomic_character', $ct); } elseif (!$ct) { wp_delete_object_term_relationships($bulk, array('webcomic_character')); } } if ($i) { $this->update['batched_posts'] = sprintf(_n('%d post updated', '%d posts updated', $i, 'webcomic'), $i); } } if ('batch_webcomic_files' == $_REQUEST['action'] && !empty($_REQUEST['bulk'])) { check_admin_referer('batch_webcomic_files'); $i = 0; $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); $week = array(1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday'); $start = $now = !empty($_REQUEST['webcomic_file_auto']) ? false : strtotime($_REQUEST['aa'] . '-' . $_REQUEST['mm'] . '-' . $_REQUEST['jj']); $status = !empty($_REQUEST['webcomic_file_draft']) ? 'draft' : 'publish'; if ($start) { $today = date('N', $start); switch ($_REQUEST['webcomic_file_interval']) { case 'day': $days = array(1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday'); break; case 'bus': $days = array(1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday'); break; case 'end': $days = array(6 => 'Saturday', 7 => 'Sunday'); break; case 'mwf': $days = array(1 => 'Monday', 3 => 'Wednesday', 5 => 'Friday'); break; case 'trd': $days = array(2 => 'Tuesday', 4 => 'Thursday'); break; case 'week': if ($_REQUEST['days']) { foreach ($_REQUEST['days'] as $d) { $a = explode('/', $d); $days[$a[0]] = $a[1]; } } else { $days[$today] = $week[$today]; } break; } } foreach ($_REQUEST['bulk'] as $bulk) { $info = pathinfo(stripslashes($bulk)); if ($start) { $date = date('Y-m-d H:i:s', $now); $gmt = get_gmt_from_date($date); $today++; while ($today < 9) { $today = 8 == $today ? 1 : $today; if (isset($days[$today])) { $key = $today; break; } $today++; } $now = strtotime('next ' . $days[$key], $now); } else { $match = array(); preg_match('#(\\d\\d\\d\\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])#', $info['filename'], $match); if (empty($match)) { continue; } $date = date('Y-m-d H:i:s', strtotime($match[0])); $gmt = get_gmt_from_date($date); } if (!is_wp_error($new_post = wp_insert_post(array('post_type' => 'webcomic_post', 'post_title' => $info['filename'], 'post_content' => '…', 'post_status' => $status, 'post_date' => $date, 'post_date_gmt' => $gmt)))) { $files = $this->fetch($bulk, 'orphan', $wc->slug); wp_set_object_terms($new_post, (int) $wc->term_id, 'webcomic_collection', false); $this->bind($new_post, 'post', $files); $post_meta = current(get_post_meta($new_post, 'webcomic')); $post_meta['alternate'] = array(); $post_meta['description'] = array(); $post_meta['transcripts'] = array(); $post_meta['transcribe_toggle'] = $this->option('transcribe_toggle'); $post_meta['paypal'] = array('prints' => $wc->webcomic_paypal, 'original' => true, 'price_d' => 0, 'price_i' => 0, 'price_o' => 0, 'shipping_d' => 0, 'shipping_i' => 0, 'shipping_o' => 0); update_post_meta($new_post, 'webcomic', $post_meta); $i++; } } if ($i) { $this->update['batched_files'] = sprintf(_n('Post generated for %d file', 'Posts generated for %d files', $i, 'webcomic'), $i); } elseif (!$t && !$i) { $this->errors['no_publish'] = __('No files could be automatically published', 'webcomic'); } } if ('bulk_webcomic_upload' == $_REQUEST['action']) { check_admin_referer('bulk_webcomic_upload'); $wc = get_term($_REQUEST['webcomic_collection'], 'webcomic_collection'); if (is_array($files = $this->upload($wc->slug, 'bulk'))) { $this->update['upload_success'] = sprintf(_n('%d file uploaded to %s', '%d files uploaded to %s', count($files['full']), 'webcomic'), count($files['full']), $wc->name); $_REQUEST['uploaded_webcomic_files'] = $files['full']; } } if ('edit_webcomic_files' == $_REQUEST['action']) { check_admin_referer('edit_webcomic_files'); if (!empty($_REQUEST['webcomic_delete'])) { foreach ($_REQUEST['webcomic_delete'] as $k => $v) { if ('full' == $k) { continue; } if (is_array($files = $this->delete($_REQUEST['id'], $_REQUEST['type'], $_REQUEST['src'], $_REQUEST['key'], $k))) { $this->errors["no_delete_{$k}"] = sprintf(__('The following files could not be deleted:<br><br>%s', 'webcomic'), implode('<br>', $files)); } else { $this->update["delete_{$k}"] = sprintf(__('Deleted %s size', 'webcomic'), $k); } } } if (is_array($files = $this->upload($_REQUEST['src'], 'singular'))) { $this->bind($_REQUEST['id'], $_REQUEST['type'], $files, true); } $this->update['edit_files'] = sprintf(__('If you uploaded any new files you may need to <a href="%s">go back and manually refresh the page</a> to see them.', 'webcomic'), $_REQUEST['referer']); } if ('convert_webcomic_characters' == $_REQUEST['action']) { check_admin_referer('convert_webcomic_characters'); $_REQUEST['webcomic_converted_terms'] = array(); $_REQUEST['webcomic_unconverted_terms'] = array(); if (!empty($_REQUEST['webcomic_convert_tags'])) { foreach ($_REQUEST['webcomic_convert_tags'] as $tag) { $tag = get_term($tag, 'post_tag'); if (!($character = get_term_by('slug', $tag->slug, 'webcomic_character'))) { if (is_wp_error($new_term = wp_insert_term($tag->name, 'webcomic_character', array('slug' => $tag->slug, 'description' => $tag->description)))) { $_REQUEST['webcomic_unconverted_terms'][] = $tag; } else { $_REQUEST['webcomic_converted_terms']['generated'][] = $new_term['term_id']; } } else { $_REQUEST['webcomic_converted_terms']['existing'][] = $character->term_id; } $id = $character ? $character->term_id : $new_term['term_id']; if ($posts = get_posts('post_type=webcomic_post&numberposts=-1&tag=' . $tag->slug)) { foreach ($posts as $p) { wp_set_object_terms($p->ID, (int) $id, 'webcomic_character', true); } } } } if (!empty($_REQUEST['webcomic_convert_categories'])) { foreach ($_REQUEST['webcomic_convert_categories'] as $category) { $category = get_term($category, 'category'); if (!($character = get_term_by('slug', $category->slug, 'webcomic_character'))) { if (is_wp_error($new_term = wp_insert_term($category->name, 'webcomic_character', array('slug' => $category->slug, 'description' => $category->description)))) { $_REQUEST['webcomic_unconverted_terms'][] = $category; } else { $_REQUEST['webcomic_converted_terms']['generated'][] = $new_term['term_id']; } } else { $_REQUEST['webcomic_converted_terms']['existing'][] = $character->term_id; } $id = $character ? $character->term_id : $new_term['term_id']; if ($posts = get_posts('post_type=webcomic_post&numberposts=-1&category=' . $category->term_id)) { foreach ($posts as $p) { wp_set_object_terms($p->ID, (int) $id, 'webcomic_character', true); } } } } if (!empty($_REQUEST['webcomic_converted_terms']['generated'])) { $c = count($_REQUEST['webcomic_converted_terms']['generated']); $this->update['characters_converted'] = sprintf(_n('%s term converted to a character.', '%s terms converted to characters.', $c, 'webcomic'), $c); } if (!empty($_REQUEST['webcomic_converted_terms']['existing'])) { $c = count($_REQUEST['webcomic_converted_terms']['existing']); $this->update['characters_existing'] = sprintf(_n('%s existing character updated.', '%s existing characters updated.', $c, 'webcomic'), $c); } if (!empty($_REQUEST['webcomic_unconverted_terms'])) { $c = count($_REQUEST['webcomic_unconverted_terms']['tags']) + count($_REQUEST['webcomic_unconverted_terms']['categories']); $this->errors['characters_unconverted'] = sprintf(_n('%s term could not be converted or updated.', '%s terms could not be converted or updated.', $c, 'webcomic'), $c); } } if ('upgrade_legacy_webcomic' == $_REQUEST['action']) { check_admin_referer('upgrade_legacy_webcomic'); $this->upgrade_legacy((int) $_REQUEST['step']); } if ('uninstall_webcomic' == $_REQUEST['action']) { check_admin_referer('uninstall_webcomic'); $this->uninstall(); } if ('webcomic_settings' == $_REQUEST['action']) { check_admin_referer('webcomic_settings'); $a = explode('/', $_REQUEST['transcribe_default']); $def_language = array($a[0] => $a[1]); $new_languages = array(); if (!empty($_REQUEST['transcribe_language'])) { foreach ($_REQUEST['transcribe_language'] as $l) { $a = explode('/', $l); $new_languages[$a[0]] = $a[1]; } } $new_languages = array_merge($new_languages, $def_language); natcasesort($new_languages); $new['version'] = $this->version; $new['default_collection'] = $this->option('default_collection'); $new['integrate_toggle'] = isset($_POST['integrate_toggle']) ? true : false; $new['secure_toggle'] = isset($_POST['secure_toggle']) ? true : false; $new['transcribe_toggle'] = isset($_POST['transcribe_toggle']) ? true : false; $new['transcribe_restrict'] = $_POST['transcribe_restrict']; $new['transcribe_language'] = $new_languages; $new['transcribe_default'] = $def_language; $new['feed_toggle'] = isset($_POST['feed_toggle']) ? true : false; $new['feed_size'] = $_POST['feed_size']; $new['buffer_toggle'] = isset($_POST['buffer_toggle']) ? true : false; $new['buffer_size'] = abs(intval($_POST['buffer_size'])); $new['age_toggle'] = isset($_POST['age_toggle']) ? true : false; $new['age_size'] = abs(intval($_POST['age_size'])); $new['shortcut_toggle'] = isset($_POST['shortcut_toggle']) ? true : false; $new['paypal_business'] = $_POST['paypal_business']; $new['paypal_currency'] = $_POST['paypal_currency']; $new['paypal_log'] = isset($_POST['paypal_log']) ? true : false; $new['paypal_prints'] = isset($_POST['paypal_prints']) ? true : false; $new['paypal_method'] = $_POST['paypal_method']; $new['paypal_price_d'] = round(abs(floatval($_POST['paypal_price_d'])), 2); $new['paypal_price_i'] = round(abs(floatval($_POST['paypal_price_i'])), 2); $new['paypal_price_o'] = round(abs(floatval($_POST['paypal_price_o'])), 2); $new['paypal_shipping_d'] = round(abs(floatval($_POST['paypal_shipping_d'])), 2); $new['paypal_shipping_i'] = round(abs(floatval($_POST['paypal_shipping_i'])), 2); $new['paypal_shipping_o'] = round(abs(floatval($_POST['paypal_shipping_o'])), 2); $new['paypal_donation'] = round(abs(floatval($_POST['paypal_donation'])), 2); $new['large_h'] = abs(intval($_POST['large_h'])); $new['large_w'] = abs(intval($_POST['large_w'])); $new['medium_h'] = abs(intval($_POST['medium_h'])); $new['medium_w'] = abs(intval($_POST['medium_w'])); $new['small_h'] = abs(intval($_POST['small_h'])); $new['small_w'] = abs(intval($_POST['small_w'])); $new['term_meta'] = $this->option('term_meta'); $this->option($new); $this->update['settings'] = __('Settings saved', 'webcomic'); } }
/** * Update the custom taxonomies' term counts when a post's status is changed. * * For example, default posts term counts (for custom taxonomies) don't include * private / draft posts. * * @since 3.3.0 * @access private * * @param string $new_status New post status. * @param string $old_status Old post status. * @param WP_Post $post Post object. */ function _update_term_count_on_transition_post_status($new_status, $old_status, $post) { // Update counts for the post's terms. foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) { $tt_ids = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'tt_ids')); wp_update_term_count($tt_ids, $taxonomy); } }
public function delete_object_terms($object_id, $tt_ids) { global $wpdb; if (is_array($tt_ids) && !empty($tt_ids)) { $taxonomies = array(); foreach ($tt_ids as $tt_id) { $term = get_term_by('term_taxonomy_id', $tt_id); $taxonomies[$term->taxonomy][] = $tt_id; } $in_tt_ids = "'" . implode("', '", $tt_ids) . "'"; /** * Fires immediately before an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action('delete_term_relationships', $object_id, $tt_ids); $deleted = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_tt_ids})", $object_id)); foreach ($taxonomies as $taxonomy => $taxonomy_tt_ids) { wp_cache_delete($object_id, $taxonomy . '_relationships'); /** * Fires immediately after an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action('deleted_term_relationships', $object_id, $taxonomy_tt_ids); wp_update_term_count($taxonomy_tt_ids, $taxonomy); } return (bool) $deleted; } return false; }
/** * Publish a post by transitioning the post status. * * @since 2.1.0 * @uses $wpdb * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data. * * @param int $post_id Post ID. * @return null */ function wp_publish_post($post_id) { global $wpdb; $post = get_post($post_id); if (empty($post)) { return; } if ('publish' == $post->post_status) { return; } $wpdb->update($wpdb->posts, array('post_status' => 'publish'), array('ID' => $post_id)); $old_status = $post->post_status; $post->post_status = 'publish'; wp_transition_post_status('publish', $old_status, $post); // Update counts for the post's terms. foreach ((array) get_object_taxonomies('post') as $taxonomy) { $tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids')); wp_update_term_count($tt_ids, $taxonomy); } do_action('edit_post', $post_id, $post); do_action('save_post', $post_id, $post); do_action('wp_insert_post', $post_id, $post); }
/** * Wrapper for \wp_update_term_count * * @param int[] $terms given by their term_taxonomy_ids * @param string $taxonomy * @param bool|false $do_deferred * * @return bool */ function wp_update_term_count($terms, $taxonomy, $do_deferred = false) { return wp_update_term_count($terms, $taxonomy, $do_deferred); }