Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Created August 23, 2018 00:02
Show Gist options
  • Save pbrocks/4ad876721abd46d3e07d4191b9d51a41 to your computer and use it in GitHub Desktop.
Save pbrocks/4ad876721abd46d3e07d4191b9d51a41 to your computer and use it in GitHub Desktop.
Build a dashboard menu with a help tab to see different ways to get levels using Paid Memberships Pro.
<?php // Don't include in Customizations plugin
/**
* Plugin Name: PMPro Primer
* Description: A dashboard menu, `pmpro_primer_page`, with some helpful PMPro functions.
* Author: pbrocks
* Version: 1.0.1
* Text-Domain: pmpro-primer-menu
*/
/**
* Add a page to the primer menu.
*
* @since 1.0.0
*
* @return array
*/
add_action( 'admin_menu', 'pmpro_primer' );
function pmpro_primer() {
global $this_menu_slug;
$this_menu_slug = preg_replace( '/_+/', '-', __FUNCTION__ );
$label = ucwords( preg_replace( '/_+/', ' ', __FUNCTION__ ) );
$pmpro_primer_screen = add_dashboard_page( __( $label, 'pmpro-primer-menu' ), __( $label, 'pmpro-primer-menu' ), 'manage_options', $this_menu_slug . '.php', 'pmpro_primer_page' );
// Adds my_help_tab when primer_page loads
add_action( 'load-' . $pmpro_primer_screen, 'add_primer_help_tab' );
}
function add_primer_help_tab() {
$screen = get_current_screen();
$help_1_id = 'using_pmpro_array';
$help_1_title = ucwords( preg_replace( '/_+/', ' ', $help_1_id ) );
$sample_output = get_sample_pmpro_levels_code();
$sample_description = 'This is a quick sample of what you can do with the code on this page. Copy and paste this into your code to see how it works.';
$screen->add_help_tab(
array(
'id' => $help_1_id,
'title' => __( $help_1_title ),
'content' => '<h4>' . __( $help_1_title ) . '</h4>' .
'<p class="description">' . esc_html( $sample_description ) .
'</p>' .
'<xmp>
$array = get_pmpro_levels_array();
foreach ( $array as $key => $value ) {
echo \'Level \' . $value[\'id\'] . \' is \' . $value[\'name\'] . \' costs $\' . $value[\'billing_amount\'] . \' <br> \';
}
</xmp>
' . 'Returns this: <p>' . $sample_output . '</p>',
)
);
$screen->add_help_tab(
array(
'id' => 'primer_help_2',
'title' => __( 'Primer Help 2' ),
'content' => '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
)
);
$screen->add_help_tab(
array(
'id' => 'primer_help_3',
'title' => __( 'Primer Help 3' ),
'content' => '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
)
);
}
/**
* Debug Information
*
* @since 1.0.0
*
* @param bool $html Optional. Return as HTML or not
*
* @return
*/
function pmpro_primer_page() {
global $level, $pmpro_levels;
echo '<div class="wrap">';
echo '<h2>' . ucwords( preg_replace( '/_+/', ' ', __FUNCTION__ ) ) . '</h2>';
$screen = get_current_screen();
echo '<h4>get_pmpro_levels_object()</h4>';
echo '<pre>';
print_r( get_pmpro_levels_object() );
echo '</pre>';
echo '<h4>get_pmpro_levels_array()</h4>';
echo '<pre>';
print_r( get_pmpro_levels_array() );
echo '</pre>';
echo '<h4>get_pmpro_levels_shortlist()</h4>';
echo '<pre>';
print_r( get_pmpro_levels_shortlist() );
echo '</pre>';
$array = get_pmpro_levels_array();
foreach ( $array as $key => $value ) {
echo 'Level ' . $value['id'] . ' is ' . $value['name'] . ' costs $' . $value['billing_amount'] . '<br>';
}
$this_plugin = get_plugin_data( __FILE__ );
echo '<h4>This plugin is ' . sprintf(
__( '%1$s, version %2$s, is %3$s.', 'pmpro-primer-menu' ),
$this_plugin['Name'],
$this_plugin['Version'],
$this_plugin['Description']
) . '</h4>';
// $pmprolevels = the_pmpro_levels_array( $pmpro_levels );
echo 'Plugin info $level <pre>';
print_r( $level );
echo '</pre>';
echo '</div>';
}
/**
* Description
*
* @return object
*/
function get_pmpro_levels_object() {
global $wpdb;
$existing_levels = $wpdb->get_results(
"
SELECT * FROM $wpdb->pmpro_membership_levels
"
);
return $existing_levels;
}
/**
* Description
*
* @return array
*/
function get_pmpro_levels_array() {
$existing_levels = get_pmpro_levels_object();
foreach ( $existing_levels as $level_key => $level_value ) {
foreach ( $level_value as $key => $value ) {
$pmpro_level_list[ $level_key ][ $key ] = $value;
}
}
return $pmpro_level_list;
}
function get_sample_pmpro_levels_code() {
$array = get_pmpro_levels_array();
foreach ( $array as $key => $value ) {
$sample .= 'Level ' . $value['id'] . ' is ' . $value['name'] . ' costs $' . $value['billing_amount'] . '<br>';
}
return $sample;
}
/**
* Description
*
* @return string
*/
function get_pmpro_levels_shortlist() {
$existing_levels = get_pmpro_levels_object();
$pmpro_level_list = '<ul>';
foreach ( $existing_levels as $key => $value ) {
$pmpro_level_list .= '<li>Level ' . $value->id . ' => ' . $value->name . '</li>';
}
$pmpro_level_list .= '<ul>';
return $pmpro_level_list;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pmpro_primer_action_links' );
/**
* Function to add links to the plugin action links
*
* @param array $links Array of links to be shown in plugin action links.
*/
function pmpro_primer_action_links( $links ) {
global $this_menu_slug;
if ( current_user_can( 'manage_options' ) ) {
$new_links = array(
'<a href="' . get_admin_url( null, 'index.php?page=' . $this_menu_slug ) . '">' . __( 'View primer', 'pmpro-primer-menu' ) . '</a>',
);
}
return array_merge( $new_links, $links );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment