@@ -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,158 @@ 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+ // Add nonce for security
101+ \wp_nonce_field ( 'press_mentions_meta_box ' , 'press_mentions_meta_box_nonce ' );
102+
103+ // Get existing values
104+ $ date_of_publication = \get_post_meta ( $ post ->ID , 'date_of_publication ' , true );
105+ // Convert Ymd format to Y-m-d for the input field
106+ if ( ! empty ( $ date_of_publication ) ) {
107+ $ date_of_publication = \DateTime::createFromFormat ( 'Ymd ' , $ date_of_publication )->format ( 'Y-m-d ' );
108+ }
109+ $ article_url = \get_post_meta ( $ post ->ID , 'article_url ' , true );
110+
111+ ?>
112+ <div class="press-mentions-fields">
113+ <p>
114+ <label for="date_of_publication"><?php \esc_html_e ( 'Date of Publication ' , 'osi-features ' ); ?> *</label><br>
115+ <input
116+ type="date"
117+ id="date_of_publication"
118+ name="date_of_publication"
119+ value="<?php echo \esc_attr ( $ date_of_publication ); ?> "
120+ required
121+ data-first-day="1"
122+ >
123+ </p>
124+ <p>
125+ <label for="article_url"><?php \esc_html_e ( 'Article URL ' , 'osi-features ' ); ?> *</label><br>
126+ <input
127+ type="url"
128+ id="article_url"
129+ name="article_url"
130+ value="<?php echo \esc_url ( $ article_url ); ?> "
131+ required
132+ style="width: 100%;"
133+ >
134+ </p>
135+ </div>
136+ <?php
137+ }
138+
139+ /**
140+ * Save custom fields
141+ *
142+ * @param int $post_id The post ID
143+ * @param \WP_Post $post The post object
144+ */
145+ public function save_custom_fields ( $ post_id , $ post ) {
146+ // Check if nonce is set
147+ if ( ! isset ( $ _POST ['press_mentions_meta_box_nonce ' ] ) ) {
148+ return ;
149+ }
150+
151+ // Verify nonce
152+ if ( ! \wp_verify_nonce ( $ _POST ['press_mentions_meta_box_nonce ' ], 'press_mentions_meta_box ' ) ) {
153+ return ;
154+ }
155+
156+ // If this is an autosave, don't do anything
157+ if ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) {
158+ return ;
159+ }
160+
161+ // Check user permissions
162+ if ( ! \current_user_can ( 'edit_post ' , $ post_id ) ) {
163+ return ;
164+ }
165+
166+ // Save date of publication
167+ if ( isset ( $ _POST ['date_of_publication ' ] ) ) {
168+ $ date = \sanitize_text_field ( $ _POST ['date_of_publication ' ] );
169+ // Convert to Ymd format
170+ $ formatted_date = gmdate ( 'Ymd ' , strtotime ( $ date ) );
171+ \update_post_meta ( $ post_id , 'date_of_publication ' , $ formatted_date );
172+ }
173+
174+ // Save article URL
175+ if ( isset ( $ _POST ['article_url ' ] ) ) {
176+ $ url = \esc_url_raw ( $ _POST ['article_url ' ] );
177+ \update_post_meta ( $ post_id , 'article_url ' , $ url );
178+ }
179+ }
180+
181+ /**
182+ * Register REST API fields
183+ */
184+ public function register_rest_fields () {
185+ \register_rest_field (
186+ self ::SLUG ,
187+ 'date_of_publication ' ,
188+ array (
189+ 'get_callback ' => function ( $ post ) {
190+ return \get_post_meta ( $ post ['id ' ], 'date_of_publication ' , true );
191+ },
192+ 'update_callback ' => function ( $ value , $ post ) {
193+ \update_post_meta ( $ post ->ID , 'date_of_publication ' , $ value );
194+ },
195+ 'schema ' => array (
196+ 'type ' => 'string ' ,
197+ 'description ' => 'Date of Publication ' ,
198+ 'required ' => true ,
199+ ),
200+ ),
201+ );
67202
203+ \register_rest_field (
204+ self ::SLUG ,
205+ 'article_url ' ,
206+ array (
207+ 'get_callback ' => function ( $ post ) {
208+ return \get_post_meta ( $ post ['id ' ], 'article_url ' , true );
209+ },
210+ 'update_callback ' => function ( $ value , $ post ) {
211+ \update_post_meta ( $ post ->ID , 'article_url ' , \esc_url_raw ( $ value ) );
212+ },
213+ 'schema ' => array (
214+ 'type ' => 'string ' ,
215+ 'description ' => 'Article URL ' ,
216+ 'required ' => true ,
217+ ),
218+ ),
219+ );
68220 }
69221}
0 commit comments