Nsp Code https://www.nsp-code.com Premium WordPress Development Fri, 10 Aug 2018 08:22:39 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 Using sorting functionality along with DIVI Filterable Portfolio module https://www.nsp-code.com/using-sorting-functionality-along-with-divi-filterable-portfolio-module/ https://www.nsp-code.com/using-sorting-functionality-along-with-divi-filterable-portfolio-module/#comments Tue, 07 Aug 2018 11:10:21 +0000 http://www.nsp-code.com/?p=1575 DIVI provided by Elegant Themes is just great! Excellent functionality provided through their visual composer and modules, make things easy to manage and stunning layout creation a breeze. Advanced Post Types order plugin provide total control over the content sorting for your DIVI powered website. Sorts are maintained easily through lists, which can apply globally […]

The post Using sorting functionality along with DIVI Filterable Portfolio module appeared first on Nsp Code.

]]>

DIVI provided by Elegant Themes is just great! Excellent functionality provided through their visual composer and modules, make things easy to manage and stunning layout creation a breeze.

Advanced Post Types order plugin provide total control over the content sorting for your DIVI powered website. Sorts are maintained easily through lists, which can apply globally or to different areas, independently, without conflicting with each other.

Recently we received questions upon the possibility to apply sorting for Filterable Portfolio module. Since the filtering is done on front side through a JavaScript code, the page does not refresh, making the customized order to do not apply for categories, except the All tab. The following code makes that possible. This create a new module called Filterable Portfolio – Sortable which should be used instead the default module.

First, we need to prepare the module loader. The following code should be placed within your theme child functions.php file:

    function DS_Custom_Modules()
        {
            if(class_exists("ET_Builder_Module"))
                {
                    include("divi-modules/filtrable-portfolio-sortable.php");
                }
        }

    function Prep_DS_Custom_Modules()
        {
            global $pagenow;

            $is_admin = is_admin();
            $action_hook = $is_admin ? 'wp_loaded' : 'wp';
            $required_admin_pages = array( 'edit.php', 'post.php', 'post-new.php', 'admin.php', 'customize.php', 'edit-tags.php', 'admin-ajax.php', 'export.php' ); // list of admin pages where we need to load builder files
            $specific_filter_pages = array( 'edit.php', 'admin.php', 'edit-tags.php' );
            $is_edit_library_page = 'edit.php' === $pagenow && isset( $_GET['post_type'] ) && 'et_pb_layout' === $_GET['post_type'];
            $is_role_editor_page = 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'et_divi_role_editor' === $_GET['page'];
            $is_import_page = 'admin.php' === $pagenow && isset( $_GET['import'] ) && 'wordpress' === $_GET['import']; 
            $is_edit_layout_category_page = 'edit-tags.php' === $pagenow && isset( $_GET['taxonomy'] ) && 'layout_category' === $_GET['taxonomy'];

            if ( ! $is_admin || ( $is_admin && in_array( $pagenow, $required_admin_pages ) && ( ! in_array( $pagenow, $specific_filter_pages ) || $is_edit_library_page || $is_role_editor_page || $is_edit_layout_category_page || $is_import_page ) ) ) 
                {
                    add_action($action_hook, 'DS_Custom_Modules', 9789);
                }
        }
    Prep_DS_Custom_Modules();

The following code is the module. Since the above calls divi-modules/filtrable-portfolio-sortable.php file, the code should be placed within filtrable-portfolio-sortable.php inside a folder named divi-modules.

<?php

class ET_Builder_Module_Filterable_Portfolio_Sortable extends ET_Builder_Module_Type_PostBased {
    function init() {
        $this->name       = esc_html__( 'Filterable Portfolio Sortable', 'et_builder' );
        $this->slug       = 'et_pb_filterable_portfolio_sortable';
        $this->vb_support = 'on';

        $this->main_css_element = '%%order_class%%.et_pb_filterable_portfolio';

        $this->settings_modal_toggles = array(
            'general'  => array(
                'toggles' => array(
                    'main_content' => esc_html__( 'Content', 'et_builder' ),
                    'elements'     => esc_html__( 'Elements', 'et_builder' ),
                ),
            ),
            'advanced' => array(
                'toggles' => array(
                    'layout'  => esc_html__( 'Layout', 'et_builder' ),
                    'overlay' => esc_html__( 'Overlay', 'et_builder' ),
                    'text'    => array(
                        'title'    => esc_html__( 'Text', 'et_builder' ),
                        'priority' => 49,
                    ),
                    'image' => esc_html__( 'Image', 'et_builder' ),
                ),
            ),
        );

        $this->advanced_fields = array(
            'fonts'                 => array(
                'title'   => array(
                    'label'    => esc_html__( 'Title', 'et_builder' ),
                    'css'      => array(
                        'main' => "{$this->main_css_element} h2, {$this->main_css_element} .et_pb_module_header",
                        'plugin_main' => "{$this->main_css_element} h2, {$this->main_css_element} h2 a, {$this->main_css_element} h1.et_pb_module_header, {$this->main_css_element} h1.et_pb_module_header a, {$this->main_css_element} h3.et_pb_module_header, {$this->main_css_element} h3.et_pb_module_header a, {$this->main_css_element} h4.et_pb_module_header, {$this->main_css_element} h4.et_pb_module_header a, {$this->main_css_element} h5.et_pb_module_header, {$this->main_css_element} h5.et_pb_module_header a, {$this->main_css_element} h6.et_pb_module_header, {$this->main_css_element} h6.et_pb_module_header a",
                        'important' => 'all',
                    ),
                    'header_level' => array(
                        'default' => 'h2',
                    ),
                ),
                'filter' => array(
                    'label'    => esc_html__( 'Filter Criteria', 'et_builder' ),
                    'hide_text_align' => true,
                    'css'      => array(
                        'main' => "{$this->main_css_element} .et_pb_portfolio_filter",
                        'plugin_main' => "{$this->main_css_element} .et_pb_portfolio_filter, {$this->main_css_element} .et_pb_portfolio_filter a",
                        'color' => "{$this->main_css_element} .et_pb_portfolio_filter a",
                    ),
                ),
                'caption' => array(
                    'label'    => esc_html__( 'Meta', 'et_builder' ),
                    'css'      => array(
                        'main' => "{$this->main_css_element} .post-meta, {$this->main_css_element} .post-meta a",
                    ),
                ),
                'pagination' => array(
                    'label'    => esc_html__( 'Pagination', 'et_builder' ),
                    'css'      => array(
                        'main' => "{$this->main_css_element} .et_pb_portofolio_pagination a",
                        'text_align' => "{$this->main_css_element} .et_pb_portofolio_pagination ul",
                    ),
                    'text_align' => array(
                        'options' => et_builder_get_text_orientation_options( array( 'justified' ), array() ),
                    ),
                ),
            ),
            'background'            => array(
                'settings' => array(
                    'color' => 'alpha',
                ),
            ),
            'borders'               => array(
                'default' => array(
                    'css' => array(
                        'main' => array(
                            'border_radii' => "{$this->main_css_element} .et_pb_portfolio_item",
                            'border_styles' => "{$this->main_css_element} .et_pb_portfolio_item",
                        ),
                    ),
                ),
                'image' => array(
                    'css'          => array(
                        'main' => array(
                            'border_radii'  => "{$this->main_css_element} .et_portfolio_image",
                            'border_styles' => "{$this->main_css_element} .et_portfolio_image",
                        ),
                    ),
                    'label_prefix' => esc_html__( 'Image', 'et_builder' ),
                    'tab_slug'      => 'advanced',
                    'toggle_slug'   => 'image',
                ),
            ),
            'box_shadow'            => array(
                'default' => array(),
                'image'   => array(
                    'label'           => esc_html__( 'Image Box Shadow', 'et_builder' ),
                    'option_category' => 'layout',
                    'tab_slug'        => 'advanced',
                    'toggle_slug'     => 'image',
                    'css'             => array(
                        'main'         => '%%order_class%% .project .et_portfolio_image',
                        'custom_style' => true,
                    ),
                    'default_on_fronts'  => array(
                        'color'    => '',
                        'position' => '',
                    ),
                ),
            ),
            'margin_padding' => array(
                'css' => array(
                    'important' => array( 'custom_margin' ), // needed to overwrite last module margin-bottom styling
                ),
            ),
            'max_width'             => array(
                'css' => array(
                    'module_alignment' => '%%order_class%%.et_pb_filterable_portfolio.et_pb_module',
                ),
            ),
            'text'                  => array(
                'use_background_layout' => true,
                'options' => array(
                    'background_layout' => array(
                        'default' => 'light',
                    ),
                ),
            ),
            'filters'               => array(
                'css' => array(
                    'main' => '%%order_class%%',
                ),
                'child_filters_target' => array(
                    'tab_slug' => 'advanced',
                    'toggle_slug' => 'image',
                ),
            ),
            'image'                 => array(
                'css' => array(
                    'main' => '%%order_class%% .et_portfolio_image',
                ),
            ),
            'button'                => false,
        );

        $this->custom_css_fields = array(
            'portfolio_filters' => array(
                'label'    => esc_html__( 'Portfolio Filters', 'et_builder' ),
                'selector' => '.et_pb_filterable_portfolio .et_pb_portfolio_filters',
                'no_space_before_selector' => true,
            ),
            'active_portfolio_filter' => array(
                'label'    => esc_html__( 'Active Portfolio Filter', 'et_builder' ),
                'selector' => '.et_pb_filterable_portfolio .et_pb_portfolio_filters li a.active',
                'no_space_before_selector' => true,
            ),
            'portfolio_image' => array(
                'label'    => esc_html__( 'Portfolio Image', 'et_builder' ),
                'selector' => '.et_portfolio_image',
            ),
            'overlay' => array(
                'label'    => esc_html__( 'Overlay', 'et_builder' ),
                'selector' => '.et_overlay',
            ),
            'overlay_icon' => array(
                'label'    => esc_html__( 'Overlay Icon', 'et_builder' ),
                'selector' => '.et_overlay:before',
            ),
            'portfolio_title' => array(
                'label'    => esc_html__( 'Portfolio Title', 'et_builder' ),
                'selector' => '.et_pb_portfolio_item h2',
            ),
            'portfolio_post_meta' => array(
                'label'    => esc_html__( 'Portfolio Post Meta', 'et_builder' ),
                'selector' => '.et_pb_portfolio_item .post-meta',
            ),
            'portfolio_pagination' => array(
                'label'    => esc_html__( 'Portfolio Pagination', 'et_builder' ),
                'selector' => '.et_pb_portofolio_pagination',
            ),
            'portfolio_pagination_active' => array(
                'label'    => esc_html__( 'Pagination Active Page', 'et_builder' ),
                'selector' => '.et_pb_portofolio_pagination a.active',
            ),
        );

        $this->help_videos = array(
            array(
                'id'   => esc_html( 'AZheY1hVcJc' ),
                'name' => esc_html__( 'An introduction to the Filterable Portfolio module', 'et_builder' ),
            ),
        );
    }

    function get_fields() {
        $fields = array(
            'fullwidth' => array(
                'label'           => esc_html__( 'Layout', 'et_builder' ),
                'type'            => 'select',
                'option_category' => 'layout',
                'options'         => array(
                    'on'  => esc_html__( 'Fullwidth', 'et_builder' ),
                    'off' => esc_html__( 'Grid', 'et_builder' ),
                ),
                'affects' => array(
                    'hover_icon',
                    'zoom_icon_color',
                    'hover_overlay_color',
                ),
                'description'      => esc_html__( 'Choose your desired portfolio layout style.', 'et_builder' ),
                'computed_affects' => array(
                    '__projects',
                ),
                'tab_slug'         => 'advanced',
                'toggle_slug'      => 'layout',
                'default_on_front' => 'on',
            ),
            'posts_number' => array(
                'default'          => 10,
                'label'            => esc_html__( 'Posts Number', 'et_builder' ),
                'type'             => 'text',
                'option_category'  => 'configuration',
                'description'      => esc_html__( 'Define the number of projects that should be displayed per page.', 'et_builder' ),
                'computed_affects' => array(
                    '__projects',
                ),
                'toggle_slug'      => 'main_content',
            ),
            'include_categories' => array(
                'label'            => esc_html__( 'Include Categories', 'et_builder' ),
                'type'             => 'categories',
                'option_category'  => 'basic_option',
                'description'      => esc_html__( 'Select the categories that you would like to include in the feed.', 'et_builder' ),
                'computed_affects' => array(
                    '__project_terms',
                    '__projects',
                ),
                'taxonomy_name'    => 'project_category',
                'toggle_slug'      => 'main_content',
            ),
            'show_title' => array(
                'label'             => esc_html__( 'Show Title', 'et_builder' ),
                'type'              => 'yes_no_button',
                'option_category'   => 'configuration',
                'options'           => array(
                    'on'  => esc_html__( 'Yes', 'et_builder' ),
                    'off' => esc_html__( 'No', 'et_builder' ),
                ),
                'toggle_slug'       => 'elements',
                'description'       => esc_html__( 'Turn project titles on or off.', 'et_builder' ),
                'default_on_front'  => 'on',
            ),
            'show_categories' => array(
                'label'             => esc_html__( 'Show Categories', 'et_builder' ),
                'type'              => 'yes_no_button',
                'option_category'   => 'configuration',
                'options'           => array(
                    'on'  => esc_html__( 'Yes', 'et_builder' ),
                    'off' => esc_html__( 'No', 'et_builder' ),
                ),
                'toggle_slug'       => 'elements',
                'description'       => esc_html__( 'Turn the category links on or off.', 'et_builder' ),
                'default_on_front'  => 'on',
            ),
            'show_pagination' => array(
                'label'             => esc_html__( 'Show Pagination', 'et_builder' ),
                'type'              => 'yes_no_button',
                'option_category'   => 'configuration',
                'options'           => array(
                    'on'  => esc_html__( 'Yes', 'et_builder' ),
                    'off' => esc_html__( 'No', 'et_builder' ),
                ),
                'toggle_slug'       => 'elements',
                'description'       => esc_html__( 'Enable or disable pagination for this feed.', 'et_builder' ),
                'default_on_front'  => 'on',
            ),
            'hover_icon' => array(
                'label'               => esc_html__( 'Hover Icon Picker', 'et_builder' ),
                'type'                => 'select_icon',
                'option_category'     => 'configuration',
                'class'               => array( 'et-pb-font-icon' ),
                'depends_show_if'     => 'off',
                'tab_slug'            => 'advanced',
                'toggle_slug'         => 'overlay',
            ),
            'zoom_icon_color' => array(
                'label'             => esc_html__( 'Zoom Icon Color', 'et_builder' ),
                'type'              => 'color-alpha',
                'custom_color'      => true,
                'depends_show_if'   => 'off',
                'tab_slug'          => 'advanced',
                'toggle_slug'       => 'overlay',
            ),
            'hover_overlay_color' => array(
                'label'             => esc_html__( 'Hover Overlay Color', 'et_builder' ),
                'type'              => 'color-alpha',
                'custom_color'      => true,
                'depends_show_if'   => 'off',
                'tab_slug'          => 'advanced',
                'toggle_slug'       => 'overlay',
            ),
            '__project_terms' => array(
                'type'                => 'computed',
                'computed_callback'   => array( 'ET_Builder_Module_Filterable_Portfolio', 'get_portfolio_terms' ),
                'computed_depends_on' => array(
                    'include_categories',
                ),
            ),
            '__projects' => array(
                'type'                => 'computed',
                'computed_callback'   => array( 'ET_Builder_Module_Filterable_Portfolio', 'get_portfolio_item' ),
                'computed_depends_on' => array(
                    'show_pagination',
                    'posts_number',
                    'include_categories',
                    'fullwidth',
                ),
            ),
        );

        return $fields;
    }

    static function get_portfolio_item( $args = array(), $conditional_tags = array(), $current_page = array() ) {
        global $et_fb_processing_shortcode_object;

        $global_processing_original_value = $et_fb_processing_shortcode_object;

        $defaults = array(
            'show_pagination'    => 'on',
            'include_categories' => '',
            'fullwidth'          => 'on',
            'nopaging'           => true,
        );

        $query_args = array();

        $args = wp_parse_args( $args, $defaults );

        $include_categories = self::filter_invalid_term_ids( explode( ',', $args['include_categories'] ), 'project_category' );

        if ( ! empty( $include_categories ) ) {
            $query_args['tax_query'] = array(
                array(
                    'taxonomy' => 'project_category',
                    'field'    => 'id',
                    'terms'    => $include_categories,
                    'operator' => 'IN',
                )
            );
        }

        $default_query_args = array(
            'post_type'   => 'project',
            'post_status' => 'publish',
            'posts_per_page' => -1,
        );

        $query_args = wp_parse_args( $query_args, $default_query_args );

        // Get portfolio query
        $query = new WP_Query( $query_args );

        // Format portfolio output, and add supplementary data
        $width     = 'on' === $args['fullwidth'] ?  1080 : 400;
        $width     = (int) apply_filters( 'et_pb_portfolio_image_width', $width );
        $height    = 'on' === $args['fullwidth'] ?  9999 : 284;
        $height    = (int) apply_filters( 'et_pb_portfolio_image_height', $height );
        $classtext = 'on' === $args['fullwidth'] ? 'et_pb_post_main_image' : '';
        $titletext = get_the_title();

        // Loop portfolio item and add supplementary data
        if( $query->have_posts() ) {
            $post_index = 0;
            while ( $query->have_posts() ) {
                $query->the_post();

                $categories = array();

                $category_classes = array( 'et_pb_portfolio_item' );

                if ( 'on' !== $args['fullwidth'] ) {
                    $category_classes[] = 'et_pb_grid_item';
                }

                $categories_object = get_the_terms( get_the_ID(), 'project_category' );
                if ( ! empty( $categories_object ) ) {
                    foreach ( $categories_object as $category ) {
                        // Update category classes which will be used for post_class
                        $category_classes[] = 'project_category_' . urldecode( $category->slug );

                        // Push category data
                        $categories[] = array(
                            'id'        => $category->term_id,
                            'slug'      => $category->slug,
                            'label'     => $category->name,
                            'permalink' => get_term_link( $category ),
                        );
                    }
                }

                // need to disable processnig to make sure get_thumbnail() doesn't generate errors
                $et_fb_processing_shortcode_object = false;

                // Get thumbnail
                $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );

                $et_fb_processing_shortcode_object = $global_processing_original_value;

                // Append value to query post
                $query->posts[ $post_index ]->post_permalink     = get_permalink();
                $query->posts[ $post_index ]->post_thumbnail     = print_thumbnail( $thumbnail['thumb'], $thumbnail['use_timthumb'], $titletext, $width, $height, '', false, true );
                $query->posts[ $post_index ]->post_categories     = $categories;
                $query->posts[ $post_index ]->post_class_name     = array_merge( get_post_class( '', get_the_ID() ), $category_classes );

                // Append category classes
                $category_classes = implode( ' ', $category_classes );

                $post_index++;
            }
        } else if ( wp_doing_ajax() ) {
            // This is for the VB
            $query = array( 'posts' => self::get_no_results_template() );
        }

        wp_reset_postdata();

        return $query;
    }

    static function get_portfolio_terms( $args = array(), $conditional_tags = array(), $current_page = array() ) {
        $portfolio = self::get_portfolio_item( $args, $conditional_tags, $current_page );

        $terms = array();

        if ( ! empty( $portfolio->posts ) ) {
            foreach ( $portfolio->posts as $post ) {
                if ( ! empty( $post->post_categories ) ) {
                    foreach ( $post->post_categories as $category ) {
                        $terms[ $category['slug'] ] = $category;
                    }
                }
            }
        }

        return $terms;
    }

    function render( $attrs, $content = null, $render_slug ) {
        $fullwidth          = $this->props['fullwidth'];
        $posts_number       = $this->props['posts_number'];
        $include_categories = $this->props['include_categories'];
        $show_title         = $this->props['show_title'];
        $show_categories    = $this->props['show_categories'];
        $show_pagination    = $this->props['show_pagination'];
        $background_layout  = $this->props['background_layout'];
        $hover_icon          = $this->props['hover_icon'];
        $zoom_icon_color     = $this->props['zoom_icon_color'];
        $hover_overlay_color = $this->props['hover_overlay_color'];
        $header_level        = $this->props['title_level'];

        wp_enqueue_script( 'hashchange' );

        if ( '' !== $zoom_icon_color ) {
            ET_Builder_Element::set_style( $render_slug, array(
                'selector'    => '%%order_class%% .et_overlay:before',
                'declaration' => sprintf(
                    'color: %1$s !important;',
                    esc_html( $zoom_icon_color )
                ),
            ) );
        }

        if ( '' !== $hover_overlay_color ) {
            ET_Builder_Element::set_style( $render_slug, array(
                'selector'    => '%%order_class%% .et_overlay',
                'declaration' => sprintf(
                    'background-color: %1$s;
                    border-color: %1$s;',
                    esc_html( $hover_overlay_color )
                ),
            ) );
        }

        $current_category   =   isset($_GET['f'])   ?   $_GET['f']  :   '';
        $posts_include_categories =   !empty($current_category)    ?   $current_category  :  $include_categories;
            
        
        $projects = self::get_portfolio_item( array(
            'show_pagination'    => $show_pagination,
            'posts_number'       => $posts_number,
            'include_categories' =>  $posts_include_categories,
            'fullwidth'          => $fullwidth,
        ) );

        $categories_included = array();
        ob_start();
        if( $projects->post_count > 0 ) {
            while ( $projects->have_posts() ) {
                $projects->the_post();

                $category_classes = array();
                $categories = get_the_terms( get_the_ID(), 'project_category' );
                if ( $categories ) {
                    foreach ( $categories as $category ) {
                        $category_classes[] = 'project_category_' . urldecode( $category->slug );
                        $categories_included[] = $category->term_id;
                    }
                }

                $category_classes = implode( ' ', $category_classes );

                $main_post_class = sprintf(
                    'et_pb_portfolio_item%1$s %2$s',
                    ( 'on' !== $fullwidth ? ' et_pb_grid_item' : '' ),
                    $category_classes
                );

                ?>
                <div id="post-<?php the_ID(); ?>" <?php post_class( $main_post_class ); ?>>
                <?php
                    $thumb = '';

                    $width = 'on' === $fullwidth ?  1080 : 400;
                    $width = (int) apply_filters( 'et_pb_portfolio_image_width', $width );

                    $height = 'on' === $fullwidth ?  9999 : 284;
                    $height = (int) apply_filters( 'et_pb_portfolio_image_height', $height );
                    $classtext = 'on' === $fullwidth ? 'et_pb_post_main_image' : '';
                    $titletext = get_the_title();
                    $permalink = get_permalink();
                    $post_meta = get_the_term_list( get_the_ID(), 'project_category', '', ', ' );
                    $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
                    $thumb = $thumbnail["thumb"];


                    if ( '' !== $thumb ) : ?>
                        <a href="<?php echo esc_url( $permalink ); ?>">
                            <span class="et_portfolio_image">
                                <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                        <?php if ( 'on' !== $fullwidth ) :

                                $data_icon = '' !== $hover_icon
                                    ? sprintf(
                                        ' data-icon="%1$s"',
                                        esc_attr( et_pb_process_font_icon( $hover_icon ) )
                                    )
                                    : '';

                                printf( '<span class="et_overlay%1$s"%2$s></span>',
                                    ( '' !== $hover_icon ? ' et_pb_inline_icon' : '' ),
                                    $data_icon
                                );

                        ?>
                        <?php endif; ?>
                            </span>
                        </a>
                <?php
                    endif;
                ?>

                <?php if ( 'on' === $show_title ) : ?>
                    <<?php echo et_pb_process_header_level( $header_level, 'h2' ) ?> class="et_pb_module_header"><a href="<?php echo esc_url( $permalink ); ?>"><?php echo $titletext; ?></a></<?php echo et_pb_process_header_level( $header_level, 'h2' ) ?>>
                <?php endif; ?>

                <?php if ( 'on' === $show_categories ) : ?>
                    <p class="post-meta"><?php echo $post_meta; ?></p>
                <?php endif; ?>

                </div><!-- .et_pb_portfolio_item -->
                <?php
            }
        }

        wp_reset_postdata();

        if ( ! $posts = ob_get_clean() ) {
            $posts            = self::get_no_results_template();
            $category_filters = '';
        } else {
            
            $actual_link    = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $query_str      = parse_url($actual_link, PHP_URL_QUERY);
            $actual_link    =   str_replace("?" . $query_str , "" ,  $actual_link);
            
            
            $categories_included = explode ( ',', $include_categories );
            $terms_args = array(
                'include' => $categories_included,
                'orderby' => 'name',
                'order' => 'ASC',
            );
            $terms = get_terms( 'project_category', $terms_args );

            $category_filters = '<ul class="clearfix">';
            $class_current  =   empty($current_category)    ?   'active'    :   '';
            $category_filters .= sprintf( '<li class="et_pb_portfolio_filter et_pb_portfolio_filter_all"><a href="'.$actual_link.'" class="'.$class_current.'" data-category-slug="all">%1$s</a></li>',
                esc_html__( 'All', 'et_builder' )
            );
            foreach ( $terms as $term  ) {
                
                $class_current  =   !empty($current_category)   &&  $term->term_id  == $current_category     ?   'active'    :   '';
                
                
                $category_filters .= sprintf( '<li class="et_pb_portfolio_filter"><a href="'.$actual_link.'?f='. $term->term_id.'" class="'.$class_current.'" data-category-slug="%1$s">%2$s</a></li>',
                    esc_attr( urldecode( $term->slug ) ),
                    esc_html( $term->name )
                );
            }
            $category_filters .= '</ul>';
        }

        $video_background = $this->video_background();
        $parallax_image_background = $this->get_parallax_image_background();

        // Images: Add CSS Filters and Mix Blend Mode rules (if set)
        if ( isset( $this->advanced_fields['image']['css'] ) ) {
            $this->add_classname( $this->generate_css_filters(
                $render_slug,
                'child_',
                self::$data_utils->array_get( $this->advanced_fields['image']['css'], 'main', '%%order_class%%' )
            ) );
        }

        // Module classnames
        $this->add_classname( array(
            'et_pb_portfolio',
            "et_pb_bg_layout_{$background_layout}",
            $this->get_text_orientation_classname(),
        ) );

        if ( 'on' === $fullwidth ) {
            $this->add_classname( 'et_pb_filterable_portfolio_fullwidth' );
        } else {
            $this->add_classname( array(
                'et_pb_filterable_portfolio_grid',
                'clearfix',
            ) );
        }

        $output = sprintf(
            '<div%4$s class="et_pb_filterable_portfolio et_pb_portfolio %1$s" data-posts-number="%5$d"%8$s>
                %10$s
                %9$s
                <div class="et_pb_portfolio_filters clearfix">%2$s</div><!-- .et_pb_portfolio_filters -->
                
                <script type="text/javascript">
                /* <![CDATA[ */
                jQuery(".et_pb_filterable_portfolio .et_pb_portfolio_filters a").on("click", function( event ) {
                    event.stopPropagation();
                    window.location.href    =   jQuery(this).attr("href");
                })
                /* ]]> */
                </script>

                <div class="et_pb_portfolio_items_wrapper %6$s">
                    <div class="et_pb_portfolio_items">%3$s</div><!-- .et_pb_portfolio_items -->
                </div>
                %7$s
            </div> <!-- .et_pb_filterable_portfolio -->',
            $this->module_classname( $render_slug ),
            $category_filters,
            $posts,
            $this->module_id(),
            esc_attr( $posts_number),
            ('on' === $show_pagination ? 'clearfix' : 'no_pagination' ),
            ('on' === $show_pagination ? '<div class="et_pb_portofolio_pagination"></div>' : '' ),
            is_rtl() ? ' data-rtl="true"' : '',
            $video_background,
            $parallax_image_background
        );

        return $output;
    }
}

new ET_Builder_Module_Filterable_Portfolio_Sortable;

?>

The new module setting are the same as default one, they should be used accordingly to this article.

The post Using sorting functionality along with DIVI Filterable Portfolio module appeared first on Nsp Code.

]]>
https://www.nsp-code.com/using-sorting-functionality-along-with-divi-filterable-portfolio-module/feed/ 8
Sort WooCommerce Products while automatically put the Out of Stock items at the end of list https://www.nsp-code.com/sort-woocommerce-products-while-automatically-put-the-out-of-stock-items-at-the-end-of-list/ https://www.nsp-code.com/sort-woocommerce-products-while-automatically-put-the-out-of-stock-items-at-the-end-of-list/#respond Thu, 10 May 2018 16:20:58 +0000 https://www.nsp-code.com/?p=15424 Sorting for WooCommerce Products is a simple process, the Advanced Post Types Order cover all possibilities to cover any type of sorting automation or manual drag&drop. The WooCommerce – Sort Products which belong to multiple categories cover the basics to get started. Already sorted WooCommerce products, are displayed accordingly on front side. Many shop owners […]

The post Sort WooCommerce Products while automatically put the Out of Stock items at the end of list appeared first on Nsp Code.

]]>
Sorting for WooCommerce Products is a simple process, the Advanced Post Types Order cover all possibilities to cover any type of sorting automation or manual drag&drop. The WooCommerce – Sort Products which belong to multiple categories cover the basics to get started.

Already sorted WooCommerce products, are displayed accordingly on front side. Many shop owners chose to promote marketable items by sorting the list and put on top of everything else. This is a smart technique which greatly increase visibility and imply revenue excedent. Often, Out of Stock products can be a pain and require sort adjustments to lower priority in the list and show at the bottom of it.

The Advanced Post Types Order plugin can help with that, through a custom code it provides the automation process to put any products in the list which are Out of Order at the end of the list. The sort list will be kept as is (per administrator customization), only when displayed, the products with no stock will be moved at the end of the grid.

The following code can be included in theme functions.php or within a custom plugin:

     add_filter('apto_get_order_list', 'custom_wooc_apto_get_order_list', 10, 2);
    function custom_wooc_apto_get_order_list( $order_list, $sort_view_id )
        {
            
            //not for admin, only for Front Side
            if ( is_admin() )
                return $order_list;    
                
            global $APTO;
            
            //retrieve the sort post
            $sort_view_data     =   get_post($sort_view_id);
            $sortID             =   $sort_view_data->post_parent;   
            
            //check if is a woocommerce sort
            if ( ! $APTO->functions->is_woocommerce($sortID) )
                return $order_list;
            
            //identify the products out of stock
            $out_of_stock   =   array();
            
            foreach ( $order_list   as  $key    =>  $post_id ) 
                {
                    if ( get_post_meta($post_id, '_manage_stock', TRUE )    !=  'yes' )
                        continue;
                        
                    if ( get_post_meta($post_id, '_stock', TRUE)    <   1 )
                        {
                            $out_of_stock[] =   $post_id;
                            unset(  $order_list[$key] );
                        }
                }
                
            //re-index the keys
            $order_list =   array_values($order_list);
            
            //put the OutOf Stock at the end of list
            if ( count ( $out_of_stock )  >   0 )
                {
                    foreach (   $out_of_stock   as  $post_id    )
                        {
                            $order_list[]   =   $post_id;   
                        }
                }
            
            return $order_list;
            
        }

The above code works if Manage Stock is turned On. If the shop products does not use that and rely on Stock status the following code should be instead:

     add_filter('apto_get_order_list', 'custom_wooc_apto_get_order_list', 10, 2);
    function custom_wooc_apto_get_order_list( $order_list, $sort_view_id )
        {
            
            //not for admin, only for Front Side
            if ( is_admin() )
                return $order_list;    
                
            global $APTO;
            
            //retrieve the sort post
            $sort_view_data     =   get_post($sort_view_id);
            $sortID             =   $sort_view_data->post_parent;   
            
            //check if is a woocommerce sort
            if ( ! $APTO->functions->is_woocommerce($sortID) )
                return $order_list;
            
            //identify the products out of stock
            $out_of_stock   =   array();
            
            foreach ( $order_list   as  $key    =>  $post_id ) 
                {
                    if ( get_post_meta($post_id, '_stock_status', TRUE)    !=   'instock' )
	                    {
	                        $out_of_stock[] =   $post_id;
	                        unset(  $order_list[$key] );
	                    }
                }
                
            //re-index the keys
            $order_list =   array_values($order_list);
            
            //put the OutOf Stock at the end of list
            if ( count ( $out_of_stock )  >   0 )
                {
                    foreach (   $out_of_stock   as  $post_id    )
                        {
                            $order_list[]   =   $post_id;   
                        }
                }
            
            return $order_list;
            
        }

The post Sort WooCommerce Products while automatically put the Out of Stock items at the end of list appeared first on Nsp Code.

]]>
https://www.nsp-code.com/sort-woocommerce-products-while-automatically-put-the-out-of-stock-items-at-the-end-of-list/feed/ 0
How to retrieve the sticky posts for a sort https://www.nsp-code.com/how-to-retrieve-the-sticky-posts-for-a-sort/ https://www.nsp-code.com/how-to-retrieve-the-sticky-posts-for-a-sort/#respond Tue, 24 Apr 2018 16:32:13 +0000 https://www.nsp-code.com/?p=15338 The default WordPress Sticky functionality is very limited. It allows to put a post on top of the list which is pretty much everything about it. Beside other missing functionality, there is no control over order, making the list totally uncontrollable. The Advanced Post Types Order plugin provides a similar internal Sticky Posts, which is […]

The post How to retrieve the sticky posts for a sort appeared first on Nsp Code.

]]>
The default WordPress Sticky functionality is very limited. It allows to put a post on top of the list which is pretty much everything about it. Beside other missing functionality, there is no control over order, making the list totally uncontrollable.

The Advanced Post Types Order plugin provides a similar internal Sticky Posts, which is very flexible and preferable to be used instead default.

Sticky Posts are adjusted/applyed automatically on front side, on a query run. Programmatically retrieval for those posts is also easy, the following code can be used:

    $attr = array(
                        '_view_selection'   =>  'taxonomy',
                        '_taxonomy'         =>  $taxonomy_name,
                        '_term_id'          =>  $term_id
                        );
    
    $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr);
    $sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);

or for archive type

    $attr = array(
                        '_view_selection'   =>  'archive'
                        );
    
    $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr);
    $sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);

The returned $sticky_list is an array containing the list of sticky posts. The key of array element is the position within the list and the value is the object_id.

The post How to retrieve the sticky posts for a sort appeared first on Nsp Code.

]]>
https://www.nsp-code.com/how-to-retrieve-the-sticky-posts-for-a-sort/feed/ 0
How to sort posts automatically by a Last Name included in title https://www.nsp-code.com/sort-posts-automatically-last-name-included-title/ https://www.nsp-code.com/sort-posts-automatically-last-name-included-title/#respond Wed, 11 Apr 2018 19:27:36 +0000 https://www.nsp-code.com/?p=15288 Automated sorting is always a fast way to maintain ordering without any admin intervention Automatic Order. In this particular scenario, the post titles include a person full name, we need to use the last name to create an automatic ordering. Once we set-up a sort, we switch to Automatic Order and use the Custom Function […]

The post How to sort posts automatically by a Last Name included in title appeared first on Nsp Code.

]]>
Automated sorting is always a fast way to maintain ordering without any admin intervention Automatic Order.

In this particular scenario, the post titles include a person full name, we need to use the last name to create an automatic ordering.

Once we set-up a sort, we switch to Automatic Order and use the Custom Function Callback functionality.

 

In this example the callback function name is called “custom_sorting_function_sort_by_last_name”, this should be placed in above field. The function should be included within theme functions.php or a custom plugin:

function custom_sorting_function_sort_by_last_name( $posts_list, $sort_view_id, $orderBy, $query)
        {
            $list_map   =   array();
            foreach($posts_list  as  $object_id)
                {
                    $object =   get_post($object_id);
                    
                    $name   =   explode(" ", $object->post_title);
                    
                    //remove empty spaces
                    $name   =   array_filter($name);
                    
                    if ( count  ($name)  > 0 )
                        {
                            end($name);
                            $list_map[$object_id]   =   current($name);
                        }
                }
                 
            //sort the dates ascending
            asort($list_map);
             
            $post_list  =   array_keys($list_map);
                 
            return $post_list;  
        }

The post How to sort posts automatically by a Last Name included in title appeared first on Nsp Code.

]]>
https://www.nsp-code.com/sort-posts-automatically-last-name-included-title/feed/ 0
Sort post objects assigned to a taxonomy terms but not it’s children https://www.nsp-code.com/sort-post-objects-assigned-taxonomy-terms-not-children/ https://www.nsp-code.com/sort-post-objects-assigned-taxonomy-terms-not-children/#respond Mon, 12 Mar 2018 16:34:38 +0000 https://www.nsp-code.com/?p=15146 Per WordPress Codex when creating a custom query, and request objects which belong to a specific term, direct assigned objects will be included. Also all other objects which belong to child terms related to queried term will be returned too. This is the default behavior observed within WordPress dashboard, when filter for a specific category, […]

The post Sort post objects assigned to a taxonomy terms but not it’s children appeared first on Nsp Code.

]]>
Per WordPress Codex when creating a custom query, and request objects which belong to a specific term, direct assigned objects will be included. Also all other objects which belong to child terms related to queried term will be returned too. This is the default behavior observed within WordPress dashboard, when filter for a specific category, all posts assigned to the term and it’s child will be show.

For example, we have a Blog category and a child Subcategory.

When filter for posts which are assigned to Blog, the dashboard also include posts assigned to it’s child Subcategory:

From above image, it can be seen that actually only 3 posts are direct objects of Blog category.

The explained practice works quite well in custom codes (themes and plugins), is the default way WordPress Query core works. The Advanced Post Types Order plugin, does just that and follow the standards.

What if we need to sort only object assigned to a taxonomy term but not it’s children?

The WordPress Codex mention a special parameter to make that happen, called include_children. This is set to TRUE as default, if being used within a custom query as FALSE, it returns only direct objects only.
The Advanced Post Types Order plugin include support for such parameter, so it let to chose through the sort settings if the children are actually required to be show, on not. Depending on the setting, the sort list include (or not) the children objects and it will apply on front side for queries which match that parameter, accordingly.

Here’s a sample code which use the include_children:

    $args = array(
                'post_type'                 => 'post',
                'tax_query'                 => array(
                                                array(
                                                    'taxonomy'          => 'category',
                                                    'field'             => 'term_id',
                                                    'terms'             => array(2),
                                                    'include_children'  =>  FALSE
                                                    )
                                                )
                );

    $query = new WP_Query($args);

The post Sort post objects assigned to a taxonomy terms but not it’s children appeared first on Nsp Code.

]]>
https://www.nsp-code.com/sort-post-objects-assigned-taxonomy-terms-not-children/feed/ 0
How to randomize the first 10 posts in a manually sorted list https://www.nsp-code.com/randomize-first-10-posts-manually-sorted-list/ https://www.nsp-code.com/randomize-first-10-posts-manually-sorted-list/#respond Wed, 07 Mar 2018 22:02:04 +0000 https://www.nsp-code.com/?p=15129 The first step will be to create a sort which match a specif area of the site. Any sort type can be used either simple sort or mix of taxonomies and post types. Further, the following code should be placed inside theme functions.php or a custom plugin. The $SORT_TO_MATCH ne to be changed with the […]

The post How to randomize the first 10 posts in a manually sorted list appeared first on Nsp Code.

]]>
The first step will be to create a sort which match a specif area of the site. Any sort type can be used either simple sort or mix of taxonomies and post types.

Further, the following code should be placed inside theme functions.php or a custom plugin. The $SORT_TO_MATCH ne to be changed with the sort ID previously created, also $RANDOMIZE_FIRST_X_POSTS should contain the number of posts to randomize:

    add_filter('apto_get_order_list', 'custom_apto_get_order_list', 10, 2);
    function custom_apto_get_order_list( $order_list, $sort_view_id )
        {
            
            $SORT_TO_MATCH              =   121;
            $RANDOMIZE_FIRST_X_POSTS    =   10;
            
            $sort_view_post =   get_post( $sort_view_id );
            
            if ( $sort_view_post->post_parent   !=  $SORT_TO_MATCH)
                return $order_list;
            
            //randomize the first 10 posts
            $slice  =   array_slice( $order_list, 0, $RANDOMIZE_FIRST_X_POSTS);
            shuffle( $slice );
            
            //add the values
            foreach  ( array_values($slice) as $key    =>  $value)
                {
                    $order_list[$key]   =   $value;
                }
            
            return $order_list;
        }

The post How to randomize the first 10 posts in a manually sorted list appeared first on Nsp Code.

]]>
https://www.nsp-code.com/randomize-first-10-posts-manually-sorted-list/feed/ 0
Update the sorts when changing the site default language https://www.nsp-code.com/update-sorts-changing-site-default-language/ https://www.nsp-code.com/update-sorts-changing-site-default-language/#respond Tue, 06 Mar 2018 14:32:32 +0000 https://www.nsp-code.com/?p=15118 To allow sorting for different languages through custom plugins like WPML the plugin implemented a custom functionality which also save the sort language. The feature becomes active right after plugin activation, even if there’s no multilingual code active yet to preserve sorts compatibility for later usage. As default the Advanced Post Types Order plugin use […]

The post Update the sorts when changing the site default language appeared first on Nsp Code.

]]>
To allow sorting for different languages through custom plugins like WPML the plugin implemented a custom functionality which also save the sort language. The feature becomes active right after plugin activation, even if there’s no multilingual code active yet to preserve sorts compatibility for later usage.

As default the Advanced Post Types Order plugin use the site default language when saving the language attribute. If for some reason, the site language is being changed, apparently the orders does not apply anymore. This is an expected result, since the sorts where created for a particular language, which now has changed. There is a workaround for such scenario:

  1. Re-create the ordering, they will be save using the new language attribute and apply accordingly.
  2. Update the old language to new one within the sorts.

To update the language attribute within the sorts, the following code can be used. This code should be placed within a file and saved within WordPress root. Or if copied over into different directory, the include(‘wp-config.php’) line should be adjusted accordingly.

The _APTO_OLD_LANGUAGE_CODE constant should include the old language code. The _APTO_NEW_LANGUAGE_CODE need to held the new one.

    //define the constants
    define ('_APTO_OLD_LANGUAGE_CODE', 'en');
    define ('_APTO_NEW_LANGUAGE_CODE', 'de');
    
    include('wp-config.php');
    
    global $post;
    
    $args   =   array(
                        'post_type'         =>  'apto_sort',
                        'post_parent'       =>  '0',
                        'posts_per_page'    =>  -1
                        );
    $custm_query        =   new WP_Query( $args );
    while( $custm_query->have_posts() )
        {
            $custm_query->the_post();   
            
            //get all sort views
            $args   =   array(
                                'post_type'         =>  'apto_sort',
                                'post_parent'       =>  $post->ID,
                                'posts_per_page'    =>  -1  
                                );
            $sort_views =   get_posts( $args );
            if ( count ( $sort_views ) < 1 ) 
                continue;
                
            foreach ( $sort_views   as  $sort_view) 
                {
                    $_view_language =   get_post_meta( $sort_view->ID, '_view_language', TRUE );
                    if ( $_view_language !=  _APTO_OLD_LANGUAGE_CODE )
                        continue;
                        
                    update_post_meta( $sort_view->ID, '_view_language', _APTO_NEW_LANGUAGE_CODE );
                    
                }
            
        }

The post Update the sorts when changing the site default language appeared first on Nsp Code.

]]>
https://www.nsp-code.com/update-sorts-changing-site-default-language/feed/ 0
Add numbers to items within the sort list interface https://www.nsp-code.com/add-numbers-items-within-sort-list-interface/ https://www.nsp-code.com/add-numbers-items-within-sort-list-interface/#respond Fri, 23 Feb 2018 15:16:19 +0000 https://www.nsp-code.com/?p=15073 The Advanced Post Types Order plugin is based on jQuery Sortable UI to implement the drag & drop interface. As default this use the Unordered List tag (ul). For better control, some users might find easier to see ordered list (ol) instead which output numbers for each item in the list, accordingly to position in […]

The post Add numbers to items within the sort list interface appeared first on Nsp Code.

]]>
The Advanced Post Types Order plugin is based on jQuery Sortable UI to implement the drag & drop interface. As default this use the Unordered List tag (ul). For better control, some users might find easier to see ordered list (ol) instead which output numbers for each item in the list, accordingly to position in the group.

The interface become to the following:

This require a filter usage and a bit of code :

    add_filter('apto/sort_interface/list_type_tag', 'apto_sort_interface_list_type_tag');
    function apto_sort_interface_list_type_tag( $list_type_tag )
        {
            
            $list_type_tag   =   'ol';
            
            return $list_type_tag;
            
        }

The custom code should be placed inside theme functions.php or within a custom plugin.

The post Add numbers to items within the sort list interface appeared first on Nsp Code.

]]>
https://www.nsp-code.com/add-numbers-items-within-sort-list-interface/feed/ 0
How to sort posts automatically by a number/year included in title https://www.nsp-code.com/sort-posts-automatically-number-year-included-title/ https://www.nsp-code.com/sort-posts-automatically-number-year-included-title/#respond Wed, 07 Feb 2018 22:12:09 +0000 https://www.nsp-code.com/?p=14986 Manual sorting is a fun process, order adjustments are easy through the Advanced Post Types Order plugin drag & drop interface. Automatisation is possible through Automatic Order functionality. This provides a powerful set-up for creating algorithmic based sorting without a human intervention. For large list, this might be a good option to save good time […]

The post How to sort posts automatically by a number/year included in title appeared first on Nsp Code.

]]>
Manual sorting is a fun process, order adjustments are easy through the Advanced Post Types Order plugin drag & drop interface. Automatisation is possible through Automatic Order functionality. This provides a powerful set-up for creating algorithmic based sorting without a human intervention. For large list, this might be a good option to save good time spent on manual sorting adjustments.

For some sites, the post title ( or any other post data like content, excerpt etc ) might contain important information on which a sort is required to apply. For example a year number can be included in the title e.g.:
– Canada 1978 edition
– Spain 2000 issues
– Early Belgium 2003 fabric
– Bohemia and Moravia 1944 history

Sorting ascending by the year in titles can be done manually, however automatisation is easy achievable through Custom Function Callback and a bit of code.

 

In this particular example the callback function name is called “custom_sorting_function_sort_by_number_in_title”. The function should be included within theme functions.php or a custom plugin:

function custom_sorting_function_sort_by_number_in_title( $posts_list, $sort_view_id, $orderBy, $query)
        {
            $list_map   =   array();
            foreach($posts_list  as  $object_id)
                {
                    $object =   get_post($object_id);
                    
		    //extract the title using a regex	
                    preg_match('/\d{4}/', $object->post_title, $match );
                    
                    if ( $match  != FALSE )
                        $list_map[$object_id]   =   $match[0];    
                        else
                        $list_map[$object_id]   =   '';
                }
                 
            //sort the dates ascending
            arsort($list_map, SORT_NUMERIC);
             
            $post_list  =   array_keys($list_map);
                 
            return $post_list;  
        }

The post How to sort posts automatically by a number/year included in title appeared first on Nsp Code.

]]>
https://www.nsp-code.com/sort-posts-automatically-number-year-included-title/feed/ 0
How to copy an order list from a category to another https://www.nsp-code.com/copy-order-list-category-another/ https://www.nsp-code.com/copy-order-list-category-another/#respond Wed, 07 Feb 2018 16:59:39 +0000 https://www.nsp-code.com/?p=14974 Customizing order of posts within categories is an easy task through Advanced Post Types Order plugin. The self explanatory interface featuring a drag & drop functionality makes sorting a breeze. For large posts lists, order customize can take a bit of time, unless use automatic ordering. Manual sorting require drag & drop for individual posts […]

The post How to copy an order list from a category to another appeared first on Nsp Code.

]]>
Customizing order of posts within categories is an easy task through Advanced Post Types Order plugin. The self explanatory interface featuring a drag & drop functionality makes sorting a breeze.

For large posts lists, order customize can take a bit of time, unless use automatic ordering. Manual sorting require drag & drop for individual posts in the list, up and down, Multiple Posts Selection can be done to speed up the process.

A category sorting (or any other taxonomy term) can be migrated to another one by a simple push of a button. This is useful when posts exists in booth terms and order is required to be the same, or similar (allow further adjustments).

There is a “Copy this sort to” option at the end of drag & drop list, which allow list order to apply o the other side.

To achieve this functionality, the following code is required. This can be placed inside theme functions.php or within a custom plugin.

add_action('APTO/re-order-interface/footer-html','apto_reorder_interface_copy_sort');
    function apto_reorder_interface_copy_sort( $sort_view_id )
        {
            
            $sort_view_settings =  APTO_functions::get_sort_view_settings($sort_view_id);
            $sort_view_post     =  get_post($sort_view_id);
            $sort_ID            =  $sort_view_post->post_parent;
            
            
            ?>
            <style>
                .major-publishing-actions  p {margin: 0px}
                #apto_sort_copy img.process_image  {display: none} 
            </style>
            <div class="alignleft actions" id="apto_sort_copy"> 
                <p>Copy this sort to 
                <select name="apto_sort_copy_to" id="apto_sort_copy_to">
                    <?php
                    
                    $args  =  array(
                                        'post_type'    =>  'apto_sort',
                                        'post_parent'  =>  $sort_ID
                                        );
                    $custom_query      =  new WP_Query( $args );
                    
                    //make a sorted list of items
                    $sort_view_list =  array();
                    foreach($custom_query->posts    as  $found_sort_view_post)
                        {
                            $sort_view_post_details =  APTO_functions::get_sort_view_settings( $found_sort_view_post->ID );      
                            
                            if(empty($sort_view_post_details['_term_id']))
                                continue;
                                
                            if($sort_view_post->ID ==  $found_sort_view_post->ID)
                                continue;
                            
                            $term_data  =  get_term_by('id', $sort_view_post_details['_term_id'], $sort_view_post_details['_taxonomy']);
                            $sort_view_list[ $sort_view_post_details['_taxonomy'] ][ $found_sort_view_post->ID ]  =  $term_data->name;
                        }
                        
                    
                    foreach($sort_view_list    as  $taxonomy_name  =>  $data)
                        {
                            
                            asort($data);
                            
                            foreach($data  as  $sort_view_id    =>  $term_name)
                                {
                            
                                    ?>
                                    <option value="<?php echo $sort_view_id ?>"><?php echo ucfirst($taxonomy_name) ?> - <?php echo $term_name ?></option>
                                    <?php  
                                } 
                            
                        }
                    
                    ?>
                </select> <a href="javascript: void(0)" class="button-secondary" id="apto_sort_copy_button" onClick="apto_sort_copy_confirmSubmit()">Copy</a> <img alt="" src="<?php echo APTO_URL ?>/images/wpspin_light.gif" class="process_image"></p>
                
                <script type="text/javascript">
                                      
                    function apto_sort_copy_confirmSubmit()
                        {
                            var agree=confirm("Are you sure you want to copy this sort? All orders will be lost on the other side.");
                            if (agree)
                                {
                                    
                                    jQuery('#apto_sort_copy img.process_image').show();
                                    
                                    var queryString = { 
                                                    action:            'apto_sort_copy', 
                                                    sort_view_id:        sort_view_id,
                                                    copy_to:            jQuery('#apto_sort_copy_to').val(),
                                                    nonce:              '<?php echo wp_create_nonce( 'apto_sort_copy-' . get_current_user_id()) ?>'
                                                        };
                                    
                                    jQuery.ajax({
                                              type: 'POST',
                                              url: ajaxurl,
                                              data: queryString,
                                              cache: false,
                                              dataType: "html",
                                              success: function(response){
                                                jQuery('#apto_sort_copy img.process_image').hide();
                                                alert('Sort successfully copied');
                                              },
                                              error: function(html){
                                                jQuery('#apto_sort_copy img.process_image').hide();
                                                  }
                                            });    
                                }
                                else
                                {
                                    return false ;
                                }
                        }
                            
                </script>
                
            </div>
            
            <?php  
            
            
            
        }
        
    add_action( 'wp_ajax_apto_sort_copy', 'wp_ajax_apto_sort_copy' );
    function wp_ajax_apto_sort_copy()
        {
            
            //check for nonce
            if(! wp_verify_nonce($_POST['nonce'],  'apto_sort_copy-' . get_current_user_id()))
                {
                    _e( 'Invalid Nonce', 'apto' );
                    die();  
                }  
            
            $sort_view_id  =  $_POST['sort_view_id'];
            $copy_to        =  $_POST['copy_to'];
            
            global $wpdb;
            
            $query = "DELETE FROM `". $wpdb->prefix ."apto_sort_list`
                            WHERE `sort_view_id`    =  ". $copy_to;
            $results = $wpdb->get_results($query);
            
            //retrieve the sort list
            $query  =  "SELECT object_id FROM `". $wpdb->prefix ."apto_sort_list`
                            WHERE sort_view_id  = '". $sort_view_id ."'";
            $results        =  $wpdb->get_results( $query );
            
            if(count($results)  <  1)
                die();
            
            $query = "INSERT INTO `". $wpdb->prefix ."apto_sort_list` 
                        (`sort_view_id`, `object_id`) 
                        VALUES ";
            
            $sort_list  =  array();
            $first      =  TRUE;
            foreach($results    as  $result)
                {
                    
                    if($first  === FALSE)
                        $query  .=  ', ';
                        
                    $query  .= " ('" . $copy_to . "', '" . $result->object_id . "') ";
                    
                    $first  =  FALSE;
                    
                }
                    
            $results = $wpdb->get_results( $query );
            
            die();
            
        }

The post How to copy an order list from a category to another appeared first on Nsp Code.

]]>
https://www.nsp-code.com/copy-order-list-category-another/feed/ 0