Skip to content

Commit 05409b9

Browse files
committed
Only register the directory custom fields when needed
1 parent 248c7b8 commit 05409b9

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

includes/wpum-directories/class-wpum-directories-editor.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@ class WPUM_Directories_Editor {
2727
*/
2828
protected $builder;
2929

30-
/**
31-
* Get things started.
32-
*/
33-
public function __construct() {
34-
30+
public function init() {
3531
add_action( 'init', [ $this, 'register_post_type' ], 0 );
36-
add_action( 'carbon_fields_register_fields', [ $this, 'register_directory_settings' ] );
32+
add_action( 'carbon_fields_register_fields', [ $this, 'maybe_register_directory_settings' ] );
3733
add_action( 'admin_footer', [ $this, 'css' ] );
3834

3935
if ( is_admin() ) {
@@ -103,6 +99,45 @@ public function register_post_type() {
10399

104100
}
105101

102+
/**
103+
* Get post type for page request (in admin)
104+
*
105+
* @return string
106+
*/
107+
public function get_post_type() {
108+
global $pagenow;
109+
110+
if ( isset( $_GET['post_type'] ) && 'post-new.php' === $pagenow ) {
111+
return $_GET['post_type'];
112+
}
113+
114+
if ( ! isset( $_GET['post'] ) && ! isset( $_POST['post_ID'] ) ) {
115+
return '';
116+
}
117+
118+
$post_id = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
119+
if ( empty( $post_id ) ) {
120+
$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_VALIDATE_INT );
121+
}
122+
123+
$post = get_post( $post_id );
124+
125+
if ( empty( $post ) || empty( $post->post_type ) ) {
126+
return '';
127+
}
128+
129+
return $post->post_type;
130+
}
131+
132+
public function maybe_register_directory_settings() {
133+
$post_type = $this->get_post_type();
134+
if ( ! $post_type || 'wpum_directory' !== $post_type ) {
135+
return;
136+
}
137+
138+
$this->register_directory_settings();
139+
}
140+
106141
/**
107142
* Register settings for the directory
108143
*
@@ -355,5 +390,3 @@ public function bulk_post_updated_messages( $bulk_messages ) {
355390
return $bulk_messages;
356391
}
357392
}
358-
359-
new WPUM_Directories_Editor;

includes/wpum-shortcodes/shortcodes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ function wpum_directory( $atts, $content = null ) {
698698

699699
ob_start();
700700

701+
WPUM()->directories_editor->register_directory_settings();
702+
701703
$directory_id = intval( $id );
702704

703705
// Check if directory exists

wp-user-manager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ final class WP_User_Manager {
146146
*/
147147
public $field_types;
148148

149+
/**
150+
* @var WPUM_Directories_Editor
151+
*/
152+
public $directories_editor;
153+
149154
/**
150155
* Main WPUM Instance.
151156
*
@@ -344,6 +349,9 @@ private function init_hooks() {
344349

345350
$this->field_types = new WPUM_Fields();
346351
$this->field_types->init();
352+
353+
$this->directories_editor = new WPUM_Directories_Editor();
354+
$this->directories_editor->init();
347355
}
348356

349357
/**

0 commit comments

Comments
 (0)