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