PluginProbe ʕ •ᴥ•ʔ
Admin Help Docs / 2.0.2
Admin Help Docs v2.0.2
2.0.2 2.0.1.1 trunk 1.4.3.2 2.0.0 2.0.0.1 2.0.0.2 2.0.1
admin-help-docs / inc / post-type-help-doc-imports.php
admin-help-docs / inc Last commit date
css 1 week ago docs 1 week ago img 1 week ago js 1 week ago tabs 1 week ago api.php 1 week ago cleanup.php 1 week ago colors.php 1 week ago deprecated.php 1 week ago header.php 1 week ago helpers.php 1 week ago index.php 1 week ago menu.php 1 week ago plugins-page.php 1 week ago post-type-help-doc-imports.php 1 week ago post-type-help-docs.php 1 week ago shortcodes.php 1 week ago taxonomy-folders.php 1 week ago
post-type-help-doc-imports.php
406 lines
1 <?php
2 /**
3 * Help Doc Imports post type (Imports Tab)
4 */
5
6 namespace PluginRx\AdminHelpDocs;
7
8 if ( ! defined( 'ABSPATH' ) ) exit;
9
10 class Imports {
11
12 /**
13 * Post type slug
14 *
15 * @var string
16 */
17 public static $post_type = 'help-doc-imports';
18
19
20 /**
21 * The single instance of the class
22 *
23 * @var self|null
24 */
25 private static ?Imports $instance = null;
26
27
28 /**
29 * Get the singleton instance
30 *
31 * @return self
32 */
33 public static function instance() : self {
34 return self::$instance ??= new self();
35 } // End instance()
36
37
38 /**
39 * Imports constructor.
40 *
41 * Private to enforce singleton pattern.
42 */
43 private function __construct() {
44
45 // Register the post type
46 add_action( 'init', [ $this, 'register_post_type' ] );
47
48 // Add the header to the top of the admin list page
49 add_action( 'load-edit.php', [ $this, 'add_header' ] );
50
51 // Move the search box to the subheader
52 add_action( 'helpdocs_subheader_right', [ $this, 'render_search_box' ] );
53
54 // Update edit links to go to the import editor
55 add_filter( 'get_edit_post_link', [ $this, 'redirect_import_edit_link' ], 10, 2 );
56 add_action( 'current_screen', [ $this, 'redirect_new_post_screen' ] );
57
58 // Add admin columns
59 add_filter( 'manage_' . self::$post_type . '_posts_columns', [ $this, 'admin_columns' ] );
60 add_action( 'manage_' . self::$post_type . '_posts_custom_column', [ $this, 'admin_column_content' ], 10, 2 );
61
62 // Remove Quick Edit
63 add_filter( 'post_row_actions', [ $this, 'remove_quick_edit' ], 10, 2 );
64
65 // Remove Bulk Edit
66 add_filter( 'bulk_actions-edit-' . self::$post_type, [ $this, 'remove_bulk_edit' ] );
67
68 // Enqueue back-end styles
69 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_styles' ] );
70
71 // AJAX toggle status
72 add_action( 'wp_ajax_helpdocs_toggle_import_status', [ $this, 'ajax_toggle_status' ] );
73
74 } // End __construct()
75
76
77 /**
78 * Register the post type
79 */
80 public function register_post_type() {
81 // Set the labels
82 $labels = [
83 'name' => _x( 'Imports', 'Post Type General Name', 'admin-help-docs' ),
84 'singular_name' => _x( 'Import', 'Post Type Singular Name', 'admin-help-docs' ),
85 'menu_name' => __( 'Imports', 'admin-help-docs' ),
86 'name_admin_bar' => __( 'Imports', 'admin-help-docs' ),
87 'archives' => __( 'Import Archives', 'admin-help-docs' ),
88 'attributes' => __( 'Import Attributes', 'admin-help-docs' ),
89 'parent_item_colon' => __( 'Parent Import:', 'admin-help-docs' ),
90 'all_items' => __( 'All Imports', 'admin-help-docs' ),
91 'add_new_item' => __( 'Add New Import', 'admin-help-docs' ),
92 'add_new' => __( 'Add New', 'admin-help-docs' ),
93 'new_item' => __( 'New Import', 'admin-help-docs' ),
94 'edit_item' => __( 'Edit Import', 'admin-help-docs' ),
95 'update_item' => __( 'Update Import', 'admin-help-docs' ),
96 'view_item' => __( 'View Import', 'admin-help-docs' ),
97 'view_items' => __( 'View Imports', 'admin-help-docs' ),
98 'search_items' => __( 'Search Imports', 'admin-help-docs' ),
99 'not_found' => __( 'Not found', 'admin-help-docs' ),
100 'not_found_in_trash' => __( 'Not found in Trash', 'admin-help-docs' ),
101 'insert_into_item' => __( 'Insert into import', 'admin-help-docs' ),
102 'uploaded_to_this_item' => __( 'Uploaded to this import', 'admin-help-docs' ),
103 'items_list' => __( 'Import list', 'admin-help-docs' ),
104 'items_list_navigation' => __( 'Import list navigation', 'admin-help-docs' ),
105 'filter_items_list' => __( 'Filter import list', 'admin-help-docs' ),
106 ];
107
108 // Allow filter for supports and taxonomies
109 $supports = apply_filters( 'helpdocs_imports_supports', [ 'title', 'excerpt' ] );
110 $taxonomies = apply_filters( 'helpdocs_imports_taxonomies', [] );
111
112 // Set the CPT args
113 $args = [
114 'label' => __( 'Imports', 'admin-help-docs' ),
115 'description' => __( 'Imports', 'admin-help-docs' ),
116 'labels' => $labels,
117 'supports' => $supports,
118 'taxonomies' => $taxonomies,
119 'public' => true,
120 'show_ui' => true,
121 'show_in_menu' => false,
122 'show_in_admin_bar' => false,
123 'show_in_nav_menus' => false,
124 'can_export' => true,
125 'has_archive' => false,
126 'exclude_from_search' => true,
127 'publicly_queryable' => false,
128 'query_var' => self::$post_type,
129 'capability_type' => 'post',
130 'show_in_rest' => false,
131 ];
132
133 // Register the CPT
134 register_post_type( self::$post_type, $args );
135 } // End register_post_type()
136
137
138 /**
139 * Add the header to the top of the admin list page
140 *
141 * @return void
142 */
143 public function add_header() {
144 $screen = get_current_screen();
145 if ( 'edit-' . self::$post_type === $screen->id ) {
146 add_action( 'in_admin_header', function() {
147 include Bootstrap::path( 'inc/header.php' );
148 } );
149 }
150 } // End add_header()
151
152
153 /**
154 * Add a search box to the subheader on the folders page
155 *
156 * @param string $current_tab The current admin tab
157 * @return void
158 */
159 public function render_search_box( string $current_tab ) {
160 if ( $current_tab !== 'imports' ) {
161 return;
162 }
163
164 $search_value = sanitize_text_field( wp_unslash( $_GET[ 's' ] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
165 ?>
166 <form method="get" class="helpdocs-posttype-search">
167 <?php foreach ( $_GET as $key => $value ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended
168 if ( in_array( $key, [ 's', 'action', 'paged' ], true ) ) continue;
169 ?>
170 <input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $value ); ?>" />
171 <?php endforeach; ?>
172
173 <input type="search"
174 name="s"
175 value="<?php echo esc_attr( $search_value ); ?>"
176 placeholder="<?php echo esc_attr__( 'Search Docs', 'admin-help-docs' ); ?>"
177 class="helpdocs-search-input" />
178
179 <input type="submit"
180 class="helpdocs-button"
181 value="<?php echo esc_attr__( 'Search', 'admin-help-docs' ); ?>" />
182
183 <a href="<?php
184 $clear_url = remove_query_arg( 's' );
185 echo esc_url( $clear_url );
186 ?>" class="helpdocs-button">
187 <?php echo esc_html__( 'Clear', 'admin-help-docs' ); ?>
188 </a>
189 </form>
190 <?php
191 } // End render_search_box()
192
193
194 /**
195 * Redirect the edit link to the import editor
196 *
197 * @param string $link The original edit link
198 * @param int $post_id The post ID
199 * @return string The modified edit link
200 */
201 public function redirect_import_edit_link( $link, $post_id ) {
202 $post = get_post( $post_id );
203 if ( $post && $post->post_type === self::$post_type ) {
204 return add_query_arg( [ 'id' => $post_id ], Bootstrap::tab_url( 'import' ) );
205 }
206
207 return $link;
208 } // End redirect_import_edit_link()
209
210
211 /**
212 * If a user manually tries to go to post-new.php for our CPT,
213 * or clicks the "Add New" on the list table, bounce them to our tab.
214 */
215 public function redirect_new_post_screen() {
216 global $pagenow;
217
218 if ( 'post-new.php' === $pagenow && isset( $_GET[ 'post_type' ] ) && sanitize_text_field( wp_unslash( $_GET[ 'post_type' ] ) ) === self::$post_type ) { // phpcs:ignore
219 wp_safe_redirect( Bootstrap::tab_url( 'editor' ) );
220 exit;
221 }
222
223 if ( 'post.php' === $pagenow && isset( $_GET[ 'post' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
224 $action = isset( $_GET[ 'action' ] ) ? sanitize_text_field( wp_unslash( $_GET[ 'action' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
225
226 if ( ! empty( $action ) && 'edit' !== $action ) {
227 return;
228 }
229
230 $post_id = isset( $_GET[ 'post' ] ) ? absint( wp_unslash( $_GET[ 'post' ] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
231 $post = get_post( $post_id );
232
233 if ( $post && $post->post_type === self::$post_type ) {
234 wp_safe_redirect( add_query_arg( [ 'id' => $post_id ], Bootstrap::tab_url( 'import' ) ) );
235 exit;
236 }
237 }
238 } // End redirect_new_post_screen()
239
240
241 /**
242 * Admin columns
243 *
244 * @param array $columns
245 * @return array
246 */
247 public function admin_columns( $columns ) {
248 $new_columns = [
249 'cb' => $columns[ 'cb' ],
250 'helpdocs_status' => __( 'Status', 'admin-help-docs' ),
251 ];
252
253 unset( $columns[ 'cb' ], $columns[ 'featured_image' ] );
254
255 $columns[ 'helpdocs_url' ] = __( 'URL', 'admin-help-docs' );
256 $columns[ 'helpdocs_all' ] = __( 'Import All?', 'admin-help-docs' );
257
258 return array_merge( $new_columns, $columns );
259 } // End admin_columns()
260
261
262 /**
263 * Admin column content
264 *
265 * @param string $column
266 * @param int $post_id
267 * @return void
268 */
269 public function admin_column_content( $column, $post_id ) {
270 // Status Toggle
271 if ( 'helpdocs_status' === $column ) {
272 $status = get_post_status( $post_id );
273 $is_published = ( 'publish' === $status );
274
275 $class = $is_published ? ' is-active' : ' is-inactive';
276
277 $label = $is_published ? __( 'Active', 'admin-help-docs' ) : __( 'Inactive', 'admin-help-docs' );
278
279 echo '<button type="button" class="helpdocs-status-toggle ' . esc_attr( $class ) . '" data-id="' . esc_attr( $post_id ) . '">
280 <span class="helpdocs-post-status">' . esc_html( $label ) . '</span>
281 </button>';
282 }
283
284 // URL
285 if ( 'helpdocs_url' === $column ) {
286 $website_url = get_post_meta( $post_id, 'helpdocs_url', true );
287 echo $website_url ? esc_url( $website_url ) : '';
288
289 $api_key = get_post_meta( $post_id, 'helpdocs_api_key', true );
290 if ( $api_key ) {
291 echo '<br><em><small>' . esc_html__( 'API Key Set', 'admin-help-docs' ) . '</small></em>';
292 }
293 }
294
295 // Import all
296 if ( 'helpdocs_all' === $column ) {
297 $auto_all = get_post_meta( $post_id, 'helpdocs_all', true );
298 $all = $auto_all ? esc_html__( 'Yes', 'admin-help-docs' ) : esc_html__( 'No', 'admin-help-docs' );
299 echo esc_html( $all );
300 }
301 } // End admin_column_content()
302
303
304 /**
305 * Remove Quick Edit from row actions
306 *
307 * @param array $actions
308 * @param \WP_Post $post
309 * @return array
310 */
311 public function remove_quick_edit( $actions, $post ) {
312 if ( $post->post_type === self::$post_type ) {
313 unset( $actions[ 'inline hide-if-no-js' ] ); // 'inline' is the key for Quick Edit
314 }
315 return $actions;
316 } // End remove_quick_edit()
317
318
319 /**
320 * Remove Bulk Edit from the bulk actions dropdown
321 *
322 * @param array $actions
323 * @return array
324 */
325 public function remove_bulk_edit( $actions ) {
326 unset( $actions[ 'edit' ] );
327 return $actions;
328 } // End remove_bulk_edit()
329
330
331 /**
332 * Enqueue admin styles.
333 */
334 public function enqueue_admin_styles() {
335 // Only load on our post type edit screen
336 global $current_screen;
337 if ( ! is_admin() || ! isset( $current_screen ) || $current_screen->id !== 'edit-' . self::$post_type ) {
338 return;
339 }
340
341 $text_domain = Bootstrap::textdomain();
342 $version = Bootstrap::script_version();
343
344 // CSS
345 wp_enqueue_style(
346 $text_domain . '-post-type-' . self::$post_type,
347 Bootstrap::url( 'inc/css/post-type-' . self::$post_type . '.css' ),
348 [],
349 $version
350 );
351
352 // JS
353 wp_enqueue_script(
354 $text_domain . '-post-type-' . self::$post_type,
355 Bootstrap::url( 'inc/js/post-type-' . self::$post_type . '.js' ),
356 [ 'jquery' ],
357 $version,
358 true
359 );
360
361 wp_localize_script( $text_domain . '-post-type-' . self::$post_type, 'helpdocs_' . str_replace( '-', '_', self::$post_type ), [
362 'nonce' => wp_create_nonce( 'helpdocs_import_status_nonce' ),
363 'active_text' => __( 'Active', 'admin-help-docs' ),
364 'inactive_text' => __( 'Inactive', 'admin-help-docs' ),
365 ] );
366 } // End enqueue_admin_styles()
367
368
369 /**
370 * Get URL for a specific tab
371 *
372 * @param string $tab The tab key (e.g., 'main', 'adminmenu', 'import', 'editor')
373 * @return string The URL for the specified tab
374 */
375 public function ajax_toggle_status() {
376 check_ajax_referer( 'helpdocs_import_status_nonce', 'nonce' );
377 if ( ! Helpers::user_can_edit() ) {
378 wp_send_json_error();
379 }
380
381 $post_id = isset( $_POST[ 'id' ] ) ? absint( $_POST[ 'id' ] ) : 0;
382 if ( ! $post_id ) {
383 wp_send_json_error( __( 'Invalid ID', 'admin-help-docs' ) );
384 }
385
386 $current_status = get_post_status( $post_id );
387 $new_status = ( 'publish' === $current_status ) ? 'draft' : 'publish';
388
389 $updated = wp_update_post( [
390 'ID' => $post_id,
391 'post_status' => $new_status,
392 ] );
393
394 if ( is_wp_error( $updated ) ) {
395 wp_send_json_error( $updated->get_error_message() );
396 }
397
398 wp_send_json_success( [
399 'active' => ( 'publish' === $new_status )
400 ] );
401 } // End ajax_toggle_status()
402
403 }
404
405
406 Imports::instance();