Нет описания

AutomatedLatestContent.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace MailPoet\API\JSON\v1;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\API\JSON\Endpoint as APIEndpoint;
  5. use MailPoet\Config\AccessControl;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. use MailPoet\WP\Posts as WPPosts;
  8. class AutomatedLatestContent extends APIEndpoint {
  9. /** @var \MailPoet\Newsletter\AutomatedLatestContent */
  10. public $ALC;
  11. private $wp;
  12. public $permissions = [
  13. 'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
  14. ];
  15. public function __construct(
  16. \MailPoet\Newsletter\AutomatedLatestContent $alc,
  17. WPFunctions $wp
  18. ) {
  19. $this->ALC = $alc;
  20. $this->wp = $wp;
  21. }
  22. public function getPostTypes() {
  23. $postTypes = array_map(function($postType) {
  24. return [
  25. 'name' => $postType->name,
  26. 'label' => $postType->label,
  27. ];
  28. }, WPPosts::getTypes([], 'objects'));
  29. return $this->successResponse(
  30. array_filter($postTypes)
  31. );
  32. }
  33. public function getTaxonomies($data = []) {
  34. $postType = (isset($data['postType'])) ? $data['postType'] : 'post';
  35. $allTaxonomies = WPFunctions::get()->getObjectTaxonomies($postType, 'objects');
  36. $taxonomiesWithLabel = array_filter($allTaxonomies, function($taxonomy) {
  37. return $taxonomy->label;
  38. });
  39. return $this->successResponse($taxonomiesWithLabel);
  40. }
  41. public function getTerms($data = []) {
  42. $taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : [];
  43. $search = (isset($data['search'])) ? $data['search'] : '';
  44. $limit = (isset($data['limit'])) ? (int)$data['limit'] : 100;
  45. $page = (isset($data['page'])) ? (int)$data['page'] : 1;
  46. $args = [
  47. 'taxonomy' => $taxonomies,
  48. 'hide_empty' => false,
  49. 'search' => $search,
  50. 'number' => $limit,
  51. 'offset' => $limit * ($page - 1),
  52. 'orderby' => 'name',
  53. 'order' => 'ASC',
  54. ];
  55. $args = $this->wp->applyFilters('mailpoet_search_terms_args', $args);
  56. $terms = WPPosts::getTerms($args);
  57. return $this->successResponse(array_values($terms));
  58. }
  59. public function getPosts($data = []) {
  60. return $this->successResponse(
  61. $this->ALC->getPosts($data)
  62. );
  63. }
  64. public function getTransformedPosts($data = []) {
  65. $posts = $this->ALC->getPosts($data);
  66. return $this->successResponse(
  67. $this->ALC->transformPosts($data, $posts)
  68. );
  69. }
  70. public function getBulkTransformedPosts($data = []) {
  71. $usedPosts = [];
  72. $renderedPosts = [];
  73. foreach ($data['blocks'] as $block) {
  74. $posts = $this->ALC->getPosts($block, $usedPosts);
  75. $renderedPosts[] = $this->ALC->transformPosts($block, $posts);
  76. foreach ($posts as $post) {
  77. $usedPosts[] = $post->ID;
  78. }
  79. }
  80. return $this->successResponse($renderedPosts);
  81. }
  82. }