| @@ -1,114 +1,140 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hurrytimer; |
| 4 | 4 | |
| 5 | +use \WP_Post; | |
| 6 | +use \Hurrytimer\Utils\Helpers; | |
| 5 | 7 | class CampaignShortcode |
| 6 | 8 | { |
| 7 | 9 | |
| 8 | 10 | /** |
| 9 | - * @var array The default shortcode attributes. | |
| 11 | + * Return shortcode content. | |
| 12 | + * | |
| 13 | + * @param $attrs | |
| 14 | + * | |
| 15 | + * @return string | |
| 10 | 16 | */ |
| 11 | - protected $default_attrs = [ | |
| 12 | - 'id' => null, | |
| 13 | - 'run_in_background' => false, | |
| 14 | - 'sticky' => false | |
| 15 | - ]; | |
| 17 | + public function content( $attrs ) | |
| 18 | + { | |
| 19 | + if(is_admin()){ | |
| 20 | + return ''; | |
| 21 | + } | |
| 16 | 22 | |
| 17 | - /** | |
| 18 | - * @var array The attributes array filter. | |
| 19 | - */ | |
| 20 | - protected $attrs_filter_def = [ | |
| 21 | - 'id' => FILTER_VALIDATE_INT, | |
| 22 | - 'run_in_background' => FILTER_VALIDATE_BOOLEAN, | |
| 23 | - 'sticky' => FILTER_VALIDATE_BOOLEAN | |
| 24 | - ]; | |
| 23 | + $id = $this->getId( $attrs ); | |
| 25 | 24 | |
| 26 | - /** | |
| 27 | - * Init shortcode | |
| 28 | - */ | |
| 29 | - function init() | |
| 30 | - { | |
| 31 | - add_shortcode( 'hurrytimer', [ $this, 'content' ] ); | |
| 32 | - add_filter( 'pre_do_shortcode_tag', [ $this, 'maybe_display' ], 10, 3 ); | |
| 33 | - add_filter( 'shortcode_atts_hurrytimer', [$this, 'translate_shortcode_id' ] ); | |
| 25 | + if ( $id === null ) { | |
| 26 | + return ''; | |
| 27 | + } | |
| 28 | + $campaign = new Campaign( $id ); | |
| 29 | + $campaign->loadSettings(); | |
| 30 | + if ( ! $campaign->isActive() | |
| 31 | + || $campaign->isScheduled() | |
| 32 | + || ! $campaign->showStickyBarOn( $this->getCurrentPageId() ) | |
| 33 | + || $campaign->isStickyDismissed() | |
| 34 | + || ( $campaign->isRecurring() && ! $campaign->canRecurToday() ) | |
| 35 | + || ( $campaign->isRecurring() && $campaign->getRecurringDeadline() === null ) | |
| 36 | + ) { | |
| 34 | 37 | |
| 35 | - } | |
| 38 | + return apply_filters( 'hurryt_no_campaign', '', $campaign->getId() ); | |
| 39 | + } | |
| 40 | + $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired(); | |
| 41 | + $isRecurringCampaignExpired = $campaign->isRecurring() && $campaign->isRecurringExpired(); | |
| 36 | 42 | |
| 37 | - function translate_shortcode_id($out){ | |
| 38 | - $out['id'] = apply_filters( 'wpml_object_id', $out['id'], HURRYT_POST_TYPE, true ); | |
| 43 | + if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) { | |
| 44 | + $actionManager = new ActionManager( $campaign ); | |
| 45 | + $actionManager->run(); | |
| 46 | + } | |
| 39 | 47 | |
| 40 | - return $out; | |
| 48 | + $template = apply_filters( 'hurryt_campaign_template', $campaign->buildTemplate() ); | |
| 49 | + | |
| 50 | + return $template === '' ? '' : $campaign->wrapTemplate( $template ); | |
| 51 | + | |
| 41 | 52 | } |
| 53 | + | |
| 42 | 54 | /** |
| 55 | + * Returns campaign ID if set. | |
| 43 | 56 | * |
| 44 | - * @param $return | |
| 45 | - * @param $tag | |
| 46 | - * @param $attr | |
| 57 | + * @param $attrs | |
| 47 | 58 | * |
| 48 | - * @return mixed|string|void | |
| 59 | + * @return int|null | |
| 49 | 60 | */ |
| 50 | - function maybe_display( $return, $tag, $attrs ) | |
| 61 | + public function getId( $attrs ) | |
| 51 | 62 | { |
| 52 | - if ( $tag !== 'hurrytimer' ) { | |
| 53 | - return $return; | |
| 63 | + if ( ! $this->hasId( $attrs ) ) { | |
| 64 | + return null; | |
| 54 | 65 | } |
| 55 | 66 | |
| 56 | - $attrs = shortcode_atts($this->default_attrs, $attrs, 'hurrytimer'); | |
| 57 | - $attrs = filter_var_array( $attrs, $this->attrs_filter_def ); | |
| 67 | + $id = intval( $attrs[ 'id' ] ); | |
| 58 | 68 | |
| 59 | - if ( empty( $attrs[ 'id' ] ) ) { | |
| 60 | - return 'HurryTimer: ' . __( 'Invalid campaign ID.', 'hurrytimer' ); | |
| 69 | + if ( ! $this->campaignExists( $id ) ) { | |
| 70 | + return null; | |
| 61 | 71 | } |
| 62 | 72 | |
| 63 | - if ( !get_post( $attrs[ 'id' ] ) ) { | |
| 64 | - return 'HurryTimer: ' . __( 'Invalid campaign ID.', 'hurrytimer' ); | |
| 65 | - } | |
| 73 | + return $id; | |
| 74 | + } | |
| 66 | 75 | |
| 67 | - $campaign = hurryt_get_campaign( $attrs[ 'id' ] ); | |
| 76 | + public function getCurrentPageId() | |
| 77 | + { | |
| 68 | 78 | |
| 69 | - if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) { | |
| 70 | - return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() ); | |
| 79 | + $objectId = get_queried_object_id(); | |
| 80 | + if ( ! Utils\Helpers::isWcActive() ) { | |
| 81 | + return $objectId; | |
| 71 | 82 | } |
| 72 | - | |
| 73 | - if ( !$campaign->is_running() ) { | |
| 74 | - | |
| 75 | - // Let developers show something else whenever the campaign isn't running. | |
| 76 | - return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() ); | |
| 83 | + $wcIds = [ | |
| 84 | + 'shop' => get_option( 'woocommerce_shop_page_id' ), | |
| 85 | + 'cart' => get_option( 'woocommerce_cart_page_id' ), | |
| 86 | + 'checkout' => get_option( 'woocommerce_checkout_page_id' ), | |
| 87 | + 'checkout_pay' => get_option( 'woocommerce_pay_page_id' ), | |
| 88 | + 'thanks' => get_option( 'woocommerce_thanks_page_id' ), | |
| 89 | + 'myaccount' => get_option( 'woocommerce_myaccount_page_id' ), | |
| 90 | + 'edit_address' => get_option( 'woocommerce_edit_address_page_id' ), | |
| 91 | + 'view_order' => get_option( 'woocommerce_view_order_page_id' ), | |
| 92 | + 'terms' => get_option( 'woocommerce_terms_page_id' ), | |
| 93 | + ]; | |
| 94 | + if ( is_shop() ) { | |
| 95 | + $objectId = $wcIds[ 'shop' ]; | |
| 96 | + } elseif ( is_account_page() ) { | |
| 97 | + $objectId = $wcIds[ 'myaccount' ]; | |
| 98 | + } elseif ( is_checkout_pay_page() ) { | |
| 99 | + $objectId = $wcIds[ 'checkout_pay' ]; | |
| 100 | + } elseif ( is_checkout() ) { | |
| 101 | + $objectId = $wcIds[ 'checkout' ]; | |
| 102 | + } elseif ( is_cart() ) { | |
| 103 | + $objectId = $wcIds[ 'cart' ]; | |
| 104 | + } elseif ( is_view_order_page() ) { | |
| 105 | + $objectId = $wcIds[ 'view_order' ]; | |
| 106 | + } elseif ( is_view_order_page() ) { | |
| 107 | + $objectId = $wcIds[ 'view_order' ]; | |
| 108 | + } elseif ( is_view_order_page() ) { | |
| 109 | + $objectId = $wcIds[ 'view_order' ]; | |
| 110 | + } elseif ( is_view_order_page() ) { | |
| 111 | + $objectId = $wcIds[ 'view_order' ]; | |
| 77 | 112 | } |
| 78 | 113 | |
| 79 | - // Let developers decide whether to show the campaign or not. | |
| 80 | - if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) { | |
| 81 | - return ''; | |
| 82 | - } | |
| 83 | - | |
| 84 | - | |
| 85 | - return $return; | |
| 114 | + return $objectId; | |
| 86 | 115 | } |
| 87 | 116 | |
| 88 | 117 | /** |
| 89 | - * Return shortcode content. | |
| 118 | + * Verify if the given attributes contain the campaign ID. | |
| 90 | 119 | * |
| 91 | 120 | * @param $attrs |
| 92 | 121 | * |
| 93 | - * @return string | |
| 122 | + * @return bool | |
| 94 | 123 | */ |
| 95 | - public function content( $attrs ) | |
| 124 | + public function hasId( $attrs ) | |
| 96 | 125 | { |
| 126 | + return isset( $attrs[ 'id' ] ); | |
| 127 | + } | |
| 97 | 128 | |
| 98 | - $attrs = shortcode_atts($this->default_attrs, $attrs, 'hurrytimer'); | |
| 99 | - $attrs = filter_var_array( $attrs, $this->attrs_filter_def ); | |
| 100 | - | |
| 101 | - $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) ); | |
| 102 | - | |
| 103 | - if ( $campaign->is_expired() ) { | |
| 104 | - ( new ActionManager( $campaign ) )->run(); | |
| 105 | - } | |
| 106 | - | |
| 107 | - // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`. | |
| 108 | - $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() ); | |
| 109 | - | |
| 110 | - return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : ''; | |
| 129 | + /** | |
| 130 | + * Verify if the given campaign exists in database. | |
| 131 | + * | |
| 132 | + * @param $id | |
| 133 | + * | |
| 134 | + * @return boolean | |
| 135 | + */ | |
| 136 | + public function campaignExists( $id ) | |
| 137 | + { | |
| 138 | + return get_post( $id ) instanceof WP_Post; | |
| 111 | 139 | } |
| 112 | - | |
| 113 | - | |
| 114 | 140 | } |