| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Hurrytimer; |
| 4 | 4 | |
| 5 | 5 | use \WP_Post; |
| 6 | - | |
| 6 | +use \Hurrytimer\Utils\Helpers; | |
| 7 | 7 | class CampaignShortcode |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | 10 | /** |
| @@ -13,48 +13,45 @@ | ||
| 13 | 13 | * @param $attrs |
| 14 | 14 | * |
| 15 | 15 | * @return string |
| 16 | 16 | */ |
| 17 | - function content($attrs) | |
| 17 | + public function content( $attrs ) | |
| 18 | 18 | { |
| 19 | - $id = $this->getId($attrs); | |
| 20 | - | |
| 21 | - if ($id === null) { | |
| 19 | + if(is_admin()){ | |
| 22 | 20 | return ''; |
| 23 | 21 | } |
| 24 | - $campaign = new Campaign($id); | |
| 25 | 22 | |
| 26 | - $campaign->loadSettings(); | |
| 23 | + $id = $this->getId( $attrs ); | |
| 27 | 24 | |
| 28 | - // Don't render the shortcode if the campaign's inactive. | |
| 29 | - if ( ! $campaign->isActive() || $campaign->isScheduled() | |
| 30 | - || ! $campaign->showStickyBarOn($this->getCurrentPageId()) | |
| 31 | - ) { | |
| 32 | - | |
| 25 | + if ( $id === null ) { | |
| 33 | 26 | return ''; |
| 34 | 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 | + ) { | |
| 35 | 37 | |
| 36 | - // We must check if the campaign is sticky | |
| 37 | - // If yes, we don't need to run actions as the campaign is no longer active. | |
| 38 | - // Running actions for dismissed campaigns is bad for UX, so we avoid it. | |
| 39 | - if($campaign->isDismissed()){ | |
| 40 | - return ''; | |
| 38 | + return apply_filters( 'hurryt_no_campaign', '', $campaign->getId() ); | |
| 41 | 39 | } |
| 40 | + $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired(); | |
| 41 | + $isRecurringCampaignExpired = $campaign->isRecurring() && $campaign->isRecurringExpired(); | |
| 42 | 42 | |
| 43 | - // If it's a regular campaign and expired, run selected actions. | |
| 44 | - if ($campaign->isRegular() && $campaign->isExpired()) { | |
| 45 | - $actionManager = new ActionManager($campaign); | |
| 43 | + if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) { | |
| 44 | + $actionManager = new ActionManager( $campaign ); | |
| 46 | 45 | $actionManager->run(); |
| 47 | 46 | } |
| 48 | 47 | |
| 48 | + $template = apply_filters( 'hurryt_campaign_template', $campaign->buildTemplate() ); | |
| 49 | 49 | |
| 50 | - $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate()); | |
| 51 | - | |
| 52 | - return $template === '' ? '': $campaign->wrapTemplate($template); | |
| 50 | + return $template === '' ? '' : $campaign->wrapTemplate( $template ); | |
| 53 | 51 | |
| 54 | 52 | } |
| 55 | 53 | |
| 56 | - | |
| 57 | 54 | /** |
| 58 | 55 | * Returns campaign ID if set. |
| 59 | 56 | * |
| 60 | 57 | * @param $attrs |
| @@ -60,17 +57,17 @@ | ||
| 60 | 57 | * @param $attrs |
| 61 | 58 | * |
| 62 | 59 | * @return int|null |
| 63 | 60 | */ |
| 64 | - function getId($attrs) | |
| 61 | + public function getId( $attrs ) | |
| 65 | 62 | { |
| 66 | - if ( ! $this->hasId($attrs)) { | |
| 63 | + if ( ! $this->hasId( $attrs ) ) { | |
| 67 | 64 | return null; |
| 68 | 65 | } |
| 69 | 66 | |
| 70 | - $id = intval($attrs['id']); | |
| 67 | + $id = intval( $attrs[ 'id' ] ); | |
| 71 | 68 | |
| 72 | - if ( ! $this->campaignExists($id)) { | |
| 69 | + if ( ! $this->campaignExists( $id ) ) { | |
| 73 | 70 | return null; |
| 74 | 71 | } |
| 75 | 72 | |
| 76 | 73 | return $id; |
| @@ -75,44 +72,44 @@ | ||
| 75 | 72 | |
| 76 | 73 | return $id; |
| 77 | 74 | } |
| 78 | 75 | |
| 79 | - function getCurrentPageId() | |
| 76 | + public function getCurrentPageId() | |
| 80 | 77 | { |
| 81 | 78 | |
| 82 | 79 | $objectId = get_queried_object_id(); |
| 83 | - if(! Utils\Helpers::isWcActive()){ | |
| 80 | + if ( ! Utils\Helpers::isWcActive() ) { | |
| 84 | 81 | return $objectId; |
| 85 | 82 | } |
| 86 | 83 | $wcIds = [ |
| 87 | - 'shop' => get_option('woocommerce_shop_page_id'), | |
| 88 | - 'cart' => get_option('woocommerce_cart_page_id'), | |
| 89 | - 'checkout' => get_option('woocommerce_checkout_page_id'), | |
| 90 | - 'checkout_pay' => get_option('woocommerce_pay_page_id'), | |
| 91 | - 'thanks' => get_option('woocommerce_thanks_page_id'), | |
| 92 | - 'myaccount' => get_option('woocommerce_myaccount_page_id'), | |
| 93 | - 'edit_address' => get_option('woocommerce_edit_address_page_id'), | |
| 94 | - 'view_order' => get_option('woocommerce_view_order_page_id'), | |
| 95 | - 'terms' => get_option('woocommerce_terms_page_id'), | |
| 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' ), | |
| 96 | 93 | ]; |
| 97 | - if (is_shop()) { | |
| 98 | - $objectId = $wcIds['shop']; | |
| 99 | - } elseif (is_account_page()) { | |
| 100 | - $objectId = $wcIds['myaccount']; | |
| 101 | - } elseif (is_checkout_pay_page()) { | |
| 102 | - $objectId = $wcIds['checkout_pay']; | |
| 103 | - } elseif (is_checkout()) { | |
| 104 | - $objectId = $wcIds['checkout']; | |
| 105 | - } elseif (is_cart()) { | |
| 106 | - $objectId = $wcIds['cart']; | |
| 107 | - } elseif (is_view_order_page()) { | |
| 108 | - $objectId = $wcIds['view_order']; | |
| 109 | - } elseif (is_view_order_page()) { | |
| 110 | - $objectId = $wcIds['view_order']; | |
| 111 | - } elseif (is_view_order_page()) { | |
| 112 | - $objectId = $wcIds['view_order']; | |
| 113 | - } elseif (is_view_order_page()) { | |
| 114 | - $objectId = $wcIds['view_order']; | |
| 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' ]; | |
| 115 | 112 | } |
| 116 | 113 | |
| 117 | 114 | return $objectId; |
| 118 | 115 | } |
| @@ -123,11 +120,11 @@ | ||
| 123 | 120 | * @param $attrs |
| 124 | 121 | * |
| 125 | 122 | * @return bool |
| 126 | 123 | */ |
| 127 | - function hasId($attrs) | |
| 124 | + public function hasId( $attrs ) | |
| 128 | 125 | { |
| 129 | - return isset($attrs['id']); | |
| 126 | + return isset( $attrs[ 'id' ] ); | |
| 130 | 127 | } |
| 131 | 128 | |
| 132 | 129 | /** |
| 133 | 130 | * Verify if the given campaign exists in database. |
| @@ -135,9 +132,9 @@ | ||
| 135 | 132 | * @param $id |
| 136 | 133 | * |
| 137 | 134 | * @return boolean |
| 138 | 135 | */ |
| 139 | - function campaignExists($id) | |
| 136 | + public function campaignExists( $id ) | |
| 140 | 137 | { |
| 141 | - return get_post($id) instanceof WP_Post; | |
| 138 | + return get_post( $id ) instanceof WP_Post; | |
| 142 | 139 | } |
| 143 | -} | |
| 140 | +} | |