| Total Complexity | 110 |
| Total Lines | 1017 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WPInv_Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WPInv_Item extends GetPaid_Data { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Which data store to load. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $data_store_name = 'item'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * This is the name of this object type. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $object_type = 'item'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Item Data array. This is the core item data exposed in APIs. |
||
| 28 | * |
||
| 29 | * @since 1.0.19 |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $data = array( |
||
| 33 | 'parent_id' => 0, |
||
| 34 | 'status' => 'draft', |
||
| 35 | 'version' => '', |
||
| 36 | 'date_created' => null, |
||
| 37 | 'date_modified' => null, |
||
| 38 | 'name' => '', |
||
| 39 | 'description' => '', |
||
| 40 | 'author' => 1, |
||
| 41 | 'price' => 0, |
||
| 42 | 'vat_rule' => null, |
||
| 43 | 'vat_class' => null, |
||
| 44 | 'type' => 'custom', |
||
| 45 | 'custom_id' => null, |
||
| 46 | 'custom_name' => null, |
||
| 47 | 'custom_singular_name' => null, |
||
| 48 | 'is_editable' => 1, |
||
| 49 | 'is_dynamic_pricing' => null, |
||
| 50 | 'minimum_price' => null, |
||
| 51 | 'is_recurring' => null, |
||
| 52 | 'recurring_period' => null, |
||
| 53 | 'recurring_interval' => null, |
||
| 54 | 'recurring_limit' => null, |
||
| 55 | 'is_free_trial' => null, |
||
| 56 | 'trial_period' => null, |
||
| 57 | 'signup_fee' => null, |
||
| 58 | 'trial_interval' => null, |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Stores meta in cache for future reads. |
||
| 63 | * |
||
| 64 | * A group must be set to to enable caching. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $cache_group = 'getpaid_items'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Stores a reference to the original WP_Post object |
||
| 72 | * |
||
| 73 | * @var WP_Post |
||
| 74 | */ |
||
| 75 | protected $post = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the item if ID is passed, otherwise the item is new and empty. |
||
| 79 | * |
||
| 80 | * @param int|object|WPInv_Item|WP_Post $item Item to read. |
||
| 81 | */ |
||
| 82 | public function __construct( $item = 0 ) { |
||
| 83 | parent::__construct( $item ); |
||
| 84 | |||
| 85 | if ( is_numeric( $item ) && $item > 0 ) { |
||
| 86 | $this->set_id( $item ); |
||
| 87 | } elseif ( $item instanceof self ) { |
||
| 88 | $this->set_id( $item->get_id() ); |
||
| 89 | } elseif ( ! empty( $item->ID ) ) { |
||
| 90 | $this->set_id( $item->ID ); |
||
| 91 | } else { |
||
| 92 | $this->set_object_read( true ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Load the datastore. |
||
| 96 | $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
||
| 97 | |||
| 98 | if ( $this->get_id() > 0 ) { |
||
| 99 | $this->post = get_post( $this->get_id() ); |
||
| 100 | $this->ID = $this->get_id(); |
||
|
|
|||
| 101 | $this->data_store->read( $this ); |
||
| 102 | } |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | /* |
||
| 107 | |-------------------------------------------------------------------------- |
||
| 108 | | CRUD methods |
||
| 109 | |-------------------------------------------------------------------------- |
||
| 110 | | |
||
| 111 | | Methods which create, read, update and delete items from the database. |
||
| 112 | | |
||
| 113 | */ |
||
| 114 | |||
| 115 | /* |
||
| 116 | |-------------------------------------------------------------------------- |
||
| 117 | | Getters |
||
| 118 | |-------------------------------------------------------------------------- |
||
| 119 | */ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get parent item ID. |
||
| 123 | * |
||
| 124 | * @since 1.0.19 |
||
| 125 | * @param string $context View or edit context. |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | public function get_parent_id( $context = 'view' ) { |
||
| 129 | return (int) $this->get_prop( 'parent_id', $context ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get item status. |
||
| 134 | * |
||
| 135 | * @since 1.0.19 |
||
| 136 | * @param string $context View or edit context. |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function get_status( $context = 'view' ) { |
||
| 140 | return $this->get_prop( 'status', $context ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get plugin version when the item was created. |
||
| 145 | * |
||
| 146 | * @since 1.0.19 |
||
| 147 | * @param string $context View or edit context. |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function get_version( $context = 'view' ) { |
||
| 151 | return $this->get_prop( 'version', $context ); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get date when the item was created. |
||
| 156 | * |
||
| 157 | * @since 1.0.19 |
||
| 158 | * @param string $context View or edit context. |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function get_date_created( $context = 'view' ) { |
||
| 162 | return $this->get_prop( 'date_created', $context ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get GMT date when the item was created. |
||
| 167 | * |
||
| 168 | * @since 1.0.19 |
||
| 169 | * @param string $context View or edit context. |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function get_date_created_gmt( $context = 'view' ) { |
||
| 173 | $date = $this->get_date_created( $context ); |
||
| 174 | |||
| 175 | if ( $date ) { |
||
| 176 | $date = get_gmt_from_date( $date ); |
||
| 177 | } |
||
| 178 | return $date; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get date when the item was last modified. |
||
| 183 | * |
||
| 184 | * @since 1.0.19 |
||
| 185 | * @param string $context View or edit context. |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function get_date_modified( $context = 'view' ) { |
||
| 189 | return $this->get_prop( 'date_modified', $context ); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get GMT date when the item was last modified. |
||
| 194 | * |
||
| 195 | * @since 1.0.19 |
||
| 196 | * @param string $context View or edit context. |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function get_date_modified_gmt( $context = 'view' ) { |
||
| 200 | $date = $this->get_date_modified( $context ); |
||
| 201 | |||
| 202 | if ( $date ) { |
||
| 203 | $date = get_gmt_from_date( $date ); |
||
| 204 | } |
||
| 205 | return $date; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the item name. |
||
| 210 | * |
||
| 211 | * @since 1.0.19 |
||
| 212 | * @param string $context View or edit context. |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function get_name( $context = 'view' ) { |
||
| 216 | return $this->get_prop( 'name', $context ); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Alias of self::get_name(). |
||
| 221 | * |
||
| 222 | * @since 1.0.19 |
||
| 223 | * @param string $context View or edit context. |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function get_title( $context = 'view' ) { |
||
| 227 | return $this->get_name( $context ); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the item description. |
||
| 232 | * |
||
| 233 | * @since 1.0.19 |
||
| 234 | * @param string $context View or edit context. |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function get_description( $context = 'view' ) { |
||
| 238 | return $this->get_prop( 'description', $context ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Alias of self::get_description(). |
||
| 243 | * |
||
| 244 | * @since 1.0.19 |
||
| 245 | * @param string $context View or edit context. |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function get_excerpt( $context = 'view' ) { |
||
| 249 | return $this->get_description( $context ); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Alias of self::get_description(). |
||
| 254 | * |
||
| 255 | * @since 1.0.19 |
||
| 256 | * @param string $context View or edit context. |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function get_summary( $context = 'view' ) { |
||
| 260 | return $this->get_description( $context ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the owner of the item. |
||
| 265 | * |
||
| 266 | * @since 1.0.19 |
||
| 267 | * @param string $context View or edit context. |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function get_author( $context = 'view' ) { |
||
| 271 | return (int) $this->get_prop( 'author', $context ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the price of the item. |
||
| 276 | * |
||
| 277 | * @since 1.0.19 |
||
| 278 | * @param string $context View or edit context. |
||
| 279 | * @return float |
||
| 280 | */ |
||
| 281 | public function get_price( $context = 'view' ) { |
||
| 282 | return (float) wpinv_sanitize_amount( $this->get_prop( 'price', $context ) ); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns a formated price. |
||
| 287 | * |
||
| 288 | * @since 1.0.19 |
||
| 289 | * @param string $context View or edit context. |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function get_the_price() { |
||
| 293 | $item_price = wpinv_price( wpinv_format_amount( $this->get_price() ) ); |
||
| 294 | |||
| 295 | return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get the VAT rule of the item. |
||
| 300 | * |
||
| 301 | * @since 1.0.19 |
||
| 302 | * @param string $context View or edit context. |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function get_vat_rule( $context = 'view' ) { |
||
| 306 | return $this->get_prop( 'vat_rule', $context ); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the VAT class of the item. |
||
| 311 | * |
||
| 312 | * @since 1.0.19 |
||
| 313 | * @param string $context View or edit context. |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function get_vat_class( $context = 'view' ) { |
||
| 317 | return $this->get_prop( 'vat_class', $context ); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the type of the item. |
||
| 322 | * |
||
| 323 | * @since 1.0.19 |
||
| 324 | * @param string $context View or edit context. |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function get_type( $context = 'view' ) { |
||
| 328 | return $this->get_prop( 'type', $context ); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the custom id of the item. |
||
| 333 | * |
||
| 334 | * @since 1.0.19 |
||
| 335 | * @param string $context View or edit context. |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function get_custom_id( $context = 'view' ) { |
||
| 339 | return $this->get_prop( 'custom_id', $context ); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the custom name of the item. |
||
| 344 | * |
||
| 345 | * @since 1.0.19 |
||
| 346 | * @param string $context View or edit context. |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function get_custom_name( $context = 'view' ) { |
||
| 350 | return $this->get_prop( 'custom_name', $context ); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the custom singular name of the item. |
||
| 355 | * |
||
| 356 | * @since 1.0.19 |
||
| 357 | * @param string $context View or edit context. |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function get_custom_singular_name( $context = 'view' ) { |
||
| 361 | return $this->get_prop( 'custom_singular_name', $context ); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Checks if an item is editable.. |
||
| 366 | * |
||
| 367 | * @since 1.0.19 |
||
| 368 | * @param string $context View or edit context. |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public function get_is_editable( $context = 'view' ) { |
||
| 372 | return (int) $this->get_prop( 'is_editable', $context ); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Alias of self::get_is_editable(). |
||
| 377 | * |
||
| 378 | * @since 1.0.19 |
||
| 379 | * @param string $context View or edit context. |
||
| 380 | * @return int |
||
| 381 | */ |
||
| 382 | public function get_editable( $context = 'view' ) { |
||
| 383 | return $this->get_is_editable( $context ); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Checks if dynamic pricing is enabled. |
||
| 388 | * |
||
| 389 | * @since 1.0.19 |
||
| 390 | * @param string $context View or edit context. |
||
| 391 | * @return int |
||
| 392 | */ |
||
| 393 | public function get_is_dynamic_pricing( $context = 'view' ) { |
||
| 394 | return (int) $this->get_prop( 'is_dynamic_pricing', $context ); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the minimum price if dynamic pricing is enabled. |
||
| 399 | * |
||
| 400 | * @since 1.0.19 |
||
| 401 | * @param string $context View or edit context. |
||
| 402 | * @return float |
||
| 403 | */ |
||
| 404 | public function get_minimum_price( $context = 'view' ) { |
||
| 405 | return (float) wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) ); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Checks if this is a recurring item. |
||
| 410 | * |
||
| 411 | * @since 1.0.19 |
||
| 412 | * @param string $context View or edit context. |
||
| 413 | * @return int |
||
| 414 | */ |
||
| 415 | public function get_is_recurring( $context = 'view' ) { |
||
| 416 | return (int) $this->get_prop( 'is_recurring', $context ); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get the recurring period. |
||
| 421 | * |
||
| 422 | * @since 1.0.19 |
||
| 423 | * @param bool $full Return abbreviation or in full. |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function get_recurring_period( $full = false ) { |
||
| 427 | $period = $this->get_prop( 'recurring_period', 'view' ); |
||
| 428 | |||
| 429 | if ( $full && ! is_bool( $full ) ) { |
||
| 430 | $full = false; |
||
| 431 | } |
||
| 432 | |||
| 433 | return getpaid_sanitize_recurring_period( $period, $full ); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the recurring interval. |
||
| 438 | * |
||
| 439 | * @since 1.0.19 |
||
| 440 | * @param string $context View or edit context. |
||
| 441 | * @return int |
||
| 442 | */ |
||
| 443 | public function get_recurring_interval( $context = 'view' ) { |
||
| 444 | $interval = absint( $this->get_prop( 'recurring_interval', $context ) ); |
||
| 445 | |||
| 446 | if ( $interval < 1 ) { |
||
| 447 | $interval = 1; |
||
| 448 | } |
||
| 449 | |||
| 450 | return $interval; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the recurring limit. |
||
| 455 | * |
||
| 456 | * @since 1.0.19 |
||
| 457 | * @param string $context View or edit context. |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | public function get_recurring_limit( $context = 'view' ) { |
||
| 461 | return (int) $this->get_prop( 'recurring_limit', $context ); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Checks if we have a free trial. |
||
| 466 | * |
||
| 467 | * @since 1.0.19 |
||
| 468 | * @param string $context View or edit context. |
||
| 469 | * @return int |
||
| 470 | */ |
||
| 471 | public function get_is_free_trial( $context = 'view' ) { |
||
| 472 | return (int) $this->get_prop( 'is_free_trial', $context ); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Alias for self::get_is_free_trial(). |
||
| 477 | * |
||
| 478 | * @since 1.0.19 |
||
| 479 | * @param string $context View or edit context. |
||
| 480 | * @return int |
||
| 481 | */ |
||
| 482 | public function get_free_trial( $context = 'view' ) { |
||
| 483 | return $this->get_is_free_trial( $context ); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the trial period. |
||
| 488 | * |
||
| 489 | * @since 1.0.19 |
||
| 490 | * @param bool $full Return abbreviation or in full. |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function get_trial_period( $full = false ) { |
||
| 494 | $period = $this->get_prop( 'trial_period', 'view' ); |
||
| 495 | |||
| 496 | if ( $full && ! is_bool( $full ) ) { |
||
| 497 | $full = false; |
||
| 498 | } |
||
| 499 | |||
| 500 | return getpaid_sanitize_recurring_period( $period, $full ); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the trial interval. |
||
| 505 | * |
||
| 506 | * @since 1.0.19 |
||
| 507 | * @param string $context View or edit context. |
||
| 508 | * @return int |
||
| 509 | */ |
||
| 510 | public function get_trial_interval( $context = 'view' ) { |
||
| 511 | return (int) $this->get_prop( 'trial_interval', $context ); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get the sign up fee. |
||
| 516 | * |
||
| 517 | * @since 1.0.19 |
||
| 518 | * @param string $context View or edit context. |
||
| 519 | * @return float |
||
| 520 | */ |
||
| 521 | public function get_signup_fee( $context = 'view' ) { |
||
| 522 | return (float) $this->get_prop( 'signup_fee', $context ); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Margic method for retrieving a property. |
||
| 527 | */ |
||
| 528 | public function __get( $key ) { |
||
| 529 | |||
| 530 | // Check if we have a helper method for that. |
||
| 531 | if ( method_exists( $this, 'get_' . $key ) ) { |
||
| 532 | return call_user_func( array( $this, 'get_' . $key ) ); |
||
| 533 | } |
||
| 534 | |||
| 535 | // Check if the key is in the associated $post object. |
||
| 536 | if ( ! empty( $this->post ) && isset( $this->post->$key ) ) { |
||
| 537 | return $this->post->$key; |
||
| 538 | } |
||
| 539 | |||
| 540 | return $this->get_prop( $key ); |
||
| 541 | |||
| 542 | } |
||
| 543 | |||
| 544 | /* |
||
| 545 | |-------------------------------------------------------------------------- |
||
| 546 | | Setters |
||
| 547 | |-------------------------------------------------------------------------- |
||
| 548 | | |
||
| 549 | | Functions for setting order data. These should not update anything in the |
||
| 550 | | database itself and should only change what is stored in the class |
||
| 551 | | object. |
||
| 552 | */ |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Set parent order ID. |
||
| 556 | * |
||
| 557 | * @since 1.0.19 |
||
| 558 | */ |
||
| 559 | public function set_parent_id( $value ) { |
||
| 560 | if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) { |
||
| 561 | return; |
||
| 562 | } |
||
| 563 | $this->set_prop( 'parent_id', absint( $value ) ); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Sets item status. |
||
| 568 | * |
||
| 569 | * @since 1.0.19 |
||
| 570 | * @param string $status New status. |
||
| 571 | * @return array details of change. |
||
| 572 | */ |
||
| 573 | public function set_status( $status ) { |
||
| 574 | $old_status = $this->get_status(); |
||
| 575 | |||
| 576 | $this->set_prop( 'status', $status ); |
||
| 577 | |||
| 578 | return array( |
||
| 579 | 'from' => $old_status, |
||
| 580 | 'to' => $status, |
||
| 581 | ); |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Set plugin version when the item was created. |
||
| 586 | * |
||
| 587 | * @since 1.0.19 |
||
| 588 | */ |
||
| 589 | public function set_version( $value ) { |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Set date when the item was created. |
||
| 595 | * |
||
| 596 | * @since 1.0.19 |
||
| 597 | * @param string $value Value to set. |
||
| 598 | * @return bool Whether or not the date was set. |
||
| 599 | */ |
||
| 600 | public function set_date_created( $value ) { |
||
| 601 | $date = strtotime( $value ); |
||
| 602 | |||
| 603 | if ( $date ) { |
||
| 604 | $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
||
| 605 | return true; |
||
| 606 | } |
||
| 607 | |||
| 608 | return false; |
||
| 609 | |||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Set date when the item was last modified. |
||
| 614 | * |
||
| 615 | * @since 1.0.19 |
||
| 616 | * @param string $value Value to set. |
||
| 617 | * @return bool Whether or not the date was set. |
||
| 618 | */ |
||
| 619 | public function set_date_modified( $value ) { |
||
| 620 | $date = strtotime( $value ); |
||
| 621 | |||
| 622 | if ( $date ) { |
||
| 623 | $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
||
| 624 | return true; |
||
| 625 | } |
||
| 626 | |||
| 627 | return false; |
||
| 628 | |||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Set the item name. |
||
| 633 | * |
||
| 634 | * @since 1.0.19 |
||
| 635 | * @param string $value New name. |
||
| 636 | */ |
||
| 637 | public function set_name( $value ) { |
||
| 638 | $name = sanitize_text_field( $value ); |
||
| 639 | $this->set_prop( 'name', $name ); |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Alias of self::set_name(). |
||
| 644 | * |
||
| 645 | * @since 1.0.19 |
||
| 646 | * @param string $value New name. |
||
| 647 | */ |
||
| 648 | public function set_title( $value ) { |
||
| 649 | $this->set_name( $value ); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Set the item description. |
||
| 654 | * |
||
| 655 | * @since 1.0.19 |
||
| 656 | * @param string $value New description. |
||
| 657 | */ |
||
| 658 | public function set_description( $value ) { |
||
| 659 | $description = wp_kses_post( $value ); |
||
| 660 | return $this->set_prop( 'description', $description ); |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Alias of self::set_description(). |
||
| 665 | * |
||
| 666 | * @since 1.0.19 |
||
| 667 | * @param string $value New description. |
||
| 668 | */ |
||
| 669 | public function set_excerpt( $value ) { |
||
| 670 | $this->set_description( $value ); |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Alias of self::set_description(). |
||
| 675 | * |
||
| 676 | * @since 1.0.19 |
||
| 677 | * @param string $value New description. |
||
| 678 | */ |
||
| 679 | public function set_summary( $value ) { |
||
| 680 | $this->set_description( $value ); |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Set the owner of the item. |
||
| 685 | * |
||
| 686 | * @since 1.0.19 |
||
| 687 | * @param int $value New author. |
||
| 688 | */ |
||
| 689 | public function set_author( $value ) { |
||
| 690 | $this->set_prop( 'author', (int) $value ); |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Set the price of the item. |
||
| 695 | * |
||
| 696 | * @since 1.0.19 |
||
| 697 | * @param float $value New price. |
||
| 698 | ] */ |
||
| 699 | public function set_price( $value ) { |
||
| 700 | $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) ); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Set the VAT rule of the item. |
||
| 705 | * |
||
| 706 | * @since 1.0.19 |
||
| 707 | * @param string $value new rule. |
||
| 708 | */ |
||
| 709 | public function set_vat_rule( $value ) { |
||
| 710 | $this->set_prop( 'vat_rule', $value ); |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Set the VAT class of the item. |
||
| 715 | * |
||
| 716 | * @since 1.0.19 |
||
| 717 | * @param string $value new class. |
||
| 718 | */ |
||
| 719 | public function set_vat_class( $value ) { |
||
| 720 | $this->set_prop( 'vat_class', $value ); |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Set the type of the item. |
||
| 725 | * |
||
| 726 | * @since 1.0.19 |
||
| 727 | * @param string $value new item type. |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function set_type( $value ) { |
||
| 731 | |||
| 732 | if ( empty( $value ) ) { |
||
| 733 | $value = 'custom'; |
||
| 734 | } |
||
| 735 | |||
| 736 | $this->set_prop( 'type', $value ); |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Set the custom id of the item. |
||
| 741 | * |
||
| 742 | * @since 1.0.19 |
||
| 743 | * @param string $value new custom id. |
||
| 744 | */ |
||
| 745 | public function set_custom_id( $value ) { |
||
| 746 | $this->set_prop( 'custom_id', $value ); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Set the custom name of the item. |
||
| 751 | * |
||
| 752 | * @since 1.0.19 |
||
| 753 | * @param string $value new custom name. |
||
| 754 | */ |
||
| 755 | public function set_custom_name( $value ) { |
||
| 756 | $this->set_prop( 'custom_name', $value ); |
||
| 757 | } |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Set the custom singular name of the item. |
||
| 761 | * |
||
| 762 | * @since 1.0.19 |
||
| 763 | * @param string $value new custom singular name. |
||
| 764 | */ |
||
| 765 | public function set_custom_singular_name( $value ) { |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Sets if an item is editable.. |
||
| 771 | * |
||
| 772 | * @since 1.0.19 |
||
| 773 | * @param int|bool $value whether or not the item is editable. |
||
| 774 | */ |
||
| 775 | public function set_is_editable( $value ) { |
||
| 776 | if ( is_numeric( $value ) ) { |
||
| 777 | $this->set_prop( 'is_editable', (int) $value ); |
||
| 778 | } |
||
| 779 | } |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Sets if dynamic pricing is enabled. |
||
| 783 | * |
||
| 784 | * @since 1.0.19 |
||
| 785 | * @param int|bool $value whether or not dynamic pricing is allowed. |
||
| 786 | */ |
||
| 787 | public function set_is_dynamic_pricing( $value ) { |
||
| 788 | $this->get_prop( 'is_dynamic_pricing', (int) $value ); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Sets the minimum price if dynamic pricing is enabled. |
||
| 793 | * |
||
| 794 | * @since 1.0.19 |
||
| 795 | * @param float $value minimum price. |
||
| 796 | */ |
||
| 797 | public function set_minimum_price( $value ) { |
||
| 798 | $this->set_prop( 'minimum_price', (float) wpinv_sanitize_amount( $value ) ); |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Sets if this is a recurring item. |
||
| 803 | * |
||
| 804 | * @since 1.0.19 |
||
| 805 | * @param int|bool $value whether or not dynamic pricing is allowed. |
||
| 806 | */ |
||
| 807 | public function set_is_recurring( $value ) { |
||
| 808 | $this->set_prop( 'is_recurring', (int) $value ); |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Set the recurring period. |
||
| 813 | * |
||
| 814 | * @since 1.0.19 |
||
| 815 | * @param string $value new period. |
||
| 816 | */ |
||
| 817 | public function set_recurring_period( $value ) { |
||
| 818 | $this->set_prop( 'recurring_period', $value ); |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Set the recurring interval. |
||
| 823 | * |
||
| 824 | * @since 1.0.19 |
||
| 825 | * @param int $value recurring interval. |
||
| 826 | */ |
||
| 827 | public function set_recurring_interval( $value ) { |
||
| 828 | return $this->set_prop( 'recurring_interval', (int) $value ); |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Get the recurring limit. |
||
| 833 | * @since 1.0.19 |
||
| 834 | * @param int $value The recurring limit. |
||
| 835 | * @return int |
||
| 836 | */ |
||
| 837 | public function set_recurring_limit( $value ) { |
||
| 838 | $this->get_prop( 'recurring_limit', (int) $value ); |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Checks if we have a free trial. |
||
| 843 | * |
||
| 844 | * @since 1.0.19 |
||
| 845 | * @param bool $value whether or not it has a free trial. |
||
| 846 | */ |
||
| 847 | public function set_is_free_trial( $value ) { |
||
| 848 | $this->set_prop( 'is_free_trial', (int) $value ); |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Set the trial period. |
||
| 853 | * |
||
| 854 | * @since 1.0.19 |
||
| 855 | * @param string $value trial period. |
||
| 856 | */ |
||
| 857 | public function set_trial_period( $value ) { |
||
| 858 | $this->set_prop( 'trial_period', $value ); |
||
| 859 | } |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Set the trial interval. |
||
| 863 | * |
||
| 864 | * @since 1.0.19 |
||
| 865 | * @param int $value trial interval. |
||
| 866 | */ |
||
| 867 | public function Set_trial_interval( $value ) { |
||
| 868 | $this->set_prop( 'trial_interval', $value ); |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Set the sign up fee. |
||
| 873 | * |
||
| 874 | * @since 1.0.19 |
||
| 875 | * @param float $value The signup fee. |
||
| 876 | */ |
||
| 877 | public function set_signup_fee( $value ) { |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Create an item. For backwards compatibilty. |
||
| 883 | * |
||
| 884 | * @deprecated |
||
| 885 | */ |
||
| 886 | public function create( $data = array() ) { |
||
| 887 | |||
| 888 | // Set the properties. |
||
| 889 | if ( is_array( $data ) ) { |
||
| 890 | $this->set_props( $data ); |
||
| 891 | } |
||
| 892 | |||
| 893 | // Save the item. |
||
| 894 | $this->save(); |
||
| 895 | |||
| 896 | return true; |
||
| 897 | } |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Updates an item. For backwards compatibilty. |
||
| 901 | * |
||
| 902 | * @deprecated |
||
| 903 | */ |
||
| 904 | public function update( $data = array() ) { |
||
| 905 | $this->create( $data ); |
||
| 906 | } |
||
| 907 | |||
| 908 | /* |
||
| 909 | |-------------------------------------------------------------------------- |
||
| 910 | | Conditionals |
||
| 911 | |-------------------------------------------------------------------------- |
||
| 912 | | |
||
| 913 | | Checks if a condition is true or false. |
||
| 914 | | |
||
| 915 | */ |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Checks whether the item has enabled dynamic pricing. |
||
| 919 | * |
||
| 920 | * @since 1.0.19 |
||
| 921 | * @return bool |
||
| 922 | */ |
||
| 923 | public function user_can_set_their_price() { |
||
| 924 | return (bool) $this->get_is_dynamic_pricing(); |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Checks whether the item is recurring. |
||
| 929 | * |
||
| 930 | * @since 1.0.19 |
||
| 931 | * @return bool |
||
| 932 | */ |
||
| 933 | public function is_recurring() { |
||
| 935 | } |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Checks whether the item has a free trial. |
||
| 939 | * |
||
| 940 | * @since 1.0.19 |
||
| 941 | * @return bool |
||
| 942 | */ |
||
| 943 | public function has_free_trial() { |
||
| 944 | $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false; |
||
| 945 | return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this ); |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Checks whether the item has a sign up fee. |
||
| 950 | * |
||
| 951 | * @since 1.0.19 |
||
| 952 | * @return bool |
||
| 953 | */ |
||
| 954 | public function has_signup_fee() { |
||
| 955 | $has_signup_fee = $this->is_recurring() && $this->get_signup_fee() > 0 ? true : false; |
||
| 956 | return (bool) apply_filters( 'wpinv_item_has_signup_fee', $has_signup_fee, $this->ID, $this ); |
||
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Checks whether the item is free. |
||
| 961 | * |
||
| 962 | * @since 1.0.19 |
||
| 963 | * @return bool |
||
| 964 | */ |
||
| 965 | public function is_free() { |
||
| 966 | $is_free = $this->get_price() == 0; |
||
| 967 | return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this ); |
||
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Checks the item status against a passed in status. |
||
| 972 | * |
||
| 973 | * @param array|string $status Status to check. |
||
| 974 | * @return bool |
||
| 975 | */ |
||
| 976 | public function has_status( $status ) { |
||
| 977 | $has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status; |
||
| 978 | return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status ); |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Checks the item type against a passed in types. |
||
| 983 | * |
||
| 984 | * @param array|string $type Type to check. |
||
| 985 | * @return bool |
||
| 986 | */ |
||
| 987 | public function is_type( $type ) { |
||
| 988 | $is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type; |
||
| 989 | return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type ); |
||
| 990 | } |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Checks whether the item is editable. |
||
| 994 | * |
||
| 995 | * @since 1.0.19 |
||
| 996 | * @return bool |
||
| 997 | */ |
||
| 998 | public function is_editable() { |
||
| 999 | $is_editable = $this->get_is_editable(); |
||
| 1000 | return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this ); |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Checks whether the item is purchasable. |
||
| 1005 | * |
||
| 1006 | * @since 1.0.19 |
||
| 1007 | * @return bool |
||
| 1008 | */ |
||
| 1009 | public function can_purchase() { |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Checks whether the item supports dynamic pricing. |
||
| 1021 | * |
||
| 1022 | * @since 1.0.19 |
||
| 1023 | * @return bool |
||
| 1024 | */ |
||
| 1025 | public function supports_dynamic_pricing() { |
||
| 1026 | return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this ); |
||
| 1027 | } |
||
| 1028 | } |
||
| 1029 |