ads
1 week ago
groups
1 week ago
metaboxes
1 year ago
pages
3 months ago
placements
1 week ago
class-action-links.php
1 week ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
1 week ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
1 week ago
class-authors.php
1 year ago
class-edd-updater.php
1 month ago
class-list-filters.php
1 week ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 week ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
1 week ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-post-types.php
206 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class is responsible for handling the edit posts views and some functionality on the edit post screen. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use stdClass; |
| 13 | use AdvancedAds\Constants; |
| 14 | use AdvancedAds\Framework\Utilities\Params; |
| 15 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Post Types. |
| 21 | */ |
| 22 | class Post_Types implements Integration_Interface { |
| 23 | |
| 24 | /** |
| 25 | * Hook into WordPress. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function hooks(): void { |
| 30 | add_action( 'before_delete_post', [ $this, 'before_delete_ad' ], 10, 2 ); |
| 31 | |
| 32 | add_filter( 'post_updated_messages', [ $this, 'post_updated_messages' ] ); |
| 33 | add_filter( 'bulk_post_updated_messages', [ $this, 'bulk_post_updated_messages' ], 10, 2 ); |
| 34 | |
| 35 | add_filter( 'wp_count_posts', [ $this, 'update_count_posts' ], 10, 2 ); |
| 36 | add_filter( 'get_edit_post_link', [ $this, 'get_edit_post_link' ], 10, 2 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Prepare the ad groups for ad deletion |
| 41 | * |
| 42 | * @param int $post_id id of the post. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function before_delete_ad( $post_id, $post ): void { |
| 47 | global $wpdb; |
| 48 | |
| 49 | if ( ! current_user_can( 'delete_posts' ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if ( $post_id > 0 ) { |
| 54 | if ( Constants::POST_TYPE_AD === $post->post_type ) { |
| 55 | /** |
| 56 | * Images uploaded to an image ad type get the `_advanced-ads_parent_id` meta key from WordPress automatically |
| 57 | * the following SQL query removes that meta data from any attachment when the ad is removed. |
| 58 | */ |
| 59 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_advanced-ads_parent_id', $post_id ) ); // phpcs:ignore |
| 60 | |
| 61 | $terms = wp_get_object_terms( $post_id, Constants::TAXONOMY_GROUP ); |
| 62 | |
| 63 | if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
| 64 | $term_ids = wp_list_pluck( $terms, 'term_id' ); |
| 65 | |
| 66 | if( ! empty( $term_ids ) ) { |
| 67 | foreach( $term_ids as $group_id ) { |
| 68 | $group = wp_advads_get_group( $group_id ); |
| 69 | |
| 70 | $ad_weights = $group->get_ad_weights(); |
| 71 | |
| 72 | unset( $ad_weights[ $post_id ] ); |
| 73 | |
| 74 | $group->set_ad_weights( $ad_weights ); |
| 75 | $group->save(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Update post counts to have expiring ads. |
| 85 | * |
| 86 | * @param stdClass $counts An object containing the current post_type's post |
| 87 | * counts by status. |
| 88 | * @param string $type Post type. |
| 89 | * |
| 90 | * @return stdClass |
| 91 | */ |
| 92 | public function update_count_posts( $counts, $type ): stdClass { |
| 93 | if ( Constants::POST_TYPE_AD !== $type ) { |
| 94 | return $counts; |
| 95 | } |
| 96 | |
| 97 | $now = time(); |
| 98 | $expiring = 0; |
| 99 | |
| 100 | foreach ( wp_advads_get_ad_summaries() as $summary ) { |
| 101 | if ( ! empty( $summary['expiry_date'] ) && $summary['expiry_date'] > $now ) { |
| 102 | ++$expiring; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | $counts->{Constants::AD_STATUS_EXPIRING} = $expiring; |
| 107 | |
| 108 | return $counts; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Change messages when a post type is updated. |
| 113 | * |
| 114 | * @since 1.4.7 |
| 115 | * |
| 116 | * @param array $messages Existing post update messages. |
| 117 | * |
| 118 | * @return array |
| 119 | */ |
| 120 | public function post_updated_messages( $messages = [] ): array { |
| 121 | global $post; |
| 122 | |
| 123 | // Added to fix error message array caused by third party code that uses post_updated_messages filter wrong. |
| 124 | if ( ! is_array( $messages ) ) { |
| 125 | $messages = []; |
| 126 | } |
| 127 | |
| 128 | $revision = Params::get( 'revision', 0, FILTER_VALIDATE_INT ); |
| 129 | |
| 130 | $messages[ Constants::POST_TYPE_AD ] = [ |
| 131 | 0 => '', // Unused. Messages start at index 1. |
| 132 | 1 => __( 'Ad updated.', 'advanced-ads' ), |
| 133 | 4 => __( 'Ad updated.', 'advanced-ads' ), |
| 134 | 5 => $revision |
| 135 | /* translators: %s: date and time of the revision */ |
| 136 | ? sprintf( __( 'Ad restored to revision from %s', 'advanced-ads' ), wp_post_revision_title( $revision, false ) ) |
| 137 | : false, |
| 138 | 6 => __( 'Ad saved.', 'advanced-ads' ), |
| 139 | 7 => __( 'Ad saved.', 'advanced-ads' ), |
| 140 | 8 => __( 'Ad submitted.', 'advanced-ads' ), |
| 141 | 9 => sprintf( |
| 142 | /* translators: %s: date */ |
| 143 | __( 'Ad scheduled for: <strong>%1$s</strong>.', 'advanced-ads' ), |
| 144 | '<strong>' . date_i18n( __( 'M j, Y @ G:i', 'advanced-ads' ), strtotime( $post->post_date ) ) . '</strong>' |
| 145 | ), |
| 146 | 10 => __( 'Ad draft updated.', 'advanced-ads' ), |
| 147 | ]; |
| 148 | |
| 149 | return $messages; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Edit ad bulk update messages |
| 154 | * |
| 155 | * @param array $messages existing bulk update messages. |
| 156 | * @param array $counts numbers of updated ads. |
| 157 | * |
| 158 | * @return array |
| 159 | */ |
| 160 | public function bulk_post_updated_messages( array $messages, array $counts ): array { |
| 161 | $messages[ Constants::POST_TYPE_AD ] = [ |
| 162 | /* translators: %s: ad count */ |
| 163 | 'updated' => _n( '%s ad updated.', '%s ads updated.', $counts['updated'], 'advanced-ads' ), |
| 164 | /* translators: %s: ad count */ |
| 165 | 'locked' => _n( '%s ad not updated, somebody is editing it.', '%s ads not updated, somebody is editing them.', $counts['locked'], 'advanced-ads' ), |
| 166 | /* translators: %s: ad count */ |
| 167 | 'deleted' => _n( '%s ad permanently deleted.', '%s ads permanently deleted.', $counts['deleted'], 'advanced-ads' ), |
| 168 | /* translators: %s: ad count */ |
| 169 | 'trashed' => _n( '%s ad moved to the Trash.', '%s ads moved to the Trash.', $counts['trashed'], 'advanced-ads' ), |
| 170 | /* translators: %s: ad count */ |
| 171 | 'untrashed' => _n( '%s ad restored from the Trash.', '%s ads restored from the Trash.', $counts['untrashed'], 'advanced-ads' ), |
| 172 | ]; |
| 173 | |
| 174 | $messages[ Constants::POST_TYPE_PLACEMENT ] = [ |
| 175 | /* translators: %s: placement count */ |
| 176 | 'updated' => _n( '%s placement updated.', '%s placements updated.', $counts['updated'], 'advanced-ads' ), |
| 177 | /* translators: %s: placement count */ |
| 178 | 'locked' => _n( '%s placement not updated, somebody is editing it.', '%s placements not updated, somebody is editing them.', $counts['locked'], 'advanced-ads' ), |
| 179 | /* translators: %s: placement count */ |
| 180 | 'deleted' => _n( '%s placement permanently deleted.', '%s placements permanently deleted.', $counts['deleted'], 'advanced-ads' ), |
| 181 | /* translators: %s: placement count */ |
| 182 | 'trashed' => _n( '%s placement moved to the Trash.', '%s placements moved to the Trash.', $counts['trashed'], 'advanced-ads' ), |
| 183 | /* translators: %s: placement count */ |
| 184 | 'untrashed' => _n( '%s placement restored from the Trash.', '%s placements restored from the Trash.', $counts['untrashed'], 'advanced-ads' ), |
| 185 | ]; |
| 186 | |
| 187 | return $messages; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Replace the edit link with a link to the modal to edit the placement. |
| 192 | * |
| 193 | * @param string $link The previous link. |
| 194 | * @param int $post_id The \WP_Post::$ID for the current item. |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | public function get_edit_post_link( string $link, int $post_id ): string { |
| 199 | if ( get_post_type( $post_id ) === Constants::POST_TYPE_PLACEMENT ) { |
| 200 | $link = admin_url( 'edit.php?post_type=' . Constants::POST_TYPE_PLACEMENT . '#modal-placement-edit-' . $post_id ); |
| 201 | } |
| 202 | |
| 203 | return $link; |
| 204 | } |
| 205 | } |
| 206 |