manager.php
209 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Settings\Base; |
| 3 | |
| 4 | use Elementor\Core\Base\Document; |
| 5 | use Elementor\Widget_Base; |
| 6 | use Elementor\Controls_Manager; |
| 7 | use Elementor\Group_Control_Image_Size; |
| 8 | use Elementor\Group_Control_Typography; |
| 9 | use Elementor\Core\Schemes\Color; |
| 10 | use Elementor\Core\Schemes\Typography; |
| 11 | use Elementor\Control_Media; |
| 12 | use Elementor\Group_Control_Border; |
| 13 | use Elementor\Group_Control_Box_Shadow; |
| 14 | use Elementor\Group_Control_Background; |
| 15 | use Elementor\Core\Kits\Controls\Repeater as Global_Style_Repeater; |
| 16 | use Elementor\Repeater; |
| 17 | |
| 18 | |
| 19 | abstract class Manager { |
| 20 | |
| 21 | public function __construct(){ |
| 22 | $this->register_hooks(); |
| 23 | } |
| 24 | |
| 25 | protected function register_hooks(){ |
| 26 | add_action( 'elementor/documents/register_controls', [ $this, 'register_controls' ] ); |
| 27 | add_action( 'elementor/document/after_save', [ $this, 'on_save_settings' ], 10, 2 ); |
| 28 | add_action( 'elementor/document/after_save', [ $this, 'elementor_after_save' ], 10, 2 ); |
| 29 | add_action( 'customize_save_after', [ $this, 'sync_customizer_with_elementor'] ); |
| 30 | add_action( 'auxin_demo_import_finish', [ $this, 'after_demo_import'] ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register Document Controls |
| 35 | * |
| 36 | * Add New Controls to Elementor Page Options |
| 37 | * @param $document |
| 38 | */ |
| 39 | abstract public function register_controls ( $document ); |
| 40 | |
| 41 | /** |
| 42 | * Stores the changed controllers |
| 43 | * |
| 44 | * @param array $settings list of settings for changes controllers |
| 45 | * @param Document $document Elementor base Document class |
| 46 | * @param array|null $data All document info passed for saving |
| 47 | * @return mixed |
| 48 | */ |
| 49 | abstract protected function save_settings( array $settings, $document, $data = null ); |
| 50 | |
| 51 | /** |
| 52 | * Parsing custom control settings |
| 53 | * |
| 54 | * @param $document |
| 55 | * @param $data |
| 56 | */ |
| 57 | public function on_save_settings( $document, $data ){ |
| 58 | if( empty( $data['settings'] ) ){ |
| 59 | return; |
| 60 | } |
| 61 | $settings_to_save = $this->get_settings_to_save( $data['settings'] ); |
| 62 | $this->save_settings( $settings_to_save, $document, $data ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Default special page settings in Elementor document |
| 67 | * |
| 68 | * @return array |
| 69 | */ |
| 70 | protected function get_special_settings_names(){ |
| 71 | return [ |
| 72 | 'id', |
| 73 | 'post_title', |
| 74 | 'post_status', |
| 75 | 'template', |
| 76 | 'post_excerpt', |
| 77 | 'post_featured_image', |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get custom control settings which are changed |
| 83 | * |
| 84 | * @param array $settings |
| 85 | * @return array |
| 86 | */ |
| 87 | private function get_settings_to_save( array $settings ){ |
| 88 | $special_settings = $this->get_special_settings_names(); |
| 89 | |
| 90 | $settings_to_save = $settings; |
| 91 | |
| 92 | foreach ( $special_settings as $special_setting ) { |
| 93 | if ( isset( $settings_to_save[ $special_setting ] ) ) { |
| 94 | unset( $settings_to_save[ $special_setting ] ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $settings_to_save; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Save auxin colors |
| 103 | * |
| 104 | * @param object $object |
| 105 | * @param array $data |
| 106 | * @return void |
| 107 | */ |
| 108 | public function elementor_after_save( $object, $data ) { |
| 109 | if ( $data && isset( $data['settings']['system_colors'] ) && is_array( $data['settings']['system_colors'] ) ) { |
| 110 | foreach ( $data['settings']['system_colors'] as $key => $value ) { |
| 111 | if ( !empty( $value['color'] ) ) { |
| 112 | auxin_update_option( 'elementor_color_' . $value['_id'], $value['color'] ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * update elementor global colors in customizer |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function after_demo_import() { |
| 125 | |
| 126 | // get and update system colors in customizer |
| 127 | $current_settings = \Elementor\Plugin::$instance->kits_manager->get_current_settings(); |
| 128 | foreach ( $current_settings['system_colors'] as $color ) { |
| 129 | auxin_update_option( 'elementor_color_' . $color['_id'], $color['color'] ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sync Customizer with elementor for elementor global colors |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | public function sync_customizer_with_elementor() { |
| 139 | |
| 140 | // get elementor settings |
| 141 | $active_kit = get_option( 'elementor_active_kit', '' ); |
| 142 | if ( empty( $active_kit ) ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | $settings = get_post_meta( $active_kit, '_elementor_page_settings', true ); |
| 147 | |
| 148 | // get elementor global custom colors from customizer |
| 149 | $added_custom_colors = auxin_get_option( 'elementor_global_custom_colors_repeater', ''); |
| 150 | $colors = json_decode( $added_custom_colors, true ); |
| 151 | $custom_colors = []; |
| 152 | if ( !empty( $colors ) ) { |
| 153 | foreach( $colors as $key => $color ) { |
| 154 | if( empty( $color['color'] ) ) { |
| 155 | continue; |
| 156 | } |
| 157 | $custom_colors[] = [ |
| 158 | '_id' => !empty( $color['_id'] ) ? sanitize_text_field( $color['_id'] ) : \Elementor\Utils::generate_random_string(), |
| 159 | 'title' => !empty( $color['title'] ) ? sanitize_text_field( $color['title'] ) : __('New Color', 'auxin-elements') . ' #' . $key, |
| 160 | 'color' => $color['color'] |
| 161 | ]; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ( empty( $settings ) ) { |
| 166 | |
| 167 | // get elementor system colors form customizer |
| 168 | $system_colors_key = ['primary', 'secondary', 'text', 'accent' ]; |
| 169 | foreach( $system_colors_key as $key ) { |
| 170 | $system_colors[] = [ |
| 171 | '_id' => $key, |
| 172 | 'title' => ucfirst( $key ), |
| 173 | 'color' => auxin_get_option( 'elementor_color_' . $key, '' ) |
| 174 | ]; |
| 175 | } |
| 176 | |
| 177 | // udpate system colors |
| 178 | \Elementor\Plugin::$instance->kits_manager->update_kit_settings_based_on_option( 'system_colors', $system_colors ); |
| 179 | |
| 180 | // update custom colors |
| 181 | if ( !empty( $custom_colors ) ) { |
| 182 | \Elementor\Plugin::$instance->kits_manager->update_kit_settings_based_on_option( 'custom_colors', $custom_colors ); |
| 183 | } |
| 184 | |
| 185 | } else { |
| 186 | // get and update system colors => executing these codes to remain title of system colors from elementor settings unchanged |
| 187 | $current_settings = \Elementor\Plugin::$instance->kits_manager->get_current_settings(); |
| 188 | foreach ( $current_settings['system_colors'] as $color ) { |
| 189 | $system_colors[] = [ |
| 190 | '_id' => $color['_id'], |
| 191 | 'title' => $color['title'], |
| 192 | 'color' => auxin_get_option( 'elementor_color_' . $color['_id'], $color['color'] ) |
| 193 | ]; |
| 194 | } |
| 195 | \Elementor\Plugin::$instance->kits_manager->update_kit_settings_based_on_option( 'system_colors', $system_colors ); |
| 196 | |
| 197 | // update custom colors -> updating via post meta because we have to replace all custom colors from customizer with the one in elemnetor settings |
| 198 | if ( ! empty( $custom_colors ) ) { |
| 199 | $settings['custom_colors'] = $custom_colors; |
| 200 | update_post_meta( $active_kit, '_elementor_page_settings', $settings ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | auxin_update_option( 'elementor_global_custom_colors_repeater', '' ); |
| 205 | \Elementor\Plugin::instance()->files_manager->clear_cache(); |
| 206 | |
| 207 | } |
| 208 | } |
| 209 |