PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.12
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.12
6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / ContentManager.php
kirki / includes Last commit date
API 2 weeks ago Admin 1 month ago Ajax 1 week ago ExportImport 2 weeks ago FormValidator 2 months ago Frontend 1 week ago Manager 2 weeks ago API.php 1 month ago Admin.php 2 months ago Ajax.php 1 week ago Apps.php 1 month ago ContentManager.php 2 months ago DbQueryUtils.php 1 month ago ElementVisibilityConditions.php 2 months ago Frontend.php 2 months ago HelperFunctions.php 1 week ago KirkiBase.php 2 months ago PostsQueryUtils.php 2 months ago Staging.php 2 months ago View.php 2 weeks ago
ContentManager.php
238 lines
1 <?php
2 /**
3 * This class act like controller for Editor, Iframe, Frontend
4 *
5 * @package kirki
6 */
7
8 namespace Kirki;
9
10 use Kirki\API\ContentManager\ContentManagerHelper;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Frontend handler class
18 */
19 class ContentManager {
20
21 /**
22 * Initialize the class
23 *
24 * @return void
25 */
26 public function __construct() {
27 add_action( 'parse_request', array( $this, 'custom_parse_request' ) );
28 add_action( 'init', array( $this, 'register_parent_post_type' ) );
29 add_filter( 'kirki_collection_kirki_cm_multi_reference', array( $this, 'get_referenced_collection' ), 10, 2 );
30 add_filter( 'kirki_collection_kirki_cm_gallery', array( $this, 'get_gallery_collection' ), 10, 2 );
31 add_filter( 'kirki_dynamic_content', array( $this, 'kirki_dynamic_content' ), 10, 2 );
32 }
33
34 public function kirki_dynamic_content( $value, $args ) {
35
36 $dynamic_content = isset( $args['dynamicContent'] ) ? $args['dynamicContent'] : array();
37
38 if ( isset( $dynamic_content['type'] ) && $dynamic_content['type'] === 'gallery' ) {
39 $content = array();
40 $collection_type = array();
41
42 if ( isset( $args['collectionItem'] ) ) {
43 $collection_type = isset( $args['collectionItem'] ) ? $args['collectionItem'] : array();
44 } else {
45 $collection_type = isset( $args['post'] ) ? $args['post'] : array();
46 }
47
48 $src = isset( $collection_type['url'] ) ? $collection_type['url'] : '';
49 $attachment_id = isset( $collection_type['id'] ) ? $collection_type['id'] : '';
50 if ( ! $src && $attachment_id ) {
51 $src = wp_get_attachment_image_url( $attachment_id, 'full' );
52 }
53
54 $content = array(
55 'wp_attachment_id' => $attachment_id,
56 'src' => $src,
57 );
58
59 return $content;
60 }
61
62 if ( isset( $dynamic_content['type'] ) && $dynamic_content['type'] === 'reference' ) {
63
64 global $wpdb;
65
66 $collection_item_id = isset( $args['collectionItem']['ID'] ) ? $args['collectionItem']['ID'] : '';
67
68 // if collection item id is not set then get it from templateEditContext
69 if ( empty( $collection_item_id ) ) {
70 $collection_item_id = isset( $args['templateEditContext']['id'] ) ? $args['templateEditContext']['id'] : '';
71 }
72
73 $ref_field_id = isset( $dynamic_content['ref_field_id'] ) ? $dynamic_content['ref_field_id'] : '';
74 $value = isset( $dynamic_content['value'] ) ? $dynamic_content['value'] : '';
75 $cm_post_id = isset( $dynamic_content['cm_post_id'] ) ? $dynamic_content['cm_post_id'] : '';
76
77 $meta_key = ContentManagerHelper::get_child_post_meta_key_using_field_id( $cm_post_id, $ref_field_id );
78
79
80
81 $ref_field_id_value = $wpdb->get_results( $wpdb->prepare( "SELECT ref_post_id FROM {$wpdb->prefix}kirki_cm_reference WHERE post_id=%d AND field_meta_key=%s", $collection_item_id, $meta_key ), ARRAY_A ); // get one refector
82
83 $cm_ref_field_post = null;
84
85 if ( count( $ref_field_id_value ) > 0 ) {
86 $ref_field_id_value = $ref_field_id_value[0]['ref_post_id'];
87 $cm_ref_field_post = get_post( $ref_field_id_value );
88 } else {
89 return '';
90 }
91
92 $content = HelperFunctions::get_post_dynamic_content( $value, $cm_ref_field_post, '', $dynamic_content );
93
94 return $content;
95 }
96
97 return $value;
98 }
99
100 public function get_referenced_collection( $value, $args ) {
101 global $wpdb;
102
103 $name = $args['name'];
104 $post = get_post( $args['post_parent'] );
105 $post_parent_post = get_post( $post->post_parent );
106
107 // if parent post is not set then return empty array
108 if ( ! $post_parent_post || ! isset( $post_parent_post->ID ) ) {
109 return array();
110 }
111
112 $cm_ref_field_meta_key = ContentManagerHelper::get_child_post_meta_key_using_field_id( $post_parent_post->ID, $name );
113
114 $results = $wpdb->get_results( $wpdb->prepare( "SELECT ref_post_id FROM {$wpdb->prefix}kirki_cm_reference WHERE post_id=%d AND field_meta_key=%s", $args['post_parent'], $cm_ref_field_meta_key ) );
115
116 $IDs = array();
117 if ( count( $results ) > 0 ) {
118 foreach ( $results as $key => $result ) {
119 $IDs[] = $result->ref_post_id;
120 }
121 }
122
123 $args['IDs'] = $IDs;
124 $posts = HelperFunctions::get_posts( $args );
125 return $posts;
126 }
127
128 public function get_gallery_collection( $value, $args ) {
129 global $wpdb;
130 $name = $args['name'];
131 $post = get_post( $args['post_parent'] );
132 $post_parent_post = get_post( $post->post_parent );
133 $item_per_page = isset( $args['item_per_page'] ) ? $args['item_per_page'] : 3;
134 $offset = isset( $args['offset'] ) ? $args['offset'] : 0;
135
136 $data_posts = array();
137
138 if ( $post_parent_post && isset( $post_parent_post->ID ) ) {
139 $cm_gallery_field_meta_key = ContentManagerHelper::get_child_post_meta_key_using_field_id( $post_parent_post->ID, $name );
140
141 $value = get_post_meta( $post->ID, $cm_gallery_field_meta_key, true );
142
143 if ( is_array( $value ) ) {
144 foreach ( array_slice( $value, $offset, $item_per_page ) as $key => $item ) {
145 $data_posts[] = $item;
146 }
147 }
148 }
149
150 return array(
151 'data' => $data_posts,
152 'pagination' => array(),
153 'itemType' => 'gallery',
154 'args' => $args,
155 );
156 }
157
158 public function register_parent_post_type( $request ) {
159 $args = array(
160 'page' => HelperFunctions::sanitize_text( isset( $request['page'] ) ? $request['page'] : 1 ),
161 );
162 $all_post_types = ContentManagerHelper::get_all_post_types( $args );
163 foreach ( $all_post_types as $key => $post_type ) {
164 $args = array(
165 'public' => true,
166 'show_ui' => false,
167 'label' => $post_type['post_title'],
168 // Add other arguments as needed
169 'rewrite' => array( 'slug' => $post_type['post_name'] ), // Define the slug for parent posts
170 );
171 register_post_type( ContentManagerHelper::get_child_post_post_type_value( $post_type['ID'] ), $args );
172 }
173 }
174
175 public function custom_parse_request( $wp ) {
176 global $wp_rewrite;
177
178 // Get the site URL and path to account for subfolder installation
179 $parsed_url_path = wp_parse_url( site_url(), PHP_URL_PATH );
180 $site_url_path = is_null( $parsed_url_path ) ? '' : trim( $parsed_url_path, '/' );
181
182 // Get the requested URL path
183 $requested_path = trim( wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' );
184
185 // Remove the subfolder part from the requested path
186 if ( $site_url_path && strpos( $requested_path, $site_url_path ) === 0 ) {
187 $requested_path = substr( $requested_path, strlen( $site_url_path ) );
188 $requested_path = trim( $requested_path, '/' );
189 }
190
191 // Get the parent and child slugs from the URL path
192 $path_parts = explode( '/', $requested_path );
193 $parent_slug = isset( $path_parts[0] ) ? $path_parts[0] : '';
194 $child_slug = isset( $path_parts[1] ) ? $path_parts[1] : '';
195
196 if ( ! $child_slug ) {
197 $post_id = HelperFunctions::get_post_id_if_possible_from_url();
198 if ( $post_id ) {
199 $post = get_post( $post_id );
200 if ( $post ) {
201 $child_slug = $post->post_name;
202 $parent_post = get_post( $post->post_parent );
203 if ( $parent_post ) {
204 $parent_slug = $parent_post->post_name;
205 }
206 }
207 }
208 }
209
210 // TODO: Need to check ?p=post_id for draft post preview.
211 // Check if the URL corresponds to a parent post
212 $parent_post = get_page_by_path( $parent_slug, OBJECT, ContentManagerHelper::PARENT_POST_TYPE );
213 if ( $parent_post ) {
214 // This is a parent post
215 $wp->query_vars[ KIRKI_CONTENT_MANAGER_PREFIX . '_parent_post' ] = $parent_post->ID;
216
217 // Check if the URL corresponds to a child post
218 if ( ! empty( $child_slug ) ) {
219 $args = array(
220 'post_type' => ContentManagerHelper::get_child_post_post_type_value( $parent_post->ID ), // Replace 'your_post_type' with your actual post type
221 'posts_per_page' => 1, // Limit to one post
222 'name' => $child_slug, // Replace 'your_post_name' with the post_name value
223 'post_parent' => $parent_post->ID, // Replace 123 with the post parent ID
224 );
225 $posts = get_posts( $args );
226 if ( count( $posts ) > 0 ) {
227 $child_post = $posts[0];
228 $wp->query_vars[ KIRKI_CONTENT_MANAGER_PREFIX . '_child_post' ] = $child_post->ID;
229 }
230 }
231
232 // Load custom template for both parent and child posts
233 // add_filter( 'template_include', [$this, 'custom_load_custom_template'] );
234 }
235 }
236
237 }
238