| @@ -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,31 +13,43 @@ | ||
| 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); | |
| 19 | + if(is_admin()){ | |
| 20 | + return ''; | |
| 21 | + } | |
| 20 | 22 | |
| 21 | - if ($id === null) { | |
| 23 | + $id = $this->getId( $attrs ); | |
| 24 | + | |
| 25 | + if ( $id === null ) { | |
| 22 | 26 | return ''; |
| 23 | 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 | + ) { | |
| 24 | 37 | |
| 25 | - $campaign = new Campaign($id); | |
| 26 | - $campaign->loadSettings(); | |
| 27 | - // Don't render the shortcode if the campaign's inactive. | |
| 28 | - if ( ! $campaign->isActive() || $campaign->isScheduled()) { | |
| 29 | - return ''; | |
| 38 | + return apply_filters( 'hurryt_no_campaign', '', $campaign->getId() ); | |
| 30 | 39 | } |
| 31 | - // Run campaign's actions if expired. | |
| 32 | - if ($campaign->isRegular() && $campaign->isExpired()) { | |
| 33 | - $actionManager = new ActionManager($campaign); | |
| 40 | + $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired(); | |
| 41 | + $isRecurringCampaignExpired = $campaign->isRecurring() && $campaign->isRecurringExpired(); | |
| 42 | + | |
| 43 | + if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) { | |
| 44 | + $actionManager = new ActionManager( $campaign ); | |
| 34 | 45 | $actionManager->run(); |
| 35 | 46 | } |
| 36 | 47 | |
| 37 | - $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate()); | |
| 38 | - return $campaign->wrapTemplate($template); | |
| 48 | + $template = apply_filters( 'hurryt_campaign_template', $campaign->buildTemplate() ); | |
| 39 | 49 | |
| 50 | + return $template === '' ? '' : $campaign->wrapTemplate( $template ); | |
| 51 | + | |
| 40 | 52 | } |
| 41 | 53 | |
| 42 | 54 | /** |
| 43 | 55 | * Returns campaign ID if set. |
| @@ -45,17 +57,17 @@ | ||
| 45 | 57 | * @param $attrs |
| 46 | 58 | * |
| 47 | 59 | * @return int|null |
| 48 | 60 | */ |
| 49 | - function getId($attrs) | |
| 61 | + public function getId( $attrs ) | |
| 50 | 62 | { |
| 51 | - if ( ! $this->hasId($attrs)) { | |
| 63 | + if ( ! $this->hasId( $attrs ) ) { | |
| 52 | 64 | return null; |
| 53 | 65 | } |
| 54 | 66 | |
| 55 | - $id = intval($attrs['id']); | |
| 67 | + $id = intval( $attrs[ 'id' ] ); | |
| 56 | 68 | |
| 57 | - if ( ! $this->campaignExists($id)) { | |
| 69 | + if ( ! $this->campaignExists( $id ) ) { | |
| 58 | 70 | return null; |
| 59 | 71 | } |
| 60 | 72 | |
| 61 | 73 | return $id; |
| @@ -60,8 +72,49 @@ | ||
| 60 | 72 | |
| 61 | 73 | return $id; |
| 62 | 74 | } |
| 63 | 75 | |
| 76 | + public function getCurrentPageId() | |
| 77 | + { | |
| 78 | + | |
| 79 | + $objectId = get_queried_object_id(); | |
| 80 | + if ( ! Utils\Helpers::isWcActive() ) { | |
| 81 | + return $objectId; | |
| 82 | + } | |
| 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' ]; | |
| 112 | + } | |
| 113 | + | |
| 114 | + return $objectId; | |
| 115 | + } | |
| 116 | + | |
| 64 | 117 | /** |
| 65 | 118 | * Verify if the given attributes contain the campaign ID. |
| 66 | 119 | * |
| 67 | 120 | * @param $attrs |
| @@ -67,11 +120,11 @@ | ||
| 67 | 120 | * @param $attrs |
| 68 | 121 | * |
| 69 | 122 | * @return bool |
| 70 | 123 | */ |
| 71 | - function hasId($attrs) | |
| 124 | + public function hasId( $attrs ) | |
| 72 | 125 | { |
| 73 | - return isset($attrs['id']); | |
| 126 | + return isset( $attrs[ 'id' ] ); | |
| 74 | 127 | } |
| 75 | 128 | |
| 76 | 129 | /** |
| 77 | 130 | * Verify if the given campaign exists in database. |
| @@ -79,9 +132,9 @@ | ||
| 79 | 132 | * @param $id |
| 80 | 133 | * |
| 81 | 134 | * @return boolean |
| 82 | 135 | */ |
| 83 | - function campaignExists($id) | |
| 136 | + public function campaignExists( $id ) | |
| 84 | 137 | { |
| 85 | - return get_post($id) instanceof WP_Post; | |
| 138 | + return get_post( $id ) instanceof WP_Post; | |
| 86 | 139 | } |
| 87 | -} | |
| 140 | +} | |