Last active
August 29, 2015 14:07
-
-
Save gabrielmerovingi/078c6391c8ca9211a583 to your computer and use it in GitHub Desktop.
This custom shortcode allows you to set any post type for sale in myCRED. Make sure you use this shortcode on a page that only you can access, select the post type to set for sale and if you want to use a custom setup instead of your Sell Content default setup, enter these details. Any field that is left empty will be populated by your default s…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_shortcode( 'sell_all_posts', 'mycred_pro_sell_all_posts' ); | |
function mycred_pro_sell_all_posts() { | |
if ( ! is_user_logged_in() || ! current_user_can( 'edit_users' ) ) | |
return 'This shortcode is only available for admins.'; | |
ob_start(); | |
// Run | |
if ( isset( $_POST['token'] ) && wp_verify_nonce( $_POST['token'], 'mycred-sell-content-run' ) ) { | |
$post_type = sanitize_text_field( $_POST['type_to_sell'] ); | |
// Clean up values provided | |
$price = sanitize_text_field( $_POST['sale_price'] ); | |
$button = sanitize_text_field( $_POST['sale_button'] ); | |
$expire = sanitize_text_field( $_POST['sale_expire'] ); | |
global $wpdb; | |
// Get all published posts | |
$posts = $wpdb->get_col( $wpdb->prepare( " | |
SELECT DISTINCT ID | |
FROM {$wpdb->posts} | |
WHERE post_type = %s | |
AND post_status = %s;", $post_type, 'publish' ) ); | |
$count = 0; | |
// If we found posts | |
if ( $posts ) { | |
// Get Sell Content settings | |
$mycred = mycred(); | |
$prefs = $mycred->sell_content; | |
// Construct the setup | |
$sales_data = array( | |
'status' => 'enabled', | |
'price' => ( $price == '' ) ? $prefs['defaults']['price'] : $price, | |
'button_label' => ( $button == '' ) ? $prefs['defaults']['button_label'] : $button, | |
'expire' => ( $expire == '' ) ? $prefs['defaults']['expire'] : $expire | |
); | |
// Loop through the posts and update their setups. | |
foreach ( $posts as $post_id ) { | |
update_post_meta( $post_id, 'myCRED_sell_content', $sales_data ); | |
$count ++; | |
} | |
} | |
// Update for admin | |
echo '<p>A total of ' . $count . ' posts were successfully set for sale.</p>'; | |
} | |
$args = array( | |
'public' => true, | |
'_builtin' => false | |
); | |
$post_types = get_post_types( $args, 'objects', 'and' ); | |
?> | |
<form action="" method="post"> | |
<p> | |
<label>Post Type</label><br /> | |
<select name="type_to_sell"><?php | |
echo '<option value="post">Post</option>'; | |
echo '<option value="page">Page</option>'; | |
foreach ( $post_types as $type ) { | |
echo '<option value="' . $type->name . '">' . $type->labels->name . '</option>'; | |
} | |
?></select> | |
</p> | |
<p> | |
<label>Price</label><br /> | |
<input type="text" size="10" name="sale_price" value="" /><br /> | |
<em>Leave empty to use your default price.</em> | |
</p> | |
<p> | |
<label>Button Label</label><br /> | |
<input type="text" size="10" name="sale_button" value="" /><br /> | |
<em>Leave empty to use your default button label.</em> | |
</p> | |
<p> | |
<label>Expire</label><br /> | |
<input type="text" size="10" name="sale_expire" value="" /><br /> | |
<em>Leave empty to use your default expire setup.</em> | |
</p> | |
<p> | |
<input type="hidden" name="token" value="<?php echo wp_create_nonce( 'mycred-sell-content-run' ); ?>" /> | |
<input type="submit" class="button button-primary btn btn-primary" value="Run" /> | |
<a href="<?php echo admin_url( 'admin.php?page=myCRED_page_settings' ); ?>" class="button button-primary btn btn-primary">Sell Content Settings</a> | |
</p> | |
</form> | |
<?php | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment