コード例 #1
0
function crb_wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug)
{
    global $wpdb;
    $check_sql = "SELECT post_name FROM {$wpdb->posts}\n\t\tWHERE post_type IN ('post_type_name', 'page')\n\t\tAND post_name = %s\n\t\tAND post_parent = %d\n\t\tAND ID != %d\n\t\tLIMIT 1";
    $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_parent, $post_ID));
    if (!$post_name_check) {
        return $slug;
    }
    $suffix = 2;
    do {
        $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-" . $suffix;
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_parent, $post_ID));
        $suffix++;
    } while ($post_name_check);
    $slug = $alt_post_name;
    return $slug;
}
コード例 #2
0
 function validate()
 {
     global $wpdb, $wp_rewrite;
     $slug = $this->value;
     $feeds = $wp_rewrite->feeds;
     if (!is_array($feeds)) {
         $feeds = array();
     }
     // Post slugs must be unique across all posts.
     $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s LIMIT 1";
     $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug));
     /**
      * Filter whether the post slug would be bad as a flat slug.
      *
      * @since 3.1.0
      *
      * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
      * @param string $slug      The post slug.
      * @param string $post_type Post type.
      */
     if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
         $suffix = 2;
         do {
             $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
             $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name));
             $suffix++;
         } while ($post_name_check);
         $slug = $alt_post_name;
         $this->value = isset($this->current) ? $this->current : '';
         $this->field['msg'] = sprintf($this->field['msg'], $slug);
         $this->error = $this->field;
     } else {
         if (isset($this->field['flush_permalinks']) && $this->field['flush_permalinks'] == true) {
             add_action('init', array($this, 'flush_permalinks'), 99);
         }
     }
 }
コード例 #3
0
ファイル: post.php プロジェクト: SayenkoDesign/ividf
/**
 * Computes a unique slug for the post, when given the desired slug and some post details.
 *
 * @since 2.8.0
 *
 * @global wpdb       $wpdb WordPress database abstraction object.
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $slug        The desired slug (post_name).
 * @param int    $post_ID     Post ID.
 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
 * @param string $post_type   Post type.
 * @param int    $post_parent Post parent ID.
 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
 */
function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent)
{
    if (in_array($post_status, array('draft', 'pending', 'auto-draft')) || 'inherit' == $post_status && 'revision' == $post_type) {
        return $slug;
    }
    global $wpdb, $wp_rewrite;
    $original_slug = $slug;
    $feeds = $wp_rewrite->feeds;
    if (!is_array($feeds)) {
        $feeds = array();
    }
    if ('attachment' == $post_type) {
        // Attachment slugs must be unique across all types.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));
        /**
         * Filter whether the post slug would make a bad attachment slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug Whether the slug would be bad as an attachment slug.
         * @param string $slug     The post slug.
         */
        if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } elseif (is_post_type_hierarchical($post_type)) {
        if ('nav_menu_item' == $post_type) {
            return $slug;
        }
        /*
         * Page slugs must be unique within their own trees. Pages are in a separate
         * namespace than posts so page slugs are allowed to overlap post slugs.
         */
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID, $post_parent));
        /**
         * Filter whether the post slug would make a bad hierarchical post slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug    Whether the post slug would be bad in a hierarchical post context.
         * @param string $slug        The post slug.
         * @param string $post_type   Post type.
         * @param int    $post_parent Post parent ID.
         */
        if ($post_name_check || in_array($slug, $feeds) || preg_match("@^({$wp_rewrite->pagination_base})?\\d+\$@", $slug) || apply_filters('wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } else {
        // Post slugs must be unique across all posts.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID));
        // Prevent new post slugs that could result in URLs that conflict with date archives.
        $post = get_post($post_ID);
        $conflicts_with_date_archive = false;
        if ('post' === $post_type && (!$post || $post->post_name !== $slug) && preg_match('/^[0-9]+$/', $slug) && ($slug_num = intval($slug))) {
            $permastructs = array_values(array_filter(explode('/', get_option('permalink_structure'))));
            $postname_index = array_search('%postname%', $permastructs);
            /*
             * Potential date clashes are as follows:
             *
             * - Any integer in the first permastruct position could be a year.
             * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
             * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
             */
            if (0 === $postname_index || $postname_index && '%year%' === $permastructs[$postname_index - 1] && 13 > $slug_num || $postname_index && '%monthnum%' === $permastructs[$postname_index - 1] && 32 > $slug_num) {
                $conflicts_with_date_archive = true;
            }
        }
        /**
         * Filter whether the post slug would be bad as a flat slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
         * @param string $slug      The post slug.
         * @param string $post_type Post type.
         */
        if ($post_name_check || in_array($slug, $feeds) || $conflicts_with_date_archive || apply_filters('wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }
    /**
     * Filter the unique post slug.
     *
     * @since 3.3.0
     *
     * @param string $slug          The post slug.
     * @param int    $post_ID       Post ID.
     * @param string $post_status   The post status.
     * @param string $post_type     Post type.
     * @param int    $post_parent   Post parent ID
     * @param string $original_slug The original post slug.
     */
    return apply_filters('wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug);
}
コード例 #4
0
ファイル: post.php プロジェクト: venizeng/pintimes-wordpress
/**
 * Computes a unique slug for the post, when given the desired slug and some post details.
 *
 * @since 2.8.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $slug        The desired slug (post_name).
 * @param int    $post_ID     Post ID.
 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
 * @param string $post_type   Post type.
 * @param int    $post_parent Post parent ID.
 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
 */
function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent)
{
    if (in_array($post_status, array('draft', 'pending', 'auto-draft')) || 'inherit' == $post_status && 'revision' == $post_type) {
        return $slug;
    }
    global $wpdb, $wp_rewrite;
    $original_slug = $slug;
    $feeds = $wp_rewrite->feeds;
    if (!is_array($feeds)) {
        $feeds = array();
    }
    if ('attachment' == $post_type) {
        // Attachment slugs must be unique across all types.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));
        /**
         * Filter whether the post slug would make a bad attachment slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug Whether the slug would be bad as an attachment slug.
         * @param string $slug     The post slug.
         */
        if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } elseif (is_post_type_hierarchical($post_type)) {
        if ('nav_menu_item' == $post_type) {
            return $slug;
        }
        /*
         * Page slugs must be unique within their own trees. Pages are in a separate
         * namespace than posts so page slugs are allowed to overlap post slugs.
         */
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID, $post_parent));
        /**
         * Filter whether the post slug would make a bad hierarchical post slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug    Whether the post slug would be bad in a hierarchical post context.
         * @param string $slug        The post slug.
         * @param string $post_type   Post type.
         * @param int    $post_parent Post parent ID.
         */
        if ($post_name_check || in_array($slug, $feeds) || preg_match("@^({$wp_rewrite->pagination_base})?\\d+\$@", $slug) || apply_filters('wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } else {
        // Post slugs must be unique across all posts.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID));
        /**
         * Filter whether the post slug would be bad as a flat slug.
         *
         * @since 3.1.0
         *
         * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
         * @param string $slug      The post slug.
         * @param string $post_type Post type.
         */
        if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }
    /**
     * Filter the unique post slug.
     *
     * @since 3.3.0
     *
     * @param string $slug          The post slug.
     * @param int    $post_ID       Post ID.
     * @param string $post_status   The post status.
     * @param string $post_type     Post type.
     * @param int    $post_parent   Post parent ID
     * @param string $original_slug The original post slug.
     */
    return apply_filters('wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug);
}
コード例 #5
0
/**
 * Update term slug.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global string $table_prefix WordPress Database Table prefix.
 * @param int|string $term_id The term ID.
 * @param int $tt_id term Taxonomy ID.
 * @param string $taxonomy Taxonomy slug.
 */
function geodir_update_term_slug($term_id, $tt_id, $taxonomy)
{
    global $wpdb, $plugin_prefix, $table_prefix;
    $tern_data = get_term_by('id', $term_id, $taxonomy);
    $slug = $tern_data->slug;
    /**
     * Filter if a term slug exists.
     *
     * @since 1.0.0
     * @package GeoDirectory
     * @param bool $bool Default: false.
     * @param string $slug The term slug.
     * @param int $term_id The term ID.
     */
    $slug_exists = apply_filters('geodir_term_slug_is_exists', false, $slug, $term_id);
    if ($slug_exists) {
        $suffix = 1;
        do {
            $new_slug = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
            /** This action is documented in geodirectory_hooks_actions.php */
            $term_slug_check = apply_filters('geodir_term_slug_is_exists', false, $new_slug, $term_id);
            $suffix++;
        } while ($term_slug_check && $suffix < 100);
        $slug = $new_slug;
        //wp_update_term( $term_id, $taxonomy, array('slug' => $slug) );
        $wpdb->query($wpdb->prepare("UPDATE " . $table_prefix . "terms SET slug=%s WHERE term_id=%d", array($slug, $term_id)));
    }
}
コード例 #6
0
ファイル: post.php プロジェクト: akalipetis/WordPress
/**
 * For a given post, add a trashed suffix.
 *
 * Store its desired (i.e. current) slug so it can try to reclaim it
 * if the post is untrashed.
 *
 * For internal use.
 *
 * @since 4.5.0
 *
 * @param WP_Post $post The post.
 */
function wp_add_trashed_suffix_to_post_name_for_post($post)
{
    global $wpdb;
    $post = get_post($post);
    if (strpos($post->post_name, '-%trashed%')) {
        return $post->post_name;
    }
    add_post_meta($post->ID, '_wp_desired_post_slug', $post->post_name);
    $post_name = _truncate_post_slug($post->post_name, 190) . '-%trashed%';
    $wpdb->update($wpdb->posts, array('post_name' => $post_name), array('ID' => $post->ID));
    clean_post_cache($post->ID);
    return $post_name;
}
コード例 #7
0
function qtranxf_slug_unique($slug, $lang, $name)
{
    global $wpdb;
    //$wpdb->show_errors(); @set_time_limit(0);
    $slug = sanitize_key($slug);
    $check_sql = 'SELECT name FROM ' . $wpdb->prefix . 'i18n_slugs WHERE slug = %s AND (lang != %s OR name != %s)';
    $query = $wpdb->prepare($check_sql, $slug, $lang, $name);
    $post_name_check = $wpdb->get_var($query);
    //qtranxf_dbg_log('qtranxf_slug_unique: $slug="'.$slug.'"; $lang="'.$lang.'"; $name='.$name.'; $post_name_check: ', $post_name_check);
    if (!$post_name_check) {
        return $slug;
    }
    $sfx = '-' . $lang;
    if (substr($slug, -3) != $sfx) {
        $alt_post_name = _truncate_post_slug($slug, 200 - 3) . $sfx;
        //qtranxf_dbg_log('qtranxf_slug_unique: $slug="'.$alt_post_name.'"; $lang="'.$lang.'"; $name='.$name.'; $post_name_check: ', $post_name_check);
        $query = $wpdb->prepare($check_sql, $alt_post_name, $lang, $name);
        $post_name_check = $wpdb->get_var($query);
        if (!$post_name_check) {
            return $alt_post_name;
        }
    }
    $suffix = 2;
    do {
        $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . '-' . $suffix;
        $query = $wpdb->prepare($check_sql, $alt_post_name, $lang, $name);
        $post_name_check = $wpdb->get_var($query);
        //qtranxf_dbg_log('qtranxf_slug_unique: $slug="'.$alt_post_name.'"; $lang="'.$lang.'"; $name='.$name.'; $post_name_check: ', $post_name_check);
        $suffix++;
    } while ($post_name_check);
    return $alt_post_name;
}
コード例 #8
0
/**
 * Computes a unique slug for the post, when given the desired slug and some post details.
 *
 * @since 2.8.0
 *
 * @global wpdb $wpdb
 * @global WP_Rewrite $wp_rewrite
 * @param string $slug the desired slug (post_name)
 * @param integer $post_ID
 * @param string $post_status no uniqueness checks are made if the post is still draft or pending
 * @param string $post_type
 * @param integer $post_parent
 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
 */
function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent)
{
    if (in_array($post_status, array('draft', 'pending', 'auto-draft')) || 'inherit' == $post_status && 'revision' == $post_type) {
        return $slug;
    }
    global $wpdb, $wp_rewrite;
    $original_slug = $slug;
    $feeds = $wp_rewrite->feeds;
    if (!is_array($feeds)) {
        $feeds = array();
    }
    $hierarchical_post_types = get_post_types(array('hierarchical' => true));
    if ('attachment' == $post_type) {
        // Attachment slugs must be unique across all types.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));
        if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } elseif (in_array($post_type, $hierarchical_post_types)) {
        if ('nav_menu_item' == $post_type) {
            return $slug;
        }
        // Page slugs must be unique within their own trees. Pages are in a separate
        // namespace than posts so page slugs are allowed to overlap post slugs.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type IN ( '" . implode("', '", esc_sql($hierarchical_post_types)) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID, $post_parent));
        if ($post_name_check || in_array($slug, $feeds) || preg_match("@^({$wp_rewrite->pagination_base})?\\d+\$@", $slug) || apply_filters('wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_ID, $post_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    } else {
        // Post slugs must be unique across all posts.
        $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID));
        if ($post_name_check || in_array($slug, $feeds) || apply_filters('wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type)) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }
    return apply_filters('wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug);
}
 public static function prevent_slug_conflict($slug)
 {
     global $wpdb;
     // slugs must be unique within their own trees
     $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_parent = 0 LIMIT 1";
     $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug));
     if ($slug_check) {
         $suffix = 2;
         do {
             $alt_slug = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
             $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_slug));
             $suffix++;
         } while ($slug_check);
         $slug = $alt_slug;
     }
     return $slug;
 }
コード例 #10
0
 function save_status()
 {
     global $wpdb;
     $table = $wpdb->prefix . "wc_crm_statuses";
     extract($_POST);
     if (!isset($status_slug) || empty($status_slug)) {
         $status_slug = sanitize_title($status_name);
     } else {
         $status_slug = sanitize_title($status_slug);
     }
     $filter = '';
     if (isset($status_id) && !empty($status_id)) {
         $filter = " AND status_id != {$status_id}";
     }
     $check_sql = "SELECT status_slug FROM {$table} WHERE status_slug = '%s' {$filter} LIMIT 1";
     $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $status_slug));
     if ($slug_check) {
         $suffix = 2;
         do {
             $alt_slug = _truncate_post_slug($status_slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
             $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_slug));
             $suffix++;
         } while ($slug_check);
         $status_slug = $alt_slug;
     }
     $data = array('status_name' => $status_name, 'status_slug' => $status_slug, 'status_icon' => $status_icon, 'status_colour' => $status_colour);
     if (isset($status_id) && !empty($status_id)) {
         $wpdb->update($table, $data, array('status_id' => $status_id));
     } else {
         $wpdb->insert($table, $data);
     }
 }
コード例 #11
0
/**
 * Update term slug.
 *
 * @since 1.0.0
 * @since 1.5.3 Modified to update tag in detail table when tag updated.
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global string $table_prefix WordPress Database Table prefix.
 * @param int|string $term_id The term ID.
 * @param int $tt_id term Taxonomy ID.
 * @param string $taxonomy Taxonomy slug.
 */
function geodir_update_term_slug($term_id, $tt_id, $taxonomy)
{
    global $wpdb, $plugin_prefix, $table_prefix;
    $tern_data = get_term_by('id', $term_id, $taxonomy);
    $slug = $tern_data->slug;
    /**
     * Filter if a term slug exists.
     *
     * @since 1.0.0
     * @package GeoDirectory
     * @param bool $bool Default: false.
     * @param string $slug The term slug.
     * @param int $term_id The term ID.
     */
    $slug_exists = apply_filters('geodir_term_slug_is_exists', false, $slug, $term_id);
    if ($slug_exists) {
        $suffix = 1;
        do {
            $new_slug = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
            /** This action is documented in geodirectory_hooks_actions.php */
            $term_slug_check = apply_filters('geodir_term_slug_is_exists', false, $new_slug, $term_id);
            $suffix++;
        } while ($term_slug_check && $suffix < 100);
        $slug = $new_slug;
        //wp_update_term( $term_id, $taxonomy, array('slug' => $slug) );
        $wpdb->query($wpdb->prepare("UPDATE " . $table_prefix . "terms SET slug=%s WHERE term_id=%d", array($slug, $term_id)));
    }
    // Update tag in detail table.
    $taxonomy_obj = get_taxonomy($taxonomy);
    $post_type = !empty($taxonomy_obj) ? $taxonomy_obj->object_type[0] : NULL;
    $post_types = geodir_get_posttypes();
    if ($post_type && in_array($post_type, $post_types) && $post_type . '_tags' == $taxonomy) {
        $posts_obj = $wpdb->get_results($wpdb->prepare("SELECT object_id FROM " . $wpdb->term_relationships . " WHERE term_taxonomy_id = %d", array($tt_id)));
        if (!empty($posts_obj)) {
            foreach ($posts_obj as $post_obj) {
                $post_id = $post_obj->object_id;
                $raw_tags = wp_get_object_terms($post_id, $post_type . '_tags', array('fields' => 'names'));
                $post_tags = !empty($raw_tags) ? implode(',', $raw_tags) : '';
                $listing_table = $plugin_prefix . $post_type . '_detail';
                $wpdb->query($wpdb->prepare("UPDATE " . $listing_table . " SET post_tags=%s WHERE post_id =%d", array($post_tags, $post_id)));
            }
        }
    }
}
コード例 #12
0
 function add_customer_status($status)
 {
     global $wpdb;
     $table = $wpdb->prefix . "wc_crm_statuses";
     $status_name = $status;
     $status_slug = sanitize_title($status);
     $check_sql = "SELECT status_slug FROM {$table} WHERE status_slug = '%s' LIMIT 1";
     $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $status_slug));
     if ($slug_check) {
         $suffix = 2;
         do {
             $alt_slug = _truncate_post_slug($status_slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
             $slug_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_slug));
             $suffix++;
         } while ($slug_check);
         $status_slug = $alt_slug;
     }
     $data = array('status_name' => $status_name, 'status_slug' => $status_slug, 'status_icon' => '57545', 'status_colour' => '#8224e3');
     $wpdb->insert($table, $data);
     $this->statuses_added[] = $status_name;
     return $status_slug;
 }
コード例 #13
0
 public static function create_user()
 {
     if (empty($_POST['user_email'])) {
         wc_crm_add_notice(__('Please enter an e-mail address.', 'wc_crm'), 'error');
     } elseif (!is_email($_POST['user_email'])) {
         wc_crm_add_notice(__("The email address isn't correct.", 'wc_crm'), 'error');
     } elseif (email_exists($_POST['user_email'])) {
         wc_crm_add_notice(__("This email is already registered, please choose another one.", 'wc_crm'), 'error');
     }
     if (wc_crm_notice_count('error') > 0) {
         return;
     }
     global $wpdb;
     $nickname = str_replace(' ', '', ucfirst(strtolower($_POST['first_name']))) . str_replace(' ', '', ucfirst(strtolower($_POST['last_name'])));
     $username_opt = get_option('wc_crm_username_add_customer');
     switch ($username_opt) {
         case 2:
             $username = str_replace(' ', '', strtolower($_POST['first_name'])) . '-' . str_replace(' ', '', strtolower($_POST['last_name']));
             break;
         case 3:
             $username = $_POST['user_email'];
             break;
         default:
             $username = strtolower($nickname);
             break;
     }
     $username = _truncate_post_slug($username, 60);
     $check_sql = "SELECT user_login FROM {$wpdb->users} WHERE user_login = '******' LIMIT 1";
     $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $username));
     if ($user_name_check) {
         $suffix = 1;
         do {
             $alt_user_name = _truncate_post_slug($username, 60 - (strlen($suffix) + 1)) . "-{$suffix}";
             $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_user_name));
             $suffix++;
         } while ($user_name_check);
         $username = $alt_user_name;
     }
     add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
     $user_id = wc_create_new_customer($_POST['user_email'], $username);
     remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
     do_action('wc_crm_create_customer', $user_id);
     if (!is_wp_error($user_id)) {
         update_user_meta($user_id, 'nickname', $nickname);
         wp_update_user(array('ID' => $user_id, 'role' => 'customer'));
         $customer_id = $wpdb->get_var("SELECT c_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE user_id = {$user_id} ");
         if ($customer_id) {
             WC_CRM_Screen_Customers_Edit::save($customer_id, true);
         }
         wc_crm_add_notice(__("Customer created.", 'wc_crm'), 'success');
         wp_safe_redirect(admin_url() . 'admin.php?page=' . WC_CRM_TOKEN);
     } else {
         wc_crm_add_notice($user_id->get_error_message(), 'error');
     }
 }
コード例 #14
0
 function create_user()
 {
     global $wpdb;
     extract($_POST);
     $user_email = trim($user_email);
     if (empty($user_email)) {
         $this->error[] = __('<p><strong>ERROR</strong>: The email address isn’t correct.</p>');
     } else {
         if (!email_exists($user_email)) {
             //$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
             $nickname = str_replace(' ', '', ucfirst(strtolower($_POST['first_name']))) . str_replace(' ', '', ucfirst(strtolower($_POST['last_name'])));
             $username_opt = get_option('woocommerce_crm_username_add_customer');
             switch ($username_opt) {
                 case 2:
                     $username = str_replace(' ', '', strtolower($_POST['first_name'])) . '-' . str_replace(' ', '', strtolower($_POST['last_name']));
                     break;
                 case 3:
                     $username = $user_email;
                     break;
                 default:
                     $username = strtolower($nickname);
                     break;
             }
             $username = _truncate_post_slug($username, 60);
             $check_sql = "SELECT user_login FROM {$wpdb->users} WHERE user_login = '******' LIMIT 1";
             $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $username));
             if ($user_name_check) {
                 $suffix = 1;
                 do {
                     $alt_user_name = _truncate_post_slug($username, 60 - (strlen($suffix) + 1)) . "-{$suffix}";
                     $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_user_name));
                     $suffix++;
                 } while ($user_name_check);
                 $username = $alt_user_name;
             }
             add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
             $user_id = wc_create_new_customer($user_email, $username);
             remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
             do_action('wc_crm_create_customer', $user_id);
             $this->save($user_id, true);
             if (!is_wp_error($user_id)) {
                 update_user_meta($user_id, 'nickname', $nickname);
                 wp_update_user(array('ID' => $user_id, 'role' => 'customer'));
             }
         } else {
             $errors = new WP_Error();
             $errors->add('invalid_email', __('<strong>ERROR</strong>: User already exists.'), array('form-field' => 'user_email'));
             $_SESSION['customer_save_errors'] = $errors;
         }
     }
 }
コード例 #15
0
            unset($query_args['pagename']);
        }
        $suffix = 2;
        $alt_post_name = $slug;
        // Search for a good slug, adding an appended numeric string if necessary.
        while (true) {
            $query = new WP_Query($query_args);
            $use_slug = true;
            // If there's a post at this URL already, bail.
            if ($query->have_posts()) {
                $use_slug = false;
            }
            // Avoid collision with `tag/tagname` even if there are no posts in the taxonomy.
            if ($query->get_queried_object()) {
                $use_slug = false;
            }
            if ($post_type === 'attachment' && apply_filters('wp_unique_post_slug_is_bad_attachment_slug', false, $slug)) {
                $use_slug = false;
            }
            if ($use_slug) {
                break;
            } else {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-{$suffix}";
                $query_args['name'] = $alt_post_name;
                $suffix++;
            }
        }
        $slug = $alt_post_name;
    }
    return $slug;
}, 10, 6);
コード例 #16
0
 /**
  * Truncate the order status slug to a maximum of 17 characters
  *
  * @since 1.1.1
  * @param string $slug          The post slug.
  * @param int    $post_ID       Post ID.
  * @param string $post_status   The post status.
  * @param string $post_type     Post type.
  */
 public function truncate_order_status_slug($slug, $post_ID, $post_status, $post_type)
 {
     $max_slug_length = 17;
     if ('wc_order_status' !== $post_type) {
         return $slug;
     }
     if (strlen($slug) <= $max_slug_length) {
         return $slug;
     }
     $slug = _truncate_post_slug($slug, $max_slug_length);
     // The following was borrowed from WP core function wp_unique_post_slug()
     global $wpdb;
     // Post slugs must be unique across all posts.
     $check_sql = "SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
     $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID));
     if ($post_name_check) {
         $suffix = 2;
         do {
             $alt_post_name = _truncate_post_slug($slug, $max_slug_length - (strlen($suffix) + 1)) . "-{$suffix}";
             $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID));
             $suffix++;
         } while ($post_name_check);
         $slug = $alt_post_name;
     }
     return $slug;
 }