'2019-11-20 00:00:01', // required; offer end 'end' => '2019-12-04 23:59:00', // required; some unique hash for offer, use for hiding the offer when user click to close. 'hash' => '9df37798-bd35-421b-b6f2-fbc095686efc', // Optional; Allow closing default is 0, set to 1 to allow user hide it 'dismissible' => 1, // 0 // required; main content required, will be filtered with wp_kses_post() 'content' => '
Biggest Sale of the year on this
Claim your discount on till 4th December
', // optional; wrapper padding wrapperPadding => '' // optional; image source url or data url 'backgroundImage' => '', // optional; 'backgroundRepeat' => '', // optional; 'backgroundSize' => '', // optional; background color to apply with the background image 'backgroundColor' => '#000', // optional; text color will be inherited by the content 'color' => '#fff', // optional; can be empty 'logo' => [ // required; image source url or data url 'src' => '', // required; 'alt' => 'Woo Feed Pro', ], // optional; can be empty 'button' => [ // required 'label' => 'Save 30%', // required 'url' => '#?utm_campaign=black_friday_&_cyber_monday&utm_medium=banner&utm_source=wp_dashboard', // optional; 'backgroundColor' => '#F71560', // optional; 'color' => '#FFF', // optional; 'after' => 'Coupon: BFCM2019', // html content filtered with wp_kses_post() ], ], ]; * */ class Promotions { /** * CTXFeed\AppServices\Client * * @var Client */ protected $client; /** * URL for Promotions source json file * @var string */ private $promotionSrc; /** * Promotions * @var bool|object[] */ private $promotions = false; /** * List of hidden promotions for current user * @var array */ private $hiddenPromotions; /** * Current User Id * @var int */ private $currentUser = 0; /** * Promotions constructor. * @param Client $client The Client. * @param string $data_source Data Source URL * @return void */ public function __construct( Client $client, $data_source = null ) { $this->client = $client; if ( ! is_null( $data_source ) ) $this->promotionSrc = esc_url( $data_source ); } /** * Set JSON Source File URL For getting promotion data * @param string $URL Set Data Source URL. * * @return Promotions */ public function set_source( $URL ) { $this->promotionSrc = esc_url( $URL ); return $this; } /** * Init Promotions * @return void */ public function init() { if ( is_null( $this->promotionSrc ) ) { _doing_it_wrong( __METHOD__, esc_html__( 'Promotion Source URL Not Set. see Promotions::set_source( $URL )', 'woo-feed' ), '1.0.0' ); } add_action( 'admin_init', [ $this, '__init_internal' ], 10 ); } /** * Set environment variables and init internal hooks * @return void */ public function __init_internal() { $this->currentUser = get_current_user_id(); $this->hiddenPromotions = (array) get_user_option( $this->client->getSlug() . '_hidden_promos', $this->currentUser ); $this->promotions = $this->__get_promos(); // only run if there is active promotions. if ( count( $this->promotions ) ) { add_action( 'admin_notices', [ $this, '__show_promos' ], 10 ); add_action( 'wp_ajax_webappick_dismiss_promo', [ $this, '__webappick_dismiss_promo' ], 10 ); add_action( 'admin_print_styles', [ $this, '__get_promo_styles' ], 99 ); add_action( 'admin_enqueue_scripts', [ $this, '__enqueue_deps' ], 10 ); add_action( 'admin_print_footer_scripts', [ $this, '__get_promo_scripts' ], 10 ); } } /** * Render Promotions * @return void */ public function __show_promos() { foreach ( $this->promotions as $promotion ) { $wrapperStyles = ''; $buttonStyles = ''; $is_dismissible = ! isset( $promotion->dismissible ) || isset( $promotion->dismissible ) && 0 == $promotion->dismissible ? false : true; $has_columns = isset( $promotion->button, $promotion->logo ); if ( isset( $promotion->color ) ) { $wrapperStyles .= 'color: ' . $promotion->color . ';'; } if ( isset( $promotion->wrapperPadding ) ) { $wrapperStyles .= 'padding: ' . $promotion->wrapperPadding . ';'; } if ( isset( $promotion->backgroundColor ) ) { $wrapperStyles .= 'background-color: ' . $promotion->backgroundColor . ';'; } if ( isset( $promotion->backgroundImage ) ) { $wrapperStyles .= 'background-image: url("' . esc_url( $promotion->backgroundImage ) . '");'; } if ( isset( $promotion->backgroundRepeat ) ) { $wrapperStyles .= 'background-repeat: ' . $promotion->backgroundRepeat . ';'; } if ( isset( $promotion->backgroundSize ) ) { $wrapperStyles .= 'background-size: ' . $promotion->backgroundSize . ';'; } if ( property_exists( $promotion, 'button' ) ) { if ( isset( $promotion->button->backgroundColor ) ) { $buttonStyles .= 'background-color: ' . $promotion->button->backgroundColor . ';border-color: ' . $promotion->button->backgroundColor . ';'; } if ( isset( $promotion->button->color ) ) { $buttonStyles .= 'color: ' . $promotion->button->color . ';'; } } $noticeClasses = 'notice notice-success wapk-promo'; if ( $is_dismissible ) { $noticeClasses .= ' is-dismissible'; } ?>