|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Main plugin class. |
| 4 | + * |
| 5 | + * @package OptimizationsAceMc |
| 6 | + */ |
| 7 | + |
| 8 | +// If this file is called directly, abort. |
| 9 | +if ( ! defined( 'WPINC' ) ) { |
| 10 | + die; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Main plugin class. |
| 15 | + */ |
| 16 | +class Optimizations_Ace_Mc { |
| 17 | + |
| 18 | + /** |
| 19 | + * The single instance of the class. |
| 20 | + * |
| 21 | + * @var Optimizations_Ace_Mc|null |
| 22 | + */ |
| 23 | + protected static $instance = null; |
| 24 | + |
| 25 | + /** |
| 26 | + * Main instance. |
| 27 | + * |
| 28 | + * @return Optimizations_Ace_Mc |
| 29 | + */ |
| 30 | + public static function instance() { |
| 31 | + if ( null === self::$instance ) { |
| 32 | + self::$instance = new self(); |
| 33 | + } |
| 34 | + return self::$instance; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Constructor. |
| 39 | + */ |
| 40 | + public function __construct() { |
| 41 | + add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); |
| 42 | + |
| 43 | + // Initialize optimizations. |
| 44 | + $this->init_optimizations(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Load plugin textdomain. |
| 49 | + */ |
| 50 | + public function load_textdomain() { |
| 51 | + load_plugin_textdomain( |
| 52 | + 'optimizations-ace-mc', |
| 53 | + false, |
| 54 | + dirname( OPTIMIZATIONS_ACE_MC_PLUGIN_BASENAME ) . '/languages/' |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Initialize optimization functions. |
| 60 | + */ |
| 61 | + private function init_optimizations() { |
| 62 | + // WooCommerce optimizations. |
| 63 | + $this->init_woocommerce_optimizations(); |
| 64 | + |
| 65 | + // WP Store Locator optimizations. |
| 66 | + $this->init_wpsl_optimizations(); |
| 67 | + |
| 68 | + // WordPress admin optimizations. |
| 69 | + $this->init_admin_optimizations(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Initialize WooCommerce-specific optimizations. |
| 74 | + */ |
| 75 | + private function init_woocommerce_optimizations() { |
| 76 | + // Only run if WooCommerce is active. |
| 77 | + if ( ! $this->is_woocommerce_active() ) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Show empty categories in WooCommerce. |
| 82 | + add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' ); |
| 83 | + |
| 84 | + // Hide category product count in product archives. |
| 85 | + add_filter( 'woocommerce_subcategory_count_html', '__return_false' ); |
| 86 | + |
| 87 | + // Add order count column to users admin. |
| 88 | + if ( is_admin() && current_user_can( 'list_users' ) ) { |
| 89 | + add_filter( 'manage_users_columns', array( $this, 'add_user_order_count_column' ) ); |
| 90 | + add_filter( 'manage_users_custom_column', array( $this, 'display_user_order_count_column' ), 10, 3 ); |
| 91 | + add_filter( 'manage_users_sortable_columns', array( $this, 'make_user_order_count_sortable' ) ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Initialize WP Store Locator optimizations. |
| 97 | + */ |
| 98 | + private function init_wpsl_optimizations() { |
| 99 | + // Only run if WP Store Locator is active. |
| 100 | + if ( ! $this->is_wpsl_active() ) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Show store categories in store locator. |
| 105 | + add_filter( 'wpsl_store_meta', array( $this, 'add_store_categories_to_meta' ), 10, 2 ); |
| 106 | + add_filter( 'wpsl_info_window_template', array( $this, 'customize_info_window_template' ) ); |
| 107 | + |
| 108 | + // Disable REST API for store locator post type. |
| 109 | + add_filter( 'wpsl_post_type_args', array( $this, 'custom_post_type_args' ) ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Initialize WordPress admin optimizations. |
| 114 | + */ |
| 115 | + private function init_admin_optimizations() { |
| 116 | + if ( ! is_admin() || ! current_user_can( 'list_users' ) ) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + // Add registration date column to users admin. |
| 121 | + add_filter( 'manage_users_columns', array( $this, 'add_user_registration_date_column' ) ); |
| 122 | + add_filter( 'manage_users_custom_column', array( $this, 'display_user_registration_date_column' ), 10, 3 ); |
| 123 | + add_filter( 'manage_users_sortable_columns', array( $this, 'make_user_registration_date_sortable' ) ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Check if WooCommerce is active. |
| 128 | + * |
| 129 | + * @return bool |
| 130 | + */ |
| 131 | + private function is_woocommerce_active() { |
| 132 | + return class_exists( 'WooCommerce' ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Check if WP Store Locator is active. |
| 137 | + * |
| 138 | + * @return bool |
| 139 | + */ |
| 140 | + private function is_wpsl_active() { |
| 141 | + return class_exists( 'WPSL_Frontend' ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Add order count column to users table. |
| 146 | + * |
| 147 | + * @param array $columns Existing columns. |
| 148 | + * @return array Modified columns. |
| 149 | + */ |
| 150 | + public function add_user_order_count_column( $columns ) { |
| 151 | + $columns['user_order_count'] = __( 'Order Count', 'optimizations-ace-mc' ); |
| 152 | + return $columns; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Display order count in users table. |
| 157 | + * |
| 158 | + * @param string $output Custom column output. |
| 159 | + * @param string $column_name Name of the column. |
| 160 | + * @param int $user_id User ID. |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + public function display_user_order_count_column( $output, $column_name, $user_id ) { |
| 164 | + if ( 'user_order_count' === $column_name ) { |
| 165 | + if ( function_exists( 'wc_get_customer_order_count' ) ) { |
| 166 | + $order_count = wc_get_customer_order_count( absint( $user_id ) ); |
| 167 | + return esc_html( number_format_i18n( $order_count ) ); |
| 168 | + } |
| 169 | + return '0'; |
| 170 | + } |
| 171 | + return $output; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Make order count column sortable. |
| 176 | + * |
| 177 | + * @param array $columns Sortable columns. |
| 178 | + * @return array |
| 179 | + */ |
| 180 | + public function make_user_order_count_sortable( $columns ) { |
| 181 | + $columns['user_order_count'] = 'user_order_count'; |
| 182 | + return $columns; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Add store categories to store meta. |
| 187 | + * |
| 188 | + * @param array $store_meta Existing store meta. |
| 189 | + * @param int $store_id Store ID. |
| 190 | + * @return array |
| 191 | + */ |
| 192 | + public function add_store_categories_to_meta( $store_meta, $store_id ) { |
| 193 | + $terms = get_the_terms( absint( $store_id ), 'wpsl_store_category' ); |
| 194 | + $store_meta['terms'] = ''; |
| 195 | + |
| 196 | + if ( $terms && ! is_wp_error( $terms ) ) { |
| 197 | + if ( count( $terms ) > 1 ) { |
| 198 | + $location_terms = array(); |
| 199 | + foreach ( $terms as $term ) { |
| 200 | + if ( ! empty( $term->name ) ) { |
| 201 | + $location_terms[] = sanitize_text_field( $term->name ); |
| 202 | + } |
| 203 | + } |
| 204 | + $store_meta['terms'] = implode( ', ', $location_terms ); |
| 205 | + } elseif ( ! empty( $terms[0]->name ) ) { |
| 206 | + $store_meta['terms'] = sanitize_text_field( $terms[0]->name ); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return $store_meta; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Customize info window template to include categories. |
| 215 | + * |
| 216 | + * @return string |
| 217 | + */ |
| 218 | + public function customize_info_window_template() { |
| 219 | + $info_window_template = '<div data-store-id="<%= id %>" class="wpsl-info-window">' . "\r\n"; |
| 220 | + $info_window_template .= ' <p>' . "\r\n"; |
| 221 | + $info_window_template .= ' ' . wpsl_store_header_template() . "\r\n"; |
| 222 | + $info_window_template .= ' <span><%= address %></span>' . "\r\n"; |
| 223 | + $info_window_template .= ' <% if ( address2 ) { %>' . "\r\n"; |
| 224 | + $info_window_template .= ' <span><%= address2 %></span>' . "\r\n"; |
| 225 | + $info_window_template .= ' <% } %>' . "\r\n"; |
| 226 | + $info_window_template .= ' <span>' . wpsl_address_format_placeholders() . '</span>' . "\r\n"; |
| 227 | + $info_window_template .= ' </p>' . "\r\n"; |
| 228 | + |
| 229 | + // Include the category names. |
| 230 | + $info_window_template .= ' <% if ( terms ) { %>' . "\r\n"; |
| 231 | + $info_window_template .= ' <p>' . esc_html__( 'Certifications:', 'optimizations-ace-mc' ) . ' <%= terms %></p>' . "\r\n"; |
| 232 | + $info_window_template .= ' <% } %>' . "\r\n"; |
| 233 | + |
| 234 | + $info_window_template .= ' <%= createInfoWindowActions( id ) %>' . "\r\n"; |
| 235 | + $info_window_template .= ' </div>' . "\r\n"; |
| 236 | + |
| 237 | + return $info_window_template; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Disable REST API for WP Store Locator post type. |
| 242 | + * |
| 243 | + * @param array $args Post type arguments. |
| 244 | + * @return array |
| 245 | + */ |
| 246 | + public function custom_post_type_args( $args ) { |
| 247 | + $args['show_in_rest'] = false; |
| 248 | + return $args; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Add registration date column to users table. |
| 253 | + * |
| 254 | + * @param array $columns Existing columns. |
| 255 | + * @return array |
| 256 | + */ |
| 257 | + public function add_user_registration_date_column( $columns ) { |
| 258 | + $columns['registration_date'] = __( 'Registration Date', 'optimizations-ace-mc' ); |
| 259 | + return $columns; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Display registration date in users table. |
| 264 | + * |
| 265 | + * @param string $output Custom column output. |
| 266 | + * @param string $column_name Name of the column. |
| 267 | + * @param int $user_id User ID. |
| 268 | + * @return string |
| 269 | + */ |
| 270 | + public function display_user_registration_date_column( $output, $column_name, $user_id ) { |
| 271 | + if ( 'registration_date' === $column_name ) { |
| 272 | + $registration_date = get_the_author_meta( 'registered', absint( $user_id ) ); |
| 273 | + if ( $registration_date ) { |
| 274 | + $date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 275 | + return esc_html( wp_date( $date_format, strtotime( $registration_date ) ) ); |
| 276 | + } |
| 277 | + return esc_html__( 'Unknown', 'optimizations-ace-mc' ); |
| 278 | + } |
| 279 | + return $output; |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Make registration date column sortable. |
| 284 | + * |
| 285 | + * @param array $columns Sortable columns. |
| 286 | + * @return array |
| 287 | + */ |
| 288 | + public function make_user_registration_date_sortable( $columns ) { |
| 289 | + $columns['registration_date'] = 'registered'; |
| 290 | + return $columns; |
| 291 | + } |
| 292 | +} |
0 commit comments