| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class is responsible for all campaign related database operation. |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Disco\App; |
| 10 |
|
| 11 |
use Disco\App\Utility\Config; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Campaign |
| 15 |
* |
| 16 |
* @package Disco |
| 17 |
* @subpackage App |
| 18 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 19 |
* @link https://webappick.com |
| 20 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 21 |
* @category Campaign |
| 22 |
*/ |
| 23 |
class Campaign { |
| 24 |
|
| 25 |
/** |
| 26 |
* Object cache group for campaign rows. |
| 27 |
*/ |
| 28 |
public const CACHE_GROUP = 'disco_campaigns'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Object cache key holding every campaign row. |
| 32 |
*/ |
| 33 |
public const ROWS_CACHE_KEY = 'disco_campaign_rows'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get campaigns from database. |
| 37 |
* |
| 38 |
* @param string $status Campaign status. |
| 39 |
* @param string $intent Campaign intent. |
| 40 |
* @return array |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public function get_campaigns( $status = null, $intent = null ) {//phpcs:ignore |
| 44 |
$campaigns = $this->get_rows(); |
| 45 |
|
| 46 |
if ( ! is_array( $campaigns ) ) { |
| 47 |
return array(); |
| 48 |
} |
| 49 |
|
| 50 |
$filtered_campaigns = array_filter( |
| 51 |
$campaigns, |
| 52 |
static function ( $campaign ) use ( $status, $intent ) { |
| 53 |
if ( ! isset( $campaign->status ) ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! isset( $campaign->intent ) ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! is_object( $campaign ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! is_null( $status ) && $status !== $campaign->status ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if ( is_array( $intent ) ) { |
| 70 |
return in_array( $campaign->intent, $intent, true ); |
| 71 |
} |
| 72 |
|
| 73 |
return is_null( $intent ) || $intent === $campaign->intent; |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
$results = array_map( |
| 78 |
static function ( $campaign ) { |
| 79 |
if ( isset( $campaign->data, $campaign->id ) ) { |
| 80 |
$data = json_decode( $campaign->data, true ); |
| 81 |
$data = (array) $data; |
| 82 |
$data['id'] = $campaign->id; |
| 83 |
|
| 84 |
return new Config( $data ); |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
}, |
| 89 |
$filtered_campaigns |
| 90 |
); |
| 91 |
|
| 92 |
$results = array_combine( array_column( $results, 'id' ), $results ); |
| 93 |
|
| 94 |
if ( is_array( $results ) ) { |
| 95 |
return $results; |
| 96 |
} |
| 97 |
|
| 98 |
return array(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get a campaign by id. |
| 103 |
* |
| 104 |
* @param int|mixed $id Campaign id. |
| 105 |
* @return \Disco\App\Utility\Config|\WP_Error |
| 106 |
* @since 1.0.0 |
| 107 |
*/ |
| 108 |
public function get_campaign( $id ) { |
| 109 |
$campaigns = $this->get_rows(); |
| 110 |
$campaigns = (array) $campaigns; |
| 111 |
$id = absint( $id ); |
| 112 |
|
| 113 |
if ( isset( $campaigns[ $id ] ) ) { |
| 114 |
$campaign = $campaigns[ $id ]; |
| 115 |
} else { |
| 116 |
// Get Campaign from cache or fetch from DB. |
| 117 |
$campaign = $this->get_row( $id ); |
| 118 |
} |
| 119 |
|
| 120 |
// Return the campaign from cache. |
| 121 |
if ( $campaign instanceof Config ) { |
| 122 |
return $campaign; |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! $campaign ) { |
| 126 |
return new \WP_Error( |
| 127 |
'rest_not_found', |
| 128 |
__( 'Sorry, Invalid campaign id.', 'disco' ), |
| 129 |
array( 'status' => 400 ) |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
if ( isset( $campaign->data ) ) { |
| 134 |
$campaign = json_decode( $campaign->data, true ); |
| 135 |
$campaign = (array) $campaign; |
| 136 |
$campaign['id'] = $id; |
| 137 |
|
| 138 |
return new Config( $campaign ); |
| 139 |
} |
| 140 |
|
| 141 |
return new \WP_Error( |
| 142 |
'rest_not_found', |
| 143 |
__( 'Sorry, Invalid campaign data.', 'disco' ), |
| 144 |
array( 'status' => 400 ) |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Save campaign into database. |
| 150 |
* |
| 151 |
* @param array $config Campaign config. |
| 152 |
* @return \WP_Error|\Disco\App\Utility\Config |
| 153 |
* @since 1.0.0 |
| 154 |
*/ |
| 155 |
public function save_campaign( $config ) {//phpcs:ignore |
| 156 |
if ( is_user_logged_in() ) { |
| 157 |
$config['created_by'] = get_current_user_id(); |
| 158 |
$config['modified_by'] = get_current_user_id(); |
| 159 |
} |
| 160 |
|
| 161 |
if ( empty( $config['status'] ) ) { |
| 162 |
$config['status'] = '0'; |
| 163 |
} |
| 164 |
|
| 165 |
$config['created_date'] = gmdate( 'Y-m-d H:i:s' ); |
| 166 |
$config['modified_date'] = gmdate( 'Y-m-d H:i:s' ); |
| 167 |
// ==================================. |
| 168 |
$campaign_id = $this->insert( $config ); |
| 169 |
// ==================================. |
| 170 |
// Get Campaign from cache or fetch from DB. |
| 171 |
$campaign = $this->get_row( $campaign_id ); |
| 172 |
|
| 173 |
if ( ! $campaign ) { |
| 174 |
return new \WP_Error( |
| 175 |
'rest_not_added', |
| 176 |
__( 'Sorry, Failed to Save Campaign.', 'disco' ), |
| 177 |
array( 'status' => 400 ) |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
$data = array(); |
| 182 |
|
| 183 |
if ( isset( $campaign->data, $campaign->id ) ) { |
| 184 |
$data = (array) json_decode( $campaign->data, true ); |
| 185 |
$data['id'] = absint( $campaign->id ); |
| 186 |
} |
| 187 |
|
| 188 |
return new Config( $data ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Update campaign into database. |
| 193 |
* |
| 194 |
* @param int $id Campaign id. |
| 195 |
* @param array $config Campaign config. |
| 196 |
* @return \Disco\App\Utility\Config|\WP_Error |
| 197 |
* @throws \Exception If the campaign is not found. |
| 198 |
* @since 1.0.0 |
| 199 |
*/ |
| 200 |
public function update_campaign( $id, $config ) { |
| 201 |
$id = absint( $id ); |
| 202 |
|
| 203 |
if ( is_user_logged_in() ) { |
| 204 |
$config['modified_by'] = get_current_user_id(); |
| 205 |
} |
| 206 |
|
| 207 |
$config['modified_date'] = gmdate( 'Y-m-d H:i:s' ); |
| 208 |
// Get Campaign from cache or fetch from DB. |
| 209 |
$campaign = $this->get_row( $id ); |
| 210 |
|
| 211 |
if ( ! $campaign ) { |
| 212 |
return new \WP_Error( |
| 213 |
'rest_not_found', |
| 214 |
__( 'Sorry, Invalid campaign id.', 'disco' ), |
| 215 |
array( 'status' => 400 ) |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
if ( ! isset( $config['status'] ) && ! empty( $campaign->status ) ) { |
| 220 |
$config['status'] = $campaign->status; |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! isset( $config['priority'] ) && ! empty( $campaign->priority ) ) { |
| 224 |
$config['priority'] = $campaign->priority; |
| 225 |
} |
| 226 |
|
| 227 |
// Update campaign into database. |
| 228 |
$update = $this->update( $config, $id ); |
| 229 |
|
| 230 |
if ( ! $update ) { |
| 231 |
return new \WP_Error( |
| 232 |
'rest_not_added', |
| 233 |
__( 'Sorry, the campaign could not be updated.', 'disco' ), |
| 234 |
array( 'status' => 400 ) |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
$data = array(); |
| 239 |
|
| 240 |
if ( isset( $campaign->data ) && isset( $campaign->id ) ) { |
| 241 |
$data = (array) json_decode( $campaign->data, true ); |
| 242 |
$data['id'] = absint( $campaign->id ); |
| 243 |
} |
| 244 |
|
| 245 |
return new Config( $data ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Delete campaign from database. |
| 250 |
* |
| 251 |
* @param int $id Campaign id. |
| 252 |
* @return int|bool |
| 253 |
* @since 1.0.0 |
| 254 |
*/ |
| 255 |
public function delete_campaign( $id ) { |
| 256 |
return $this->delete( $id ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Insert campaign into database. |
| 261 |
* |
| 262 |
* @param array $config Campaign config. |
| 263 |
* @return int |
| 264 |
* @since 1.0.0 |
| 265 |
*/ |
| 266 |
public function insert( $config ) { |
| 267 |
global $wpdb; |
| 268 |
|
| 269 |
$insert = $wpdb->insert( //phpcs:ignore |
| 270 |
"{$wpdb->prefix}disco_campaigns", |
| 271 |
array( |
| 272 |
'intent' => $config['discount_intent'], |
| 273 |
'status' => $config['status'], |
| 274 |
'data' => wp_json_encode( $config ), |
| 275 |
), |
| 276 |
array( |
| 277 |
'%s', |
| 278 |
'%s', |
| 279 |
'%s', |
| 280 |
) |
| 281 |
); |
| 282 |
|
| 283 |
if ( $insert ) { |
| 284 |
self::flush_rows_cache(); |
| 285 |
|
| 286 |
return $wpdb->insert_id; |
| 287 |
} |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Update campaign into database. |
| 294 |
* |
| 295 |
* @param array $config Campaign config. |
| 296 |
* @param int $id Campaign id. |
| 297 |
* @return bool|int |
| 298 |
* @since 1.0.0 |
| 299 |
*/ |
| 300 |
public function update( $config, $id ) { |
| 301 |
global $wpdb; |
| 302 |
|
| 303 |
$table_name = $wpdb->prefix . 'disco_campaigns'; |
| 304 |
|
| 305 |
$updated = $wpdb->update( //phpcs:ignore |
| 306 |
$table_name, |
| 307 |
array( |
| 308 |
'intent' => $config['discount_intent'], |
| 309 |
'status' => $config['status'], |
| 310 |
'priority' => $config['priority'], |
| 311 |
'data' => wp_json_encode( $config ), |
| 312 |
), |
| 313 |
array( 'id' => $id ), |
| 314 |
array( |
| 315 |
'%s', |
| 316 |
'%s', |
| 317 |
'%s', |
| 318 |
'%s', |
| 319 |
), |
| 320 |
array( '%d' ) |
| 321 |
); |
| 322 |
|
| 323 |
self::flush_rows_cache(); |
| 324 |
|
| 325 |
return $updated; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Delete campaign from database. |
| 330 |
* |
| 331 |
* @param int $id Campaign id. |
| 332 |
* @return bool|int |
| 333 |
* @since 1.0.0 |
| 334 |
*/ |
| 335 |
public function delete( $id ) { |
| 336 |
global $wpdb; |
| 337 |
|
| 338 |
$table_name = $wpdb->prefix . 'disco_campaigns'; |
| 339 |
|
| 340 |
$deleted = $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) );//phpcs:ignore |
| 341 |
|
| 342 |
self::flush_rows_cache(); |
| 343 |
\Disco\App\Features\UserLimit::flush_cache( $id ); |
| 344 |
|
| 345 |
return $deleted; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Get all rows from database. |
| 350 |
* |
| 351 |
* @noinspection SqlResolve |
| 352 |
* @return array|null |
| 353 |
* @since 1.0.0 |
| 354 |
*/ |
| 355 |
public function get_rows() { |
| 356 |
global $wpdb; |
| 357 |
|
| 358 |
$cached = wp_cache_get( self::ROWS_CACHE_KEY, self::CACHE_GROUP ); |
| 359 |
|
| 360 |
if ( is_array( $cached ) ) { |
| 361 |
return $cached; |
| 362 |
} |
| 363 |
|
| 364 |
// Get all rows |
| 365 |
$get_campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}disco_campaigns", OBJECT ); //phpcs:ignore |
| 366 |
$campaigns = array(); |
| 367 |
|
| 368 |
foreach ( $get_campaigns as $result ) { |
| 369 |
$campaigns[ $result->id ] = $result; |
| 370 |
} |
| 371 |
|
| 372 |
wp_cache_set( self::ROWS_CACHE_KEY, $campaigns, self::CACHE_GROUP, MINUTE_IN_SECONDS * 5 ); |
| 373 |
|
| 374 |
return $campaigns; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Single Row Query by id. |
| 379 |
* First check if the row is available in cache. |
| 380 |
* If not, then fetch from the database. |
| 381 |
* |
| 382 |
* @noinspection SqlResolve |
| 383 |
* @param int $id Campaign id. |
| 384 |
* @return object|null |
| 385 |
* @since 1.0.0 |
| 386 |
*/ |
| 387 |
public function get_row( $id ) { |
| 388 |
global $wpdb; |
| 389 |
$query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}disco_campaigns WHERE id = %d", $id ); //phpcs:ignore |
| 390 |
|
| 391 |
return $wpdb->get_row( $query, OBJECT );//phpcs:ignore |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Invalidate the cached campaign rows. |
| 396 |
* |
| 397 |
* Called from every write path so the cache can never serve stale campaigns. |
| 398 |
* |
| 399 |
* @return void |
| 400 |
* @since 1.4.5 |
| 401 |
*/ |
| 402 |
public static function flush_rows_cache() { |
| 403 |
wp_cache_delete( self::ROWS_CACHE_KEY, self::CACHE_GROUP ); |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|