• Resolved Fee

    (@wdfee)


    Hello Gabriel,
    thanks for your wonderful plugin – it’s really fun!
    I tried to create a custom hook for Favorites in bbPress 2.0 forums: if somebody adds the topic to his favorites, the topic author should get points.
    I followed your tutorial for custom hooks, but didn’t get it working. The hook settings show up in wp admin, but when somebody favorites a topic, no points are added.
    What do you think: would you be interested to add this functionality to the core of your plugin?
    Otherwise – would you mind taking a look at my code if I did something wrong?

    add_filter( 'mycred_setup_hooks', 'register_my_bbp_fav_hook' );
    function register_my_bbp_fav_hook( $installed )
    {
    	$installed['bbp_fav_topic'] = array(
    		'title'       => __( 'bbPress Favorites', 'mytheme' ),
    		'description' => __( 'Give points to topic author when topic is favorited.', 'mytheme' ),
    		'callback'    => array( 'my_bbp_fav_hook' )
    	);
    	return $installed;
    }
    
    if ( !class_exists( 'my_bbp_fav_hook' ) ) {
    	class my_bbp_fav_hook extends myCRED_Hook {
    
    		function __construct( $hook_prefs ) {
    			parent::__construct( array(
    				'id'       => 'bbp_fav_topic',
    				'defaults' => array(
    					'fav_topic' => array(
    						'creds'   => 1,
    						'log'     => '%plural% for someone favorited your forum topic'
    					)
    				)
    			), $hook_prefs );
    		}
    
    		public function run() {
    			add_action( 'bbp_add_user_favorite',  array( $this, 'fav_topic' ) );
    		}
    
    		/**
    		 * Check if the user qualifies for points
    		 */
    		public function fav_topic( $user_id, $topic_id ) {
    			// $user_id is loggedin_user, not author
    
    			// get topic author
    			$topic_author = get_post_field( 'post_author', $topic_id );
    
    			// Check if user is excluded (required)
    			if ( $this->core->exclude_user( $topic_author ) ) return;
    
    			// Make sure this is a unique event
    			if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return;
    
    			// Execute
    			$this->core->add_creds(
    				'topic_favorited',
    				$topic_author,
    				$this->prefs['fav_topic']['creds'],
    				$this->prefs['fav_topic']['log'],
    				$topic_id
    			);
    
    			// Clean up
    			//unset( $this );
    		}
    
    		/**
    		 * Add Settings
    		 */
    		 public function preferences() {
    			// Our settings are available under $this->prefs
    			$prefs = $this->prefs; ?>
    
    	<!-- First we set the amount -->
    	<label class="subheader"><?php echo $this->core->plural(); ?></label>
    	<ol>
    		<li>
    			<div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo $this->core->format_number( $prefs['creds'] ); ?>" size="8" /></div>
    		</li>
    	</ol>
    	<!-- Then the log template -->
    	<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
    	<ol>
    		<li>
    			<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" class="long" /></div>
    		</li>
    	</ol>
    	<?php
    		}
    
    		/**
    		 * Sanitize Preferences
    		 */
    		public function sanitise_preferences( $data ) {
    			$new_data = $data;
    
    			// Apply defaults if any field is left empty
    			$new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
    			$new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
    
    			return $new_data;
    		}
    	}
    }

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

Viewing 15 replies - 1 through 15 (of 22 total)
  • Plugin Author myCred

    (@designbymerovingi)

    Hey!

    Looking at your code, I see one error.

    Change:

    add_action( 'bbp_add_user_favorite', array( $this, 'fav_topic' ) );

    to this

    add_action( 'bbp_add_user_favorite', array( $this, 'fav_topic' ), 10, 2 );

    This is because in your fav_topic method you are asking for 2 arguments but by default add_action returns the first one only unless you tell it that there is more then one. The number 10 stands for the priority and 10 is the default. For more info have a look at add_action() in the WP Codex.

    Since the second argument is the topic_id, you are most likelly getting an error when you call get_post_field().

    Lets start by changing this and see if that solves the issue. If not, we do some more troubleshooting.

    Thread Starter Fee

    (@wdfee)

    Hey Gabriel,
    thanks for your answer!
    No, it doesn’t solve it yet. I think the function run() isn’t fired at all. I tried the following for testing purposes (added the hook to new_reply, too):

    public function run() {
    add_action( 'bbp_new_reply', array( $this, 'fav_topic' ), 20, 5 );
    add_action( 'bbp_add_user_favorite', array( $this, 'fav_topic'), 10, 2 );
    }

    and I changed the function fav_topic for testing like this:

    public function fav_topic( $user_id, $topic_id ) {
    // get topic author
    $topic_author = get_post_field( 'post_author', $topic_id );
    update_user_meta( $topic_author, 'bbp_fav_topic_test_yes', $topic_id );
    }

    If I put the function fav_topic directly in my thmes functions.php with the same hook, it’s fired. So somehow the class is not acting.
    add_action( 'bbp_add_user_favorite', 'fav_topic', 10, 2 );

    Plugin Author myCred

    (@designbymerovingi)

    When you add your custom hook to myCRED you use the ID “bbp_fav_topic”, but when you award points and try to get the amount and log template you use the reference “fav_topic”. This must be the same as your settings is saved under bbp_fav_topic. So to get the saved amount, you need to use $this->prefs[‘bbp_fav_topic’][‘creds’] and $this->prefs[‘bbp_fav_topic’][‘log’].

    Since there is no value under $this->prefs[‘fav_topic’], you are probably trying to add points that does not exist. myCRED will not award points if the amount is zero or not a number at all.

    So your execution should be:

    $this->core->add_creds(
    	'topic_favorited',
    	$topic_author,
    	$this->prefs['bbp_fav_topic']['creds'],
    	$this->prefs['bbp_fav_topic']['log'],
    	$topic_id
    );
    Plugin Author myCred

    (@designbymerovingi)

    To see if the run() method executes you could add the following:

    update_option( 'test_to_see_if_run_runs', 'it does' );

    to the run method. Then on any page, you can check for the value:

    echo get_option( 'test_to_see_if_run_runs' );

    If you get no result, the run method is not fired. If it is, the above code should echo “it does”.

    Thread Starter Fee

    (@wdfee)

    uhm, I thought I followed the logic…

    I tried now to changed (core hacked) your plugins-file bp-press.php – and there it works. Maybe you want to adapt it for your next version?

    <?php
    if ( !defined( 'myCRED_VERSION' ) ) exit;
    /**
     * myCRED_BuddyPress_bbPress class
     *
     * Creds for bbPress 2.0
     * @since 0.1
     * @version 1.0
     */
    if ( !class_exists( 'myCRED_BuddyPress_bbPress' ) ) {
    	class myCRED_BuddyPress_bbPress extends myCRED_Hook {
    
    		/**
    		 * Construct
    		 */
    		function __construct( $hook_prefs ) {
    			parent::__construct( array(
    				'id'       => 'hook_bp_bbpress',
    				'defaults' => array(
    					'new_topic' => array(
    						'creds'    => 1,
    						'log'      => '%plural% for new forum topic'
    					),
    					'new_reply' => array(
    						'creds'    => 1,
    						'log'      => '%plural% for new forum reply'
    					),
    					'fav_topic' => array(
    						'creds'   => 1,
    						'log'     => '%plural% for someone favorited your forum topic'
    					)
    				)
    			), $hook_prefs );
    		}
    
    		/**
    		 * Run
    		 * @since 0.1
    		 * @version 1.0
    		 */
    		public function run() {
    			if ( $this->prefs['new_topic']['creds'] != 0 )
    				add_action( 'bbp_new_topic', array( $this, 'new_topic' ), 20, 4 );
    
    			if ( $this->prefs['new_reply']['creds'] != 0 )
    				add_action( 'bbp_new_reply', array( $this, 'new_reply' ), 20, 5 );
    
    			if ( $this->prefs['fav_topic']['creds'] != 0 )
    			add_action( 'bbp_add_user_favorite', array( $this, 'fav_topic'), 10, 2 );
    		}
    
    		/**
    		 * New Topic
    		 * @since 0.1
    		 * @version 1.0
    		 */
    		public function new_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
    			// Check if user is excluded
    			if ( $this->core->exclude_user( $topic_author ) ) return;
    
    			// Make sure this is unique event
    			if ( $this->has_entry( 'new_forum_topic', $topic_id, $topic_author ) ) return;
    
    			// Execute
    			$this->core->add_creds(
    				'new_forum_topic',
    				$topic_author,
    				$this->prefs['new_topic']['creds'],
    				$this->prefs['new_topic']['log'],
    				$topic_id,
    				array( 'ref_type' => 'post' )
    			);
    
    			// Clean up
    			unset( $this );
    		}
    
    		/**
    		 * New Reply
    		 * @since 0.1
    		 * @version 1.0
    		 */
    		public function new_reply( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ) {
    			// Check if user is excluded
    			if ( $this->core->exclude_user( $reply_author ) ) return;
    
    			// Make sure this is unique event
    			if ( $this->has_entry( 'new_forum_reply', $reply_id, $reply_author ) ) return;
    
    			// Execute
    			$this->core->add_creds(
    				'new_forum_reply',
    				$reply_author,
    				$this->prefs['new_reply']['creds'],
    				$this->prefs['new_reply']['log'],
    				$reply_id,
    				array( 'ref_type' => 'post' )
    			);
    
    			// Clean up
    			unset( $this );
    		}
    
    		/**
    		 * Favorited Topic
    		 * @since 1.2
    		 * @version 1.2
    		 */
    		public function fav_topic( $user_id, $topic_id ) {
    
    			// get topic author
    			$topic_author = get_post_field( 'post_author', $topic_id );
    			update_user_meta( $topic_author, 'bbp_fav_topic_test_yes', $topic_id );
    
    			// Check if user is excluded (required)
    			if ( $this->core->exclude_user( $topic_author ) ) return;
    
    			// Make sure this is a unique event
    			if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return;
    
    			// Execute
    			$this->core->add_creds(
    				'topic_favorited',
    				$topic_author,
    				$this->prefs['fav_topic']['creds'],
    				$this->prefs['fav_topic']['log'],
    				$topic_id
    			);
    
    			// Clean up
    			unset( $this );
    		}
    
    		/**
    		 * Preferences
    		 * @since 0.1
    		 * @version 1.0
    		 */
    		public function preferences() {
    			$prefs = $this->prefs; ?>
    
    					<!-- Creds for New Topic -->
    					<label for="<?php echo $this->field_id( array( 'new_topic', 'creds' ) ); ?>" class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for New Topic', 'mycred' ) ); ?></label>
    					<ol id="">
    						<li>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_topic', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_topic', 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['new_topic']['creds'] ); ?>" size="8" /></div>
    						</li>
    						<li class="empty">&nbsp;</li>
    						<li>
    							<label for="<?php echo $this->field_id( array( 'new_topic', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_topic', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_topic', 'log' ) ); ?>" value="<?php echo $prefs['new_topic']['log']; ?>" class="long" /></div>
    							<span class="description"><?php _e( 'Available template tags: General, Post', 'mycred' ); ?></span>
    						</li>
    					</ol>
    					<!-- Creds for New Reply -->
    					<label for="<?php echo $this->field_id( array( 'new_reply', 'creds' ) ); ?>" class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for New Reply', 'mycred' ) ); ?></label>
    					<ol id="">
    						<li>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_reply', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_reply', 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['new_reply']['creds'] ); ?>" size="8" /></div>
    						</li>
    						<li class="empty">&nbsp;</li>
    						<li>
    							<label for="<?php echo $this->field_id( array( 'new_reply', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_reply', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_reply', 'log' ) ); ?>" value="<?php echo $prefs['new_reply']['log']; ?>" class="long" /></div>
    							<span class="description"><?php _e( 'Available template tags: General, Post', 'mycred' ); ?></span>
    						</li>
    					</ol>
    					<!-- Creds for Favorited Topic -->
    					<label for="<?php echo $this->field_id( array( 'fav_topic', 'creds' ) ); ?>" class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for Favorited Topic', 'mycred' ) ); ?></label>
    					<ol id="">
    						<li>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'fav_topic', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'fav_topic', 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['fav_topic']['creds'] ); ?>" size="8" /></div>
    						</li>
    						<li class="empty">&nbsp;</li>
    						<li>
    							<label for="<?php echo $this->field_id( array( 'fav_topic', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
    							<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'fav_topic', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'fav_topic', 'log' ) ); ?>" value="<?php echo $prefs['fav_topic']['log']; ?>" class="long" /></div>
    							<span class="description"><?php _e( 'Available template tags: General, Post', 'mycred' ); ?></span>
    						</li>
    					</ol>
    <?php		unset( $this );
    		}
    	}
    }
    ?>

    To follow your advice, I stepped back and changed bbp_fav_topic to fav_topic everywhere, but it didn’t help. So I would be very glad if you take the extended code to your next plugin version πŸ™‚

    Thread Starter Fee

    (@wdfee)

    no, run method isn’t fired, using your test (I also took a look into the database)

    Thread Starter Fee

    (@wdfee)

    oh, of course you’d have to take this line out:
    update_user_meta( $topic_author, 'bbp_fav_topic_test_yes', $topic_id );
    in my posted code. I added it for testing.

    Thread Starter Fee

    (@wdfee)

    Hey Gabriel,
    I changed the function a little bit, to exclude the topic_author getting points if he favorites his own topic:

    public function fav_topic( $user_id, $topic_id ) {
    
    			// get topic author
    			$topic_author = get_post_field( 'post_author', $topic_id );
    
    			// Check if user is excluded (required) or user is topic_author himself
    			if ( $this->core->exclude_user( $topic_author ) || $topic_author == $user_id ) return;
    
    			// Make sure this is a unique event
    			//if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return;
    
    			// Execute
    			$this->core->add_creds(
    				'topic_favorited',
    				$topic_author,
    				$this->prefs['fav_topic']['creds'],
    				$this->prefs['fav_topic']['log'],
    				$topic_id,
    				array( 'ref_user' => $user_id )
    			);
    
    			// Clean up
    			unset( $this );
    		}

    instead of checking for a unique event if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return; I’d like to check if the sam user already gave a favorite. Therefore I added the ref_user to the data field. Is there already a function to check it directly?

    Plugin Author myCred

    (@designbymerovingi)

    Arg.

    I have to appologize. You did correct in regards of the ID. I got confused and mixed the hook id with the points id.

    When you edit this hooks settings, are your input fields empty? You see, you have defined the default settings for the instance “fav_topic”. If you look at the default myCRED Hooks, when a hook has just once instance the default values are in the default array. When we have multiple instances we have several arrays in our default array. One array per instance.

    Let me show you:

    // Single instance default settings:
    'defaults' => array(
    	'creds'   => 1,
    	'log'     => '%plural% for logging in',
    	'limit'   => 'daily'
    )
    // Accessing this single instance in settings
    <div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo $this->core->format_number( $prefs['creds'] ); ?>" size="8" /></div>
    
    // Multiple instances
    'defaults' => array(
    	'post'    => array(
    		'creds'  => 1,
    		'log'    => '%plural% for new Post'
    	),
    	'page'    => array(
    		'creds'  => 1,
    		'log'    => '%plural% for new Page'
    	)
    )
    // Accessing one of the multiple instance values
    <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'post' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'post' => 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['post']['creds'] ); ?>" size="8" /></div>

    You see I am passing an array to the field_name() and field_id() functions. Same when I request the saved value under $prefs[‘instance_id’][‘creds’];

    I hope it makes sense.

    I have updated your original version and you can find it here (pastebin is better for large amounts of code).

    Since myCRED presents you with the hook settings, it should have no problem running the run() function either.

    Also, just in case, you are not adding a topic to favorites from an account that is excluded from using myCRED right?

    Also, I would comment out the “exclude check” and the “check to make sure it is unique” just in case and just let it give points each time the hook fires for troubleshooting.

    Plugin Author myCred

    (@designbymerovingi)

    Updated pastebin code with your change (we posted the same time it seems )

    Plugin Author myCred

    (@designbymerovingi)

    If the code does not work from your functions.php file, try to move it into the bottom of the mycred/modules/mycred-module-hooks.php file. If it does work from there then the run() method is at fault. Im thinking it might run to early (before your theme is loaded).

    Thread Starter Fee

    (@wdfee)

    ok, the code didn’t work neither and it seems that the problem starts in the settings. I tried it in mycred/modules/mycred-module-hooks.php there it works and now I can see the difference in settings:
    – first testing from functions.php: the fields in settings were empty.
    – second testings from bp-press.php: the fields in settings seemed to take the predefinition from new_topic.
    – third testing from mycred/modules/mycred-module-hooks.php: the fields in settings are correct like defined in __construct.

    Plugin Author myCred

    (@designbymerovingi)

    Ok so if I understand you correctly, while in the mycred/modules/mycred-modules-hooks.php, the settings were correct? If you change any of these settings and hit save, are these settings saved correctly?

    Thread Starter Fee

    (@wdfee)

    no, I’ve to revise it. I changed now ‘fav_topic’ to ‘topic_fav’, also the hook name and so on, that no already made settings come in between, then I tried it again from functions.php. Now the fields in settings are correct like defined in __construct. But adding points this way still doesn’t work. also still no result of run method (checking options).
    the settings were always saved correctly.

    Plugin Author myCred

    (@designbymerovingi)

    Ok, as I mentioned before i think the issue is with the run() method.

    In mycred-modules-hooks.php on line 43, change:

    public function module_pre_init() {

    to

    public function module_init() {

    This will move the run method from plugins_loaded to init.

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Points for bbPress topic author when topic is favorited’ is closed to new replies.