| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
/** |
| 4 |
* Create the "Dashboard" submenu page |
| 5 |
* |
| 6 |
*/ |
| 7 |
function wppb_register_dashboard_submenu_page() { |
| 8 |
if( isset( $_GET['subpage'] ) && $_GET['subpage'] == 'wppb-setup' ) |
| 9 |
$page_title = __( 'Setup Wizard', 'profile-builder' ); |
| 10 |
else |
| 11 |
$page_title = __( 'Dashboard', 'profile-builder' ); |
| 12 |
|
| 13 |
add_submenu_page( 'profile-builder', $page_title, $page_title, 'manage_options', 'profile-builder-dashboard', 'wppb_dashboard_content_output' ); |
| 14 |
} |
| 15 |
add_action( 'admin_menu', 'wppb_register_dashboard_submenu_page', 2 ); |
| 16 |
|
| 17 |
function wppb_dashboard_content_output() { |
| 18 |
if( isset( $_GET['subpage'] ) && $_GET['subpage'] == 'wppb-setup' ) |
| 19 |
do_action( 'wppb_output_dashboard_setup_wizard' ); |
| 20 |
else |
| 21 |
wppb_dashboard_page_content(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add content to the "Dashboard" submenu page |
| 26 |
* |
| 27 |
*/ |
| 28 |
function wppb_dashboard_page_content() { |
| 29 |
|
| 30 |
?> |
| 31 |
|
| 32 |
<div class="wrap cozmoslabs-wrap cozmoslabs-wrap--big"> |
| 33 |
|
| 34 |
<h1></h1> |
| 35 |
<!-- WordPress Notices are added after the h1 tag --> |
| 36 |
|
| 37 |
<div class="cozmoslabs-page-header"> |
| 38 |
<div class="cozmoslabs-section-title"> |
| 39 |
<h3 class="cozmoslabs-page-title"><?php esc_html_e( 'Dashboard', 'profile-builder' ); ?></h3> |
| 40 |
</div> |
| 41 |
</div> |
| 42 |
|
| 43 |
<div class="cozmoslabs-page-grid wppb-dashboard-overview"> |
| 44 |
|
| 45 |
<div class="postbox cozmoslabs-form-subsection-wrapper"> |
| 46 |
|
| 47 |
<div class="wppb-dashboard-stats__title"> |
| 48 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Totals', 'profile-builder' ); ?></h4> |
| 49 |
|
| 50 |
<select name="wppb_dashboard_stats_select" id="wppb-dashboard-stats-select"> |
| 51 |
<option value="30days" selected><?php esc_html_e( '30 days', 'profile-builder' ); ?></option> |
| 52 |
<option value="this_month"><?php esc_html_e( 'This Month', 'profile-builder' ); ?></option> |
| 53 |
<option value="last_month"><?php esc_html_e( 'Last Month', 'profile-builder' ); ?></option> |
| 54 |
<option value="this_year"><?php esc_html_e( 'This Year', 'profile-builder' ); ?></option> |
| 55 |
</select> |
| 56 |
|
| 57 |
<input type="hidden" id="wppb-dashboard-stats-select__nonce" value="<?php echo esc_html( wp_create_nonce( 'wppb_dashboard_get_stats' ) ); ?>" /> |
| 58 |
</div> |
| 59 |
|
| 60 |
<div class="wppb-dashboard-stats"> |
| 61 |
<?php |
| 62 |
$stats = wppb_users_stats(); |
| 63 |
$stats_labels = wppb_users_stats_labels(); |
| 64 |
|
| 65 |
if( !empty( $stats ) ){ |
| 66 |
foreach( $stats as $key => $value ) : ?> |
| 67 |
|
| 68 |
<div class="wppb-dashboard-box <?php echo esc_html( $key ); ?>"> |
| 69 |
<div class="label"> |
| 70 |
<?php echo esc_html( $stats_labels[ $key ] ); ?> |
| 71 |
</div> |
| 72 |
|
| 73 |
<div class="value"> |
| 74 |
<?php |
| 75 |
echo esc_html( $value ); |
| 76 |
?> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
|
| 80 |
<?php endforeach; |
| 81 |
} |
| 82 |
?> |
| 83 |
</div> |
| 84 |
|
| 85 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Recent Registrations', 'profile-builder' ); ?></h4> |
| 86 |
|
| 87 |
<div class="wppb-dashboard-registrations"> |
| 88 |
<?php $recent_registrations = wppb_recent_registrations(); ?> |
| 89 |
|
| 90 |
<?php if( !empty( $recent_registrations ) ): ?> |
| 91 |
<?php foreach( $recent_registrations as $user_data ): ?> |
| 92 |
<div class="wppb-dashboard-registrations__row"> |
| 93 |
<a href="<?php echo esc_url( add_query_arg( array( 'user_id' => $user_data['id'] ), admin_url( 'user-edit.php' ) ) ); ?>"> |
| 94 |
<?php printf( esc_html__( '%1s registered as a %2s role', 'profile-builder' ), esc_html( $user_data['display_name'] ), esc_html( $user_data['role'] ) ); ?> |
| 95 |
</a> |
| 96 |
<div class="wppb-dashboard-registrations__date"> |
| 97 |
<?php printf( '%1s', esc_html( $user_data['reg_date'] ) ) ?> |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
<?php endforeach; ?> |
| 101 |
<?php endif; ?> |
| 102 |
</div> |
| 103 |
|
| 104 |
<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'users.php' ) ); ?>"><?php esc_html_e( 'View All Users', 'profile-builder' ); ?></a> |
| 105 |
</div> |
| 106 |
|
| 107 |
<div class="postbox cozmoslabs-form-subsection-wrapper wppb-dashboard-progress"> |
| 108 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Setup Progress Review', 'profile-builder' ); ?></h4> |
| 109 |
|
| 110 |
<?php WPPB_Setup_Wizard::output_progress_steps(); ?> |
| 111 |
|
| 112 |
<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=profile-builder-dashboard&subpage=wppb-setup' ) ); ?>"><?php esc_html_e( 'Open the Setup Wizard', 'profile-builder' ); ?></a> |
| 113 |
</div> |
| 114 |
|
| 115 |
<div class="postbox cozmoslabs-form-subsection-wrapper"> |
| 116 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Have a question? Not sure how to proceed?', 'profile-builder' ); ?><span class="dashicons dashicons-editor-help" style="color: #0F15B0;"> </span></h4> |
| 117 |
|
| 118 |
<p><strong><span class="dashicons dashicons-plus" style="color: #0F15B0;"></span><?php esc_html_e( ' Open a new ticket over at', 'profile-builder' ); ?></strong> |
| 119 |
|
| 120 |
<br> |
| 121 |
|
| 122 |
<a href="https://wordpress.org/support/plugin/profile-builder/" target="_blank" style="display:block;padding-left:24px;margin-top:4px;">https://wordpress.org/support/plugin/profile-builder/</a></p> |
| 123 |
|
| 124 |
<p><strong><span class="dashicons dashicons-welcome-write-blog" style="color: #0F15B0;"></span><?php esc_html_e( ' Describe your problem:', 'profile-builder' ); ?></strong></p> |
| 125 |
|
| 126 |
<ul style="padding-left:24px;"> |
| 127 |
<li><?php esc_html_e( 'What you tried to do', 'profile-builder' ); ?></li> |
| 128 |
<li><?php esc_html_e( 'What you expected to happen', 'profile-builder' ); ?></li> |
| 129 |
<li><?php esc_html_e( 'What actually happened', 'profile-builder' ); ?></li> |
| 130 |
<li><?php printf( esc_html__( 'Screenshots help. Use a service like %1s snipboard.io %2s and share the link.', 'profile-builder' ), '<a href="https://snipboard.io/">', '</a>' ); ?></li> |
| 131 |
</ul> |
| 132 |
|
| 133 |
<p><strong><span class="dashicons dashicons-yes" style="color: #0F15B0;"></span><?php esc_html_e( 'Get help from our team', 'profile-builder' ); ?></strong></p> |
| 134 |
|
| 135 |
</div> |
| 136 |
|
| 137 |
<div class="postbox cozmoslabs-form-subsection-wrapper"> |
| 138 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Useful shortcodes for setup', 'profile-builder' ); ?></h4> |
| 139 |
|
| 140 |
<p class="wppb-dashboard-shortcodes__description"><?php esc_html_e( 'Use these shortcodes to quickly setup and customize your membership website.', 'profile-builder' ); ?></p> |
| 141 |
|
| 142 |
<div class="wppb-dashboard-shortcodes"> |
| 143 |
<div class="wppb-dashboard-shortcodes__row"> |
| 144 |
<div class="wppb-dashboard-shortcodes__row__wrap"> |
| 145 |
<div class="label"><?php esc_html_e( 'Register', 'profile-builder' ); ?></div> |
| 146 |
<p><?php esc_html_e( 'Add registration forms where users can sign-up and enter their initial details.', 'profile-builder' ); ?></p> |
| 147 |
</div> |
| 148 |
|
| 149 |
<div title='Click to copy' class="wppb-shortcode_copy-text wppb-dashboard-shortcodes__row__input"> |
| 150 |
[wppb-register] |
| 151 |
</div> |
| 152 |
<span style='display: none; margin-left: 10px' class='wppb-copy-message'><?php esc_html_e( 'Shortcode copied', 'profile-builder' ); ?></span> |
| 153 |
</div> |
| 154 |
<div class="wppb-dashboard-shortcodes__row"> |
| 155 |
<div class="wppb-dashboard-shortcodes__row__wrap"> |
| 156 |
<div class="label"><?php esc_html_e( 'Login', 'profile-builder' ); ?></div> |
| 157 |
<p><?php esc_html_e( 'Allow members to login.', 'profile-builder' ); ?></p> |
| 158 |
</div> |
| 159 |
|
| 160 |
<div title='Click to copy' class="wppb-shortcode_copy-text wppb-dashboard-shortcodes__row__input"> |
| 161 |
[wppb-login] |
| 162 |
</div> |
| 163 |
<span style='display: none; margin-left: 10px' class='wppb-copy-message'><?php esc_html_e( 'Shortcode copied', 'profile-builder' ); ?></span> |
| 164 |
</div> |
| 165 |
<div class="wppb-dashboard-shortcodes__row"> |
| 166 |
<div class="wppb-dashboard-shortcodes__row__wrap"> |
| 167 |
<div class="label"><?php esc_html_e( 'Edit Profile', 'profile-builder' ); ?></div> |
| 168 |
<p><?php esc_html_e( 'Allow members to edit their account information.', 'profile-builder' ); ?></p> |
| 169 |
</div> |
| 170 |
|
| 171 |
<div title='Click to copy' class="wppb-shortcode_copy-text wppb-dashboard-shortcodes__row__input"> |
| 172 |
[wppb-edit-profile] |
| 173 |
</div> |
| 174 |
<span style='display: none; margin-left: 10px' class='wppb-copy-message'><?php esc_html_e( 'Shortcode copied', 'profile-builder' ); ?></span> |
| 175 |
</div> |
| 176 |
<div class="wppb-dashboard-shortcodes__row"> |
| 177 |
<div class="wppb-dashboard-shortcodes__row__wrap"> |
| 178 |
<div class="label"><?php esc_html_e( 'Restrict Content', 'profile-builder' ); ?></div> |
| 179 |
<p><?php esc_html_e( 'Restrict pieces of content on individual posts and pages based on user role.', 'profile-builder' ); ?></p> |
| 180 |
</div> |
| 181 |
|
| 182 |
<div title='Click to copy' class="wppb-shortcode_copy-text wppb-dashboard-shortcodes__row__input"> |
| 183 |
[wppb-restrict user_roles="subscriber"] |
| 184 |
</div> |
| 185 |
<span style='display: none; margin-left: 10px' class='wppb-copy-message'><?php esc_html_e( 'Shortcode copied', 'profile-builder' ); ?></span> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
|
| 189 |
<a class="button button-secondary" href="https://www.cozmoslabs.com/docs/profile-builder/shortcodes/?utm_source=pb-dashboard&utm_medium=client-site&utm_campaign=pb-shortcodes" target="_blank"><?php esc_html_e( 'Learn more about shortcodes', 'profile-builder' ); ?></a> |
| 190 |
</div> |
| 191 |
</div> |
| 192 |
|
| 193 |
</div> |
| 194 |
<?php |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Get Dashboard stats |
| 200 |
* |
| 201 |
*/ |
| 202 |
function wppb_get_dashboard_stats(){ |
| 203 |
check_admin_referer( 'wppb_dashboard_get_stats' ); |
| 204 |
|
| 205 |
if( !current_user_can( 'manage_options' ) ) |
| 206 |
die(); |
| 207 |
|
| 208 |
if( empty( $_POST['interval'] ) ) |
| 209 |
return; |
| 210 |
|
| 211 |
$interval = sanitize_text_field( $_POST['interval'] ); |
| 212 |
$return = array( |
| 213 |
'success' => true, |
| 214 |
'data' => array(), |
| 215 |
); |
| 216 |
|
| 217 |
// generate filter data |
| 218 |
$args = array(); |
| 219 |
|
| 220 |
if( $interval == 'this_month' ){ |
| 221 |
|
| 222 |
$args['interval'][] = date( 'Y-m-01', time() ); |
| 223 |
$args['interval'][] = date( 'Y-m-d', time() ); |
| 224 |
|
| 225 |
} else if( $interval == 'last_month' ){ |
| 226 |
|
| 227 |
$args['interval'][] = date( 'Y-m-01', strtotime( '-1 month' ) ); |
| 228 |
$args['interval'][] = date( 'Y-m-t', strtotime( '-1 month' ) ); |
| 229 |
|
| 230 |
} else if( $interval == 'this_year' ){ |
| 231 |
|
| 232 |
$args['interval'][] = date( 'Y-01-01', time() ); |
| 233 |
$args['interval'][] = date( 'Y-m-d', time() ); |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
$return['data'] = wppb_users_stats( $args ); |
| 238 |
|
| 239 |
echo json_encode( $return ); |
| 240 |
die(); |
| 241 |
} |
| 242 |
add_action( 'wp_ajax_wppb_get_dashboard_stats', 'wppb_get_dashboard_stats' ); |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Get User stats |
| 247 |
* |
| 248 |
*/ |
| 249 |
function wppb_users_stats( $args = array() ) { |
| 250 |
|
| 251 |
$total_users = count_users(); |
| 252 |
$users_stats = array( |
| 253 |
'all_users' => $total_users['total_users'], |
| 254 |
'newly_registered' => '', |
| 255 |
); |
| 256 |
|
| 257 |
if ( empty( $args ) ) { |
| 258 |
$reg_start_date = date('Y-m-d', strtotime('-30 days')); |
| 259 |
$reg_end_date = date('Y-m-d' ); |
| 260 |
} |
| 261 |
else { |
| 262 |
$reg_start_date = $args['interval'][0]; |
| 263 |
$reg_end_date = $args['interval'][1]; |
| 264 |
} |
| 265 |
|
| 266 |
$query_args = array( |
| 267 |
'date_query' => array( |
| 268 |
array( |
| 269 |
'after' => $reg_start_date, |
| 270 |
'before' => $reg_end_date, |
| 271 |
'inclusive' => true, |
| 272 |
), |
| 273 |
), |
| 274 |
); |
| 275 |
|
| 276 |
$user_query = new WP_User_Query( $query_args ); |
| 277 |
$users_stats['newly_registered'] = $user_query->get_total(); |
| 278 |
|
| 279 |
return $users_stats; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/** |
| 284 |
* Get the Labels for User stats |
| 285 |
* |
| 286 |
*/ |
| 287 |
function wppb_users_stats_labels() { |
| 288 |
$labels = array( |
| 289 |
'all_users' => __( 'All Users', 'profile-builder' ), |
| 290 |
'newly_registered' => __( 'New Registered Users', 'profile-builder' ), |
| 291 |
); |
| 292 |
return $labels; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Get recently registered Users |
| 298 |
* |
| 299 |
*/ |
| 300 |
function wppb_recent_registrations( $args = array() ) { |
| 301 |
|
| 302 |
$query_args = array( |
| 303 |
'orderby' => 'user_registered', |
| 304 |
'order' => 'DESC', |
| 305 |
'number' => 5, |
| 306 |
); |
| 307 |
|
| 308 |
$user_query = new WP_User_Query( $query_args ); |
| 309 |
|
| 310 |
$users_stats = array(); |
| 311 |
|
| 312 |
if( !empty( $user_query->results ) ){ |
| 313 |
foreach ( $user_query->results as $user_data ) { |
| 314 |
$user_roles = wppb_user_role_names( $user_data->roles ); |
| 315 |
|
| 316 |
$user_info = array( |
| 317 |
'id' => $user_data->ID, |
| 318 |
'display_name' => $user_data->display_name, |
| 319 |
'role' => $user_roles, |
| 320 |
'reg_date' => $user_data->user_registered, |
| 321 |
); |
| 322 |
|
| 323 |
$users_stats[] = $user_info; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
return $users_stats; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
/** |
| 333 |
* Get a list of User Role Names out of Role Slugs |
| 334 |
* |
| 335 |
*/ |
| 336 |
function wppb_user_role_names( $role_slugs ) { |
| 337 |
if ( empty( $role_slugs ) ) |
| 338 |
return ''; |
| 339 |
|
| 340 |
$role_names = array(); |
| 341 |
foreach ( $role_slugs as $slug ) { |
| 342 |
$role_names[] = wp_roles()->get_names()[$slug]; |
| 343 |
} |
| 344 |
|
| 345 |
if ( !empty( $role_names ) ) |
| 346 |
$role_names = implode(', ', $role_names); |
| 347 |
|
| 348 |
return $role_names; |
| 349 |
} |
| 350 |
|