|
/** |
|
* Award myCRED Points based on Tags |
|
* This script will award points when a post is published based on |
|
* what tags a post has. The amount is defined in the post tag description field. |
|
* Requires myCRED 1.4 or higher! |
|
* @version 1.1 |
|
*/ |
|
add_action( 'transition_post_status', 'mycred_pro_points_by_post_tags', 10, 3 ); |
|
function mycred_pro_points_by_post_tags( $new_status, $old_status, $post ) { |
|
// Make sure myCRED is installed |
|
if ( ! function_exists( 'mycred' ) ) return; |
|
|
|
// Only award points when a "Post" gets published |
|
if ( $new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post' ) { |
|
|
|
// Load myCRED |
|
$mycred = mycred(); |
|
|
|
// If the post author is excluded from using myCRED, leave now |
|
if ( $mycred->exclude_user( $post->post_author ) ) return; |
|
|
|
// Get all the tags that are connected to this post |
|
$tags = wp_get_object_terms( $post->ID, 'post_tag' ); |
|
|
|
// If there are tags |
|
$total = 0; |
|
$tags_used = array(); |
|
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) { |
|
|
|
// loop though tags |
|
foreach ( $tags as $tag ) { |
|
|
|
// Value should be stored in description |
|
if ( empty( $tag->description ) ) continue; |
|
|
|
// A value is set, add this up |
|
$total = $total + $tag->description; |
|
|
|
// Save the id of this tag for the log |
|
$tags_used[] = $tag->term_id; |
|
|
|
} |
|
|
|
} |
|
|
|
// No need to continue if no tags with points were found |
|
if ( $total == 0 ) return; |
|
|
|
// Make sure value is formated for myCRED |
|
$total = $mycred->number( $total ); |
|
|
|
// Make sure points are only awarded once |
|
if ( $mycred->has_entry( 'publishing_content', $post->ID, $post->post_author ) ) return; |
|
|
|
// Award points |
|
$mycred->add_creds( |
|
'publishing_content', |
|
$post->post_author, |
|
$total, |
|
'Points for published post', |
|
$post->ID, |
|
array( |
|
'ref_type' => 'post', |
|
'tags' => implode( ',', $tags_used ) |
|
) |
|
); |
|
|
|
} |
|
} |