array( 'name' => __('Products', 'aliprice'), 'singular_name' => __('Item', 'aliprice'), 'add_new' => __('Add new', 'aliprice'), 'add_new_item' => __('Add a new Item', 'aliprice'), 'edit_item' => __('Edit Item', 'aliprice'), 'new_item' => __('New Item', 'aliprice'), 'all_items' => __('All Items', 'aliprice'), 'view_item' => __('View', 'aliprice'), 'search_items' => __('Search', 'aliprice'), 'not_found' => __('Products not found', 'aliprice'), 'not_found_in_trash' => __('Trash is empty', 'aliprice'), 'parent_item_colon' => '', 'menu_name' => __('Products', 'aliprice')), 'singular_label' => __('Item', 'aliprice'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'products', 'with_front' => false), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_icon' => plugins_url( 'aliprice/img/shop.png'), 'supports' => array('title', 'editor', 'thumbnail', 'comments'), ); register_post_type( 'products' , $args ); register_taxonomy( 'shopcategory', array( 'products' ), array ( 'labels' => array( 'name' => __('Categories', 'aliprice'), 'singular_name' => __('Category', 'aliprice'), 'search_items' => __('Search', 'aliprice'), 'all_items' => __('All categories', 'aliprice'), 'parent_item' => __('Parent category', 'aliprice'), 'parent_item_colon' => __('Parent category: ', 'aliprice'), 'edit_item' => __('Edit category', 'aliprice'), 'update_item' => __('Update category', 'aliprice'), 'add_new_item' => __('Add a new category', 'aliprice'), 'new_item_name' => __('The new name of the category', 'aliprice'), 'menu_name' => __('Categories', 'aliprice') ), 'public' => true, 'show_in_nav_menus' => true, 'hierarchical' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'shopcategory' ), ) ); } //fix a bag in custom terms from WP add_action( 'init', 'aliprice_fix_terms_bug' ); function aliprice_fix_terms_bug( ) { if( is_admin() ) { $val = get_site_option("shopcategory_children"); if( $val && !empty($val) ) delete_option("shopcategory_children"); } } /** * add filter to ensure the text Product, or product, is displayed when user updates a product */ add_filter( 'post_updated_messages', 'aliprice_updated_messages' ); function aliprice_updated_messages( $messages ) { global $post, $post_ID; $messages['products'] = array( 0 => '', 1 => sprintf( __('Product updated. View product', 'aliprice'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.', 'aliprice'), 3 => __('Custom field deleted.', 'aliprice'), 4 => __('Product updated.', 'aliprice'), 5 => isset($_GET['revision']) ? sprintf( __('Product restored to revision from %s', 'aliprice'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Product published. View product', 'aliprice'), esc_url( get_permalink($post_ID) ) ), 7 => __('Product saved.', 'aliprice'), 8 => sprintf( __('Product submitted. Preview product', 'aliprice'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Product scheduled for: %1$s. Preview product', 'aliprice'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Product draft updated. Preview product', 'aliprice'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } function aliprice_add_meta_box() { $post_types = get_post_types( array( 'public' => true ) ); if ( is_array( $post_types ) ) { foreach ( $post_types as $post_type ) { if( $post_type != 'products' ) add_meta_box( 'aliprice_seo_metadata', __( 'SEO Metadata', 'aliprice' ), 'aliprice_options_meta', $post_type, 'normal', 'high' ); } } } function aliprice_options_meta( ) { global $post; $description = get_post_meta($post->ID, 'description', true); $keywords = get_post_meta($post->ID, 'keywords', true); $noindex = get_post_meta($post->ID, 'noindex', true); $seotitle = get_post_meta($post->ID, 'seo-title', true); $special = get_post_meta($post->ID, 'special', true); ?>

ID, 'aliprice_can_edit', true); $edited = ( empty($edited) || $edited == 'can' ) ? true : false; $etitle = get_post_meta($post->ID, 'aliprice_can_edit_title', true); $etitle = ( empty($edited) || $edited == 'can' ) ? true : false; $noindex = get_post_meta($post->ID, 'aliprice_noindex', true); $noindex = ( !empty($noindex) && $noindex == '1' ) ? true : false; $special = get_post_meta($post->ID, 'aliprice_special', true); $special = ( !empty($special) && $special == '1' ) ? true : false; $seotitle = get_post_meta($post->ID, 'aliprice_seo-title', true); $info = new AEProducts(); $info->set($post->ID); ?>

" class="regular-text" readonly="readonly"/>

getImages(); if( $images ) : $images = unserialize( $images ); if( count($images) > 1 ) : foreach( $images as $key => $img ) { ?>
getAttribute(); $count = count( $attributes ); if( $count && $count > 0 ) : ?>

$v ) : ?>
update( $wpdb->prefix . 'aliprice_products', $args, array( 'post_id' => $post_id ) ); if( isset($_POST['can_edited']) ) update_post_meta($post_id, 'aliprice_can_edit', 'can'); else update_post_meta($post_id, 'aliprice_can_edit', ''); if( isset($_POST['can_edited_title']) ) update_post_meta($post_id, 'aliprice_can_edit_title', 'can'); else update_post_meta($post_id, 'aliprice_can_edit_title', ''); if( isset($_POST['seo-title']) ) update_post_meta($post_id, 'aliprice_seo-title', sanitize_text_field($_POST['seo-title'])); if( isset($_POST['noindex']) ) update_post_meta($post_id, 'aliprice_noindex', '1'); else update_post_meta($post_id, 'aliprice_noindex', '0'); if( isset($_POST['special']) ) update_post_meta($post_id, 'aliprice_special', '1'); else update_post_meta($post_id, 'aliprice_special', '0'); } // add a hook to save seo add_action( 'save_post', 'aliprice_save_seo', 10, 1 ); function aliprice_save_seo( $post_id ){ if ( !isset($_POST['aliprice_noncename_aeseo']) || !wp_verify_nonce( $_POST['aliprice_noncename_aeseo'], 'aliprice_noncename_aeseo') ) return; if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; if ( !current_user_can( 'edit_page', $post_id ) ) return; $foo = array('keywords', 'description'); foreach( $foo as $key ) { update_post_meta($post_id, $key, strip_tags($_POST[$key])); } if( isset($_POST['seo-title']) ) update_post_meta($post_id, 'seo-title', sanitize_text_field($_POST['seo-title'])); if( isset($_POST['noindex']) ) update_post_meta($post_id, 'noindex', '1'); else update_post_meta($post_id, 'noindex', '0'); if( isset($_POST['special']) ) update_post_meta($post_id, 'special', '1'); else update_post_meta($post_id, 'special', '0'); } /** * Modify columns title */ add_filter("manage_edit-products_columns", "aliprice_columns"); function aliprice_columns( $columns ) { unset($columns['author']); unset($columns['cb']); unset($columns['title']); return array( "cb" => __('Select All', 'wp'), "thumb" => __("Thumbnail", 'aliprice'), "title" => __("Title", 'wp'), "shopcategory" => __("Category", 'aliprice'), "views" => __("Views", 'aliprice'), "redirects" => __("Redirects", 'aliprice'), "price" => __("Price", 'aliprice'), "quantity" => __("Stock", 'aliprice'), ) + $columns; } /** * Content columns */ add_action("manage_products_posts_custom_column", "aliprice_custom_columns"); function aliprice_custom_columns( $column ) { global $post; if ( "thumb" == $column ) { if( !empty($post->imageUrl) ) echo ''; } elseif ( "price" == $column ) { echo $post->price; } elseif ( "views" == $column ) { $view = get_post_meta( $post->ID, 'views', true ); echo !empty( $view ) ? $view : 0; } elseif ("redirects" == $column) { $view = get_post_meta( $post->ID, 'redirects', true ); echo !empty( $view ) ? $view : 0; } elseif ( "shopcategory" == $column ) { $categories = wp_get_object_terms( $post->ID, 'shopcategory' ); if ( is_array( $categories ) ) foreach( $categories as $k => $category ) echo '' . $category->name . '
'; } elseif ( "quantity" == $column ) { $color = ($post->quantity <= 15) ? '#db0f1a' : '#6adb4b'; printf('%d', $color, $post->quantity); } } /** * Adding a Taxonomy Filter to Admin List for a Custom Post Type */ add_action( 'restrict_manage_posts', 'aliprice_restrict_manage_posts' ); function aliprice_restrict_manage_posts( ) { global $typenow; if ( $typenow == 'products' ) { $filters = array( 'shopcategory' ); foreach ( $filters as $tax_slug ) { $tax_obj = get_taxonomy( $tax_slug ); $tax_name = $tax_obj->labels->name; ?> 1 ); if( !is_null( $parent ) ) $args = array( 'parent' => $parent ); $terms = get_terms( $tax_slug, $args ); $tab = ''; for( $i = 0; $i < $level; $i++ ) $tab .= '-- '; foreach ( $terms as $term ) { echo ''; aliprice_generate_taxonomy_options( $tax_slug, $term->term_id, $level+1, $selected ); } } /** * Sorting columns for custom post type Products */ add_filter( 'manage_edit-products_sortable_columns', 'aliprice_view_sortable_column' ); function aliprice_view_sortable_column( $columns ) { $columns['views'] = 'views'; $columns['redirects'] = 'redirects'; $columns['quantity'] = 'quantity'; return $columns; } /** * Request to sorting columns for custom post type Products */ add_filter( 'request', 'aliprice_view_column_orderby' ); function aliprice_view_column_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'views' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'views', 'orderby' => 'meta_value_num' ) ); } if ( isset( $vars['orderby'] ) && 'redirects' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'redirects', 'orderby' => 'meta_value_num' ) ); } return $vars; } /** * Alter different parts of the query * * @param array $pieces * @return array $pieces */ function aliprice_query_clauses( $pieces ) { $screen = get_current_screen(); if( !isset($screen->post_type) || $screen->post_type != 'products' || $screen->base != 'edit' ) return $pieces; global $wpdb; $pieces['join'] = $pieces['join'] . " INNER JOIN `{$wpdb->prefix}aliprice_products` ON ({$wpdb->posts}.ID = `{$wpdb->prefix}aliprice_products`.`post_id`) "; $pieces['fields'] = $pieces['fields'] . ", price, commissionRate, quantity, imageUrl"; $pieces['posts_groupby'] = "GROUP BY `{$wpdb->postmeta}`.`post_id`"; return $pieces; } /** * Additional order by custom query vars * * @param $orderby_statement * * @return string */ function aliprice_query_posts_orderby( $orderby_statement ) { global $wpdb; $orderby = get_query_var( 'orderby', false ); $order = get_query_var( 'order', false ); $foo = array('quantity'); if ( $orderby && in_array($orderby, $foo) ) { $order = ( $order == 'ASC' ) ? 'ASC' : 'DESC'; $orderby_statement = $wpdb->products . '.' . $orderby . ' ' . $order; } return $orderby_statement; } add_action( 'admin_init', 'aliprice_get_screen' ); function aliprice_get_screen( ) { if( is_admin() ){ add_filter( 'posts_clauses', 'aliprice_query_clauses', 20, 1 ); add_filter( 'posts_orderby', 'aliprice_query_posts_orderby', 20, 1 ); } } add_action( 'shopcategory_edit_form_fields', 'aliprice_custom_metabox_taxonomy', 10, 2); add_action( 'category_edit_form_fields', 'aliprice_custom_metabox_taxonomy', 10, 2); add_action( 'post_tag_edit_form_fields', 'aliprice_custom_metabox_taxonomy', 10, 2); function aliprice_custom_metabox_taxonomy($tag, $taxonomy) { $noindex = get_site_option( "noindex_$tag->term_id" ); ?>