WordPress.org

Ready to get started?Download WordPress

Forums

CubePoints
Cubepoints And Custom Post Types (19 posts)

  1. VegasKev88
    Member
    Posted 1 year ago #

    I have a few custom post types and I'd like to give members the ability to earn points for their posts within custom post types. Is there a way to accomplish this?

    http://wordpress.org/extend/plugins/cubepoints/

  2. Gabriel
    Member
    Posted 1 year ago #

    You might already have found a solution for this but you can just add a custom module to CubePoints to accomplish what you are looking for.

    Here is a module for you:

    <?php
    /** Points for Publishing of Multiple Custom Post Type Module */
    cp_module_register( 
    
    	__( 'Publish Custom Post Types', 'cp' ),
    	'mulcustomposttypepoints',
    	'1.0',
    	'dbm',
    	'http://www.merovingi.com',
    	'http://www.merovingi.com',
    	__('This module awards points for publishing custom post types.', 'cp' ), 1 );
    
    if ( cp_module_activated( 'mulcustomposttypepoints' ) ) {
    
    	/** Module Configuration */
    	add_action( 'cp_config_form', 'cp_multi_customtypes_admin_setting' );
    	function cp_multi_customtypes_admin_setting() {
    
    		echo '
    		<br />
    		<h3>Custom Post Types</h3>
    
    		<table class="form-table">
    			<tr valign="top">
    				<th scope="row"><label for="cp_customtype_used">All post types give same points?</label></th>
    				<td valign="middle">
    					<select id="cp_multi_option" name="cp_multi_option">' . "\n";
    
    				$multiple_types = get_option( 'cp_multi_type' );
    
    				// defaults to yes
    				if ( empty( $multiple_types ) ) $multiple_types = 'yes';
    				$our_option = array( 'yes', 'no' );
    				foreach ( $our_option as $option ) {
    					echo '<option value="' . $option . '"';
    					if ( $option == $multiple_types ) echo ' selected="selected"';
    					echo '>  ' . ucfirst( $option ) . '</option>' . "\n";
    				}
    		echo '
    					</select>
    				</td>
    			</tr>';
    
    		// If all custom post types are given the same points we only want to know one thing, how much.
    		if ( $multiple_types == 'yes' || empty( $multiple_types ) ) {
    			echo '
    			<tr valign="top">
    				<th scope="row"><label for="cp_customtype_points">Points for Publishing a custom post type:</label></th>
    				<td valign="middle"><input type="text" id="cp_multi_type_points" name="cp_multi_type_points" value="' . get_option( 'cp_multi_type_points' ) . '" size="30" /></td>
    			</tr>';
    		}
    		// Otherwise we list all custom post types with its own points
    		else {
    			$post_types = cp_module_get_custom_post_types( 'names' );
    			foreach ( $post_types  as $post_type ) {
    				$post_type_points = get_option( 'cp_mctype_' . $post_type );
    				$name = 'cp_mctype_' . $post_type;
    				echo '
    			<tr valign="top">
    					<th scope="row"><label for="cp_customtype_points">Publishing <code>' . $post_type . '</code>:</label></th>
    				<td valign="middle"><input type="text" id="' . $name . '" name="' . $name . '" value="' . $post_type_points . '" size="30" /></td>
    			</tr>';
    			}
    		}
    		$remove_settings = get_option( 'cp_multi_remove_points' );
    		echo '
    			<tr valign="top">
    				<th scope="row"><label for="cp_customtype_points">Remove Points:</label></th>
    				<td valign="middle">
    					<input type="checkbox" id="cp_multi_unpublish" name="cp_multi_remove_points" value="1" ';
    					checked( $remove_settings, 1 ); echo ' />
    					<span class="description">Remove points if a </span><code>Published</code><span class="description"> item gets changed to </span><code>Draft</code><span class="description"> or </span><code>Pending</code>
    				</td>
    			</tr>
    		</table>';
    	}
    
    	/** Save Module Congif */
    	add_action( 'cp_config_process', 'cp_multi_customtypes_save' );
    	function cp_multi_customtypes_save() {
    		// If all post types get the same points
    		if ( $_POST['cp_multi_option'] == 'yes' ) {
    			update_option( 'cp_multi_type', 'yes' );
    			update_option( 'cp_multi_type_points', (int)$_POST['cp_multi_type_points'] );
    		} else {
    			update_option( 'cp_multi_type', 'no' );
    			delete_option( 'cp_multi_type_points' );
    		}
    
    		update_option( 'cp_multi_remove_points', $_POST['cp_multi_remove_points'] );
    
    		// Delete any custom points previously used.
    		$post_types = cp_module_get_custom_post_types( 'names' );
    		if ( $_POST['cp_multi_option'] == 'yes' ) {
    			foreach ( $post_types  as $post_type ) {
    				delete_option( 'cp_mctype_' . $post_type );
    			}
    		} else {
    			foreach ( $post_types  as $post_type ) {
    				update_option( 'cp_mctype_' . $post_type, (int)$_POST['cp_mctype_' . $post_type] );
    			}
    		}
    	}
    
    	// Get custom post types
    	function cp_module_get_custom_post_types( $type = 'names' ) {
    
    		$args = array(
    			'public'   => true,
    			'_builtin' => false
    		);
    		$post_types = get_post_types( $args, $type, 'and' );
    
    		return $post_types;
    	}
    
    	// Check if we have already given a post points
    	function cp_module_already_got_points( $post_id, $post_type ) {
    
    		global $wpdb;
    		$count = (int) $wpdb->get_var("select count(id) from <code>&quot; . CP_DB . &quot;</code> where <code>type</code>='$post_type' and <code>data</code>=" . $post_id );
    
    		if ( $count == 0 )
    			return false;
    		else
    			return true;
    	}
    
    	// Check if we want to remove points on unpublishing
    	function cp_module_remove_points_unpublish() {
    		if ( get_option( 'cp_multi_remove_points' ) )
    			return true;
    		else
    			return false;
    	}
    
    	// Hook for each registered custom post type
    	// Add an action for each post type you want to give points for
    	add_action( 'publish_book', 'cp_module_publish_post_type' );
    	//add_action( 'publish_posttype', 'cp_module_publish_post_type' );
    
    	// When a published item goes back to "draft" or "pending" we remove points until they get published again.
    	if ( cp_module_remove_points_unpublish() ) {
    		add_action( 'publish_to_draft', 'cp_module_multi_type_remove_points' );
    		add_action( 'publish_to_pending', 'cp_module_multi_type_remove_points' );
    	}
    
    	// When we add points
    	function cp_module_publish_post_type( $post_id ) {
    
    		// If we dont want to remove points on unpublishing and points has already been given, bail.
    		if ( !cp_module_remove_points_unpublish() && cp_module_already_got_points() ) return;
    
    		$post_type = get_post_type( $post_id );
    		if ( $post_type != 'post' ) {
    			$post = get_post( $post_id );
    			$user_id = $post->post_author;
    			$post_type_rule = get_option( 'cp_multi_type' );
    
    			if ( $post_type_rule == 'yes' )
    				$points = get_option( 'cp_multi_type_points' );
    			else
    				$points = get_option( 'cp_mctype_' . $post_type );
    
    			cp_points( $post_type, $user_id, (int) $points, $post_id );
    		}
    	}
    
    	// When we remove posts
    	function cp_module_multi_type_remove_points( $post ) {
    
    		// get the posts post type
    		$post_id = $post->ID;
    		$post_type = get_post_type( $post_id );
    
    		if ( $post_type != 'post' ) {
    			// get our setting
    			$post_type_rule = get_option( 'cp_multi_type' );
    			// grab how many points we want to remove
    
    			if ( $post_type_rule == 'yes' ) $points_to_take = get_option( 'cp_multi_type_points' );
    			else $points_to_take = get_option( 'cp_mctype_' . $post_type );
    			cp_points( 'negative', $post->post_author, '-' . $points_to_take, $post_id );
    		}
    	}
    
    	/** Adjust the Log. Overwrite custom post type entries. */
    	add_action( 'cp_logs_description', 'cp_admin_logs_desc_multi_customtypes', 15, 4 );
    	function cp_admin_logs_desc_multi_customtypes( $type, $uid, $points, $data ) {
    		// Grab details about the post type and the post itself.
    		$post = get_post( $data );
    		if ( $post->post_type == $type && $type != 'post' ) {
    			$obj = get_post_type_object( $type );
    			echo 'Points given for ' . $obj->labels->singular_name . ': <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a>';
    		}
    		elseif ( $type == 'negative' ) {
    			echo 'Removed points for: <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a> for getting drafted or pending.';
    		}
    	}
    }
    ?>

    You will need to add a an action for each custom post type you want to use. In the above example I am giving points for the post type books.

  3. webwarrior.wng
    Member
    Posted 1 year ago #

    How could i use Cubepoint with Achievement plugin?
    My problem is if someone has increase his Cubepoints points upto 150 (it will be vary on the admin input) then that user will have some real award like a Dinner with someone special.
    Thanks!

  4. Gabriel
    Member
    Posted 1 year ago #

    Most plugins comes with hooks that you can take advantage off by hooking into them and manipulating CubePoints. Do a search for the plugin name and cubepoints. Could be that someone out there has already made a module for it.

  5. webwarrior.wng
    Member
    Posted 1 year ago #

    Thank you designbymerovingi, but i only found something which will add an action i mean addaction() function but it is not working well.
    Anyway, i have to know how the event is triggered in Achievement plugin so that it could be easier to make my own code as the need.

    Thanks

  6. Gabriel
    Member
    Posted 1 year ago #

    What is the name of the plugin your are using?

  7. shiftdel15589
    Member
    Posted 1 year ago #

    Hi, I use geoplaces4 at http://templatic.com/app-themes/geo-places-city-directory-wordpress-theme/.
    How to when user submit new place, they will get new points by this module ?

    Thanks.

  8. Gabriel
    Member
    Posted 1 year ago #

    shiftdel15589 please open a new post with your question, as this post is regarding "Cubepoints And Custom Post Types".

  9. webwarrior.wng
    Member
    Posted 1 year ago #

    Hi @shiftdel15589
    The below code will alter your points as much you want. If i want to reward the user bt 10 points, then i put this code exactly.

    • <?php cp_alterPoints(cp_currentUser(), 10); ?>

    You can add this code on click of that Submit New Place button.

  10. Alexandru Linca
    Member
    Posted 1 year ago #

    Using this module, and the solution is super good.
    but please i have a little problem:
    My users can edit the Custom Post and when they do they always receive points. Any idea how to limit this.

    Currently I can not move on myCRED :(
    myCRED it's better and does not have this problem.

  11. J.D. Grimes
    Member
    Posted 1 year ago #

    The problem is in the function. The forum software messed up the code.

    $count = (int) $wpdb->get_var("select count(id) from <code>" . CP_DB . "</code> where <code>type</code>='$post_type' and <code>data</code>=" . $post_id );

    Should be this:

    $count = (int) $wpdb->get_var("select count(id) from " . CP_DB . " where type='$post_type' and data=" . $post_id );

  12. Alexandru Linca
    Member
    Posted 1 year ago #

    User still get points every time I update that post :(

  13. Alexandru Linca
    Member
    Posted 1 year ago #

    I tried so:
    // Check if we have already given a post points
    function cp_module_already_got_points( $post_id, $post_type ) {

    global $wpdb;
    $count = (int) $wpdb->get_var("select count(id) from ".CP_DB." where type='post' and data=". $pid);
    if ( $count == 0 )
    return false;
    else
    return true;
    }

    I tried with: Multiple Custom Post Type Points

    I get the same problem with my Custom Post, do not understand that the posts have no problem.

  14. J.D. Grimes
    Member
    Posted 1 year ago #

    You see where it says where type = 'post'. That needs to be where type = '$post_type'. It is checking if a post of the type 'post' has been awarded points. You want to check for the post type passed to the function ($post_type).

  15. Alexandru Linca
    Member
    Posted 1 year ago #

    my mistake, I did try with the correct form:
    $count = (int) $wpdb->get_var("SELECT COUNT(ID) FROM ".CP_DB." where type='$post_type' and data=". $post_id);
    and it did not work.

  16. Gabriel
    Member
    Posted 1 year ago #

    Hey.

    I have made a new version of this module using the transition_post_status action instead of publish_$post_type. Try it out and see what you think. It should only award point when the custom post type gets published.

    Using pastebin instead of pasting code here to avoid any errors.
    http://pastebin.com/ZH4nYJba

  17. Alexandru Linca
    Member
    Posted 1 year ago #

    Gabriel you are the MAN! it WORKS
    I will certainly call on your services for my project.

    Wish you the best day possible.

  18. TribalDancer
    Member
    Posted 1 year ago #

    Can someone explain to me where I need to place this code? I am unclear as to where this fix is applied.

    And to be clear, this is to correct the problem whereby if a post is UPDATED it is assigning additional points to the author. And when applying the fix, it will check to see if the post is new or if it is simply updated, and will NOT apply points on any updates?

    I ask because my guest bloggers often need many edits and tweaks, and I want to make sure they get credit only the first time it is posted and not anytime after that during edits/updates.

    Thanks so much for any help you can provide!

  19. Gabriel
    Member
    Posted 1 year ago #

    Hey.

    1. Copy the code
    2. Create a new file and name is anything you want as long as it ends with .php
    3. Paste the code in and save.
    4. Upload this file to your websites wp-content/plugins/cubepoints/modules/ folder.
    4. Login to your website and visit the CubePoints Modules page.
    5. You should now see a new module called "Publish Custom Post Types". Enable it.
    6. Visit your Config page and apply your settings.
    7. Done.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.