Nenhuma Descrição

DynamicSegment.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Entities\SegmentEntity;
  5. use MailPoet\Models\Segment as MailPoetSegment;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. /**
  8. * @deprecated This model is deprecated. Use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository and respective Doctrine entities instead.
  9. * This class can be removed after 2021-09-25
  10. */
  11. /**
  12. * @property int $id
  13. * @property string $name
  14. * @property string $description
  15. * @property string $createdAt
  16. * @property string $updatedAt
  17. * @property string|null $deletedAt
  18. */
  19. class DynamicSegment extends MailPoetSegment {
  20. const TYPE_DYNAMIC = SegmentEntity::TYPE_DYNAMIC;
  21. public function save() {
  22. self::deprecationError(__FUNCTION__);
  23. $this->set('type', DynamicSegment::TYPE_DYNAMIC);
  24. return parent::save();
  25. }
  26. public function dynamicSegmentFilters() {
  27. self::deprecationError(__FUNCTION__);
  28. return $this->has_many(__NAMESPACE__ . '\DynamicSegmentFilter', 'segment_id');
  29. }
  30. public static function findAll() {
  31. self::deprecationError(__FUNCTION__);
  32. $query = self::select('*');
  33. return $query->where('type', DynamicSegment::TYPE_DYNAMIC)
  34. ->whereNull('deleted_at')
  35. ->findMany();
  36. }
  37. public static function listingQuery(array $data = []) {
  38. self::deprecationError(__FUNCTION__);
  39. $query = self::select('*');
  40. $query->where('type', DynamicSegment::TYPE_DYNAMIC);
  41. if (isset($data['group'])) {
  42. $query->filter('groupBy', $data['group']);
  43. }
  44. if (isset($data['search'])) {
  45. $query->filter('search', $data['search']);
  46. }
  47. return $query;
  48. }
  49. public static function groups() {
  50. self::deprecationError(__FUNCTION__);
  51. return [
  52. [
  53. 'name' => 'all',
  54. 'label' => WPFunctions::get()->__('All', 'mailpoet'),
  55. 'count' => DynamicSegment::getPublished()->where('type', DynamicSegment::TYPE_DYNAMIC)->count(),
  56. ],
  57. [
  58. 'name' => 'trash',
  59. 'label' => WPFunctions::get()->__('Trash', 'mailpoet'),
  60. 'count' => parent::getTrashed()->where('type', DynamicSegment::TYPE_DYNAMIC)->count(),
  61. ],
  62. ];
  63. }
  64. public function delete() {
  65. self::deprecationError(__FUNCTION__);
  66. DynamicSegmentFilter::where('segment_id', $this->id)->deleteMany();
  67. return parent::delete();
  68. }
  69. public static function bulkTrash($orm) {
  70. self::deprecationError(__FUNCTION__);
  71. $count = parent::bulkAction($orm, function($ids) {
  72. $placeholders = join(',', array_fill(0, count($ids), '?'));
  73. DynamicSegment::rawExecute(join(' ', [
  74. 'UPDATE `' . DynamicSegment::$_table . '`',
  75. 'SET `deleted_at` = NOW()',
  76. 'WHERE `id` IN (' . $placeholders . ')',
  77. ]), $ids);
  78. });
  79. return ['count' => $count];
  80. }
  81. public static function bulkDelete($orm) {
  82. self::deprecationError(__FUNCTION__);
  83. $count = parent::bulkAction($orm, function($ids) {
  84. $placeholders = join(',', array_fill(0, count($ids), '?'));
  85. DynamicSegmentFilter::rawExecute(join(' ', [
  86. 'DELETE FROM `' . DynamicSegmentFilter::$_table . '`',
  87. 'WHERE `segment_id` IN (' . $placeholders . ')',
  88. ]), $ids);
  89. DynamicSegment::rawExecute(join(' ', [
  90. 'DELETE FROM `' . DynamicSegment::$_table . '`',
  91. 'WHERE `id` IN (' . $placeholders . ')',
  92. ]), $ids);
  93. });
  94. return ['count' => $count];
  95. }
  96. /**
  97. * @deprecated This is here for displaying the deprecation warning for properties.
  98. */
  99. public function __get($key) {
  100. self::deprecationError('property "' . $key . '"');
  101. return parent::__get($key);
  102. }
  103. /**
  104. * @deprecated This is here for displaying the deprecation warning for static calls.
  105. */
  106. public static function __callStatic($name, $arguments) {
  107. self::deprecationError($name);
  108. return parent::__callStatic($name, $arguments);
  109. }
  110. private static function deprecationError($methodName) {
  111. trigger_error('Calling ' . $methodName . ' is deprecated and will be removed. Use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository and respective Doctrine entities instead.', E_USER_DEPRECATED);
  112. }
  113. }