class-ads.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Crons Ads using Action Scheduler. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | */ |
| 7 | |
| 8 | namespace AdvancedAds\Crons; |
| 9 | |
| 10 | use DateTimeImmutable; |
| 11 | use AdvancedAds\Constants; |
| 12 | use AdvancedAds\Abstracts\Ad; |
| 13 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 14 | use WP_Post; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Crons Ads. |
| 20 | */ |
| 21 | class Ads implements Integration_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Post ID allowed to bypass caps during cron expiration update. |
| 25 | * |
| 26 | * @var int |
| 27 | */ |
| 28 | private $bypass_cap_post_id = 0; |
| 29 | |
| 30 | /** |
| 31 | * Hook into WordPress |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function hooks(): void { |
| 36 | add_action( 'advanced-ads-ad-pre-save', [ $this, 'save_expiration_date' ] ); |
| 37 | add_action( Constants::CRON_JOB_AD_EXPIRATION, [ $this, 'update_ad_status' ] ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Create Action Scheduler job and save into independent meta |
| 42 | * |
| 43 | * @param Ad $ad Ad instance. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function save_expiration_date( Ad $ad ): void { |
| 48 | $post_id = $ad->get_id(); |
| 49 | if ( ! $post_id || ! function_exists( 'as_unschedule_all_actions' ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $args = [ $post_id ]; |
| 54 | $group = 'advanced_ads'; |
| 55 | $expiry = (int) $ad->get_expiry_date( 'edit' ); |
| 56 | |
| 57 | as_unschedule_all_actions( |
| 58 | Constants::CRON_JOB_AD_EXPIRATION, |
| 59 | $args, |
| 60 | $group |
| 61 | ); |
| 62 | |
| 63 | if ( $expiry <= 0 ) { |
| 64 | delete_post_meta( $post_id, Constants::AD_META_EXPIRATION_TIME ); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $datetime = ( new DateTimeImmutable() )->setTimestamp( $expiry ); |
| 69 | |
| 70 | update_post_meta( |
| 71 | $post_id, |
| 72 | Constants::AD_META_EXPIRATION_TIME, |
| 73 | $datetime->format( 'Y-m-d H:i:s' ) |
| 74 | ); |
| 75 | |
| 76 | if ( $expiry <= time() ) { |
| 77 | $this->update_ad_status( $post_id ); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | as_schedule_single_action( |
| 82 | $expiry, |
| 83 | Constants::CRON_JOB_AD_EXPIRATION, |
| 84 | $args, |
| 85 | $group, |
| 86 | true |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Update post status to expired |
| 92 | * |
| 93 | * @param int $post_id Post ID. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function update_ad_status( $post_id ): void { |
| 98 | $post_id = absint( $post_id ); |
| 99 | if ( ! $post_id ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | $post = get_post( $post_id ); |
| 104 | if ( ! $post instanceof WP_Post || Constants::POST_TYPE_AD !== $post->post_type ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if ( Constants::AD_STATUS_EXPIRED === $post->post_status || 'trash' === $post->post_status ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | $this->bypass_cap_post_id = $post_id; |
| 113 | add_filter( 'user_has_cap', [ $this, 'grant_edit_cap_for_expiration' ], 10, 3 ); |
| 114 | |
| 115 | kses_remove_filters(); |
| 116 | |
| 117 | wp_update_post( |
| 118 | [ |
| 119 | 'ID' => $post_id, |
| 120 | 'post_status' => Constants::AD_STATUS_EXPIRED, |
| 121 | ] |
| 122 | ); |
| 123 | |
| 124 | kses_init_filters(); |
| 125 | |
| 126 | remove_filter( 'user_has_cap', [ $this, 'grant_edit_cap_for_expiration' ], 10 ); |
| 127 | $this->bypass_cap_post_id = 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Allow Action Scheduler to expire ads when no user is logged in. |
| 132 | * |
| 133 | * @param array<string, bool> $allcaps All capabilities. |
| 134 | * @param array<int, string> $caps Required capabilities. |
| 135 | * @param array<int, mixed> $args Capability check args. |
| 136 | * |
| 137 | * @return array<string, bool> |
| 138 | */ |
| 139 | public function grant_edit_cap_for_expiration( array $allcaps, array $caps, array $args ): array { |
| 140 | if ( ! $this->bypass_cap_post_id || empty( $args[0] ) || 'edit_post' !== $args[0] ) { |
| 141 | return $allcaps; |
| 142 | } |
| 143 | |
| 144 | if ( isset( $args[2] ) && (int) $args[2] === $this->bypass_cap_post_id ) { |
| 145 | $allcaps['advanced_ads_edit_ads'] = true; |
| 146 | } |
| 147 | |
| 148 | return $allcaps; |
| 149 | } |
| 150 | } |
| 151 |