Skip to content

Commit

Permalink
Added flag for lite plan users. Added checks to avoid asking tiers fr…
Browse files Browse the repository at this point in the history
…om Patreon api if creator is on lite plan. Will prevent 401 errors and constant checks. Now will only check creator tiers when creators load post editor page after the initial check done in installation/first run. Modified the info text in Patreon level metabox to inform lite users about not being able to use tiers. Patreon level metabox info text now links users the page to upgrade their Patreon plan if they are on lite plan and they want to use tiers. Added checks to text above locked post interface and text in valid patron footer to not use tiers in interface text if creator is on lite plan. Now uses $ amount if creator is on lite plan. Added logic to override tier matching over currently_entitled_tiers with currently_entitled_amount_cents for patrons of lite plan creators. Prevented Patreon connection wizard nag from appearing to non-admins in WP admin.
  • Loading branch information
codebard committed Sep 22, 2020
1 parent 2e05b23 commit 19c39b2
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 123 deletions.
215 changes: 115 additions & 100 deletions classes/patreon_frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,67 +367,76 @@ public static function getLabelOverUniversalButton( $patreon_level, $args = fals
}

$creator_url .= $append_with . $utm_params;

// Get Patreon creator tiers

$tiers = get_option( 'patreon-creator-tiers', false );

foreach( $tiers['included'] as $key => $value ) {

// If its not a reward element, continue, just to make sure

if(
!isset( $tiers['included'][$key]['type'] )
OR ( $tiers['included'][$key]['type'] != 'reward'
AND $tiers['included'][$key]['type'] != 'tier' )
) {
continue;
}

$reward = $tiers['included'][$key];

// Special conditions for label for element 0, which is 'everyone' and '1, which is 'patron only'

if ( $reward['id'] == -1 ) {
$tier_title = PATREON_TEXT_EVERYONE;
}
if ( $reward['id'] == 0 ) {
$tier_title = PATREON_TEXT_ANY_PATRON;
}

if ( ( $reward['attributes']['amount_cents'] / 100 ) >= $patreon_level ) {

if ( get_option( 'patreon-creator-has-tiers', 'yes' ) == 'yes' ) {

// Get Patreon creator tiers

$tiers = get_option( 'patreon-creator-tiers', false );

// Matching level was present, but now found. Set selected and toggle flag.
// selected = selected for XHTML compatibility
foreach( $tiers['included'] as $key => $value ) {

// Use title if it exists, description if it does not.
// If its not a reward element, continue, just to make sure

if ( isset( $reward['attributes']['title'] ) ) {
$tier_title = $reward['attributes']['title'];
if(
!isset( $tiers['included'][$key]['type'] )
OR ( $tiers['included'][$key]['type'] != 'reward'
AND $tiers['included'][$key]['type'] != 'tier' )
) {
continue;
}

if ( $tier_title == '' ) {

/// Detect if this is an old non bas64 desc
if (base64_decode($reward['attributes']['description'], true) === false) {
$tier_title = $reward['attributes']['description'];
}
else {
$tier_title = base64_decode( $reward['attributes']['description'] );
}
$reward = $tiers['included'][$key];

// Special conditions for label for element 0, which is 'everyone' and '1, which is 'patron only'

if ( $reward['id'] == -1 ) {
$tier_title = PATREON_TEXT_EVERYONE;
}

// If the title is too long, snip it
if ( strlen( $tier_title ) > 23 ) {
$tier_title = substr( $tier_title , 0 , 23 ) .'...';
if ( $reward['id'] == 0 ) {
$tier_title = PATREON_TEXT_ANY_PATRON;
}

if ( ( $reward['attributes']['amount_cents'] / 100 ) >= $patreon_level ) {


// Matching level was present, but now found. Set selected and toggle flag.
// selected = selected for XHTML compatibility

// Use title if it exists, description if it does not.

if ( isset( $reward['attributes']['title'] ) ) {
$tier_title = $reward['attributes']['title'];
}

if ( $tier_title == '' ) {

/// Detect if this is an old non bas64 desc
if (base64_decode($reward['attributes']['description'], true) === false) {
$tier_title = $reward['attributes']['description'];
}
else {
$tier_title = base64_decode( $reward['attributes']['description'] );
}
}

$tier_title = '"' . $tier_title . '"';

break;
}
// If the title is too long, snip it
if ( strlen( $tier_title ) > 23 ) {
$tier_title = substr( $tier_title , 0 , 23 ) .'...';
}

$tier_title = '"' . $tier_title . '"';

break;
}

}

}
else {
// Creator has no tiers, possibly lite plan. Override text here.

$tier_title = '';
}

// Exception - when content is locked for 'any patron' and the user is not a patron, the interface text shows $0.01. For this special case, check and manipulate the chosen label to avoid this:
Expand Down Expand Up @@ -1209,67 +1218,73 @@ public static function MakeValidPatronFooter( $patreon_level, $user_patronage, $
}


// Get Patreon creator tiers
if ( get_option( 'patreon-creator-has-tiers', 'yes' ) == 'yes' ) {

$tiers = get_option( 'patreon-creator-tiers', false );

foreach( $tiers['included'] as $key => $value ) {

// If its not a reward element, continue, just to make sure

if(
!isset( $tiers['included'][$key]['type'] )
OR ( $tiers['included'][$key]['type'] != 'reward'
AND $tiers['included'][$key]['type'] != 'tier' )
) {
continue;
}

$reward = $tiers['included'][$key];

// Special conditions for label for element 0, which is 'everyone' and '1, which is 'patron only'
// Get Patreon creator tiers

$tiers = get_option( 'patreon-creator-tiers', false );

if ( $reward['id'] == -1 ) {
$tier_title = PATREON_TEXT_EVERYONE;
}
if ( $reward['id'] == 0 ) {
$tier_title = PATREON_TEXT_ANY_PATRON;
}

if ( ( $reward['attributes']['amount_cents'] / 100 ) >= $patreon_level ) {

// Matching level was present, but now found. Set selected and toggle flag.
// selected = selected for XHTML compatibility
foreach( $tiers['included'] as $key => $value ) {

// Use title if it exists, description if it does not.
// If its not a reward element, continue, just to make sure

if ( isset( $reward['attributes']['title'] ) ) {
$tier_title = $reward['attributes']['title'];
if(
!isset( $tiers['included'][$key]['type'] )
OR ( $tiers['included'][$key]['type'] != 'reward'
AND $tiers['included'][$key]['type'] != 'tier' )
) {
continue;
}

$reward = $tiers['included'][$key];

if ( $tier_title == '' ) {
// Special conditions for label for element 0, which is 'everyone' and '1, which is 'patron only'

if ( $reward['id'] == -1 ) {
$tier_title = PATREON_TEXT_EVERYONE;
}
if ( $reward['id'] == 0 ) {
$tier_title = PATREON_TEXT_ANY_PATRON;
}

if ( ( $reward['attributes']['amount_cents'] / 100 ) >= $patreon_level ) {

// Matching level was present, but now found. Set selected and toggle flag.
// selected = selected for XHTML compatibility

// Use title if it exists, description if it does not.

/// Detect if this is an old non bas64 desc
if (base64_decode($reward['attributes']['description'], true) === false) {
$tier_title = $reward['attributes']['description'];
}
else {
$tier_title = base64_decode( $reward['attributes']['description'] );
if ( isset( $reward['attributes']['title'] ) ) {
$tier_title = $reward['attributes']['title'];
}

if ( $tier_title == '' ) {

/// Detect if this is an old non bas64 desc
if (base64_decode($reward['attributes']['description'], true) === false) {
$tier_title = $reward['attributes']['description'];
}
else {
$tier_title = base64_decode( $reward['attributes']['description'] );
}
}
}

// If the title is too long, snip it
if ( strlen( $tier_title ) > 23 ) {
$tier_title = substr( $tier_title , 0 , 23 ) .'...';
// If the title is too long, snip it
if ( strlen( $tier_title ) > 23 ) {
$tier_title = substr( $tier_title , 0 , 23 ) .'...';
}

$tier_title = '"' . $tier_title . '"';

break;
}

$tier_title = '"' . $tier_title . '"';

break;
}

}
}

else {
$tier_title = '';
}

// Get creator url

$creator_url = get_option( 'patreon-creator-url', false );
Expand Down
57 changes: 43 additions & 14 deletions classes/patreon_metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,30 @@ function patreon_plugin_meta_box( $object, $box ) {
$current_user = wp_get_current_user();

global $post;

$label = 'Require the below membership tier or higher to view this post. (Makes entire post patron only) <a href="https://www.patreondevelopers.com/t/patreon-wordpress-locking-options-guide/1135#heading--section-1?utm_source=' . urlencode( site_url() ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=&utm_content=post_locking_metabox_link_1&utm_term=" target="_blank">(?)</a>';

// Override messaging for lite plans

$creator_tiers = get_option( 'patreon-creator-tiers', false );

$advanced_post_options_toggle_status = get_user_meta( $current_user->ID, 'patreon-wordpress-advanced-options-toggle', true );

$no_tiers = false;

if ( !$creator_tiers OR $creator_tiers == '' OR !is_array( $creator_tiers['included'][1] ) ) {
$no_tiers = true;
}

if ( $no_tiers ) {

$label = 'Require the below membership tier or higher to view this post. (Makes entire post patron only) <a href="https://www.patreondevelopers.com/t/patreon-wordpress-locking-options-guide/1135#heading--section-1?utm_source=' . urlencode( site_url() ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=&utm_content=post_locking_metabox_link_1&utm_term=" target="_blank">(?)</a>';
$readonly = '';
$label = 'Patreon Lite plan does not support tiers. If you want to gate your content for different tiers <a href="https://www.patreon.com/edit/tiers?utm_source=' . urlencode( site_url() ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=&utm_content=post_locking_metabox_patreon_plan_upgrade_message&utm_term=" target="_blank">upgrade your Patreon plan to Pro and above here.</a>';

$advanced_post_options_toggle_status = 'on';

}

$readonly = '';
$disabled = '';

if ( !get_option( 'patreon-creator-id', false ) ) {
Expand All @@ -67,15 +87,17 @@ function patreon_plugin_meta_box( $object, $box ) {
<div id="patreon_level_select_wrapper"><select id="patreon_level_select" name="patreon-level"<?php echo $disabled ?> pw_post_id="<?php echo $object->ID; ?>"><option value="<?php echo get_post_meta( $object->ID, 'patreon-level', true ); ?>"><?php echo Patreon_Wordpress::make_tiers_select( $post ); ?></option></select> <img id="patreon_level_refresh" src="<?php echo PATREON_PLUGIN_ASSETS; ?>/img/refresh_tiers_18.png" style="width: 18px; height: 18px;" /></div>
</p>

<p> If you set a precise amount in advanced settings below, or had one set before, that will be used instead.
</p>


<?php

<?php
if ( !$no_tiers ) {
?>

<p> If you set a precise amount in advanced settings below, or had one set before, that will be used instead.</p>

<?php

}

$advanced_post_options_toggle_status = get_user_meta( $current_user->ID, 'patreon-wordpress-advanced-options-toggle', true );

$advanced_post_options_toggle_status_display = 'style=" display: block;" ';

if ( $advanced_post_options_toggle_status == '' OR $advanced_post_options_toggle_status == 'off' ) {
Expand All @@ -87,7 +109,14 @@ function patreon_plugin_meta_box( $object, $box ) {
<?php

$label = 'Require the below precise $ monthly membership or over to view this post. (optional - overrides the above select box when used) <a href="https://www.patreondevelopers.com/t/patreon-wordpress-locking-options-guide/1135#heading--section-11?utm_source=' . urlencode( site_url() ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=&utm_content=post_locking_metabox_link_2&utm_term=" target="_blank">(?)</a>';
$readonly = '';

$readonly = '';

if ( $no_tiers ) {

$label = 'Gate this content for the below amount. <a href="https://www.patreondevelopers.com/t/patreon-wordpress-locking-options-guide/1135#heading--section-11?utm_source=' . urlencode( site_url() ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=&utm_content=post_locking_metabox_link_2&utm_term=" target="_blank">(?)</a> Patreon will default it to $5, but your patrons can enter a custom amount while pledging at Patreon';

}

if ( !get_option( 'patreon-creator-id', false ) ) {

Expand All @@ -98,10 +127,10 @@ function patreon_plugin_meta_box( $object, $box ) {
?>

<p>
<label for="patreon-level-exact"><?php _e( $label, '1' ); ?></label>
<br><br>
<strong>&#36; </strong><input type="text" id="patreon-level-exact" name="patreon-level-exact" value="<?php echo get_post_meta( $object->ID, 'patreon-level', true ); ?>" <?php echo $readonly ?>>
</p>
<label for="patreon-level-exact"><?php _e( $label, '1' ); ?></label>
<br><br>
<strong>&#36; </strong><input type="text" id="patreon-level-exact" name="patreon-level-exact" value="<?php echo get_post_meta( $object->ID, 'patreon-level', true ); ?>" <?php echo $readonly ?>>
</p>

<?php

Expand Down
Loading

0 comments on commit 19c39b2

Please sign in to comment.