Integrations
6 years ago
Products
6 years ago
Utilities
6 years ago
test
6 years ago
AJAX.php
6 years ago
Admin.php
6 years ago
Lifecycle.php
6 years ago
Products.php
6 years ago
fbasync.php
6 years ago
fbbackground.php
6 years ago
fbgraph.php
6 years ago
fbinfobanner.php
6 years ago
fbproduct.php
6 years ago
fbproductfeed.php
6 years ago
fbutils.php
6 years ago
fbwpml.php
6 years ago
fbwpml.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 4 | * |
| 5 | * This source code is licensed under the license found in the |
| 6 | * LICENSE file in the root directory of this source tree. |
| 7 | * |
| 8 | * @package FacebookCommerce |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WC_Facebook_WPML_Injector' ) ) : |
| 16 | |
| 17 | class FB_WPML_Language_Status { |
| 18 | const VISIBLE = 1; |
| 19 | const HIDDEN = 2; |
| 20 | const NOT_SYNCED = 0; |
| 21 | } |
| 22 | |
| 23 | class WC_Facebook_WPML_Injector { |
| 24 | public static $settings = null; |
| 25 | public static $default_lang = null; |
| 26 | const OPTION = 'fb_wmpl_language_visibility'; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Constructor for WC_Facebook_WPML_Injector class. |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | |
| 34 | self::$settings = get_option( self::OPTION ); |
| 35 | self::$default_lang = apply_filters( 'wpml_default_language', null ); |
| 36 | |
| 37 | if ( is_admin() ) { |
| 38 | add_action( 'icl_menu_footer', [ $this, 'wpml_support' ] ); |
| 39 | add_action( 'icl_ajx_custom_call', [ $this, 'wpml_ajax_support' ], 10, 2 ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | public static function should_hide( $wp_id ) { |
| 45 | $product_lang = apply_filters( 'wpml_post_language_details', null, $wp_id ); |
| 46 | $settings = self::$settings; |
| 47 | if ( $product_lang && isset( $product_lang['language_code'] ) ) { |
| 48 | $product_lang = $product_lang['language_code']; |
| 49 | } |
| 50 | |
| 51 | // Option doesn't exist : Backwards Compatibility |
| 52 | if ( ! $settings ) { |
| 53 | return ( $product_lang && self::$default_lang !== $product_lang ); |
| 54 | } |
| 55 | // Hide products from non-active languages. |
| 56 | if ( ! isset( $settings[ $product_lang ] ) ) { |
| 57 | return true; |
| 58 | } |
| 59 | return $settings[ $product_lang ] !== FB_WPML_Language_Status::VISIBLE; |
| 60 | } |
| 61 | |
| 62 | public function wpml_ajax_support( $call, $REQUEST ) { |
| 63 | global $sitepress; |
| 64 | if ( isset( $REQUEST['icl_ajx_action'] ) ) { |
| 65 | $call = $REQUEST['icl_ajx_action']; |
| 66 | } |
| 67 | if ( $call === 'icl_fb_woo' ) { |
| 68 | $active_languages = array_keys( $sitepress->get_active_languages() ); |
| 69 | $settings = array(); |
| 70 | foreach ( $active_languages as $lang ) { |
| 71 | $settings[ $lang ] = $REQUEST[ $lang ] === 'on' ? |
| 72 | FB_WPML_Language_Status::VISIBLE : FB_WPML_Language_Status::HIDDEN; |
| 73 | } |
| 74 | |
| 75 | update_option( 'fb_wmpl_language_visibility', $settings, false ); |
| 76 | self::$settings = $settings; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Prints the content for Facebook Visibility section. |
| 83 | * |
| 84 | * The section is shown at the bottom of the WPML > Languages settings page. |
| 85 | */ |
| 86 | public function wpml_support() { |
| 87 | /** @var object $sitepress */ |
| 88 | global $sitepress; |
| 89 | |
| 90 | // there is no nonce to check here and the value of $_GET['page] is being compared against a known and safe string |
| 91 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 92 | if ( isset( $_GET['page'] ) && false !== strpos( esc_url_raw( wp_unslash( $_GET['page'] ) ), 'languages.php' ) ) { |
| 93 | |
| 94 | /** @var array $active_languages */ |
| 95 | $active_languages = $sitepress->get_active_languages(); |
| 96 | $settings = get_option( self::OPTION ); |
| 97 | |
| 98 | // Default setting is only show default lang. |
| 99 | if ( ! $settings ) { |
| 100 | |
| 101 | $settings = array_fill_keys( |
| 102 | array_keys( $active_languages ), |
| 103 | FB_WPML_Language_Status::HIDDEN |
| 104 | ); |
| 105 | |
| 106 | if ( self::$default_lang ) { |
| 107 | $settings[ self::$default_lang ] = FB_WPML_Language_Status::VISIBLE; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | ?> |
| 112 | <div id="lang-sec-fb" class="wpml-section wpml-section-languages"> |
| 113 | <div class="wpml-section-header"> |
| 114 | <h3><?php esc_html_e( 'Facebook Visibility', 'facebook-for-woocommerce' ); ?></h3> |
| 115 | </div> |
| 116 | <div class="wpml-section-content"> |
| 117 | <?php esc_html_e( 'WooCommerce Products with languages that are selected here will be visible to customers who see your Facebook Shop.', 'facebook-for-woocommerce' ); ?> |
| 118 | |
| 119 | <div class="wpml-section-content-inner"> |
| 120 | <form id="icl_fb_woo" name="icl_fb_woo" action=""> |
| 121 | |
| 122 | <?php foreach ( $settings as $language => $set ) : ?> |
| 123 | |
| 124 | <p> |
| 125 | <label> |
| 126 | <input type="checkbox" id="icl_fb_woo_chk" name="<?php echo esc_attr( $language ); ?>" <?php checked( $set, FB_WPML_Language_Status::VISIBLE ); ?>> |
| 127 | <?php echo isset( $active_languages[ $language ]['native_name'] ) ? esc_html( $active_languages[ $language ]['native_name'] ) : esc_html( $language ); ?> |
| 128 | </label> |
| 129 | </p> |
| 130 | |
| 131 | <?php endforeach; ?> |
| 132 | |
| 133 | <?php // TODO: restore success message removed in e7f290f when FBE 2.0 changes are available {WV 2020-04-27} ?> |
| 134 | <p class="icl_ajx_response_fb" id="icl_ajx_response_fb" hidden="true"><?php esc_html_e( "Saved. An automated sync from Facebook will run every hour to update the catalog with any changes you've made.", 'facebook-for-woocommerce' ); ?></p> |
| 135 | |
| 136 | <p class="buttons-wrap"> |
| 137 | <input |
| 138 | class="button button-primary" |
| 139 | name="save" |
| 140 | value="<?php esc_attr_e( 'Save', 'sitepress' ); ?>" |
| 141 | type="submit" |
| 142 | /> |
| 143 | </p> |
| 144 | </form> |
| 145 | <script type="text/javascript"> |
| 146 | addLoadEvent( function() { |
| 147 | jQuery( '#icl_fb_woo' ).submit( iclSaveForm ); |
| 148 | jQuery( '#icl_fb_woo' ).submit( function() { |
| 149 | jQuery( '#icl_ajx_response_fb' ).show(); |
| 150 | } ); |
| 151 | } ); |
| 152 | </script> |
| 153 | </div> |
| 154 | </div> |
| 155 | </div> |
| 156 | <?php |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | } |
| 162 | |
| 163 | endif; |
| 164 |