Skip to content

Commit

Permalink
Merge pull request #59 from kimcoleman/settings-page-ui
Browse files Browse the repository at this point in the history
Settings UI improvements; fixing text domain; fixing JS error
  • Loading branch information
ideadude authored Jul 2, 2024
2 parents 53cc2be + ab96864 commit 9e853a1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 47 deletions.
61 changes: 46 additions & 15 deletions adminpages/limitpostviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,37 @@
require_once( PMPRO_DIR . '/adminpages/admin_header.php' );

/**
* Display membership limits section.
* Display non-member limits section.
*
* @since 0.3.0
* @since TBD
*/
function pmprolpv_settings_section_limits() {
echo '<p>' . esc_html( __( 'Allow visitors or members limited access to view posts they do not already have access to view.', 'pmpro-limit-post-views' ) ) . '</p>';
function pmprolpv_settings_section_non_member_limits() { ?>
<div id="pmprolpv-non-member-limits" class="pmpro_section_toggle" data-visibility="hidden" data-activated="false">
<button class="pmpro_section-toggle-button" type="button" aria-expanded="false">
<span class="dashicons dashicons-arrow-up-alt2"></span>
<?php esc_html_e( 'Non-Member Post Views', 'pmpro-limit-post-views' ); ?>
</button>
</div>
<div class="pmpro_section_inside">
<p><?php esc_html_e( 'Give non-members limited access to view protected posts. This setting includes site visitors and logged-in users without an active membership level.', 'pmpro-limit-post-views' ); ?></p>
<?php
}

/**
* Display member limits section.
*
* @since TBD
*/
function pmprolpv_settings_section_member_limits() { ?>
<div id="pmprolpv-member-limits" class="pmpro_section_toggle" data-visibility="hidden" data-activated="false">
<button class="pmpro_section-toggle-button" type="button" aria-expanded="false">
<span class="dashicons dashicons-arrow-down-alt2"></span>
<?php esc_html_e( 'Member Post Views', 'pmpro-limit-post-views' ); ?>
</button>
</div>
<div class="pmpro_section_inside" style="display: none;">
<p><?php esc_html_e( 'Give members limited access to view posts that they do not already have access to view.', 'pmpro-limit-post-views' ); ?></p>
<?php
}

/**
Expand All @@ -32,10 +57,10 @@ function pmprolpv_settings_section_limits() {
function pmprolpv_settings_field_limits( $level_id ) {
$limit = pmprolpv_get_level_limit( $level_id );

$period = ( !empty( $limit['period'] ) ) ? $limit['period'] : '';
$period = ( !empty( $limit['period'] ) ) ? $limit['period'] : 'month';
$views = ( !empty( $limit['views'] ) ) ? $limit['views'] : '';
?>
<input size="2" type="text" id="level_<?php echo esc_attr( $level_id ); ?>_views"
<input size="2" type="number" id="level_<?php echo esc_attr( $level_id ); ?>_views"
name="pmprolpv_limit_<?php echo esc_attr( $level_id ); ?>[views]" value="<?php echo esc_attr( $views ); ?>">
<?php esc_html_e( ' views per ', 'pmpro-limit-post-views' ); ?>
<select name="pmprolpv_limit_<?php echo esc_attr( $level_id ); ?>[period]" id="level_<?php echo esc_attr( $level_id ); ?>_period">
Expand All @@ -56,7 +81,15 @@ function pmprolpv_settings_field_limits( $level_id ) {
*
* @since 0.3.0
*/
function pmprolpv_settings_section_redirection() {
function pmprolpv_settings_section_redirection() { ?>
<div id="pmprolpv-redirection" class="pmpro_section_toggle" data-visibility="hidden" data-activated="false">
<button class="pmpro_section-toggle-button" type="button" aria-expanded="false">
<span class="dashicons dashicons-arrow-up-alt2"></span>
<?php esc_html_e( 'Redirection Settings', 'pmpro-limit-post-views' ); ?>
</button>
</div>
<div class="pmpro_section_inside">
<?php
}

/**
Expand All @@ -74,19 +107,17 @@ function pmprolpv_settings_field_redirect_page() {
}

wp_dropdown_pages( array(
'selected' => $page_id,
'selected' => esc_html( $page_id ),
'name' => 'pmprolpv_redirect_page',
));
) );

echo '<p class="description">' . esc_html( __( 'Set the page to redirect users to once they have reached their view limit.', 'pmpro-limit-post-views' ) ) . '</p>';

}

// Display settings page.
?>
<h1><?php esc_html_e( 'Limit Post Views Add On', 'pmpro-limit-post-views' ); ?></h1>
<hr />
<h2><?php esc_html_e( 'How this Plugin Works', 'pmpro-limit-post-views' );?></h2>
<p><?php _e( "Users who visit a post and don't have access to it will be allowed to view the post as long as they haven't reached their limits set below. Once their limit is reached, they will be redirected to the page set below.", 'pmpro-limit-post-views'); ?>
<p><?php printf( __( "By default, this plugin does not work with pages or other Custom Post Types. You can <a href='%s'>apply Limit Post Views to other post types by following the instructions here</a>.", 'pmpro-limit-post-views' ), 'https://www.paidmembershipspro.com/offer-limited-access-to-restricted-page-or-custom-post-type-content-using-the-limit-post-views-add-on/' ); ?></p>
<hr />
<h1><?php esc_html_e( 'Limit Post Views Settings', 'pmpro-limit-post-views' ); ?></h1>
<form action="options.php" method="POST">
<?php settings_fields( 'pmpro-limitpostviews' ); ?>
<?php do_settings_sections( 'pmpro-limitpostviews' ); ?>
Expand Down
63 changes: 49 additions & 14 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function pmprolpv_admin_menu() {
$cap = apply_filters( 'pmpro_edit_member_capability', 'manage_options' );

if( version_compare( PMPRO_VERSION, '2.0' ) >= 0 ) {
add_submenu_page( 'pmpro-dashboard', __( 'Limit Post Views', 'pmpro-limit-post-views' ), __( 'Limit Post View', 'pmpro-limit-post-views' ), $cap, 'pmpro-limitpostviews', 'pmprolpv_settings_page' );
add_submenu_page( 'pmpro-dashboard', __( 'Limit Post Views', 'pmpro-limit-post-views' ), __( 'Limit Post Views', 'pmpro-limit-post-views' ), $cap, 'pmpro-limitpostviews', 'pmprolpv_settings_page' );
} else {
add_submenu_page( 'pmpro-membershiplevels', __( 'Limit Post Views', 'pmpro-limit-post-views' ), __( 'Limit Post View', 'pmpro-limit-post-views' ), $cap, 'pmpro-limitpostviews', 'pmprolpv_settings_page' );
add_submenu_page( 'pmpro-membershiplevels', __( 'Limit Post Views', 'pmpro-limit-post-views' ), __( 'Limit Post Views', 'pmpro-limit-post-views' ), $cap, 'pmpro-limitpostviews', 'pmprolpv_settings_page' );
}
}
add_action( 'admin_menu', 'pmprolpv_admin_menu' );
Expand All @@ -44,26 +44,61 @@ function pmprolpv_settings_page() {
*/
function pmprolpv_admin_init() {
if ( function_exists( 'pmpro_getAllLevels' ) ) {
// Register limits settings section.
// Register non-member limits settings section.
add_settings_section(
'pmprolpv_limits',
'Membership Post View Limits',
'pmprolpv_settings_section_limits',
'pmpro-limitpostviews'
'pmprolpv_non_member_limits',
'',
'pmprolpv_settings_section_non_member_limits',
'pmpro-limitpostviews',
array(
'before_section' => '<div class="pmpro_section">',
'after_section' => '</div></div>',
),
);

// Register member limits settings section.
add_settings_section(
'pmprolpv_member_limits',
'',
'pmprolpv_settings_section_member_limits',
'pmpro-limitpostviews',
array(
'before_section' => '<div class="pmpro_section">',
'after_section' => '</div></div>',
),
);

// Register redirection settings section.
add_settings_section(
'pmprolpv_redirection',
'Redirection',
'',
'pmprolpv_settings_section_redirection',
'pmpro-limitpostviews'
'pmpro-limitpostviews',
array(
'before_section' => '<div class="pmpro_section">',
'after_section' => '</div></div>',
),
);

// Register non-member limit settings field.
add_settings_field(
'pmprolpv_limit_0',
__( 'Non-members', 'pmpro-limit-post-views' ),
'pmprolpv_settings_field_limits',
'pmpro-limitpostviews',
'pmprolpv_non_member_limits',
0
);

// Register JavaScript setting.
register_setting(
'pmpro-limitpostviews',
'pmprolpv_limit_0',
'pmprolpv_sanitize_limit'
);

// Register limits settings fields.
// Register member limit settings field.
$levels = pmpro_getAllLevels( true, true );
$levels[0] = new stdClass();
$levels[0]->name = __( 'Non-members', 'pmpro' );
asort( $levels );
foreach ( $levels as $id => $level ) {
$title = $level->name;
Expand All @@ -72,7 +107,7 @@ function pmprolpv_admin_init() {
$title,
'pmprolpv_settings_field_limits',
'pmpro-limitpostviews',
'pmprolpv_limits',
'pmprolpv_member_limits',
$id
);

Expand All @@ -87,7 +122,7 @@ function pmprolpv_admin_init() {
// Register redirection settings field.
add_settings_field(
'pmprolpv_redirect_page',
'Redirect to',
__( 'Redirect to', 'pmpro-limit-post-views' ),
'pmprolpv_settings_field_redirect_page',
'pmpro-limitpostviews',
'pmprolpv_redirection'
Expand Down
2 changes: 1 addition & 1 deletion includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
function pmprolpv_get_level_limit( $level_id ) {
$default_option = array(
'views' => '',
'period' => 'Month',
'period' => 'month',
);
return get_option( 'pmprolpv_limit_' . $level_id, $default_option );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function pmprolpv_has_membership_access_filter( $has_access, $post ) {
* @since 1.0
*/
function pmprolpv_wp_enqueue_scripts() {
wp_register_script( 'pmprolpv', plugins_url( 'js/pmprolpv.js', PMPROLPV_BASE_FILE ), array( 'jquery' ), PMPROLPV_VERSION );
wp_register_script( 'pmprolpv', plugins_url( 'js/pmprolpv.js', PMPROLPV_BASE_FILE ), array( 'jquery' ), PMPROLPV_VERSION, array( 'in_footer' => true ) );
wp_localize_script( 'pmprolpv', 'pmprolpv', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'pmprolpv' );
}
Expand Down
30 changes: 16 additions & 14 deletions js/pmprolpv.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
jQuery( document ).ready( function( $ ) {
// Make an AJAX call to the pmprolpv_get_restriction_js action.
$.ajax( {
url: pmprolpv.ajaxurl + '?action=pmprolpv_get_restriction_js',
type: 'POST',
data: {
url: window.location.href
},
success: function( response ) {
// If the response is not empty, eval it.
$.ajax( {
url: pmprolpv.ajaxurl + '?action=pmprolpv_get_restriction_js',
type: 'POST',
data: {
url: window.location.href
},
success: function( response ) {
// If the response is not empty, eval it.
console.log(response);
if ( response.success ) {
eval( response.data );
}
}
} );
} );
if (response.success) {
// Wrap the response data in a function to avoid illegal return statement
var wrappedCode = '(function() {' + response.data + '})();';
eval(wrappedCode);
}
}
} );
} );
4 changes: 2 additions & 2 deletions pmpro-limit-post-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function pmprolpv_plugin_action_links( $links ) {
function pmprolpv_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-limit-post-views.php' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/pmpro-limit-post-views/' ) . '" title="' . esc_attr( __( 'View Documentation', 'limit-post-views' ) ) . '">' . __( 'Docs', 'limit-post-views' ) . '</a>',
'<a href="' . esc_url( 'http://paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'limit-post-views' ) ) . '">' . __( 'Support', 'limit-post-views' ) . '</a>',
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/pmpro-limit-post-views/' ) . '" title="' . esc_attr( __( 'View Documentation', 'pmpro-limit-post-views' ) ) . '">' . __( 'Docs', 'pmpro-limit-post-views' ) . '</a>',
'<a href="' . esc_url( 'http://paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro-limit-post-views' ) ) . '">' . __( 'Support', 'pmpro-limit-post-views' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
Expand Down

0 comments on commit 9e853a1

Please sign in to comment.