Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Register Custom Hook
- * @since 1.0
- * @version 1.0
- */
- add_filter( 'mycred_setup_hooks', 'mycred_pro_register_publish_content_category_hook' );
- function mycred_pro_register_publish_content_category_hook( $installed ) {
- $installed['publish_category_content'] = array(
- 'title' => __( '%plural% for Publishing Content (Categories)', 'mycred' ),
- 'description' => __( 'Award users points based on the category a post is published in.', 'mycred' ),
- 'callback' => array( 'myCRED_Publish_Content_Category' )
- );
- return $installed;
- }
- /**
- * Load Custom Hook
- * @version 1.0
- */
- add_action( 'mycred_load_hooks', 'mycred_pro_load_publish_content_category_hook' );
- function mycred_pro_load_publish_content_category_hook() {
- class myCRED_Publish_Content_Category extends myCRED_Hook {
- public $categories = array();
- /**
- * Construct
- */
- function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
- $this->categories = get_categories( array( 'hide_empty' => false ) );
- $defaults = array();
- foreach ( $this->categories as $cat )
- $defaults[ $cat->slug ] = array(
- 'creds' => 0,
- 'log' => '%plural% for publishing content',
- 'limit' => '0/x'
- );
- if ( isset( $hook_prefs['publish_category_content'] ) )
- $defaults = $hook_prefs['publish_category_content'];
- parent::__construct( array(
- 'id' => 'publish_category_content',
- 'defaults' => $defaults
- ), $hook_prefs, $type );
- }
- /**
- * Run
- * @version 1.0
- */
- public function run() {
- add_action( 'transition_post_status', array( $this, 'maybe_publish' ), 10, 3 );
- add_filter( 'mycred_all_references', array( $this, 'add_badge_support' ) );
- }
- /**
- * Add Badge Support
- * @version 1.0
- */
- public function add_badge_support( $references ) {
- foreach ( $this->categories as $cat ) {
- $ref = 'published_' . $cat->slug;
- if ( ! array_key_exists( $ref, $references ) )
- $references[ $ref ] = sprintf( 'Published content in %s', $cat->name );
- }
- return $references;
- }
- /**
- * Maybe Publish
- * @version 1.0
- */
- public function maybe_publish( $new_status, $old_status, $post ) {
- // Check for exclusions
- if ( $this->core->exclude_user( $post->post_author ) === true ) return;
- // We want to fire when content get published or when it gets privatly published
- $status = apply_filters( 'mycred_publish_hook_old', array( 'new', 'auto-draft', 'draft', 'private', 'pending' ) );
- $publish_status = apply_filters( 'mycred_publish_hook_new', array( 'publish', 'private' ) );
- // Make sure this is the right transition
- if ( in_array( $old_status, $status ) && in_array( $new_status, $publish_status ) ) {
- // Get all the categories the post belongs to in case we have more than one
- $assigned_categories = get_the_category( $post->ID );
- // Loop through all assigned categories
- if ( ! empty( $assigned_categories ) ) {
- foreach ( $assigned_categories as $category ) {
- // If we set a vale of zero, skip
- if ( ! array_key_exists( $category->slug, $this->prefs ) || $this->prefs[ $category->slug ]['creds'] == 0 ) continue;
- // Add this under a custom reference
- $reference = 'published_' . $category->slug;
- $data = array( 'ref_type' => 'post' );
- if ( ! $this->over_hook_limit( $category->slug, $reference, $post->post_author ) )
- $this->core->add_creds(
- $reference,
- $post->post_author,
- $this->prefs[ $category->slug ]['creds'],
- $this->prefs[ $category->slug ]['log'],
- $post->ID,
- $data,
- $this->mycred_type
- );
- }
- }
- }
- }
- /**
- * Preference for Viewing Content Hook
- * @version 1.0
- */
- public function preferences() {
- foreach ( $this->categories as $cat ) {
- $prefs = shortcode_atts( array(
- 'creds' => 0,
- 'log' => '%plural% for publishing content',
- 'limit' => '0/x'
- ), ( ( array_key_exists( $cat->slug, $this->prefs ) ) ? $this->prefs[ $cat->slug ] : array() ) );
- ?>
- <div class="hook-instance">
- <h3><?php printf( 'Published in: %s', $cat->name ); ?></h3>
- <div class="row">
- <div class="col-lg-2 col-md-6 col-sm-12 col-xs-12">
- <div class="form-group">
- <label for="<?php echo $this->field_id( array( $cat->slug => 'creds' ) ); ?>"><?php _e( 'Amount', 'mycred' ); ?></label>
- <input type="text" name="<?php echo $this->field_name( array( $cat->slug => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $cat->slug => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['creds'] ); ?>" class="form-control" />
- </div>
- </div>
- <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
- <div class="form-group">
- <label for="<?php echo $this->field_id( array( $cat->slug, 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
- <?php echo $this->hook_limit_setting( $this->field_name( array( $cat->slug, 'limit' ) ), $this->field_id( array( $cat->slug, 'limit' ) ), $prefs['limit'] ); ?>
- </div>
- </div>
- <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">
- <div class="form-group">
- <label for="<?php echo $this->field_id( array( $cat->slug => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
- <input type="text" name="<?php echo $this->field_name( array( $cat->slug => 'log' ) ); ?>" id="<?php echo $this->field_id( array( $cat->slug => 'log' ) ); ?>" placeholder="<?php _e( 'required', 'mycred' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="form-control" />
- <span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
- </div>
- </div>
- </div>
- </div>
- <?php
- }
- }
- /**
- * Sanitise Preferences
- * @version 1.1
- */
- public function sanitise_preferences( $data ) {
- foreach ( $this->categories as $category ) {
- if ( isset( $data[ $category->slug ]['limit'] ) && isset( $data[ $category->slug ]['limit_by'] ) ) {
- $limit = sanitize_text_field( $data[ $category->slug ]['limit'] );
- if ( $limit == '' ) $limit = 0;
- $data[ $category->slug ]['limit'] = $limit . '/' . $data[ $category->slug ]['limit_by'];
- unset( $data[ $category->slug ]['limit_by'] );
- }
- }
- return $data;
- }
- }
- }
Add Comment
Please, Sign In to add comment