types
3 months ago
class-group-ad-relation.php
5 days ago
class-group-factory.php
1 day ago
class-group-ordered.php
1 year ago
class-group-repository.php
1 day ago
class-group-slider.php
5 months ago
class-group-standard.php
1 year ago
class-group-types.php
1 day ago
class-groups.php
1 day ago
index.php
1 year ago
class-group-ad-relation.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage group and ad relationship. |
| 4 | * |
| 5 | * @since 2.0.7 |
| 6 | * @package AdvancedAds |
| 7 | * @author Advanced Ads <info@wpadvancedads.com> |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Groups; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use AdvancedAds\Abstracts\Group; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Group ad relation class. |
| 19 | */ |
| 20 | class Group_Ad_Relation { |
| 21 | |
| 22 | /** |
| 23 | * Hold ads. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private $ads = []; |
| 28 | |
| 29 | /** |
| 30 | * Create ad group relation. |
| 31 | * |
| 32 | * @param Group $group Group object. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function relate( &$group ): void { |
| 37 | $data = $group->get_data(); |
| 38 | $changes = $group->get_changes(); |
| 39 | $old_ads = $data['ad_weights'] ? array_keys( $data['ad_weights'] ) : []; |
| 40 | $new_ads = $changes['ad_weights'] ? array_keys( $changes['ad_weights'] ) : []; |
| 41 | |
| 42 | $removed_ads = array_diff( $old_ads, $new_ads ); |
| 43 | $added_ads = array_diff( $new_ads, $old_ads ); |
| 44 | |
| 45 | $affected_ad_ids = array_unique( array_map( 'intval', array_merge( $removed_ads, $added_ads ) ) ); |
| 46 | |
| 47 | if ( ! empty( $affected_ad_ids ) ) { |
| 48 | update_object_term_cache( $affected_ad_ids, Constants::TAXONOMY_GROUP ); |
| 49 | } |
| 50 | |
| 51 | $this->handle_removed_ads( $removed_ads, $group->get_id() ); |
| 52 | $this->handle_added_ads( $added_ads, $group ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handles the removed ads for a group. |
| 57 | * |
| 58 | * @param array $removed_ads An array of ad IDs that have been removed. |
| 59 | * @param int $group_id The ID of the group. |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | private function handle_removed_ads( $removed_ads, $group_id ): void { |
| 64 | foreach ( $removed_ads as $ad_id ) { |
| 65 | $terms = wp_get_object_terms( $ad_id, Constants::TAXONOMY_GROUP ); |
| 66 | |
| 67 | if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 68 | $term_ids = wp_list_pluck( $terms, 'term_id' ); |
| 69 | $term_ids = array_diff( $term_ids, [ $group_id ] ); |
| 70 | wp_set_object_terms( $ad_id, $term_ids, Constants::TAXONOMY_GROUP ); |
| 71 | update_post_meta( $ad_id, Constants::AD_META_GROUP_IDS, $term_ids ); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Handles the added ads for a group. |
| 78 | * |
| 79 | * @param array $added_ads An array of ad IDs that have been added. |
| 80 | * @param Group $group Group instance. |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | private function handle_added_ads( array $added_ads, &$group ): void { |
| 85 | $group_id = $group->get_id(); |
| 86 | $ad_weights = $group->get_ad_weights(); |
| 87 | |
| 88 | foreach ( $added_ads as $ad_id ) { |
| 89 | /** |
| 90 | * Check if this ad is representing the current group and remove it in this case |
| 91 | * could cause an infinite loop otherwise. |
| 92 | */ |
| 93 | if ( $this->is_ad_type_group( $ad_id, $group ) ) { |
| 94 | unset( $ad_weights[ $ad_id ] ); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | $terms = wp_get_object_terms( $ad_id, Constants::TAXONOMY_GROUP ); |
| 99 | |
| 100 | if ( ! is_wp_error( $terms ) ) { |
| 101 | $term_ids = wp_list_pluck( $terms, 'term_id' ); |
| 102 | $term_ids[] = $group_id; |
| 103 | $term_ids = array_unique( $term_ids ); |
| 104 | |
| 105 | wp_set_object_terms( $ad_id, $term_ids, Constants::TAXONOMY_GROUP ); |
| 106 | update_post_meta( $ad_id, Constants::AD_META_GROUP_IDS, $term_ids ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $group->set_ad_weights( $ad_weights ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check if ad is of type 'group' and belongs to the current group. |
| 115 | * |
| 116 | * @param int $ad_id Ad id. |
| 117 | * @param Group $group Group instance. |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | private function is_ad_type_group( int $ad_id, Group $group ): bool { |
| 122 | if ( ! isset( $this->ads[ $ad_id ] ) ) { |
| 123 | $this->ads[ $ad_id ] = wp_advads_get_ad( $ad_id ); |
| 124 | } |
| 125 | |
| 126 | $ad = $this->ads[ $ad_id ]; |
| 127 | return $ad && $ad->is_type( 'group' ) && $ad->get_group_id() === $group->get_id(); |
| 128 | } |
| 129 | } |
| 130 |