class-yith-wcwl-admin-panel.php
1 year ago
class-yith-wcwl-admin.php
1 year ago
class-yith-wcwl-rendering-method-admin-handler.php
1 year ago
class-yith-wcwl-rendering-method-admin-handler.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class that manage the admin handling rendering methods |
| 4 | * |
| 5 | * @since 4.0.0 |
| 6 | * @package YITH\Wishlist\Admin\ |
| 7 | * @author YITH <plugins@yithemes.com> |
| 8 | */ |
| 9 | |
| 10 | defined( 'YITH_WCWL' ) || exit; |
| 11 | |
| 12 | if ( ! class_exists( 'YITH_WCWL_Rendering_Method_Admin_Handler' ) ) { |
| 13 | /** |
| 14 | * Class that manage the admin handling rendering methods |
| 15 | */ |
| 16 | class YITH_WCWL_Rendering_Method_Admin_Handler { |
| 17 | use YITH_WCWL_Singleton_Trait; |
| 18 | use YITH_WCWL_Rendering_Method_Access_Trait; |
| 19 | |
| 20 | /** |
| 21 | * Class constructor |
| 22 | */ |
| 23 | protected function __construct() { |
| 24 | $this->maybe_update_rendering_method(); |
| 25 | add_action( 'admin_notices', array( $this, 'add_notices' ) ); |
| 26 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 27 | } |
| 28 | |
| 29 | protected function maybe_update_rendering_method() { |
| 30 | if ( ! empty( $_REQUEST[ self::$exit_from_preview_mode_param ] ) ) { |
| 31 | self::update_preview_options( false ); |
| 32 | } |
| 33 | if ( ! empty( $_REQUEST[ self::$convert_to_react_rendering_param ] ) ) { |
| 34 | self::update_preview_options( false ); |
| 35 | self::$updated_rendering_method = self::update_rendering_method( 'react-components' ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add notices used to update the rendering method |
| 41 | */ |
| 42 | public function add_notices() { |
| 43 | $rendering_method = self::get_rendering_method_option(); |
| 44 | $args = array(); |
| 45 | |
| 46 | if ( 'php-templates' === $rendering_method && YITH_WCWL_Admin_Panel::get_instance()->is_panel() ) { |
| 47 | $args[ 'message' ] = sprintf( |
| 48 | // translators: %1$s is the opening tag to hyperlink the text; %2$s id the closing tag to hyperlink the text; |
| 49 | __( 'YITH Wishlist 4.0 includes the new React Components. %1$s Read how to convert to them to improve the plugin performance %2$s', 'yith-woocommerce-wishlist' ), |
| 50 | '<a id="yith-wcwl-open-rendering-methods-modal" href="#">', |
| 51 | '</a>' |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | if ( 'react-components' === $rendering_method && self::$updated_rendering_method ) { |
| 56 | $args[ 'message' ] = sprintf( |
| 57 | __( 'Successfully switched to the new React Components management!', 'yith-woocommerce-wishlist' ), |
| 58 | '<a id="yith-wcwl-open-rendering-methods-modal" href="#">', |
| 59 | '</a>' |
| 60 | ); |
| 61 | $args[ 'notice_type' ] = 'success'; |
| 62 | } |
| 63 | |
| 64 | if ( ! empty( $args[ 'message' ] ) ) { |
| 65 | $args = wp_parse_args( |
| 66 | $args, |
| 67 | array( |
| 68 | 'type' => 'notice', |
| 69 | 'notice_type' => 'warning', |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | echo yith_plugin_fw_get_component( $args ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Enqueue scripts |
| 79 | */ |
| 80 | public function enqueue_scripts() { |
| 81 | if ( 'react-components' === self::get_rendering_method_option() ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'unminified/' : ''; |
| 86 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 87 | |
| 88 | wp_register_script( 'yith-wcwl-rendering-methods', YITH_WCWL_ASSETS_JS_URL . $prefix . 'admin/rendering-methods' . $suffix . '.js', array( 'jquery' ), YITH_WCWL_VERSION ); |
| 89 | wp_register_style( 'yith-wcwl-rendering-methods', YITH_WCWL_ASSETS_CSS_URL . 'admin/rendering-methods.css', array(), YITH_WCWL_VERSION ); |
| 90 | |
| 91 | $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ]; |
| 92 | |
| 93 | wp_localize_script( |
| 94 | 'yith-wcwl-rendering-methods', |
| 95 | 'yithWcwlRenderingMethods', |
| 96 | array( |
| 97 | 'i18n' => array( |
| 98 | 'reactOnlyOptionModalTitle' => __( 'This option is available only in the new React templates. Convert now to unlock all the new features!', 'yith-woocommerce-wishlist' ), |
| 99 | ), |
| 100 | 'modals' => array( |
| 101 | 'updateRenderingMethod' => array( |
| 102 | 'title' => __( 'YITH Wishlist 4.0 includes new React Components. Convert now to improve the plugin performance!', 'yith-woocommerce-wishlist' ), |
| 103 | 'content' => __( "In this update, we converted our frontend templates using React to improve code readability and plugin performance.<br><br><b>Our PHP templates will be deprecated soon</b> (in the next major update) so we suggest you convert them now to enjoy the new system and check if everything works properly.<br><br>Any doubts? Don't worry! You can click on the button below to see a preview of the new system on your site before you apply the changes!", 'yith-woocommerce-wishlist' ), |
| 104 | 'footer' => yith_wcwl_get_view( |
| 105 | 'rendering-methods/modal/footer.php', |
| 106 | array( |
| 107 | 'preview_url' => add_query_arg( |
| 108 | array( |
| 109 | self::$react_preview_key => true, |
| 110 | ), |
| 111 | wc_get_page_permalink( 'shop' ) |
| 112 | ), |
| 113 | 'convert_to_react_url' => add_query_arg( array( self::$convert_to_react_rendering_param => true ), $current_url ), |
| 114 | ), |
| 115 | true ), |
| 116 | ), |
| 117 | ), |
| 118 | 'templates' => array( |
| 119 | 'reactOnlyOptionNotice' => yith_wcwl_get_view( 'rendering-methods/react-only-option-notice.php', array(), true ), |
| 120 | ), |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | if ( YITH_WCWL_Admin_Panel::get_instance()->is_panel() ) { |
| 125 | wp_enqueue_script( 'yith-wcwl-rendering-methods' ); |
| 126 | wp_enqueue_style( 'yith-wcwl-rendering-methods' ); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |