css
2 years ago
images
2 years ago
js
1 year ago
partials
1 year ago
class-linked-variation-admin.php
1 year ago
class-linked-variation-admin.php
1165 lines
| 1 | <?php |
| 2 | |
| 3 | // If this file is called directly, abort. |
| 4 | if ( !defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | /** |
| 8 | * The admin-specific functionality of the plugin. |
| 9 | * |
| 10 | * @link http://www.multidots.com |
| 11 | * @since 1.0.0 |
| 12 | * |
| 13 | * @package DSALV_Advanced_Linked_Variations |
| 14 | * @subpackage DSALV_Advanced_Linked_Variations/admin |
| 15 | */ |
| 16 | /** |
| 17 | * The admin-specific functionality of the plugin. |
| 18 | * |
| 19 | * Defines the plugin name, version, and two examples hooks for how to |
| 20 | * enqueue the admin-specific stylesheet and JavaScript. |
| 21 | * |
| 22 | * @package DSALV_Advanced_Linked_Variations |
| 23 | * @subpackage DSALV_Advanced_Linked_Variations/admin |
| 24 | * @author Multidots <inquiry@multidots.in> |
| 25 | */ |
| 26 | if ( !class_exists( 'DSALV_Advanced_Linked_Variations_Admin' ) ) { |
| 27 | class DSALV_Advanced_Linked_Variations_Admin |
| 28 | { |
| 29 | /** |
| 30 | * The ID of this plugin. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @access private |
| 34 | * @var string $plugin_name The ID of this plugin. |
| 35 | */ |
| 36 | private $plugin_name ; |
| 37 | /** |
| 38 | * The version of this plugin. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | * @access private |
| 42 | * @var string $version The current version of this plugin. |
| 43 | */ |
| 44 | private $version ; |
| 45 | |
| 46 | /** |
| 47 | * Initialize the class and set its properties. |
| 48 | * |
| 49 | * @param string $plugin_name The name of this plugin. |
| 50 | * @param string $version The version of this plugin. |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function __construct( $plugin_name, $version ) |
| 55 | { |
| 56 | $this->plugin_name = $plugin_name; |
| 57 | $this->version = $version; |
| 58 | } |
| 59 | /** |
| 60 | * Register the stylesheets for the admin area. |
| 61 | * |
| 62 | * @param string $hook display current page name |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | * |
| 66 | */ |
| 67 | public function dsalv_enqueue_styles( $hook ) |
| 68 | { |
| 69 | $post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 70 | if( empty( $post_type ) ){ |
| 71 | $post_type = get_post_type( get_the_ID() ); |
| 72 | } |
| 73 | if ( false !== strpos( $hook, '_page_alv' ) || ( isset( $post_type ) && 'dsalv' === $post_type )) { |
| 74 | wp_enqueue_style( |
| 75 | $this->plugin_name . 'select2-min-style', |
| 76 | plugin_dir_url( __FILE__ ) . 'css/select2.min.css', |
| 77 | array(), |
| 78 | 'all' |
| 79 | ); |
| 80 | wp_enqueue_style( |
| 81 | $this->plugin_name . 'jquery-ui-min-style', |
| 82 | plugin_dir_url( __FILE__ ) . 'css/jquery-ui.min.css', |
| 83 | array(), |
| 84 | 'all' |
| 85 | ); |
| 86 | wp_enqueue_style( |
| 87 | $this->plugin_name . 'font-awesome-style', |
| 88 | plugin_dir_url( __FILE__ ) . 'css/font-awesome.min.css', |
| 89 | array(), |
| 90 | 'all' |
| 91 | ); |
| 92 | wp_enqueue_style( |
| 93 | $this->plugin_name . 'main-style', |
| 94 | plugin_dir_url( __FILE__ ) . 'css/style.css', |
| 95 | array(), |
| 96 | 'all' |
| 97 | ); |
| 98 | wp_enqueue_style( |
| 99 | $this->plugin_name . 'media-style', |
| 100 | plugin_dir_url( __FILE__ ) . 'css/media.css', |
| 101 | array(), |
| 102 | 'all' |
| 103 | ); |
| 104 | wp_enqueue_style( |
| 105 | $this->plugin_name . 'notice-css', |
| 106 | plugin_dir_url( __FILE__ ) . 'css/notice.css', |
| 107 | array(), |
| 108 | 'all' |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Register the JavaScript for the admin area. |
| 116 | * |
| 117 | * @param string $hook display current page name |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | * |
| 121 | */ |
| 122 | public function dsalv_enqueue_scripts( $hook ) |
| 123 | { |
| 124 | $post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 125 | if( empty( $post_type ) ){ |
| 126 | $post_type = get_post_type( get_the_ID() ); |
| 127 | } |
| 128 | if ( false !== strpos( $hook, '_page_alv' ) || ( isset( $post_type ) && 'dsalv' === $post_type ) ) { |
| 129 | wp_enqueue_script( |
| 130 | $this->plugin_name . 'select2', |
| 131 | plugin_dir_url( __FILE__ ) . 'js/select2.min.js', |
| 132 | array( 'jquery' ), |
| 133 | $this->version, |
| 134 | true |
| 135 | ); |
| 136 | wp_enqueue_script( |
| 137 | $this->plugin_name . 'admin-js', |
| 138 | plugin_dir_url( __FILE__ ) . 'js/linked-variations-admin.js', |
| 139 | array(), |
| 140 | $this->version, |
| 141 | true |
| 142 | ); |
| 143 | wp_localize_script( $this->plugin_name . 'admin-js', 'coditional_vars', array( |
| 144 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 145 | 'dsalv_ajax_nonce' => wp_create_nonce( 'dsalv_ajax_request_nonce' ), |
| 146 | ) ); |
| 147 | |
| 148 | wp_enqueue_script( |
| 149 | $this->plugin_name . 'lvg-admin-js', |
| 150 | plugin_dir_url( __FILE__ ) . 'js/linked-variations-group-admin.js', |
| 151 | array( |
| 152 | 'jquery', |
| 153 | 'wc-enhanced-select', |
| 154 | 'jquery-ui-sortable' |
| 155 | ), |
| 156 | $this->version, |
| 157 | true |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Shipping method Pro Menu |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | public function dsalv_admin_menu_intigration() |
| 169 | { |
| 170 | global $GLOBALS ; |
| 171 | if ( empty($GLOBALS['admin_page_hooks']['dots_store']) ) { |
| 172 | add_menu_page( |
| 173 | 'DotStore Plugins', |
| 174 | __( 'DotStore Plugins', 'linked-variation' ), |
| 175 | 'null', |
| 176 | 'dots_store', |
| 177 | 'dot_store_menu_page', |
| 178 | 'dashicons-marker', |
| 179 | 25 |
| 180 | ); |
| 181 | } |
| 182 | add_submenu_page( |
| 183 | 'dots_store', |
| 184 | 'Advanced Linked Variations', |
| 185 | __( 'Advanced Linked Variations', 'linked-variation' ), |
| 186 | 'manage_options', |
| 187 | 'alv-settings', |
| 188 | array( $this, 'dsalv_admin_settings_page' ) |
| 189 | ); |
| 190 | add_submenu_page( |
| 191 | 'edit.php?post_type=dsalv', |
| 192 | 'Linked Variations Group', |
| 193 | __( 'Advanced Linked Variations', 'linked-variation' ), |
| 194 | 'manage_options', |
| 195 | 'alv-linked-variations-group', |
| 196 | array( $this, 'dsalv_admin_variation_group_page' ) |
| 197 | ); |
| 198 | add_submenu_page( |
| 199 | 'dots_store', |
| 200 | 'Getting Started', |
| 201 | __( 'Getting Started', 'linked-variation' ), |
| 202 | 'manage_options', |
| 203 | 'alv-get-started', |
| 204 | array( $this, 'dsalv_get_started_page' ) |
| 205 | ); |
| 206 | add_submenu_page( |
| 207 | 'dots_store', |
| 208 | 'Quick info', |
| 209 | __( 'Quick info', 'linked-variation' ), |
| 210 | 'manage_options', |
| 211 | 'alv-information', |
| 212 | array( $this, 'dsalv_information_page' ) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Add custom css for dotstore icon in admin area |
| 218 | * |
| 219 | * @since 1.0.2 |
| 220 | * |
| 221 | */ |
| 222 | public function dsalv_admin_main_menu_icon_css() { |
| 223 | echo '<style> |
| 224 | .toplevel_page_dots_store .dashicons-marker::after{content:"";border:3px solid;position:absolute;top:14px;left:15px;border-radius:50%;opacity: 0.6;} |
| 225 | li.toplevel_page_dots_store:hover .dashicons-marker::after,li.toplevel_page_dots_store.current .dashicons-marker::after{opacity: 1;} |
| 226 | @media only screen and (max-width: 960px){ |
| 227 | .toplevel_page_dots_store .dashicons-marker::after{left:14px;} |
| 228 | } |
| 229 | </style>'; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Quick guide page |
| 234 | * |
| 235 | * @since 1.0.0 |
| 236 | */ |
| 237 | public function dsalv_get_started_page() |
| 238 | { |
| 239 | require_once plugin_dir_path( __FILE__ ) . 'partials/dsalv-get-started-page.php'; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Plugin information page |
| 244 | * |
| 245 | * @since 1.0.0 |
| 246 | */ |
| 247 | public function dsalv_information_page() |
| 248 | { |
| 249 | require_once plugin_dir_path( __FILE__ ) . 'partials/dsalv-information-page.php'; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Plugin information page |
| 254 | * |
| 255 | * @since 1.0.0 |
| 256 | */ |
| 257 | public function dsalv_admin_settings_page() |
| 258 | { |
| 259 | require_once plugin_dir_path( __FILE__ ) . 'partials/dsalv-admin-settings-page.php'; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Plugin variation group page |
| 264 | * |
| 265 | * @since 1.0.0 |
| 266 | */ |
| 267 | public function dsalv_admin_variation_group_page() |
| 268 | { |
| 269 | require_once plugin_dir_path( __FILE__ ) . 'partials/dsalv-variation-group-page.php'; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Remove submenu from admin screeen |
| 274 | * |
| 275 | * @since 1.0.0 |
| 276 | */ |
| 277 | public function dsalv_remove_admin_submenus() |
| 278 | { |
| 279 | remove_submenu_page( 'dots_store', 'alv-get-started' ); |
| 280 | remove_submenu_page( 'dots_store', 'alv-information' ); |
| 281 | remove_submenu_page( 'dots_store', 'alv-linked-variations-group' ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * plugin admin review notice. |
| 286 | */ |
| 287 | public function dsalv_admin_notice_review_callback() { |
| 288 | $get_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 289 | if( empty( $get_post_type ) ){ |
| 290 | $get_post_type = get_post_type( get_the_ID() ); |
| 291 | } |
| 292 | if ( isset( $get_post_type ) && $get_post_type === 'dsalv' ) { |
| 293 | include_once plugin_dir_path( __FILE__ ) . 'partials/header/plugin-header.php'; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Show admin footer review text. |
| 299 | * |
| 300 | * @since 1.0.0 |
| 301 | */ |
| 302 | public function dsalv_admin_footer_review() |
| 303 | { |
| 304 | $url = esc_url( 'https://wordpress.org/plugins/linked-variation/#reviews' ); |
| 305 | $html = sprintf( wp_kses( __( '<strong>We need your support</strong> to keep updating and improving the plugin. Please <a href="%1$s" target="_blank">help us by leaving a good review</a> :) Thanks!', 'linked-variation' ), array( |
| 306 | 'strong' => array(), |
| 307 | 'a' => array( |
| 308 | 'href' => array(), |
| 309 | 'target' => 'blank', |
| 310 | ), |
| 311 | ) ), esc_url( $url ) ); |
| 312 | echo wp_kses_post( $html ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Save For Later welcome page |
| 317 | * |
| 318 | * @since 1.0.0 |
| 319 | */ |
| 320 | public function dsalv_welcome_screen_do_activation_redirect() |
| 321 | { |
| 322 | // if no activation redirect |
| 323 | if ( !get_transient( '_welcome_screen_activation_redirect_ds_advanced_linked_variations' ) ) { |
| 324 | return; |
| 325 | } |
| 326 | // Delete the redirect transient |
| 327 | delete_transient( '_welcome_screen_activation_redirect_ds_advanced_linked_variations' ); |
| 328 | // Redirect to save for later welcome page |
| 329 | wp_safe_redirect( add_query_arg( array( |
| 330 | 'page' => 'alv-get-started', |
| 331 | ), admin_url( 'admin.php' ) ) ); |
| 332 | exit; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Save settings data |
| 337 | * |
| 338 | * @since 1.0.0 |
| 339 | */ |
| 340 | public function dsalv_save_settings(){ |
| 341 | // Security check |
| 342 | check_ajax_referer( 'dsalv_ajax_request_nonce', 'security' ); |
| 343 | |
| 344 | // Save settings |
| 345 | $alv_settings_positions = filter_input( INPUT_GET, 'alv_settings_positions', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 346 | $alv_settings_tooltip_pos = filter_input( INPUT_GET, 'alv_settings_tooltip_pos', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 347 | $alv_settings_hide_emt_terms = filter_input( INPUT_GET, 'alv_settings_hide_emt_terms', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 348 | $alv_settings_exl_hidden_product = filter_input( INPUT_GET, 'alv_settings_exl_hidden_product', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 349 | $alv_settings_exl_unpurcha_product = filter_input( INPUT_GET, 'alv_settings_exl_unpurcha_product', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 350 | $alv_settings_link_individual_product = filter_input( INPUT_GET, 'alv_settings_link_individual_product', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 351 | $alv_settings_use_unfollow_links = filter_input( INPUT_GET, 'alv_settings_use_unfollow_links', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 352 | |
| 353 | $alv_settings_positions = ( !empty($alv_settings_positions) ? sanitize_text_field( wp_unslash( $alv_settings_positions ) ) : '' ); |
| 354 | $alv_settings_tooltip_pos = ( !empty($alv_settings_tooltip_pos) ? sanitize_text_field( wp_unslash( $alv_settings_tooltip_pos ) ) : '' ); |
| 355 | $alv_settings_hide_emt_terms = ( !empty($alv_settings_hide_emt_terms) ? sanitize_text_field( wp_unslash( $alv_settings_hide_emt_terms ) ) : '' ); |
| 356 | $alv_settings_exl_hidden_product = ( !empty($alv_settings_exl_hidden_product) ? sanitize_text_field( wp_unslash( $alv_settings_exl_hidden_product ) ) : '' ); |
| 357 | $alv_settings_exl_unpurcha_product = ( !empty($alv_settings_exl_unpurcha_product) ? sanitize_text_field( wp_unslash( $alv_settings_exl_unpurcha_product ) ) : '' ); |
| 358 | $alv_settings_link_individual_product = ( !empty($alv_settings_link_individual_product) ? sanitize_text_field( wp_unslash( $alv_settings_link_individual_product ) ) : '' ); |
| 359 | $alv_settings_use_unfollow_links = ( !empty($alv_settings_use_unfollow_links) ? sanitize_text_field( wp_unslash( $alv_settings_use_unfollow_links ) ) : '' ); |
| 360 | |
| 361 | if ( isset( $alv_settings_positions ) && !empty($alv_settings_positions) ) { |
| 362 | update_option( 'alv_settings_positions', $alv_settings_positions ); |
| 363 | } else { |
| 364 | update_option( 'alv_settings_positions', '' ); |
| 365 | } |
| 366 | |
| 367 | if ( isset( $alv_settings_tooltip_pos ) && !empty($alv_settings_tooltip_pos) ) { |
| 368 | update_option( 'alv_settings_tooltip_pos', $alv_settings_tooltip_pos ); |
| 369 | } else { |
| 370 | update_option( 'alv_settings_tooltip_pos', '' ); |
| 371 | } |
| 372 | |
| 373 | if ( isset( $alv_settings_hide_emt_terms ) && !empty($alv_settings_hide_emt_terms) ) { |
| 374 | update_option( 'alv_settings_hide_emt_terms', $alv_settings_hide_emt_terms ); |
| 375 | } else { |
| 376 | update_option( 'alv_settings_hide_emt_terms', '' ); |
| 377 | } |
| 378 | |
| 379 | if ( isset( $alv_settings_exl_hidden_product ) && !empty($alv_settings_exl_hidden_product) ) { |
| 380 | update_option( 'alv_settings_exl_hidden_product', $alv_settings_exl_hidden_product ); |
| 381 | } else { |
| 382 | update_option( 'alv_settings_exl_hidden_product', '' ); |
| 383 | } |
| 384 | |
| 385 | if ( isset( $alv_settings_exl_unpurcha_product ) && !empty($alv_settings_exl_unpurcha_product) ) { |
| 386 | update_option( 'alv_settings_exl_unpurcha_product', $alv_settings_exl_unpurcha_product ); |
| 387 | } else { |
| 388 | update_option( 'alv_settings_exl_unpurcha_product', '' ); |
| 389 | } |
| 390 | |
| 391 | if ( isset( $alv_settings_link_individual_product ) && !empty($alv_settings_link_individual_product) ) { |
| 392 | update_option( 'alv_settings_link_individual_product', $alv_settings_link_individual_product ); |
| 393 | } else { |
| 394 | update_option( 'alv_settings_link_individual_product', '' ); |
| 395 | } |
| 396 | |
| 397 | if ( isset( $alv_settings_use_unfollow_links ) && !empty($alv_settings_use_unfollow_links) ) { |
| 398 | update_option( 'alv_settings_use_unfollow_links', $alv_settings_use_unfollow_links ); |
| 399 | } else { |
| 400 | update_option( 'alv_settings_use_unfollow_links', '' ); |
| 401 | } |
| 402 | |
| 403 | wp_die(); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * |
| 408 | * Register post type of linked variations. |
| 409 | * |
| 410 | */ |
| 411 | function dsalv_alv_postype() { |
| 412 | |
| 413 | // post type |
| 414 | $labels = array( |
| 415 | 'name' => _x( 'Advanced Linked Variations', 'Post Type General Name', 'linked-variation' ), |
| 416 | 'singular_name' => _x( 'Advanced Linked Variation', 'Post Type Singular Name', 'linked-variation' ), |
| 417 | 'add_new_item' => esc_html__( 'Add New Advanced Linked Variation', 'linked-variation' ), |
| 418 | 'add_new' => esc_html__( 'Add New', 'linked-variation' ), |
| 419 | 'edit_item' => esc_html__( 'Edit Advanced Linked Variation', 'linked-variation' ), |
| 420 | 'update_item' => esc_html__( 'Update Advanced Linked Variation', 'linked-variation' ), |
| 421 | 'search_items' => esc_html__( 'Search', 'linked-variation' ), |
| 422 | ); |
| 423 | |
| 424 | $args = array( |
| 425 | 'label' => esc_html__( 'Advanced Linked Variation', 'linked-variation' ), |
| 426 | 'labels' => $labels, |
| 427 | 'supports' => array( 'title' ), |
| 428 | 'hierarchical' => false, |
| 429 | 'public' => false, |
| 430 | 'show_ui' => true, |
| 431 | 'show_in_menu' => false, |
| 432 | 'show_in_nav_menus' => true, |
| 433 | 'show_in_admin_bar' => true, |
| 434 | 'menu_position' => 28, |
| 435 | 'menu_icon' => 'dashicons-admin-links', |
| 436 | 'can_export' => true, |
| 437 | 'has_archive' => false, |
| 438 | 'exclude_from_search' => true, |
| 439 | 'publicly_queryable' => false, |
| 440 | 'capability_type' => 'post', |
| 441 | 'show_in_rest' => false, |
| 442 | ); |
| 443 | |
| 444 | register_post_type( 'dsalv', $args ); |
| 445 | } |
| 446 | |
| 447 | function dsalv_custom_column( $columns ) { |
| 448 | $columns['dsalv_configuration'] = esc_html__( 'Configuration', 'linked-variation' ); |
| 449 | |
| 450 | return $columns; |
| 451 | } |
| 452 | |
| 453 | function dsalv_custom_column_value( $column, $postid ) { |
| 454 | if ( $column === 'dsalv_configuration' ) { |
| 455 | $info = get_post_meta( $postid, 'dsalv_link', true ); |
| 456 | if( is_array( $info ) && !empty( $info ) ){ |
| 457 | $info = $info[0]; |
| 458 | } |
| 459 | if ( ! empty( $info ) && isset( $info['source'] ) ) { |
| 460 | switch ( $info['source'] ) { |
| 461 | case 'products': |
| 462 | echo esc_html__( 'Products', 'linked-variation' ) . ': '; |
| 463 | |
| 464 | if ( ! empty( $info['products'] ) ) { |
| 465 | $products = explode( ',', $info['products'] ); |
| 466 | |
| 467 | foreach ( $products as $pid ) { |
| 468 | echo esc_html__( get_the_title( $pid ), 'linked-variation' ) . ', '; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | break; |
| 473 | case 'categories': |
| 474 | echo esc_html__( 'Categories', 'linked-variation' ) . ': ' . esc_html__( $info['categories'], 'linked-variation' ); |
| 475 | |
| 476 | break; |
| 477 | case 'tags': |
| 478 | echo esc_html__( 'Tags', 'linked-variation' ) . ': ' . esc_html__( $info['tags'], 'linked-variation' ); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | function dsalv_add_meta_boxes() { |
| 485 | add_meta_box( 'dsalv_configuration', esc_html__( 'Configuration', 'linked-variation' ), array( |
| 486 | $this, |
| 487 | 'dsalv_meta_box_callback' |
| 488 | ), 'dsalv', 'advanced', 'low' ); |
| 489 | } |
| 490 | |
| 491 | function dsalv_meta_box_callback( $post ) { |
| 492 | $post_id = $post->ID; |
| 493 | $link = get_post_meta( $post_id, 'dsalv_link', true ); |
| 494 | |
| 495 | // Add nonce |
| 496 | wp_nonce_field( 'dsalv_save_meta', 'dsalv_meta_box_nonce' ); |
| 497 | ?> |
| 498 | <table class="form-table"> |
| 499 | <tr> |
| 500 | <td colspan="2"> |
| 501 | <div class="dsalv_links"> |
| 502 | <?php |
| 503 | if ( ! empty( $link ) ) { |
| 504 | $this->dsalv_link_2( $link ); |
| 505 | } else { |
| 506 | $this->dsalv_link_2(); |
| 507 | } |
| 508 | |
| 509 | ?> |
| 510 | </div> |
| 511 | </td> |
| 512 | </tr> |
| 513 | <tr> |
| 514 | <td colspan="2"> |
| 515 | <a href="javascript:void(0);" id="dsalv_add_lv"><?php echo esc_html__( '+ Add Linked Variations', 'linked-variation' ); ?></a> |
| 516 | </td> |
| 517 | </tr> |
| 518 | </table> |
| 519 | <?php |
| 520 | } |
| 521 | |
| 522 | function dsalv_save_meta_boxes( $post_id ) { |
| 523 | $nonce = filter_input( INPUT_POST, 'dsalv_meta_box_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 524 | if ( ! isset( $nonce ) || ! wp_verify_nonce( $nonce, 'dsalv_save_meta' ) ) { |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | $dsalv_link = filter_input(INPUT_POST, 'dsalv_link', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY); |
| 533 | $dsalv_link_data = isset( $dsalv_link ) ? $dsalv_link : array(); |
| 534 | if ( !empty( $dsalv_link_data ) ) { |
| 535 | update_post_meta( $post_id, 'dsalv_link', $dsalv_link_data ); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | function dsalv_link_2( $link = null ) { |
| 540 | $all_link = $link; |
| 541 | |
| 542 | if( !$all_link && empty( $all_link ) ){ |
| 543 | $all_link = Array ( 0 => Array( |
| 544 | 'attributes' => array(), |
| 545 | 'images' => array(), |
| 546 | ) |
| 547 | ); |
| 548 | } |
| 549 | |
| 550 | foreach ( $all_link as $key=>$link ) { |
| 551 | |
| 552 | isset( $link['attributes'] ) ?: $link['attributes'] = array(); |
| 553 | isset( $link['images'] ) ?: $link['images'] = array(); |
| 554 | |
| 555 | $link_source = isset( $link['source'] ) ? $link['source'] : 'products'; |
| 556 | $link_categories = isset( $link['categories'] ) ? $link['categories'] : ''; |
| 557 | $link_tags = isset( $link['tags'] ) ? $link['tags'] : ''; |
| 558 | $link_prod = isset( $link['products'] ) ? $link['products'] : ''; |
| 559 | $wc_attributes = wc_get_attribute_taxonomies(); |
| 560 | $attributes = array(); |
| 561 | |
| 562 | foreach ( $wc_attributes as $wc_attribute ) { |
| 563 | $attributes[ 'id:' . $wc_attribute->attribute_id ] = $wc_attribute->attribute_label; |
| 564 | } |
| 565 | ?> |
| 566 | <div class="dsalv_link" data-index="<?php echo esc_attr( $key ); ?>"> |
| 567 | <div class="dsalv_tr"> |
| 568 | <div class="dsalv_th"> |
| 569 | <select class="dsalv-source" name="dsalv_link[<?php echo esc_attr( $key ); ?>][source]"> |
| 570 | <option value="products" <?php echo( $link_source === 'products' ? 'selected' : '' ); ?>><?php esc_html_e( 'Products', 'linked-variation' ); ?></option> |
| 571 | <option value="categories" <?php echo( $link_source === 'categories' ? 'selected' : '' ); ?>><?php esc_html_e( 'Categories', 'linked-variation' ); ?></option> |
| 572 | <option value="tags" <?php echo( $link_source === 'tags' ? 'selected' : '' ); ?>><?php esc_html_e( 'Tags', 'linked-variation' ); ?></option> |
| 573 | </select> |
| 574 | </div> |
| 575 | <div class="dsalv_td dsalv_link_td"> |
| 576 | <div class="dsalv-source-hide dsalv-source-products"> |
| 577 | <input class="dsalv-products" |
| 578 | name="dsalv_link[<?php echo esc_attr( $key ); ?>][products]" type="hidden" |
| 579 | value="<?php echo esc_attr( $link_prod ); ?>"/> |
| 580 | <select class="wc-product-search dsalv-product-search" multiple="multiple" |
| 581 | data-placeholder="<?php esc_attr_e( 'Search for a product…', 'linked-variation' ); ?>" |
| 582 | data-action="woocommerce_json_search_products"> |
| 583 | <?php |
| 584 | $_product_ids = explode( ',', $link_prod ); |
| 585 | |
| 586 | foreach ( $_product_ids as $_product_id ) { |
| 587 | $_product = wc_get_product( $_product_id ); |
| 588 | |
| 589 | if ( $_product ) { |
| 590 | echo '<option value="' . esc_attr( $_product_id ) . '" selected="selected">' . wp_kses_post( $_product->get_formatted_name() ) . '</option>'; |
| 591 | } |
| 592 | } |
| 593 | ?> |
| 594 | </select> |
| 595 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 596 | </div> |
| 597 | <div class="dsalv-source-hide dsalv-source-categories" style="display:none;"> |
| 598 | <input class="dsalv-categories" |
| 599 | name="dsalv_link[<?php echo esc_attr( $key ); ?>][categories]" type="hidden" |
| 600 | value="<?php echo esc_attr( $link_categories ); ?>"/> |
| 601 | <select class="wc-category-search dsalv-category-search" multiple="multiple" |
| 602 | data-placeholder="<?php esc_attr_e( 'Search for a category…', 'linked-variation' ); ?>"> |
| 603 | <?php |
| 604 | $category_slugs = explode( ',', $link_categories ); |
| 605 | |
| 606 | if ( count( $category_slugs ) > 0 ) { |
| 607 | foreach ( $category_slugs as $category_slug ) { |
| 608 | $category = get_term_by( 'slug', $category_slug, 'product_cat' ); |
| 609 | |
| 610 | if ( $category ) { |
| 611 | echo '<option value="' . esc_attr( $category_slug ) . '" selected="selected">' . wp_kses_post( $category->name ) . '</option>'; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | ?> |
| 616 | </select> |
| 617 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 618 | </div> |
| 619 | <div class="dsalv-source-hide dsalv-source-tags" style="display:none;"> |
| 620 | <input class="dsalv-tags" name="dsalv_link[<?php echo esc_attr( $key ); ?>][tags]" |
| 621 | type="hidden" style="width: 100%" |
| 622 | value="<?php echo esc_attr( $link_tags ); ?>"/> |
| 623 | <select class="wc-tag-search dsalv-tag-search" multiple="multiple" data-placeholder="<?php esc_attr_e( 'Search for a tags…', 'linked-variation' ); ?>"> |
| 624 | <?php |
| 625 | $link_tags = explode( ',', $link_tags ); |
| 626 | $dsalv_source_tags = get_terms( array( |
| 627 | 'hide_empty' => false, |
| 628 | 'taxonomy' => 'product_tag', |
| 629 | ) |
| 630 | ); |
| 631 | if ( ! empty( $dsalv_source_tags ) && ! is_wp_error( $dsalv_source_tags ) ) { |
| 632 | foreach ( $dsalv_source_tags as $dsalv_cart_tag ) { |
| 633 | ?> |
| 634 | <option value="<?php echo esc_attr( $dsalv_cart_tag->slug ); ?>" <?php selected( true, in_array( $dsalv_cart_tag->slug, $link_tags, true ), true ); ?> > |
| 635 | <?php echo esc_html_e( $dsalv_cart_tag->name, 'linked-variation' ); ?> |
| 636 | </option> |
| 637 | <?php |
| 638 | } |
| 639 | } |
| 640 | ?> |
| 641 | </select> |
| 642 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 643 | </div> |
| 644 | </div> |
| 645 | </div> |
| 646 | <div class="dsalv_tr"> |
| 647 | <div class="dsalv_th"><?php esc_html_e( 'Linked by (attributes)', 'linked-variation' ); ?></div> |
| 648 | <div class="dsalv_td dsalv_link_td dsalv_all_attr"> |
| 649 | <div id="dsalv_toggle"> |
| 650 | <span class="expand"><?php esc_html_e( 'Expand+', 'linked-variation' ); ?></span> |
| 651 | <span class="collapse" style="display:none;"><?php esc_html_e( 'Collapse-', 'linked-variation' ); ?></span> |
| 652 | </div> |
| 653 | <div id="dsalv_swtich" style="display:none;"> |
| 654 | <?php |
| 655 | $saved_attributes = $merge_attributes = array(); |
| 656 | |
| 657 | foreach ( $link['attributes'] as $attr ) { |
| 658 | $saved_attributes[ $attr ] = $attributes[ $attr ]; |
| 659 | } |
| 660 | |
| 661 | $merge_attributes = array_merge( $saved_attributes, $attributes ); |
| 662 | |
| 663 | if ( $merge_attributes ) { |
| 664 | echo '<div class="dsalv-attributes">'; |
| 665 | |
| 666 | foreach ( $merge_attributes as $attribute_id => $attribute_label ) { |
| 667 | echo '<div class="dsalv-attribute"><span class="move">' . esc_html__( 'Move', 'linked-variation' ) . '</span><span class="checkbox"><label><input type="checkbox" name="dsalv_link['. esc_attr( $key ) .'][attributes][]" value="' . esc_attr( $attribute_id ) . '" ' . ( is_array( $link['attributes'] ) && in_array( $attribute_id, $link['attributes'], true ) ? 'checked' : '' ) . '/>' . esc_html( $attribute_label ) . '</label></span><span class="display"><label><input type="checkbox" class="dsalv_display_checkbox" name="dsalv_link['. esc_attr( $key ) .'][images][]" value="' . esc_attr( $attribute_id ) . '" ' . ( is_array( $link['images'] ) && in_array( $attribute_id, $link['images'], true ) ? 'checked' : '' ) . '/>' . esc_html__( 'Show images', 'linked-variation' ) . '</label></span></div>'; |
| 668 | } |
| 669 | |
| 670 | echo '</div>'; |
| 671 | } |
| 672 | ?> |
| 673 | </div> |
| 674 | <?php if( 0 !== $key ){ ?> |
| 675 | <span id="dsalv_delete"><?php esc_html_e( 'Delete', 'linked-variation' ); ?></span> |
| 676 | <?php } ?> |
| 677 | </div> |
| 678 | </div> |
| 679 | </div> |
| 680 | <?php |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | public static function show_link_variations( $product_id = null ) { |
| 685 | |
| 686 | if ( ! $product_id ) { |
| 687 | global $product; |
| 688 | $_product = $product; |
| 689 | $product_id = $_product->get_id(); |
| 690 | } else { |
| 691 | $_product = wc_get_product( $product_id ); |
| 692 | } |
| 693 | |
| 694 | if ( ! $_product ) { |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | $link_data = self::get_linked_data( $product_id ); |
| 699 | |
| 700 | if ( empty( $link_data ) ) { |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | $link_attributes = isset( $link_data['attributes'] ) ? $link_data['attributes'] : array(); |
| 705 | $link_images = isset( $link_data['images'] ) ? $link_data['images'] : array(); |
| 706 | |
| 707 | // get product ids |
| 708 | $link_products = array(); |
| 709 | $link_source = isset( $link_data['source'] ) ? $link_data['source'] : 'products'; |
| 710 | |
| 711 | if ( ( $link_source === 'products' ) && isset( $link_data['products'] ) && ! empty( $link_data['products'] ) ) { |
| 712 | $link_products = explode( ',', $link_data['products'] ); |
| 713 | } |
| 714 | |
| 715 | if ( ( $link_source === 'categories' ) && isset( $link_data['categories'] ) && ! empty( $link_data['categories'] ) ) { |
| 716 | $categories = array_map( 'trim', explode( ',', $link_data['categories'] ) ); |
| 717 | |
| 718 | if ( ! empty( $categories ) ) { |
| 719 | // phpcs:disable |
| 720 | $args = array( |
| 721 | 'post_type' => 'product', |
| 722 | 'post_status' => 'publish', |
| 723 | 'ignore_sticky_posts' => 1, |
| 724 | 'posts_per_page' => 500, |
| 725 | 'tax_query' => array( |
| 726 | array( |
| 727 | 'taxonomy' => 'product_cat', |
| 728 | 'field' => 'slug', |
| 729 | 'terms' => $categories, |
| 730 | 'operator' => 'IN', |
| 731 | ) |
| 732 | ) |
| 733 | ); |
| 734 | // phpcs:enable |
| 735 | |
| 736 | $products = new WP_Query( $args ); |
| 737 | |
| 738 | if ( $products->have_posts() ) { |
| 739 | while ( $products->have_posts() ) { |
| 740 | $products->the_post(); |
| 741 | $link_products[] = get_the_ID(); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | wp_reset_postdata(); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if ( ( $link_source === 'tags' ) && isset( $link_data['tags'] ) && ! empty( $link_data['tags'] ) ) { |
| 750 | $tags = array_map( 'trim', explode( ',', $link_data['tags'] ) ); |
| 751 | |
| 752 | if ( ! empty( $tags ) ) { |
| 753 | // phpcs:disable |
| 754 | $args = array( |
| 755 | 'post_type' => 'product', |
| 756 | 'post_status' => 'publish', |
| 757 | 'ignore_sticky_posts' => 1, |
| 758 | 'posts_per_page' => 500, |
| 759 | 'tax_query' => array( |
| 760 | array( |
| 761 | 'taxonomy' => 'product_tag', |
| 762 | 'field' => 'slug', |
| 763 | 'terms' => $tags, |
| 764 | 'operator' => 'IN', |
| 765 | ) |
| 766 | ) |
| 767 | ); |
| 768 | // phpcs:enable |
| 769 | |
| 770 | $products = new WP_Query( $args ); |
| 771 | |
| 772 | if ( $products->have_posts() ) { |
| 773 | while ( $products->have_posts() ) { |
| 774 | $products->the_post(); |
| 775 | $link_products[] = get_the_ID(); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | wp_reset_postdata(); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // exclude hidden or unpurchasable |
| 784 | if ( ( get_option( 'alv_settings_exl_hidden_product', 'no' ) === 'yes' ) || ( get_option( 'alv_settings_exl_unpurcha_product', 'no' ) === 'yes' ) ) { |
| 785 | foreach ( $link_products as $key => $link_product_id ) { |
| 786 | $link_product = wc_get_product( $link_product_id ); |
| 787 | |
| 788 | if ( ! $link_product || ( ! $link_product->is_visible() && ( get_option( 'alv_settings_exl_hidden_product', 'no' ) === 'yes' ) ) || ( ( ! $link_product->is_purchasable() || ! $link_product->is_in_stock() ) && ( get_option( 'alv_settings_exl_unpurcha_product', 'no' ) === 'yes' ) ) ) { |
| 789 | unset( $link_products[ $key ] ); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // exclude current product |
| 795 | $link_products = array_diff( $link_products, array( $product_id ) ); |
| 796 | |
| 797 | if ( empty( $link_products ) ) { |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | $all_taxonomies = []; |
| 802 | $filter_assigned_attributes = array_filter( $_product->get_attributes(), 'wc_attributes_array_filter_visible' ); |
| 803 | $assigned_attributes = array_keys( $filter_assigned_attributes ); |
| 804 | $product_attributes = []; |
| 805 | |
| 806 | foreach ( $assigned_attributes as $assigned_attribute ) { |
| 807 | $product_attributes[ $assigned_attribute ] = wc_get_product_terms( $product_id, $assigned_attribute, array( 'fields' => 'slugs' ) ); |
| 808 | } |
| 809 | |
| 810 | if ( ! empty( $link_attributes ) ) { ?> |
| 811 | <div class="dsalv-attributes"> |
| 812 | <?php |
| 813 | foreach ( $link_attributes as $link_attribute ) { |
| 814 | $link_attribute_id = (int) filter_var( $link_attribute, FILTER_SANITIZE_NUMBER_INT ); |
| 815 | $attribute = wc_get_attribute( $link_attribute_id ); |
| 816 | |
| 817 | $terms = get_terms( array( 'taxonomy' => $attribute->slug, 'hide_empty' => false ) ); |
| 818 | $current_terms = wp_get_post_terms( $product_id, $attribute->slug, array( 'fields' => 'ids' ) ); |
| 819 | array_push( $all_taxonomies, $attribute->slug ); |
| 820 | ?> |
| 821 | <div class="dsalv-attribute"> |
| 822 | <?php if ( ! empty( $terms ) ) { ?> |
| 823 | <div class="dsalv-attribute-label"> |
| 824 | <?php echo esc_html( $attribute->name ); ?> |
| 825 | </div> |
| 826 | <div class="dsalv-terms"> |
| 827 | <?php foreach ( $terms as $term ) { |
| 828 | if ( in_array( $term->term_id, $current_terms, true ) ) { |
| 829 | if ( in_array( $link_attribute, $link_images, true ) ) { |
| 830 | self::dsalv_term( 'image', $attribute, $term, true, $product_id ); |
| 831 | } else { |
| 832 | self::dsalv_term( 'button', $attribute, $term, true, $product_id ); |
| 833 | } |
| 834 | } else { |
| 835 | $tax_query = array( 'relation' => 'AND' ); |
| 836 | |
| 837 | $tax_query_ori = [ |
| 838 | 'taxonomy' => $term->taxonomy, |
| 839 | 'field' => 'slug', |
| 840 | 'terms' => $term->slug |
| 841 | ]; |
| 842 | |
| 843 | foreach ( $product_attributes as $product_attribute_key => $product_attribute ) { |
| 844 | if ( $term->taxonomy !== $product_attribute_key ) { |
| 845 | $tax_query[] = array( |
| 846 | 'taxonomy' => $product_attribute_key, |
| 847 | 'field' => 'slug', |
| 848 | 'terms' => $product_attribute |
| 849 | ); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | array_push( $tax_query, $tax_query_ori ); |
| 854 | |
| 855 | $linked_id = self::get_linked_product_id( $tax_query, $link_products ); |
| 856 | |
| 857 | if ( $linked_id ) { |
| 858 | if ( in_array( $link_attribute, $link_images, true ) ) { |
| 859 | self::dsalv_term( 'image', $attribute, $term, false, $linked_id ); |
| 860 | } else { |
| 861 | self::dsalv_term( 'button', $attribute, $term, false, $linked_id ); |
| 862 | } |
| 863 | } else { |
| 864 | $linked_id = self::get_linked_product_id( array( $tax_query_ori ), $link_products ); |
| 865 | if ( $linked_id ) { |
| 866 | if ( in_array( $link_attribute, $link_images, true ) ) { |
| 867 | self::dsalv_term( 'image', $attribute, $term, false, $linked_id ); |
| 868 | } else { |
| 869 | self::dsalv_term( 'button', $attribute, $term, false, $linked_id ); |
| 870 | } |
| 871 | } elseif ( get_option( 'alv_settings_hide_emt_terms', 'yes' ) === 'no' ) { |
| 872 | if ( in_array( $link_attribute, $link_images, true ) ) { |
| 873 | self::dsalv_term( 'image', $attribute, $term, false ); |
| 874 | } else { |
| 875 | self::dsalv_term( 'button', $attribute, $term, false ); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } ?> |
| 881 | </div> |
| 882 | <?php } ?> |
| 883 | </div> |
| 884 | <?php } ?> |
| 885 | </div> |
| 886 | <?php } |
| 887 | } |
| 888 | |
| 889 | public static function dsalv_term( $type, $attribute, $term, $active, $product_id = 0 ) { |
| 890 | $link = get_option( 'alv_settings_link_individual_product', 'open_in_the_same_tab' ); |
| 891 | $nofollow = get_option( 'alv_settings_use_unfollow_links', 'no' ) === 'yes'; |
| 892 | $hint = get_option( 'alv_settings_tooltip_pos', 'top' ); |
| 893 | $hint_class = $hint !== 'no' ? 'hint--' . $hint : ''; |
| 894 | |
| 895 | switch ( $type ) { |
| 896 | case 'image': |
| 897 | if ( $product_id && ( $product_thumbnail_id = get_post_thumbnail_id( $product_id ) ) ) { // phpcs:ignore |
| 898 | $term_image = '<img src="' . wp_get_attachment_image_url( $product_thumbnail_id ) . '" alt="' . esc_attr( $term->name ) . '"/>'; |
| 899 | } else { |
| 900 | $term_image = wc_placeholder_img(); |
| 901 | } |
| 902 | |
| 903 | $html = '<div class="dsalv-term dsalv-term-image ' . $hint_class . ' ' . ( $active ? 'active' : '' ) . '" aria-label="' . esc_attr( apply_filters( 'dsalv_term_label', $term->name, $term, $product_id ) ) . '">'; |
| 904 | |
| 905 | if ( $product_id && ! $active ) { |
| 906 | $html .= '<a href="' . ( get_the_permalink( $product_id ) ) . '" ' . ( $nofollow ? 'rel="nofollow"' : '' ) . ' title="' . esc_attr( apply_filters( 'dsalv_term_title', get_the_title( $product_id ), $term, $product_id ) ) . '" ' . ( $link === 'open_in_the_new_tab' ? 'target="_blank"' : '' ) . '>' . $term_image . '</a>'; |
| 907 | } else { |
| 908 | $html .= '<span>' . $term_image . '</span>'; |
| 909 | } |
| 910 | |
| 911 | $html .= '</div>'; |
| 912 | |
| 913 | echo wp_kses_post(apply_filters( 'dsalv_term_image', $html )); |
| 914 | break; |
| 915 | |
| 916 | case 'button': |
| 917 | $html = '<div class="dsalv-term ' . $hint_class . ' ' . ( $active ? 'active' : '' ) . '" aria-label="' . esc_attr( apply_filters( 'dsalv_term_label', $term->name, $term, $product_id ) ) . '">'; |
| 918 | |
| 919 | if ( $product_id && ! $active ) { |
| 920 | $html .= '<a href="' . ( get_the_permalink( $product_id ) ) . '" ' . ( $nofollow ? 'rel="nofollow"' : '' ) . ' title="' . esc_attr( apply_filters( 'dsalv_term_title', get_the_title( $product_id ), $term, $product_id ) ) . '" ' . ( $link === 'open_in_the_new_tab' ? 'target="_blank"' : '' ) . '>' . esc_html( $term->name ) . '</a>'; |
| 921 | } else { |
| 922 | $html .= '<span>' . esc_html( $term->name ) . '</span>'; |
| 923 | } |
| 924 | |
| 925 | $html .= '</div>'; |
| 926 | |
| 927 | echo wp_kses_post(apply_filters( 'dsalv_term_button', $html )); |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | public static function get_linked_data( $product_id ) { |
| 933 | // new data, get the ID only |
| 934 | $new_links = get_posts( array( // phpcs:ignore |
| 935 | 'post_type' => 'dsalv', |
| 936 | 'post_status' => 'publish', |
| 937 | 'posts_per_page' => - 1, |
| 938 | 'fields' => 'ids', |
| 939 | 'suppress_filters' => false |
| 940 | ) ); |
| 941 | |
| 942 | if ( ! empty( $new_links ) ) { |
| 943 | foreach ( $new_links as $new_link ) { |
| 944 | $link = get_post_meta( $new_link, 'dsalv_link', true ); |
| 945 | |
| 946 | if ( ! empty( $link ) ) { |
| 947 | foreach ( $link as $link ) { |
| 948 | $link_source = isset( $link['source'] ) ? $link['source'] : 'products'; |
| 949 | |
| 950 | if ( ( $link_source === 'products' ) && isset( $link['products'] ) && ! empty( $link['products'] ) ) { |
| 951 | $product_ids = array_map( 'intval', explode( ',', $link['products'] ) ); |
| 952 | |
| 953 | if ( in_array( $product_id, $product_ids, true ) ) { |
| 954 | return $link; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | if ( ( $link_source === 'categories' ) && isset( $link['categories'] ) && ! empty( $link['categories'] ) ) { |
| 959 | $categories = array_map( 'trim', explode( ',', $link['categories'] ) ); |
| 960 | |
| 961 | if ( has_term( $categories, 'product_cat', $product_id ) ) { |
| 962 | return $link; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | if ( ( $link_source === 'tags' ) && isset( $link['tags'] ) && ! empty( $link['tags'] ) ) { |
| 967 | $tags = array_map( 'trim', explode( ',', $link['tags'] ) ); |
| 968 | |
| 969 | if ( has_term( $tags, 'product_tag', $product_id ) ) { |
| 970 | return $link; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | return false; |
| 979 | } |
| 980 | // return post id |
| 981 | public static function get_linked_product_id( $tax_query, $link_products = [], $order = 'ASC' ) { |
| 982 | $args = [ |
| 983 | 'post_type' => 'product', |
| 984 | 'posts_per_page' => - 1, |
| 985 | 'order' => $order, |
| 986 | 'fields' => 'ids', |
| 987 | 'suppress_filters' => false |
| 988 | ]; |
| 989 | |
| 990 | if ( $tax_query ) { |
| 991 | $args['tax_query'] = $tax_query; // phpcs:ignore |
| 992 | } |
| 993 | |
| 994 | if ( $link_products ) { |
| 995 | $args['post__in'] = $link_products; |
| 996 | } |
| 997 | |
| 998 | $filter_product = get_posts( $args ); // phpcs:ignore |
| 999 | |
| 1000 | if ( $filter_product ) { |
| 1001 | return $filter_product[0]; |
| 1002 | } else { |
| 1003 | return false; |
| 1004 | } |
| 1005 | } |
| 1006 | public static function dsalv_lv_shortcode( $attrs ) { |
| 1007 | $output = ''; |
| 1008 | $attrs = shortcode_atts( array( 'id' => null ), $attrs ); |
| 1009 | |
| 1010 | if ( ! $attrs['id'] ) { |
| 1011 | global $product; |
| 1012 | |
| 1013 | if ( $product ) { |
| 1014 | $attrs['id'] = $product->get_id(); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if ( $attrs['id'] ) { |
| 1019 | ob_start(); |
| 1020 | self::show_link_variations( $attrs['id'] ); |
| 1021 | $output = ob_get_clean(); |
| 1022 | } |
| 1023 | |
| 1024 | return $output; |
| 1025 | } |
| 1026 | public static function dsalv_add_new_variation(){ |
| 1027 | // Security check |
| 1028 | check_ajax_referer( 'dsalv_ajax_request_nonce', 'security' ); |
| 1029 | |
| 1030 | // Add new variation |
| 1031 | $link = array(); |
| 1032 | $index = filter_input( INPUT_POST, 'index', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 1033 | isset( $link['attributes'] ) ?: $link['attributes'] = array(); |
| 1034 | isset( $link['images'] ) ?: $link['images'] = array(); |
| 1035 | |
| 1036 | $link_source = isset( $link['source'] ) ? $link['source'] : 'products'; |
| 1037 | $link_categories = isset( $link['categories'] ) ? $link['categories'] : ''; |
| 1038 | $link_tags = isset( $link['tags'] ) ? $link['tags'] : ''; |
| 1039 | $link_prod = isset( $link['products'] ) ? $link['products'] : ''; |
| 1040 | $wc_attributes = wc_get_attribute_taxonomies(); |
| 1041 | $attributes = array(); |
| 1042 | |
| 1043 | foreach ( $wc_attributes as $wc_attribute ) { |
| 1044 | $attributes[ 'id:' . $wc_attribute->attribute_id ] = $wc_attribute->attribute_label; |
| 1045 | } |
| 1046 | ?> |
| 1047 | <div class="dsalv_link" data-index="<?php echo esc_attr( $index ); ?>"> |
| 1048 | <div class="dsalv_tr"> |
| 1049 | <div class="dsalv_th"> |
| 1050 | <select class="dsalv-source" name="dsalv_link[<?php echo esc_attr( $index ); ?>][source]"> |
| 1051 | <option value="products" <?php echo( $link_source === 'products' ? 'selected' : '' ); ?>><?php esc_html_e( 'Products', 'linked-variation' ); ?></option> |
| 1052 | <option value="categories" <?php echo( $link_source === 'categories' ? 'selected' : '' ); ?>><?php esc_html_e( 'Categories', 'linked-variation' ); ?></option> |
| 1053 | <option value="tags" <?php echo( $link_source === 'tags' ? 'selected' : '' ); ?>><?php esc_html_e( 'Tags', 'linked-variation' ); ?></option> |
| 1054 | </select> |
| 1055 | </div> |
| 1056 | <div class="dsalv_td dsalv_link_td"> |
| 1057 | <div class="dsalv-source-hide dsalv-source-products"> |
| 1058 | <input class="dsalv-products" |
| 1059 | name="dsalv_link[<?php echo esc_attr( $index ); ?>][products]" type="hidden" |
| 1060 | value="<?php echo esc_attr($link_prod); ?>"/> |
| 1061 | <select class="wc-product-search dsalv-product-search" multiple="multiple" |
| 1062 | data-placeholder="<?php esc_attr_e( 'Search for a product…', 'linked-variation' ); ?>" |
| 1063 | data-action="woocommerce_json_search_products"> |
| 1064 | <?php |
| 1065 | $_product_ids = explode( ',', $link_prod ); |
| 1066 | |
| 1067 | foreach ( $_product_ids as $_product_id ) { |
| 1068 | $_product = wc_get_product( $_product_id ); |
| 1069 | |
| 1070 | if ( $_product ) { |
| 1071 | echo '<option value="' . esc_attr( $_product_id ) . '" selected="selected">' . wp_kses_post( $_product->get_formatted_name() ) . '</option>'; |
| 1072 | } |
| 1073 | } |
| 1074 | ?> |
| 1075 | </select> |
| 1076 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 1077 | </div> |
| 1078 | <div class="dsalv-source-hide dsalv-source-categories" style="display:none;"> |
| 1079 | <input class="dsalv-categories" |
| 1080 | name="dsalv_link[<?php echo esc_attr( $index ); ?>][categories]" type="hidden" |
| 1081 | value="<?php echo esc_attr( $link_categories ); ?>"/> |
| 1082 | <select class="wc-category-search dsalv-category-search" multiple="multiple" |
| 1083 | data-placeholder="<?php esc_attr_e( 'Search for a category…', 'linked-variation' ); ?>"> |
| 1084 | <?php |
| 1085 | $category_slugs = explode( ',', $link_categories ); |
| 1086 | |
| 1087 | if ( count( $category_slugs ) > 0 ) { |
| 1088 | foreach ( $category_slugs as $category_slug ) { |
| 1089 | $category = get_term_by( 'slug', $category_slug, 'product_cat' ); |
| 1090 | |
| 1091 | if ( $category ) { |
| 1092 | echo '<option value="' . esc_attr( $category_slug ) . '" selected="selected">' . wp_kses_post( $category->name ) . '</option>'; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | ?> |
| 1097 | </select> |
| 1098 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 1099 | </div> |
| 1100 | <div class="dsalv-source-hide dsalv-source-tags" style="display:none;"> |
| 1101 | <input class="dsalv-tags" name="dsalv_link[<?php echo esc_attr( $index ); ?>][tags]" |
| 1102 | type="hidden" style="width: 100%" |
| 1103 | value="<?php echo esc_attr( $link_tags ); ?>"/> |
| 1104 | <select class="wc-tag-search dsalv-tag-search" multiple="multiple" |
| 1105 | data-placeholder="<?php esc_attr_e( 'Search for a tags…', 'linked-variation' ); ?>"> |
| 1106 | <?php |
| 1107 | $link_tags = explode( ',', $link_tags ); |
| 1108 | $dsalv_source_tags = get_terms( array( |
| 1109 | 'hide_empty' => false, |
| 1110 | 'taxonomy' => 'product_tag', |
| 1111 | ) |
| 1112 | ); |
| 1113 | if ( ! empty( $dsalv_source_tags ) && ! is_wp_error( $dsalv_source_tags ) ) { |
| 1114 | foreach ( $dsalv_source_tags as $dsalv_cart_tag ) { |
| 1115 | ?> |
| 1116 | <option value="<?php echo esc_attr( $dsalv_cart_tag->slug ); ?>" <?php selected( true, in_array( $dsalv_cart_tag->slug, $link_tags, true ), true ); ?> > |
| 1117 | <?php echo esc_html_e( $dsalv_cart_tag->name, 'linked-variation' ); ?> |
| 1118 | </option> |
| 1119 | <?php |
| 1120 | } |
| 1121 | } |
| 1122 | ?> |
| 1123 | |
| 1124 | </select> |
| 1125 | <?php echo '<script>jQuery(document.body).trigger( \'wc-enhanced-select-init\' );</script>'; ?> |
| 1126 | </div> |
| 1127 | </div> |
| 1128 | </div> |
| 1129 | <div class="dsalv_tr"> |
| 1130 | <div class="dsalv_th"><?php esc_html_e( 'Linked by (attributes)', 'linked-variation' ); ?></div> |
| 1131 | <div class="dsalv_td dsalv_link_td"> |
| 1132 | <div id="dsalv_toggle"> |
| 1133 | <span class="expand"><?php esc_html_e( 'Expand+', 'linked-variation' ); ?></span> |
| 1134 | <span class="collapse" style="display:none;"><?php esc_html_e( 'Collapse-', 'linked-variation' ); ?></span> |
| 1135 | </div> |
| 1136 | <div id="dsalv_swtich" style="display:none;"> |
| 1137 | <?php |
| 1138 | $saved_attributes = $merge_attributes = array(); |
| 1139 | |
| 1140 | foreach ( $link['attributes'] as $attr ) { |
| 1141 | $saved_attributes[ $attr ] = $attributes[ $attr ]; |
| 1142 | } |
| 1143 | |
| 1144 | $merge_attributes = array_merge( $saved_attributes, $attributes ); |
| 1145 | |
| 1146 | if ( $merge_attributes ) { |
| 1147 | echo '<div class="dsalv-attributes">'; |
| 1148 | |
| 1149 | foreach ( $merge_attributes as $attribute_id => $attribute_label ) { |
| 1150 | echo '<div class="dsalv-attribute"><span class="move">' . esc_html__( 'Move', 'linked-variation' ) . '</span><span class="checkbox"><label><input type="checkbox" name="dsalv_link['. esc_attr( $index ) .'][attributes][]" value="' . esc_attr($attribute_id) . '" ' . ( is_array( $link['attributes'] ) && in_array( $attribute_id, $link['attributes'], true ) ? 'checked' : '' ) . '/>' . esc_html($attribute_label) . '</label></span><span class="display"><label><input type="checkbox" class="dsalv_display_checkbox" name="dsalv_link['.esc_attr( $index ).'][images][]" value="' . esc_attr($attribute_id) . '" ' . ( is_array( $link['images'] ) && in_array( $attribute_id, $link['images'], true ) ? 'checked' : '' ) . '/>' . esc_html__( 'Show images', 'linked-variation' ) . '</label></span></div>'; |
| 1151 | } |
| 1152 | |
| 1153 | echo '</div>'; |
| 1154 | } |
| 1155 | ?> |
| 1156 | </div> |
| 1157 | </div> |
| 1158 | </div> |
| 1159 | <span id="dsalv_delete"><?php esc_html_e( 'Delete', 'linked-variation' ); ?></span> |
| 1160 | </div> |
| 1161 | <?php |
| 1162 | exit(); |
| 1163 | } |
| 1164 | } |
| 1165 | } |