PluginProbe
Bulk Page Generator – LPagery / trunk
Bulk Page Generator – LPagery vtrunk
2.5.8 2.5.7 1.4.24 1.4.25 1.4.26 1.4.27 1.4.28 1.4.29 1.4.3 1.4.31 1.4.32 1.4.33 1.4.5 1.4.7 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.5 1.5.6 2.0.0 2.0.1 2.0.10 2.0.11 All 154 releases
lpagery / src / controller / PostController.php

PostController.php in Bulk Page Generator – LPagery trunk, at src/controller/PostController.php

159 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LPagery\controller;
4
5 use LPagery\data\LPageryDao;
6 use LPagery\data\SearchPostService;
7 use LPagery\io\Mapper;
8 use LPagery\wpml\WpmlHelper;
9
10 /**
11 * Controller for handling post-related operations
12 */
13 class PostController
14 {
15 private static $instance;
16 private SearchPostService $searchPostService;
17 private LPageryDao $lpageryDao;
18 private Mapper $mapper;
19
20 /**
21 * PostController constructor.
22 *
23 * @param SearchPostService $searchPostService
24 * @param LPageryDao $lpageryDao
25 * @param Mapper $mapper
26 */
27 public function __construct(SearchPostService $searchPostService, LPageryDao $lpageryDao, Mapper $mapper)
28 {
29 $this->searchPostService = $searchPostService;
30 $this->lpageryDao = $lpageryDao;
31 $this->mapper = $mapper;
32 }
33
34 /**
35 * Singleton pattern implementation
36 */
37 public static function get_instance(): self
38 {
39 if (null === self::$instance) {
40 self::$instance = new self(
41 SearchPostService::get_instance(),
42 LPageryDao::get_instance(),
43 Mapper::get_instance()
44 );
45 }
46 return self::$instance;
47 }
48
49 /**
50 * Gets posts based on search criteria
51 *
52 * @param string $search Search term
53 * @param array $custom_post_types Custom post types to include
54 * @param string $mode Mode of operation
55 * @param string $select Selection mode
56 * @param int|null $template_id Template ID
57 * @return array Array of mapped posts
58 */
59 public function getPosts(string $search, array $custom_post_types, string $mode, string $select, ?int $template_id = null): array
60 {
61 $posts = $this->searchPostService->lpagery_search_posts($search, $custom_post_types, $mode, $select, $template_id);
62 return array_map([$this->mapper, 'lpagery_map_post'], $posts);
63 }
64
65 /**
66 * Gets post type by post ID
67 *
68 * @param int $post_id Post ID
69 * @return string Post type
70 */
71 public function getPostType(int $post_id): string
72 {
73 $post = get_post($post_id);
74 return $post->post_type;
75 }
76
77 /**
78 * Gets single post by ID with extended information
79 *
80 * @param int $post_id Post ID
81 * @return array Post data
82 */
83 public function getPost(int $post_id): array
84 {
85 $WP_Post = get_post($post_id);
86 if (!$WP_Post) {
87 return ["found" => false];
88 }
89
90 $wpml_data = WpmlHelper::get_wpml_language_data($post_id);
91 $array = [
92 "title" => $WP_Post->post_title,
93 "found" => true,
94 "permalink" => get_permalink($post_id)
95 ];
96
97 if ($wpml_data->language_code) {
98 $array["language_code"] = $wpml_data->language_code;
99 }
100
101 $post_type_object = get_post_type_object($WP_Post->post_type);
102
103 if ($post_type_object->name) {
104 $singular = $post_type_object->labels->singular_name ?? ($WP_Post->post_type === 'post' ? 'Post' : ($WP_Post->post_type === 'page' ? 'Page' : ucfirst(str_replace(['_', '-'], ' ', $post_type_object->name))));
105
106 $plural = $post_type_object->labels->name ?? ($WP_Post->post_type === 'post' ? 'Posts' : ($WP_Post->post_type === 'page' ? 'Pages' : ucfirst(str_replace(['_', '-'], ' ', $post_type_object->name)) . 's'));
107
108 if($WP_Post->post_type === 'post' ) {
109 $singular = "Post";
110 $plural = "Posts";
111 }
112 if($WP_Post->post_type === 'page' ) {
113 $singular = "Page";
114 $plural = "Pages";
115 }
116 $post_type_array = [
117 "name" => $post_type_object->name,
118 "singular" => $singular,
119 "plural" => $plural,
120 "hierarchical" => $post_type_object && is_post_type_hierarchical($WP_Post->post_type)
121 ];
122
123 $array["type"] = $post_type_array;
124 }
125
126 return $array;
127 }
128
129 /**
130 * Gets template posts
131 *
132 * @return array Template posts
133 */
134 public function getTemplatePosts(): array
135 {
136 $template_posts = $this->lpageryDao->lpagery_get_template_posts();
137
138 if ($this->hasWpmlSupport()) {
139 foreach ($template_posts as &$post_array) {
140 $wpmlData = WpmlHelper::get_wpml_language_data($post_array->id);
141 if ($wpmlData->language_code) {
142 $post_array->language_code = $wpmlData->language_code;
143 }
144 }
145 }
146
147 return $template_posts;
148 }
149
150 /**
151 * Check if WPML support is available
152 *
153 * @return bool Whether WPML support is available
154 */
155 protected function hasWpmlSupport(): bool
156 {
157 return function_exists('wpml_get_language_information');
158 }
159 }