BeforeHooks.php
2 weeks ago
class-wcml-setup-handlers.php
2 weeks ago
class-wcml-setup-ui.php
2 weeks ago
class-wcml-setup.php
2 weeks ago
class-wcml-setup.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Setup { |
| 4 | const MULTI_CURRENCY_STATUS_GET_KEY = 'enabled'; |
| 5 | |
| 6 | private $ui; |
| 7 | private $handlers; |
| 8 | private $steps; |
| 9 | private $woocommerce_wpml; |
| 10 | private $sitepress; |
| 11 | |
| 12 | public function __construct( WCML_Setup_UI $ui, WCML_Setup_Handlers $handlers, woocommerce_wpml $woocommerce_wpml, SitePress $sitepress ) { |
| 13 | |
| 14 | $this->ui = $ui; |
| 15 | $this->handlers = $handlers; |
| 16 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 17 | $this->sitepress = $sitepress; |
| 18 | |
| 19 | $stepUrlStorePages = \WCML\Utilities\AdminUrl::getSetup( WCML_Setup_Store_Pages_UI::SLUG ); |
| 20 | $stepUrlAttributes = \WCML\Utilities\AdminUrl::getSetup( WCML_Setup_Attributes_UI::SLUG ); |
| 21 | $stepUrlMulticurrency = \WCML\Utilities\AdminUrl::getSetup( WCML_Setup_Multi_Currency_UI::SLUG ); |
| 22 | |
| 23 | $this->steps = [ |
| 24 | WCML_Setup_Introduction_UI::SLUG => [ |
| 25 | 'name' => __( 'Introduction', 'woocommerce-multilingual' ), |
| 26 | 'view' => new WCML_Setup_Introduction_UI( |
| 27 | $stepUrlStorePages |
| 28 | ), |
| 29 | 'handler' => '', |
| 30 | ], |
| 31 | WCML_Setup_Store_Pages_UI::SLUG => [ |
| 32 | 'name' => __( 'Store Pages', 'woocommerce-multilingual' ), |
| 33 | 'view' => new WCML_Setup_Store_Pages_UI( |
| 34 | $this->woocommerce_wpml, |
| 35 | $this->sitepress, |
| 36 | $stepUrlAttributes, |
| 37 | \WCML\Utilities\AdminUrl::getSetup( WCML_Setup_Introduction_UI::SLUG ) |
| 38 | ), |
| 39 | 'handler' => [ $this->handlers, 'install_store_pages' ], |
| 40 | ], |
| 41 | WCML_Setup_Attributes_UI::SLUG => [ |
| 42 | 'name' => __( 'Global Attributes', 'woocommerce-multilingual' ), |
| 43 | 'view' => new WCML_Setup_Attributes_UI( |
| 44 | $this->woocommerce_wpml, |
| 45 | $stepUrlMulticurrency, |
| 46 | $stepUrlStorePages |
| 47 | ), |
| 48 | 'handler' => [ $this->handlers, 'save_attributes' ], |
| 49 | ], |
| 50 | WCML_Setup_Multi_Currency_UI::SLUG => [ |
| 51 | 'name' => __( 'Multiple Currencies', 'woocommerce-multilingual' ), |
| 52 | 'view' => new WCML_Setup_Multi_Currency_UI( |
| 53 | $stepUrlMulticurrency, |
| 54 | $stepUrlAttributes |
| 55 | ), |
| 56 | 'handler' => [ $this->handlers, 'save_multi_currency' ], |
| 57 | ], |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | private function is_submitting_last_step_multicurrency_status(): bool { |
| 62 | $value = \WPML\FP\Obj::prop( self::MULTI_CURRENCY_STATUS_GET_KEY, $_GET ); |
| 63 | |
| 64 | return in_array( $value, [ "0", "1" ], true ); |
| 65 | } |
| 66 | |
| 67 | public function add_hooks() { |
| 68 | if ( current_user_can( 'manage_options' ) ) { |
| 69 | add_action( 'admin_init', [ $this, 'wizard' ] ); |
| 70 | add_action( 'admin_init', [ $this, 'handle_steps' ], 0 ); |
| 71 | add_filter( 'wp_redirect', [ $this, 'redirect_filters' ] ); |
| 72 | } |
| 73 | |
| 74 | if ( ! $this->has_completed() ) { |
| 75 | $this->ui->add_wizard_notice_hook(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public function setup_redirect() { |
| 80 | if ( get_transient( '_wcml_activation_redirect' ) ) { |
| 81 | delete_transient( '_wcml_activation_redirect' ); |
| 82 | |
| 83 | if ( ! $this->do_not_redirect_to_setup() && ! $this->has_completed() ) { |
| 84 | wcml_safe_redirect( admin_url( 'index.php?page=wcml-setup' ) ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private function do_not_redirect_to_setup() { |
| 90 | $woocommerce_notices = get_option( 'woocommerce_admin_notices', [] ); |
| 91 | $woocommerce_setup_not_run = in_array( 'install', $woocommerce_notices, true ); |
| 92 | |
| 93 | $needsWcWizardFirst = get_transient( '_wc_activation_redirect' ); |
| 94 | |
| 95 | return $this->is_wcml_setup_page() || |
| 96 | is_network_admin() || |
| 97 | isset( $_GET['activate-multi'] ) || |
| 98 | ! current_user_can( 'manage_options' ) || |
| 99 | $woocommerce_setup_not_run || |
| 100 | $needsWcWizardFirst || |
| 101 | wpml_is_ajax(); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | private function is_wcml_setup_page() { |
| 106 | return isset( $_GET['page'] ) && WCML_Setup_UI::SLUG === $_GET['page']; |
| 107 | } |
| 108 | |
| 109 | private function is_wcml_admin_page() { |
| 110 | return isset( $_GET['page'] ) && 'wcml' === $_GET['page']; |
| 111 | } |
| 112 | |
| 113 | public function wizard() { |
| 114 | |
| 115 | $this->splash_wizard_on_wcml_pages(); |
| 116 | |
| 117 | if ( ! $this->is_wcml_setup_page() ) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : current( array_keys( $this->steps ) ); |
| 122 | |
| 123 | wp_enqueue_style( 'otgs-icons' ); |
| 124 | wp_enqueue_style( |
| 125 | 'wcml-setup', |
| 126 | WCML_PLUGIN_URL . '/res/css/wcml-setup.css', |
| 127 | [ |
| 128 | 'dashicons', |
| 129 | 'install', |
| 130 | OTGS_Assets_Handles::POPOVER_TOOLTIP, |
| 131 | ], |
| 132 | WCML_VERSION |
| 133 | ); |
| 134 | |
| 135 | wp_enqueue_script( 'wcml-setup', WCML_PLUGIN_URL . '/res/js/wcml-setup.js', [ 'jquery', OTGS_Assets_Handles::POPOVER_TOOLTIP ], WCML_VERSION, true ); |
| 136 | |
| 137 | $this->ui->setup_header( $this->steps, $step ); |
| 138 | $this->ui->setup_steps( $this->steps, $step ); |
| 139 | $this->ui->setup_content( $this->steps[ $step ]['view'] ); |
| 140 | $this->ui->setup_footer( ! empty( $this->steps[ $step ]['handler'] ) ); |
| 141 | |
| 142 | if ( $this->is_setup_complete( $step ) ) { |
| 143 | $this->complete_setup(); |
| 144 | $this->redirect_to_tm_dashboard_on_setup_complete(); |
| 145 | } |
| 146 | |
| 147 | wp_die(); |
| 148 | } |
| 149 | |
| 150 | private function is_setup_complete( $step ): bool { |
| 151 | if ( WCML_Setup_Multi_Currency_UI::SLUG !== $step ) { |
| 152 | return false; |
| 153 | } |
| 154 | return $this->is_submitting_last_step_multicurrency_status(); |
| 155 | } |
| 156 | |
| 157 | private function redirect_to_tm_dashboard_on_setup_complete() { |
| 158 | wcml_safe_redirect( \WCML\Utilities\AdminUrl::getWPMLTMDashboard() ); |
| 159 | } |
| 160 | |
| 161 | private function splash_wizard_on_wcml_pages() { |
| 162 | |
| 163 | if ( isset( $_GET['src'] ) && \WCML\Utilities\AdminUrl::SRC_SETUP_LATER === $_GET['src'] ) { |
| 164 | $this->woocommerce_wpml->settings['set_up_wizard_splash'] = 1; |
| 165 | $this->woocommerce_wpml->update_settings(); |
| 166 | } |
| 167 | |
| 168 | if ( $this->is_wcml_admin_page() && ! $this->has_completed() && empty( $this->woocommerce_wpml->settings['set_up_wizard_splash'] ) ) { |
| 169 | wcml_safe_redirect( \WCML\Utilities\AdminUrl::getSetup() ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | public function complete_setup() { |
| 174 | $this->save_product_translation_mode(); |
| 175 | $this->save_term_meta_thumbnail_id_to_copy(); |
| 176 | |
| 177 | $this->woocommerce_wpml->settings['set_up_wizard_run'] = 1; |
| 178 | $this->woocommerce_wpml->settings['set_up_wizard_splash'] = 1; |
| 179 | $this->woocommerce_wpml->update_settings(); |
| 180 | |
| 181 | do_action( 'wcml_setup_completed' ); |
| 182 | } |
| 183 | |
| 184 | public function save_term_meta_thumbnail_id_to_copy() { |
| 185 | $tm_settings = $this->sitepress->get_setting( 'translation-management', [] ); |
| 186 | if ( ! isset( $tm_settings['custom_term_fields_translation']['thumbnail_id'] ) ) { |
| 187 | $tm_settings['custom_term_fields_translation']['thumbnail_id'] = "1"; |
| 188 | $this->sitepress->set_setting( 'translation-management', $tm_settings, true ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | public function save_product_translation_mode() { |
| 193 | $custom_posts_unlocked = apply_filters( 'wpml_get_setting', false, 'custom_posts_unlocked_option' ); |
| 194 | $custom_posts_sync = apply_filters( 'wpml_get_setting', false, 'custom_posts_sync_option' ); |
| 195 | |
| 196 | $is_display_as_translated_checked = isset( $custom_posts_unlocked['product'], $custom_posts_sync['product'] ) |
| 197 | && 1 === $custom_posts_unlocked['product'] |
| 198 | && WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED === $custom_posts_sync['product']; |
| 199 | |
| 200 | $settings_helper = wpml_load_settings_helper(); |
| 201 | |
| 202 | if ( $is_display_as_translated_checked ) { |
| 203 | $settings_helper->set_post_type_display_as_translated( 'product' ); |
| 204 | $settings_helper->set_post_type_translation_unlocked_option( 'product' ); |
| 205 | $settings_helper->set_taxonomy_display_as_translated( 'product_cat' ); |
| 206 | $settings_helper->set_taxonomy_translation_unlocked_option( 'product_cat' ); |
| 207 | } else { |
| 208 | $settings_helper->set_post_type_translatable( 'product' ); |
| 209 | $settings_helper->set_post_type_translation_unlocked_option( 'product', false ); |
| 210 | $settings_helper->set_taxonomy_translatable( 'product_cat' ); |
| 211 | $settings_helper->set_taxonomy_translation_unlocked_option( 'product_cat', false ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private function has_completed(): bool { |
| 216 | return ! empty( $this->woocommerce_wpml->settings['set_up_wizard_run'] ); |
| 217 | } |
| 218 | |
| 219 | public function redirect_filters( $url ) { |
| 220 | if ( isset( $_POST['next_step_url'] ) && $_POST['next_step_url'] ) { |
| 221 | $url = sanitize_text_field( $_POST['next_step_url'] ); |
| 222 | } |
| 223 | |
| 224 | return $url; |
| 225 | } |
| 226 | |
| 227 | private function get_handler( $step ) { |
| 228 | $handler = ! empty( $this->steps[ $step ]['handler'] ) ? $this->steps[ $step ]['handler'] : ''; |
| 229 | |
| 230 | return $handler; |
| 231 | } |
| 232 | |
| 233 | public function handle_steps() { |
| 234 | if ( isset( $_POST['handle_step'] ) && wp_create_nonce( $_POST['handle_step'] ) === $_POST['nonce'] ) { |
| 235 | $step_name = sanitize_text_field( $_POST['handle_step'] ); |
| 236 | if ( $handler = $this->get_handler( $step_name ) ) { |
| 237 | if ( is_callable( $handler, true ) ) { |
| 238 | call_user_func( $handler, $_REQUEST ); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 |