-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-post-types-dashboard-widget.php
218 lines (196 loc) · 5.78 KB
/
custom-post-types-dashboard-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
Plugin Name: Custom Post Types Dashboard Widget
Description: Add a customizable dashboard widgets to display selected custom post types.
Version: 1.0.1
Author: phirebase
Author URI: https://phirebase.com/
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: custom-post-types-dashboard-widget
*
* @package CustomPostTypesDashboardWidget
*/
defined( 'ABSPATH' ) || exit;
/**
* Load the plugin's text domain for translations.
*/
function cptdw_load_textdomain() {
load_plugin_textdomain(
'custom-post-types-dashboard-widget',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
add_action( 'init', 'cptdw_load_textdomain' );
/**
* Enqueue the settings page styles and scripts.
*
* @param string $hook_suffix The current admin page hook suffix.
*/
function cptdw_enqueue_dashboard_assets( $hook_suffix ) {
if ( 'index.php' === $hook_suffix ) {
wp_enqueue_style( 'cptdw-dashboard-style', plugin_dir_url( __FILE__ ) . 'css/admin-style.css', array(), '1.1' );
wp_enqueue_script( 'cptdw-dashboard-script', plugin_dir_url( __FILE__ ) . 'js/admin-script.js', array( 'jquery' ), '1.1', true );
}
}
add_action( 'admin_enqueue_scripts', 'cptdw_enqueue_dashboard_assets' );
/**
* Register the settings page.
*/
function cptdw_register_settings_page() {
add_options_page(
__( 'Custom Post Widget Settings', 'custom-post-types-dashboard-widget' ), // Translated title.
__( 'Custom Post Widget Settings', 'custom-post-types-dashboard-widget' ), // Translated menu title.
'manage_options',
'cptdw_settings',
'cptdw_render_settings_page'
);
}
add_action( 'admin_menu', 'cptdw_register_settings_page' );
/**
* Render the settings page.
*/
function cptdw_render_settings_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Custom Post Widget Settings', 'custom-post-types-dashboard-widget' ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'cptdw_settings_group' );
do_settings_sections( 'cptdw_settings' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register settings.
*/
function cptdw_register_settings() {
register_setting(
'cptdw_settings_group',
'cptdw_selected_post_types',
array(
'sanitize_callback' => 'cptdw_sanitize_post_types',
)
);
add_settings_section(
'cptdw_main_section',
__( 'Widget Settings', 'custom-post-types-dashboard-widget' ),
'__return_null',
'cptdw_settings'
);
add_settings_field(
'cptdw_post_types',
__( 'Select Post Types', 'custom-post-types-dashboard-widget' ),
'cptdw_render_post_types_field',
'cptdw_settings',
'cptdw_main_section'
);
}
add_action( 'admin_init', 'cptdw_register_settings' );
/**
* Add "Settings" link to plugin list actions.
*/
function cptdw_add_settings_link( $links ) {
$settings_link = '<a href="' . admin_url( 'options-general.php?page=cptdw_settings' ) . '">' . __( 'Settings', 'custom-post-types-dashboard-widget' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'cptdw_add_settings_link' );
/**
* Sanitize post types.
*/
function cptdw_sanitize_post_types( $input ) {
if ( ! is_array( $input ) ) {
return array();
}
return array_map( 'sanitize_text_field', $input );
}
/**
* Render the post types field.
*/
function cptdw_render_post_types_field() {
$post_types = get_post_types( array( 'public' => true ), 'objects' );
$selected_post_types = get_option( 'cptdw_selected_post_types', array() );
foreach ( $post_types as $post_type ) {
// Exclude 'attachment' post type (Media).
if ( $post_type->name === 'attachment' ) {
continue;
}
?>
<label>
<input type="checkbox" name="cptdw_selected_post_types[]" value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( in_array( $post_type->name, $selected_post_types ) ); ?>>
<?php echo esc_html( $post_type->label ); ?>
</label><br>
<?php
}
}
/**
* Add the dashboard widgets.
*/
function cptdw_add_dashboard_widgets() {
$selected_post_types = get_option( 'cptdw_selected_post_types', array() );
if ( empty( $selected_post_types ) ) {
return;
}
foreach ( $selected_post_types as $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object ) {
continue;
}
wp_add_dashboard_widget(
'cptdw_dashboard_widget_' . $post_type,
$post_type_object->label,
function () use ( $post_type ) {
cptdw_render_dashboard_widget( $post_type );
}
);
}
}
add_action( 'wp_dashboard_setup', 'cptdw_add_dashboard_widgets' );
/**
* Render a single dashboard widget.
*/
function cptdw_render_dashboard_widget( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object ) {
/* translators: Displayed when the selected post type is invalid. */
printf(
'<p>%s</p>',
esc_html__( 'Invalid post type.', 'custom-post-types-dashboard-widget' )
);
return;
}
$count = wp_count_posts( $post_type )->publish;
/* translators: Text shown before the number of published posts. */
$text = __( 'Total published:', 'custom-post-types-dashboard-widget' );
printf( '<p>%s %d</p>', esc_html( $text ), intval( $count ) );
$recent_posts = get_posts(
array(
'post_type' => $post_type,
'posts_per_page' => 5,
)
);
if ( empty( $recent_posts ) ) {
/* translators: Displayed when no recent posts are found for the post type. */
printf(
'<p>%s</p>',
esc_html__( 'No recent posts.', 'custom-post-types-dashboard-widget' )
);
} else {
echo '<ul class="cptdw-recent-posts">';
foreach ( $recent_posts as $post ) {
$title = get_the_title( $post ) ?: __( '(No title)', 'custom-post-types-dashboard-widget' );
printf(
'<li><a href="%s">%s</a></li>',
esc_url( get_edit_post_link( $post->ID ) ),
esc_html( $title )
);
}
echo '</ul>';
}
}