@@ -26,14 +26,23 @@ class Post_Type_Press_Mentions extends Base {
2626 */
2727 const LABEL = 'Press mentions ' ;
2828
29+ /**
30+ * Initialize the class and set up hooks
31+ */
32+ public function init () {
33+ \add_action ( 'add_meta_boxes ' , array ( $ this , 'register_custom_fields ' ) );
34+ \add_action ( 'save_post_ ' . self ::SLUG , array ( $ this , 'save_custom_fields ' ), 10 , 2 );
35+ \add_action ( 'rest_api_init ' , array ( $ this , 'register_rest_fields ' ) );
36+ }
37+
2938 /**
3039 * To get list of labels for post type.
3140 *
3241 * @return array
3342 */
3443 public function get_labels () {
3544
36- return [
45+ return array (
3746 'name ' => __ ( 'Press mentions ' , 'osi-features ' ),
3847 'singular_name ' => __ ( 'Press mentions ' , 'osi-features ' ),
3948 'all_items ' => __ ( 'Press mentions ' , 'osi-features ' ),
@@ -45,8 +54,7 @@ public function get_labels() {
4554 'search_items ' => __ ( 'Search Press mentions ' , 'osi-features ' ),
4655 'not_found ' => __ ( 'No Press mentions found ' , 'osi-features ' ),
4756 'not_found_in_trash ' => __ ( 'No Press mentions found in Trash ' , 'osi-features ' ),
48- ];
49-
57+ );
5058 }
5159
5260 /**
@@ -56,14 +64,205 @@ public function get_labels() {
5664 */
5765 public function get_args () {
5866
59- return [
67+ return array (
6068 'show_in_rest ' => true ,
6169 'public ' => true ,
6270 'has_archive ' => true ,
6371 'menu_position ' => 6 ,
64- 'supports ' => [ 'title ' , 'author ' , 'excerpt ' ],
65- 'rewrite ' => [ 'slug ' => 'press-mentions ' , 'with_front ' => false ]
66- ];
72+ 'supports ' => array ( 'title ' , 'author ' , 'excerpt ' ),
73+ 'rewrite ' => array (
74+ 'slug ' => 'press-mentions ' ,
75+ 'with_front ' => false ,
76+ ),
77+ );
78+ }
79+
80+ /**
81+ * Register custom fields for press mentions
82+ */
83+ public function register_custom_fields () {
84+ \add_meta_box (
85+ 'press_mentions_meta_box ' ,
86+ \__ ( 'Press Mention Details ' , 'osi-features ' ),
87+ array ( $ this , 'render_meta_box ' ),
88+ self ::SLUG ,
89+ 'normal ' ,
90+ 'high '
91+ );
92+ }
93+
94+ /**
95+ * Render the meta box
96+ *
97+ * @param \WP_Post $post The post object
98+ */
99+ public function render_meta_box ( $ post ) {
100+ \wp_nonce_field ( 'press_mentions_meta_box ' , 'press_mentions_meta_box_nonce ' );
101+
102+ $ date_of_publication = \get_post_meta ( $ post ->ID , 'date_of_publication ' , true );
103+ // Convert Ymd format to Y-m-d for the input field
104+ if ( ! empty ( $ date_of_publication ) ) {
105+ $ date_of_publication = \DateTime::createFromFormat ( 'Ymd ' , $ date_of_publication )->format ( 'Y-m-d ' );
106+ }
107+ $ article_url = \get_post_meta ( $ post ->ID , 'article_url ' , true );
108+
109+ ?>
110+ <div class="press-mentions-fields">
111+ <p>
112+ <label for="date_of_publication"><?php \esc_html_e ( 'Date of Publication ' , 'osi-features ' ); ?> *</label><br>
113+ <input
114+ type="date"
115+ id="date_of_publication"
116+ name="date_of_publication"
117+ value="<?php echo \esc_attr ( $ date_of_publication ); ?> "
118+ required
119+ data-first-day="1"
120+ >
121+ </p>
122+ <p>
123+ <label for="article_url"><?php \esc_html_e ( 'Article URL ' , 'osi-features ' ); ?> *</label><br>
124+ <input
125+ type="url"
126+ id="article_url"
127+ name="article_url"
128+ value="<?php echo \esc_url ( $ article_url ); ?> "
129+ required
130+ style="width: 100%;"
131+ >
132+ </p>
133+ </div>
134+ <?php
135+ }
136+
137+ /**
138+ * Save custom fields
139+ *
140+ * @param int $post_id The post ID
141+ * @param \WP_Post $post The post object
142+ */
143+ public function save_custom_fields ( $ post_id , $ post ) {
144+ if ( ! isset ( $ _POST ['press_mentions_meta_box_nonce ' ] ) ) {
145+ return ;
146+ }
147+
148+ if ( ! \wp_verify_nonce ( $ _POST ['press_mentions_meta_box_nonce ' ], 'press_mentions_meta_box ' ) ) {
149+ return ;
150+ }
151+
152+ if ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) {
153+ return ;
154+ }
155+
156+ if ( ! \current_user_can ( 'edit_post ' , $ post_id ) ) {
157+ return ;
158+ }
159+
160+ // Save date of publication
161+ if ( isset ( $ _POST ['date_of_publication ' ] ) ) {
162+ $ date = \sanitize_text_field ( $ _POST ['date_of_publication ' ] );
163+ // Convert to Ymd format
164+ $ formatted_date = gmdate ( 'Ymd ' , strtotime ( $ date ) );
165+ \update_post_meta ( $ post_id , 'date_of_publication ' , $ formatted_date );
166+ }
167+
168+ // Save article URL
169+ if ( isset ( $ _POST ['article_url ' ] ) ) {
170+ $ url = \esc_url_raw ( $ _POST ['article_url ' ] );
171+ \update_post_meta ( $ post_id , 'article_url ' , $ url );
172+ }
173+ }
174+
175+ /**
176+ * Register REST API fields
177+ */
178+ public function register_rest_fields () {
179+ \register_rest_field (
180+ self ::SLUG ,
181+ 'date_of_publication ' ,
182+ array (
183+ 'get_callback ' => function ( $ post ) {
184+ return \get_post_meta ( $ post ['id ' ], 'date_of_publication ' , true );
185+ },
186+ 'update_callback ' => function ( $ value , $ post ) {
187+ // Handle array input from Make.com
188+ $ date_value = is_array ( $ value ) && ! empty ( $ value ) ? $ value [0 ] : $ value ;
189+ if ( ! empty ( $ date_value ) ) {
190+ // Convert the date to Ymd format
191+ $ formatted_date = gmdate ( 'Ymd ' , strtotime ( $ date_value ) );
192+ \update_post_meta ( $ post ->ID , 'date_of_publication ' , $ formatted_date );
193+ }
194+ },
195+ 'schema ' => array (
196+ 'type ' => array ( 'string ' , 'array ' ),
197+ 'items ' => array ( 'type ' => 'string ' ),
198+ 'description ' => 'Date of Publication in Ymd format ' ,
199+ 'required ' => true ,
200+ ),
201+ ),
202+ );
203+
204+ \register_rest_field (
205+ self ::SLUG ,
206+ 'article_url ' ,
207+ array (
208+ 'get_callback ' => function ( $ post ) {
209+ return \get_post_meta ( $ post ['id ' ], 'article_url ' , true );
210+ },
211+ 'update_callback ' => function ( $ value , $ post ) {
212+ // Handle array input from Make.com
213+ $ url_value = is_array ( $ value ) && ! empty ( $ value ) ? $ value [0 ] : $ value ;
214+ if ( ! empty ( $ url_value ) ) {
215+ \update_post_meta ( $ post ->ID , 'article_url ' , \esc_url_raw ( $ url_value ) );
216+ }
217+ },
218+ 'schema ' => array (
219+ 'type ' => array ( 'string ' , 'array ' ),
220+ 'items ' => array ( 'type ' => 'string ' ),
221+ 'description ' => 'Article URL ' ,
222+ 'required ' => true ,
223+ ),
224+ ),
225+ );
226+
227+ \register_rest_field (
228+ self ::SLUG ,
229+ 'publication_name ' ,
230+ array (
231+ 'get_callback ' => function ( $ post ) {
232+ $ terms = \get_the_terms ( $ post ['id ' ], 'taxonomy-publication ' );
233+ if ( ! empty ( $ terms ) && ! \is_wp_error ( $ terms ) ) {
234+ return $ terms [0 ]->name ;
235+ }
236+ return '' ;
237+ },
238+ 'update_callback ' => function ( $ value , $ post ) {
239+ // Handle array input from Make.com
240+ $ pub_value = is_array ( $ value ) && ! empty ( $ value ) ? $ value [0 ] : $ value ;
241+ if ( ! empty ( $ pub_value ) ) {
242+ $ term = \get_term_by ( 'name ' , $ pub_value , 'taxonomy-publication ' );
243+
244+ if ( ! $ term ) {
245+ $ new_term = \wp_insert_term ( $ pub_value , 'taxonomy-publication ' );
246+ if ( ! \is_wp_error ( $ new_term ) ) {
247+ $ term_id = $ new_term ['term_id ' ];
248+ }
249+ } else {
250+ $ term_id = $ term ->term_id ;
251+ }
67252
253+ if ( isset ( $ term_id ) ) {
254+ // Set the taxonomy term for the post
255+ \wp_set_object_terms ( $ post ->ID , array ( $ term_id ), 'taxonomy-publication ' );
256+ }
257+ }
258+ },
259+ 'schema ' => array (
260+ 'type ' => array ( 'string ' , 'array ' ),
261+ 'items ' => array ( 'type ' => 'string ' ),
262+ 'description ' => 'Publication Name ' ,
263+ 'required ' => true ,
264+ ),
265+ ),
266+ );
68267 }
69268}
0 commit comments