wpadverts.php
187 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WPAdverts core integrations file |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\WPAdverts; |
| 10 | |
| 11 | use SureTriggers\Controllers\IntegrationsController; |
| 12 | use SureTriggers\Integrations\Integrations; |
| 13 | use SureTriggers\Traits\SingletonLoader; |
| 14 | |
| 15 | /** |
| 16 | * Class SureTrigger |
| 17 | * |
| 18 | * @package SureTriggers\Integrations\WPAdverts |
| 19 | */ |
| 20 | class WPAdverts extends Integrations { |
| 21 | |
| 22 | use SingletonLoader; |
| 23 | |
| 24 | /** |
| 25 | * ID |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $id = 'WPAdverts'; |
| 30 | |
| 31 | /** |
| 32 | * SureTrigger constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->name = __( 'WP Adverts', 'suretriggers' ); |
| 36 | $this->description = __( 'WPAdverts is the lightest plugin to create a classifieds or job board on your WordPress site.', 'suretriggers' ); |
| 37 | $this->icon_url = SURE_TRIGGERS_URL . 'assets/icons/wpadverts.svg'; |
| 38 | |
| 39 | parent::__construct(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Is Plugin depended plugin is installed or not. |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public function is_plugin_installed() { |
| 48 | return defined( 'ADVERTS_FILE' ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get advert context data. |
| 53 | * |
| 54 | * @param int $post_id Advert post ID. |
| 55 | * @return array |
| 56 | */ |
| 57 | public static function get_advert_context( $post_id ) { |
| 58 | $post = get_post( $post_id ); |
| 59 | |
| 60 | if ( ! $post instanceof \WP_Post ) { |
| 61 | return []; |
| 62 | } |
| 63 | |
| 64 | $context = [ |
| 65 | 'advert_id' => $post->ID, |
| 66 | 'advert_title' => $post->post_title, |
| 67 | 'advert_content' => $post->post_content, |
| 68 | 'advert_status' => $post->post_status, |
| 69 | 'advert_url' => get_permalink( $post->ID ), |
| 70 | 'advert_date' => $post->post_date, |
| 71 | 'advert_author' => $post->post_author, |
| 72 | ]; |
| 73 | |
| 74 | $person = get_post_meta( $post_id, 'adverts_person', true ); |
| 75 | if ( ! empty( $person ) ) { |
| 76 | $context['advert_contact_name'] = $person; |
| 77 | } |
| 78 | |
| 79 | $email = get_post_meta( $post_id, 'adverts_email', true ); |
| 80 | if ( ! empty( $email ) ) { |
| 81 | $context['advert_contact_email'] = $email; |
| 82 | } |
| 83 | |
| 84 | $phone = get_post_meta( $post_id, 'adverts_phone', true ); |
| 85 | if ( ! empty( $phone ) ) { |
| 86 | $context['advert_contact_phone'] = $phone; |
| 87 | } |
| 88 | |
| 89 | $price = get_post_meta( $post_id, 'adverts_price', true ); |
| 90 | if ( ! empty( $price ) ) { |
| 91 | $context['advert_price'] = $price; |
| 92 | } |
| 93 | |
| 94 | $location = get_post_meta( $post_id, 'adverts_location', true ); |
| 95 | if ( ! empty( $location ) ) { |
| 96 | $context['advert_location'] = $location; |
| 97 | } |
| 98 | |
| 99 | $featured_image = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $post->ID ), 'full' ); |
| 100 | if ( ! empty( $featured_image ) && is_array( $featured_image ) ) { |
| 101 | $context['advert_featured_image'] = $featured_image[0]; |
| 102 | } |
| 103 | |
| 104 | $categories = get_the_terms( $post->ID, 'advert_category' ); |
| 105 | if ( ! empty( $categories ) && is_array( $categories ) ) { |
| 106 | $cat_names = []; |
| 107 | $cat_ids = []; |
| 108 | foreach ( $categories as $category ) { |
| 109 | $cat_names[] = $category->name; |
| 110 | $cat_ids[] = $category->term_id; |
| 111 | } |
| 112 | $context['advert_categories'] = implode( ', ', $cat_names ); |
| 113 | $context['advert_category_ids'] = implode( ', ', $cat_ids ); |
| 114 | } |
| 115 | |
| 116 | $expiration = get_post_meta( $post_id, '_expiration_date', true ); |
| 117 | if ( ! empty( $expiration ) ) { |
| 118 | $context['advert_expiration_date'] = is_numeric( $expiration ) ? gmdate( 'Y-m-d H:i:s', absint( $expiration ) ) : ''; |
| 119 | } |
| 120 | |
| 121 | return $context; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get payment context data. |
| 126 | * |
| 127 | * @param \WP_Post $payment Payment post object. |
| 128 | * @return array |
| 129 | */ |
| 130 | public static function get_payment_context( $payment ) { |
| 131 | if ( ! $payment instanceof \WP_Post ) { |
| 132 | return []; |
| 133 | } |
| 134 | |
| 135 | $context = [ |
| 136 | 'payment_id' => $payment->ID, |
| 137 | 'payment_title' => $payment->post_title, |
| 138 | 'payment_status' => $payment->post_status, |
| 139 | 'payment_date' => $payment->post_date, |
| 140 | ]; |
| 141 | |
| 142 | $object_id = get_post_meta( $payment->ID, '_adverts_object_id', true ); |
| 143 | if ( ! empty( $object_id ) ) { |
| 144 | $context['payment_advert_id'] = $object_id; |
| 145 | } |
| 146 | |
| 147 | $paid = get_post_meta( $payment->ID, '_adverts_payment_paid', true ); |
| 148 | if ( '' !== $paid ) { |
| 149 | $context['payment_amount_paid'] = $paid; |
| 150 | } |
| 151 | |
| 152 | $total = get_post_meta( $payment->ID, '_adverts_payment_total', true ); |
| 153 | if ( '' !== $total ) { |
| 154 | $context['payment_amount_total'] = $total; |
| 155 | } |
| 156 | |
| 157 | $gateway = get_post_meta( $payment->ID, '_adverts_payment_gateway', true ); |
| 158 | if ( ! empty( $gateway ) ) { |
| 159 | $context['payment_gateway'] = $gateway; |
| 160 | } |
| 161 | |
| 162 | $payment_type = get_post_meta( $payment->ID, '_adverts_payment_type', true ); |
| 163 | if ( ! empty( $payment_type ) ) { |
| 164 | $context['payment_type'] = $payment_type; |
| 165 | } |
| 166 | |
| 167 | $buyer_name = get_post_meta( $payment->ID, 'adverts_person', true ); |
| 168 | if ( ! empty( $buyer_name ) ) { |
| 169 | $context['payment_buyer_name'] = $buyer_name; |
| 170 | } |
| 171 | |
| 172 | $buyer_email = get_post_meta( $payment->ID, 'adverts_email', true ); |
| 173 | if ( ! empty( $buyer_email ) ) { |
| 174 | $context['payment_buyer_email'] = $buyer_email; |
| 175 | } |
| 176 | |
| 177 | $user_id = get_post_meta( $payment->ID, '_adverts_user_id', true ); |
| 178 | if ( ! empty( $user_id ) ) { |
| 179 | $context['payment_user_id'] = $user_id; |
| 180 | } |
| 181 | |
| 182 | return $context; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | IntegrationsController::register( WPAdverts::class ); |
| 187 |