Conditions
3 years ago
Documents
2 years ago
assets
3 years ago
ElementorServiceProvider.php
2 years ago
ReusableFormWidget.php
3 years ago
ElementorServiceProvider.php
168 lines
| 1 | <?php |
| 2 | namespace SureCart\Integrations\Elementor; |
| 3 | |
| 4 | use SureCart\Integrations\Elementor\Conditions\Conditions; |
| 5 | use SureCart\Integrations\Elementor\Documents\ProductDocument; |
| 6 | use SureCart\Models\Product; |
| 7 | use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 8 | |
| 9 | /** |
| 10 | * Elementor service provider. |
| 11 | */ |
| 12 | class ElementorServiceProvider implements ServiceProviderInterface { |
| 13 | /** |
| 14 | * Register all dependencies in the IoC container. |
| 15 | * |
| 16 | * @param \Pimple\Container $container Service container. |
| 17 | * @return void |
| 18 | */ |
| 19 | public function register( $container ) { |
| 20 | // nothing to register. |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * {@inheritDoc} |
| 25 | * |
| 26 | * @param \Pimple\Container $container Service Container. |
| 27 | */ |
| 28 | public function bootstrap( $container ) { |
| 29 | if ( ! class_exists( '\Elementor\Plugin' ) ) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // Elementor integration. |
| 34 | add_action( 'elementor/widgets/register', [ $this, 'widget' ] ); |
| 35 | add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'load_scripts' ] ); |
| 36 | add_action( 'elementor/elements/categories_registered', [ $this, 'categories_registered' ] ); |
| 37 | |
| 38 | // Register product theme condition. |
| 39 | if ( defined( 'ELEMENTOR_PRO_VERSION' ) ) { |
| 40 | add_action( 'elementor/documents/register', [ $this, 'register_document' ] ); |
| 41 | add_action( 'elementor/theme/register_conditions', [ $this, 'product_theme_conditions' ] ); |
| 42 | add_filter( 'elementor/query/get_autocomplete/surecart-product', [ $this, 'get_autocomplete' ], 10, 2 ); |
| 43 | add_filter( 'elementor/query/get_value_titles/surecart-product', [ $this, 'get_titles' ], 10, 2 ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the titles for the query control |
| 49 | * This is important as it shows the previously selected items when the conditions load in. |
| 50 | * |
| 51 | * @param array $results The results. |
| 52 | * @param array $request The request. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function get_titles( $results, $request ) { |
| 57 | if ( 'surecart-product' !== $request['get_titles']['object'] || empty( $request['id'] ) ) { |
| 58 | return $results; |
| 59 | } |
| 60 | |
| 61 | $products = Product::where( |
| 62 | [ |
| 63 | 'ids' => [ $request['id'] ], |
| 64 | ] |
| 65 | )->get(); |
| 66 | |
| 67 | foreach ( $products as $product ) { |
| 68 | $results[ $product->id ] = $product->name; |
| 69 | } |
| 70 | return $results; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get autocomplete |
| 75 | * |
| 76 | * @param array $results The results. |
| 77 | * @param array $data Request data. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function get_autocomplete( $results, $data ) { |
| 82 | if ( 'surecart-product' !== $data['autocomplete']['object'] ) { |
| 83 | return $results; |
| 84 | } |
| 85 | |
| 86 | $products = Product::where( |
| 87 | [ |
| 88 | 'query' => $data['q'], |
| 89 | 'archived' => false, |
| 90 | ] |
| 91 | )->get(); |
| 92 | |
| 93 | foreach ( $products as $product ) { |
| 94 | $results[] = [ |
| 95 | 'id' => $product->id, |
| 96 | 'text' => $product->name, |
| 97 | ]; |
| 98 | } |
| 99 | return $results; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Elementor load scripts |
| 105 | * |
| 106 | * @return void |
| 107 | */ |
| 108 | public function load_scripts() { |
| 109 | wp_enqueue_script( 'surecart-elementor-editor', plugins_url( 'assets/editor.js', __FILE__ ), array( 'jquery' ), \SureCart::plugin()->version(), true ); |
| 110 | wp_enqueue_style( 'surecart-elementor-style', plugins_url( 'assets/editor.css', __FILE__ ), '', \SureCart::plugin()->version(), 'all' ); |
| 111 | wp_localize_script( |
| 112 | 'surecart-elementor-editor', |
| 113 | 'scElementorData', |
| 114 | [ |
| 115 | 'site_url' => site_url(), |
| 116 | ] |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Elementor surecart categories register |
| 122 | * |
| 123 | * @param Obj $elements_manager Elementor category manager. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function categories_registered( $elements_manager ) { |
| 128 | $elements_manager->add_category( |
| 129 | 'surecart-elementor', |
| 130 | [ |
| 131 | 'title' => esc_html__( 'SureCart', 'surecart' ), |
| 132 | 'icon' => 'fa fa-plug', |
| 133 | ] |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Elementor widget register |
| 139 | * |
| 140 | * @return void |
| 141 | */ |
| 142 | public function widget( $widgets_manager ) { |
| 143 | $widgets_manager->register( new ReusableFormWidget() ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Add product theme condition |
| 148 | * |
| 149 | * @param \ElementorPro\Modules\ThemeBuilder\Classes\Documents_Manager $documents_manager The documents manager. |
| 150 | * |
| 151 | * @return void |
| 152 | */ |
| 153 | public function register_document( $documents_manager ) { |
| 154 | $documents_manager->register_document_type( 'surecart-product', ProductDocument::get_class_full_name() ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Add product theme condition |
| 159 | * |
| 160 | * @param \ElementorPro\Modules\ThemeBuilder\Classes\Conditions_Manager $conditions_manager The conditions manager. |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | public function product_theme_conditions( $conditions_manager ) { |
| 165 | $conditions_manager->register_condition_instance( new Conditions() ); |
| 166 | } |
| 167 | } |
| 168 |