modules
2 months ago
CompareExtension.php
2 months ago
loader.js
2 months ago
loader.php
2 months ago
CompareExtension.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | class BRCMP_CompareExtension extends DiviExtension { |
| 4 | public $gettext_domain = 'brcompare-my-extension'; |
| 5 | public $name = 'brcompare-extension'; |
| 6 | public $version = '1.0.0'; |
| 7 | public function __construct( $name = 'brcompare-extension', $args = array() ) { |
| 8 | $this->plugin_dir = plugin_dir_path( __FILE__ ); |
| 9 | $this->plugin_dir_url = plugin_dir_url( $this->plugin_dir ); |
| 10 | |
| 11 | parent::__construct( $name, $args ); |
| 12 | add_action('wp_ajax_brcompare_compare_table', array($this, 'compare_table')); |
| 13 | add_action('wp_ajax_brcompare_compare_button', array($this, 'compare_button')); |
| 14 | add_action('wp_ajax_brcompare_compare_widget', array($this, 'compare_widget')); |
| 15 | } |
| 16 | public function compare_widget() { |
| 17 | if ( ! current_user_can( 'manage_options' ) ) { |
| 18 | wp_die(); |
| 19 | } |
| 20 | $atts = berocket_sanitize_array($_POST); |
| 21 | $atts = self::convert_on_off($atts); |
| 22 | if( ! empty($atts['toolbar']) ) { |
| 23 | echo '<div style="height:50px;"></div>'; |
| 24 | } |
| 25 | the_widget( 'berocket_compare_products_widget', $atts ); |
| 26 | wp_die(); |
| 27 | } |
| 28 | public function compare_table() { |
| 29 | if ( ! current_user_can( 'manage_options' ) ) { |
| 30 | wp_die(); |
| 31 | } |
| 32 | $filter = do_shortcode('[br_compare_table]'); |
| 33 | echo $filter; |
| 34 | wp_die(); |
| 35 | } |
| 36 | public function compare_button() { |
| 37 | if ( ! current_user_can( 'manage_options' ) ) { |
| 38 | wp_die(); |
| 39 | } |
| 40 | $atts = berocket_sanitize_array($_POST); |
| 41 | $atts = self::convert_on_off($atts) ; |
| 42 | if( ! empty($atts['product']) ) { |
| 43 | if( $atts['product'] == 'latest' ) { |
| 44 | global $wpdb; |
| 45 | $atts['product'] = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product' AND post_status = 'publish' ORDER BY ID DESC LIMIT 1"); |
| 46 | } elseif( $atts['product'] == 'current' ) { |
| 47 | $atts['product'] = ''; |
| 48 | } |
| 49 | } |
| 50 | do_action('br_compare_button_options', $atts); |
| 51 | wp_die(); |
| 52 | } |
| 53 | public function wp_hook_enqueue_scripts() { |
| 54 | if ( $this->_debug ) { |
| 55 | $this->_enqueue_debug_bundles(); |
| 56 | } else { |
| 57 | $this->_enqueue_bundles(); |
| 58 | } |
| 59 | |
| 60 | if ( et_core_is_fb_enabled() && ! et_builder_bfb_enabled() ) { |
| 61 | $this->_enqueue_backend_styles(); |
| 62 | } |
| 63 | |
| 64 | // Normalize the extension name to get actual script name. For example from 'divi-custom-modules' to `DiviCustomModules`. |
| 65 | $extension_name = str_replace( ' ', '', ucwords( str_replace( '-', ' ', $this->name ) ) ); |
| 66 | |
| 67 | // Enqueue frontend bundle's data. |
| 68 | if ( ! empty( $this->_frontend_js_data ) ) { |
| 69 | wp_localize_script( "{$this->name}-frontend-bundle", "{$extension_name}FrontendData", $this->_frontend_js_data ); |
| 70 | } |
| 71 | |
| 72 | // Enqueue builder bundle's data. |
| 73 | if ( et_core_is_fb_enabled() && ! empty( $this->_builder_js_data ) ) { |
| 74 | wp_localize_script( "{$this->name}-builder-bundle", "{$extension_name}BuilderData", $this->_builder_js_data ); |
| 75 | } |
| 76 | } |
| 77 | public static function convert_on_off($atts) { |
| 78 | foreach($atts as &$attr) { |
| 79 | if( $attr === 'on' || $attr === 'off' ) { |
| 80 | $attr = ( $attr === 'on' ? TRUE : FALSE ); |
| 81 | } |
| 82 | } |
| 83 | return $atts; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | new BRCMP_CompareExtension; |
| 88 |