/**
 * get popular term of each post
 *
 * @since 1.1
 *
 */
function stt2extat_popular_each_post_id($args = array())
{
    $data_query = stt2extat_data_query($args);
    $list = array_column($data_query, 'post_id');
    $list = array_values(array_unique($list));
    $data = array();
    $count = count($list);
    for ($i = 0; $i < $count; $i++) {
        $post_meta = stt2extat_get_post_terms($list[$i]);
        $maxvalue = 0;
        while (list($key, $value) = each($post_meta)) {
            if ($value[1] > $maxvalue) {
                $maxvalue = $value[1];
                $maxindex = $key;
            }
        }
        $data[$maxvalue] = $data_query[$maxindex];
    }
    krsort($data);
    return $data;
}
 /**
  * fire to delete unused terms
  * argument was set on plugin panel
  *
  * @since 1.1
  *
  */
 public function delete_unused_terms()
 {
     global $stt2extat_settings;
     $set = $stt2extat_settings;
     $args = array('date_query' => array('before' => intval(-$set['schedule']['post_modified']) . ' day'), 'schedule' => array('count' => absint($set['schedule']['count'])));
     $data = stt2extat_data_query($args);
     if (0 == count($data)) {
         exit;
     }
     $list = array_column($data, 'post_id');
     $list = array_unique($list);
     $func = function ($terms, $list, $meta_key) {
         $update = array();
         foreach ($list as $k) {
             $prev_value = stt2extat_get_post_terms($k);
             if (empty($prev_value)) {
                 delete_post_meta($k, $meta_key);
                 continue;
             }
             $meta_value = array();
             foreach ($prev_value as $key => $val) {
                 if (isset($terms[$key])) {
                     continue;
                 }
                 $meta_value[$key] = $val;
             }
             if (array_filter($meta_value)) {
                 $update['update'][] = update_post_meta($k, $meta_key, $meta_value);
             } else {
                 $update['delete'][] = delete_post_meta($k, $meta_key);
             }
         }
         return $update;
     };
     $success = call_user_func_array($func, array($data, $list, '_stt2extat'));
     if (isset($success['update'])) {
         echo 'Update ' . count($success['update']) . ' postmeta.<br />';
     }
     if (isset($success['delete'])) {
         echo 'Delete ' . count($success['delete']) . ' postmeta.';
     }
     exit;
 }
Ejemplo n.º 3
0
/**
 * give notice on edited terms
 *
 * @since 1.0
 *
*/
function stt2extat_edit_term_notice($code, $error, $add_setting_error)
{
    if (isset($_GET['term_ID']) && '' == stt2extat_get_post_terms(absint($_GET['post_ID']))) {
        return;
    }
    if (false == $code) {
        $code = 5;
    }
    $code = absint($code);
    $messages['searchterms'] = array(0 => '', 1 => __('Term added.'), 2 => __('Term deleted.'), 3 => __('Term updated.'), 4 => __('Term not added.'), 5 => __('Term not updated.'), 6 => __('Term empty.', 'stt2extat'), 7 => __('Term was exists.', 'stt2extat'), 8 => __('Terms deleted.'), 9 => __('You do not have permission to do that.'), 10 => __('You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?.'), 11 => __('Term was deleted.'), 12 => __('Term too short ( min 4 characters ).', 'stt2extat'), 13 => __('Term too long ( max 70 characters ).', 'stt2extat'), 14 => __('An unidentified error has occurred.', 'stt2extat'), 15 => __('Terms migrated.', 'stt2extat'));
    if (wp_validate_boolean($add_setting_error)) {
        if (isset($messages['searchterms'][$code])) {
            return $messages['searchterms'][$code];
        }
        return $messages['searchterms'][1];
    }
    $class = 'updated';
    if (wp_validate_boolean($error)) {
        $class = 'error';
    }
    printf('<div id="message" class="%1$s notice is-dismissible"><p>%2$s</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">%3$s.</span></button></div>', sanitize_html_class($class), esc_html($messages['searchterms'][$code]), __('Dismiss this notice', 'stt2extat'));
    if (defined('DOING_AJAX') && DOING_AJAX) {
        wp_die();
    }
}