Plugin Directory

source: ultimakit-for-wp/tags/1.2.0/modules/term-order/class-wpultimakit-module-term-order.php @ 3096242

Last change on this file since 3096242 was 3096242, checked in by ankitmaru, 8 weeks ago

1.2.0. - New modules added: (Free) - Change Admin Email, Show Active Plugins First, Redirect After Login, Redirect After Logout, Term Order; (PRO) - Front-End Login Form, UTM Builder; Modules listing layout updated; Minimize and maximize modules listing; Search modules feature added; Minor fixes & improvements.

File size: 7.1 KB
Line 
1<?php
2/**
3 * Class UltimaKit_Module_Term_Order
4 *
5 * @since 1.0.0
6 * @package    UltimaKit
7 */
8
9/**
10 * Class UltimaKit_Module_Term_Order
11 *
12 * @since 1.0.0
13 */
14class UltimaKit_Module_Term_Order extends UltimaKit_Module_Manager {
15        /**
16         * @var string
17         */
18        protected $ID = 'ultimakit_module_term_order';
19
20        /**
21         * The name of the module.
22         *
23         * @var string
24         */
25        protected $name;
26
27        /**
28         * A brief description of what the module does.
29         *
30         * @var string
31         */
32        protected $description;
33
34        /**
35         * The pricing plan associated with the module.
36         *
37         * @var string
38         */
39        protected $plan = 'free';
40
41        /**
42         * The category of functionality the module falls under.
43         *
44         * @var string
45         */
46        protected $category = 'Admin';
47
48        /**
49         * The type of module, indicating its platform or use case.
50         *
51         * @var string
52         */
53        protected $type = 'WordPress';
54
55        /**
56         * Flag indicating whether the module is active.
57         *
58         * @var bool
59         */
60        protected $is_active;
61
62        /**
63         * URL providing more detailed information about the module.
64         *
65         * @var string
66         */
67        protected $read_more_link = 'set-term-order-in-wordpress';
68
69        /**
70         * The settings associated with the module, if any.
71         *
72         * @var array
73         */
74        protected $settings;
75
76        /**
77         * Initializes the module with default values for properties and prepares
78         * any necessary setup or hooks into WordPress. This may include setting
79         * initial values, registering hooks, or preparing resources needed for
80         * the module to function properly within WordPress.
81         */
82        public function __construct() {
83                $this->name        = __( 'Term Order', 'ultimakit-for-wp' );
84                $this->description = __( 'Enable drag-and-drop term reordering in WordPress.', 'ultimakit-for-wp' );
85                $this->is_active   = $this->isModuleActive( $this->ID );
86                $this->settings    = 'yes';
87                $this->initializeModule();
88
89                // add_action('ultimakit_module_action_fired', array($this,'module_updated'), 1, 2 );
90        }
91
92        /**
93         * Initializes the specific module within the application.
94         *
95         * This function is responsible for performing the initial setup required to get the module
96         * up and running. This includes registering hooks and filters, enqueing styles and scripts,
97         * and any other preliminary setup tasks that need to be performed before the module can
98         * start functioning as expected.
99         *
100         * It's typically called during the plugin or theme's initialization phase, ensuring that
101         * all module dependencies are loaded and ready for use.
102         *
103         * @return void
104         */
105        protected function initializeModule() {
106                if ( $this->is_active ) {
107                        add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
108                        add_action( 'admin_footer', array( $this, 'add_modal' ) );
109
110                        add_action( 'wp_ajax_ultimakit_save_term_order', array( $this, 'save_term_order' ) );
111                        add_filter( 'get_terms_args', array( $this, 'include_term_order'), 10, 2 );
112                       
113                        // Add filter conditionally for the admin
114                        if ( is_admin() ) {
115                            add_filter('get_terms', array($this, 'order_terms_by_term_meta'), 10, 3);
116                        }
117
118                        if( 'on' === $this->getModuleSettings( $this->ID, 'show_on_frontend' ) ){
119                                add_filter( 'get_terms',  array( $this, 'order_terms_by_term_meta' ), 10, 3 );
120                        }
121                       
122                }
123        }
124
125        /**
126         * Enqueues scripts for the theme or plugin.
127         *
128         * This function handles the registration and enqueuing of JavaScript files required
129         * by the theme or plugin. It ensures that scripts are loaded in the correct order and
130         * that dependencies are managed properly. Scripts can include both local and external
131         * resources, and may be conditionally loaded based on the context or user actions.
132         *
133         * Use this function to enqueue all JavaScript necessary for the functionality of your
134         * theme or plugin, adhering to WordPress best practices for script registration and
135         * enqueuing.
136         *
137         * @return void
138         */
139        public function add_scripts( $hook ) {
140
141                if ( $hook == 'edit-tags.php' || 'toplevel_page_wp-ultimakit-dashboard' === $hook ) {
142                wp_enqueue_script('jquery-ui-sortable');
143
144                wp_enqueue_script(
145                                'ultimakit-module-script-' . $this->ID,
146                                plugins_url( '/module-script.js', __FILE__ ),
147                                array('jquery', 'jquery-ui-sortable'),
148                                ULTIMAKIT_FOR_WP_VERSION,
149                                true
150                        );
151
152                        wp_localize_script(
153                                'ultimakit-module-script-' . $this->ID,
154                                'ultimakit_term_order',
155                                array(
156                                        'ajax_url' => admin_url( 'admin-ajax.php' ),
157                                        'ajax_nonce' => wp_create_nonce( 'ultimakit-term-order' ),
158                                        'taxonomy' => isset($_GET['taxonomy'])?$_GET['taxonomy']:''
159                                )
160                        );
161            }
162        }
163
164        /**
165         * Adds a modal dialog to the page.
166         *
167         * This function is responsible for initiating and rendering a modal dialog within the
168         * application or website interface. It typically involves setting up the necessary HTML
169         * and JavaScript for the modal to function and display correctly. The modal can be used
170         * for various purposes, such as displaying information, confirming actions, or collecting
171         * user input.
172         *
173         * @return void
174         */
175        public function add_modal() {
176                $arguments          = array();
177                $arguments['ID']    = $this->ID;
178                $arguments['title'] = __( 'Term Order', 'ultimakit-for-wp' );
179
180                $arguments['fields'] = array(
181                        'show_on_frontend'    => array(
182                                'type'  => 'switch',
183                                'label' => __( 'Display the custom order of terms in frontend queries.', 'ultimakit-for-wp' ),
184                                'value' => $this->getModuleSettings( $this->ID, 'show_on_frontend' ),
185                        ),
186                );
187                $this->ultimakit_generate_modal( $arguments );
188
189        }
190
191        public function save_term_order() {
192            // Security check (adjust capability as needed)
193            if ( ! current_user_can('manage_categories') || ! wp_verify_nonce( $_POST['nonce'], 'ultimakit-term-order' ) ) {
194                wp_send_json_error(array('message' => __('Unauthorized', 'ultimakit-for-wp')), 401);
195            }
196
197
198            // Check if it's an AJAX request
199            if ( defined('DOING_AJAX') && DOING_AJAX ) { 
200                $order = $_POST['order'];
201                // parse_str($order, $term_order);
202                global $wpdb;
203
204                foreach ($order as $term_id) {
205                        update_term_meta( $term_id['term_id'], 'term_order', $term_id['position'] );
206                    }
207
208                // Respond with success
209                wp_send_json_success( __('Terms order saved successfully!', 'ultimakit-for-wp') ); 
210            } else {
211                // Not an AJAX request, handle it accordingly (e.g., error message)
212                wp_send_json_error( __('Something went wrong, Please try again.', 'ultimakit-for-wp') );
213            }
214
215        }
216       
217        public function include_term_order($args, $taxonomies) {
218            if ( isset($_GET['taxonomy']) && in_array( $_GET['taxonomy'], $taxonomies)) {
219                $args['orderby'] = 'term_order';
220                $args['order'] = 'ASC';
221            }
222            return $args;
223        }
224
225        public function order_terms_by_term_meta($terms, $taxonomies, $args) {
226       
227            if (isset($args['orderby']) && $args['orderby'] === 'term_order') {
228                usort($terms, function($a, $b) {
229                   
230                    $order_a = get_term_meta($a->term_id, 'term_order', true);
231                    $order_b = get_term_meta($b->term_id, 'term_order', true);
232
233                    $order_a = is_numeric($order_a) ? (int)$order_a : 0;
234                    $order_b = is_numeric($order_b) ? (int)$order_b : 0;
235
236                    return $order_a - $order_b; // Sort in ascending order
237                });
238            }
239
240            return $terms;
241        }
242}
Note: See TracBrowser for help on using the repository browser.