| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use \WP_Post; |
| 6 |
use \Hurrytimer\Utils\Helpers; |
| 7 |
class CampaignShortcode |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* Return shortcode content. |
| 12 |
* |
| 13 |
* @param $attrs |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public function content( $attrs ) |
| 18 |
{ |
| 19 |
if(is_admin() || |
| 20 |
// Don't run shortcode in Block editor. |
| 21 |
( |
| 22 |
isset($_SERVER['HTTP_REFERER']) |
| 23 |
&& !empty($_SERVER['HTTP_REFERER']) |
| 24 |
&& strpos($_SERVER['HTTP_REFERER'], '/wp-admin/post.php' ) !== false |
| 25 |
&& ! is_preview() |
| 26 |
) |
| 27 |
){ |
| 28 |
return ''; |
| 29 |
} |
| 30 |
|
| 31 |
$id = $this->getId( $attrs ); |
| 32 |
|
| 33 |
if ( $id === null ) { |
| 34 |
return ''; |
| 35 |
} |
| 36 |
$campaign = new Campaign( $id ); |
| 37 |
$campaign->loadSettings(); |
| 38 |
if ( ! $campaign->isActive() |
| 39 |
|| $campaign->isScheduled() |
| 40 |
|| ! $campaign->showStickyBarOn( $this->getCurrentPageId() ) |
| 41 |
|| $campaign->isStickyDismissed() |
| 42 |
|| ( $campaign->isRecurring() && ! $campaign->canRecurToday() ) |
| 43 |
|| ( $campaign->isRecurring() && $campaign->getRecurringDeadline() === null ) |
| 44 |
) { |
| 45 |
|
| 46 |
return apply_filters( 'hurryt_no_campaign', '', $campaign->getId() ); |
| 47 |
} |
| 48 |
$isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired(); |
| 49 |
$isRecurringCampaignExpired = $campaign->isRecurring() && $campaign->isRecurringExpired(); |
| 50 |
|
| 51 |
if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) { |
| 52 |
$actionManager = new ActionManager( $campaign ); |
| 53 |
$actionManager->run(); |
| 54 |
} |
| 55 |
|
| 56 |
$template = apply_filters( 'hurryt_campaign_template', $campaign->buildTemplate() ); |
| 57 |
|
| 58 |
return $template === '' ? '' : $campaign->wrapTemplate( $template ); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns campaign ID if set. |
| 64 |
* |
| 65 |
* @param $attrs |
| 66 |
* |
| 67 |
* @return int|null |
| 68 |
*/ |
| 69 |
public function getId( $attrs ) |
| 70 |
{ |
| 71 |
if ( ! $this->hasId( $attrs ) ) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
$id = intval( $attrs[ 'id' ] ); |
| 76 |
|
| 77 |
if ( ! $this->campaignExists( $id ) ) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
return $id; |
| 82 |
} |
| 83 |
|
| 84 |
public function getCurrentPageId() |
| 85 |
{ |
| 86 |
|
| 87 |
$objectId = get_queried_object_id(); |
| 88 |
if ( ! Utils\Helpers::isWcActive() ) { |
| 89 |
return $objectId; |
| 90 |
} |
| 91 |
$wcIds = [ |
| 92 |
'shop' => get_option( 'woocommerce_shop_page_id' ), |
| 93 |
'cart' => get_option( 'woocommerce_cart_page_id' ), |
| 94 |
'checkout' => get_option( 'woocommerce_checkout_page_id' ), |
| 95 |
'checkout_pay' => get_option( 'woocommerce_pay_page_id' ), |
| 96 |
'thanks' => get_option( 'woocommerce_thanks_page_id' ), |
| 97 |
'myaccount' => get_option( 'woocommerce_myaccount_page_id' ), |
| 98 |
'edit_address' => get_option( 'woocommerce_edit_address_page_id' ), |
| 99 |
'view_order' => get_option( 'woocommerce_view_order_page_id' ), |
| 100 |
'terms' => get_option( 'woocommerce_terms_page_id' ), |
| 101 |
]; |
| 102 |
if ( is_shop() ) { |
| 103 |
$objectId = $wcIds[ 'shop' ]; |
| 104 |
} elseif ( is_account_page() ) { |
| 105 |
$objectId = $wcIds[ 'myaccount' ]; |
| 106 |
} elseif ( is_checkout_pay_page() ) { |
| 107 |
$objectId = $wcIds[ 'checkout_pay' ]; |
| 108 |
} elseif ( is_checkout() ) { |
| 109 |
$objectId = $wcIds[ 'checkout' ]; |
| 110 |
} elseif ( is_cart() ) { |
| 111 |
$objectId = $wcIds[ 'cart' ]; |
| 112 |
} elseif ( is_view_order_page() ) { |
| 113 |
$objectId = $wcIds[ 'view_order' ]; |
| 114 |
} elseif ( is_view_order_page() ) { |
| 115 |
$objectId = $wcIds[ 'view_order' ]; |
| 116 |
} elseif ( is_view_order_page() ) { |
| 117 |
$objectId = $wcIds[ 'view_order' ]; |
| 118 |
} elseif ( is_view_order_page() ) { |
| 119 |
$objectId = $wcIds[ 'view_order' ]; |
| 120 |
} |
| 121 |
|
| 122 |
return $objectId; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Verify if the given attributes contain the campaign ID. |
| 127 |
* |
| 128 |
* @param $attrs |
| 129 |
* |
| 130 |
* @return bool |
| 131 |
*/ |
| 132 |
public function hasId( $attrs ) |
| 133 |
{ |
| 134 |
return isset( $attrs[ 'id' ] ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Verify if the given campaign exists in database. |
| 139 |
* |
| 140 |
* @param $id |
| 141 |
* |
| 142 |
* @return boolean |
| 143 |
*/ |
| 144 |
public function campaignExists( $id ) |
| 145 |
{ |
| 146 |
return get_post( $id ) instanceof WP_Post; |
| 147 |
} |
| 148 |
} |
| 149 |
|