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