| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\ThirdPartySupport\Elementor\Traits; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Campaigns\Repositories\CampaignRepository; |
| 7 |
use Give\Log\Log; |
| 8 |
use Give\Campaigns\ValueObjects\CampaignPageMetaKeys; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trait to get campaign options for Elementor widgets |
| 12 |
* |
| 13 |
* @since 4.7.0 |
| 14 |
*/ |
| 15 |
trait HasCampaignOptions |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Get the default campaign option |
| 19 |
* |
| 20 |
* @since 4.7.0 |
| 21 |
*/ |
| 22 |
public function getDefaultCampaignOption(array $options): string |
| 23 |
{ |
| 24 |
$default = !empty($options) ? array_key_first($options) : ''; |
| 25 |
|
| 26 |
|
| 27 |
$campaignId = get_post_meta(get_the_ID(), CampaignPageMetaKeys::CAMPAIGN_ID, true); |
| 28 |
|
| 29 |
if (!$campaignId) { |
| 30 |
return $default; |
| 31 |
} |
| 32 |
|
| 33 |
return array_key_exists($campaignId, $options) ? $campaignId : $default; |
| 34 |
} |
| 35 |
/** |
| 36 |
* Get campaign options for select dropdown |
| 37 |
* |
| 38 |
* @since 4.7.0 |
| 39 |
*/ |
| 40 |
public function getCampaignOptions(): array |
| 41 |
{ |
| 42 |
try { |
| 43 |
$campaignRepository = give(CampaignRepository::class); |
| 44 |
$campaigns = $campaignRepository->prepareQuery() |
| 45 |
->where('status', 'active') |
| 46 |
->orderBy('campaign_title', 'ASC') |
| 47 |
->getAll(); |
| 48 |
|
| 49 |
if (empty($campaigns)) { |
| 50 |
return []; |
| 51 |
} |
| 52 |
|
| 53 |
$options = []; |
| 54 |
foreach ($campaigns as $campaign) { |
| 55 |
$options[$campaign->id] = $campaign->title; |
| 56 |
} |
| 57 |
|
| 58 |
return $options; |
| 59 |
} catch (Exception $e) { |
| 60 |
Log::error('Elementor Campaign Options Error: ' . $e->getMessage()); |
| 61 |
|
| 62 |
return []; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|