Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active March 14, 2021 20:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/021c2a129e2e2db49eb3 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/021c2a129e2e2db49eb3 to your computer and use it in GitHub Desktop.
This custom myCRED hook allows you to award points for users publishing any post type on your website AND adds the option for you to award badges based on publishing specific post types.
/**
* Register Custom myCRED Hooks
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_setup_hooks', 'mycred_pro_register_publishposttype_hook' );
function mycred_pro_register_publishposttype_hook( $installed ) {
$installed['publish_posttypes'] = array(
'title' => 'Publishing Post Types',
'description' => 'Award %_plural% to users for publishing different post types.',
'callback' => array( 'myCRED_Hook_Publishing_Posttypes' )
);
return $installed;
}
/**
* Hook for publishing content types
* @since 1.0
* @version 1.0
*/
if ( ! class_exists( 'myCRED_Hook_Publishing_Posttypes' ) && class_exists( 'myCRED_Hook' ) ) {
class myCRED_Hook_Publishing_Posttypes extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
$defaults = array(
'disable' => 0,
'post' => array(
'creds' => 1,
'log' => '%plural% for new Post'
),
'page' => array(
'creds' => 1,
'log' => '%plural% for new Page'
)
);
if ( isset( $hook_prefs['publish_posttypes'] ) )
$defaults = $hook_prefs['publish_posttypes'];
parent::__construct( array(
'id' => 'publish_posttypes',
'defaults' => $defaults
), $hook_prefs, $type );
}
/**
* Run
* @since 1.0
* @version 1.0
*/
public function run() {
add_action( 'transition_post_status', array( $this, 'publishing_content' ), 10, 3 );
add_filter( 'mycred_all_references', array( $this, 'add_references' ) );
if ( $this->prefs['disable'] == 1 )
add_filter( 'mycred_setup_hooks', array( $this, 'hide_publish_content_hook' ), 9999, 2 );
}
/**
* Hide Publish Content Hook
* @since 1.0
* @version 1.0
*/
public function hide_publish_content_hook( $installed, $type ) {
// Only for this type
if ( $this->mycred_type != $type ) return $installed;
if ( isset( $installed['publishing_content'] ) )
unset( $installed['publishing_content'] );
return $installed;
}
/**
* Publish Content Hook
* @since 1.0
* @version 1.0
*/
public function publishing_content( $new_status, $old_status, $post ) {
$user_id = $post->post_author;
// Check for exclusions
if ( $this->core->exclude_user( $user_id ) === true ) return;
$post_id = $post->ID;
$post_type = $post->post_type;
// Make sure we award points other then zero
if ( ! isset( $this->prefs[ $post_type ]['creds'] ) ) return;
if ( empty( $this->prefs[ $post_type ]['creds'] ) || $this->prefs[ $post_type ]['creds'] == 0 ) 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' ) );
if ( in_array( $old_status, $status ) && in_array( $new_status, $publish_status ) && array_key_exists( $post_type, $this->prefs ) ) {
// Prep
$entry = $this->prefs[ $post_type ]['log'];
$data = array( 'ref_type' => 'post' );
// Make sure this is unique
if ( $this->core->has_entry( 'publishing_' . $post_type, $post_id, $user_id, $data, $this->mycred_type ) ) return;
// Add Creds
$this->core->add_creds(
'publishing_' . $post_type,
$user_id,
$this->prefs[ $post_type ]['creds'],
$entry,
$post_id,
$data,
$this->mycred_type
);
}
}
/**
* Register Custom myCRED References
* @since 1.0
* @version 1.0
*/
public function add_references( $references ) {
$post_type_args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $post_type_args, 'objects', 'and' );
foreach ( $post_types as $post_type ) {
// Start by checking if this post type should be excluded
if ( ! $this->include_post_type( $post_type->name ) ) continue;
$references[ 'publishing_' . $post_type->name ] = 'Publishing ' . $post_type->labels->name;
}
return $references;
}
/**
* Preference for Publish Post Types Hook
* @since 1.0
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs; ?>
<label class="subheader">&nbsp;</label>
<ol>
<li>
<label for="<?php echo $this->field_id( 'disable' ); ?>"><input type="checkbox" name="<?php echo $this->field_name( 'disable' ); ?>" id="<?php echo $this->field_id( 'disable' ); ?>"<?php checked( $prefs['disable'], 1 ); ?> value="1" /> <?php echo $this->core->template_tags_general( __( 'Hide the %plural% for publishing Content Hook', 'mycred' ) ); ?></label>
</li>
</ol>
<label class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for Posts', 'mycred' ) ); ?></label>
<ol>
<li>
<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->number( $prefs['post']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'post' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'post' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['post']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<label class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for Pages', 'mycred' ) ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'page' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'page' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['page']['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'page' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'page' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['page']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<?php
// Get all not built-in post types (excludes posts, pages, media)
$post_type_args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $post_type_args, 'objects', 'and' );
foreach ( $post_types as $post_type ) {
// Start by checking if this post type should be excluded
if ( ! $this->include_post_type( $post_type->name ) ) continue;
// Points to award/deduct
if ( isset( $prefs[ $post_type->name ]['creds'] ) )
$_creds = $prefs[ $post_type->name ]['creds'];
else
$_creds = 0;
// Log template
if ( isset( $prefs[ $post_type->name ]['log'] ) )
$_log = $prefs[ $post_type->name ]['log'];
else
$_log = ''; ?>
<label class="subheader"><?php echo sprintf( $this->core->template_tags_general( __( '%plural% for %s', 'mycred' ) ), $post_type->labels->name ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( $post_type->name => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $post_type->name => 'creds' ) ); ?>" value="<?php echo $this->core->number( $_creds ); ?>" size="8" /></div>
</li>
</ol>
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( $post_type->name => 'log' ) ); ?>" id="<?php echo $this->field_id( array( $post_type->name => 'log' ) ); ?>" value="<?php echo esc_attr( $_log ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<?php
}
}
/**
* Sanitise Preference
* @since 1.0
* @version 1.0
*/
public function sanitise_preferences( $data ) {
$new_data = $data;
$new_data['disable'] = ( isset( $data['disable'] ) ) ? $data['disable'] : 0;
return $new_data;
}
/**
* Include Post Type
* Checks if a given post type should be excluded
* @since 1.0
* @version 1.0
*/
public function include_post_type( $post_type ) {
// Exclude Core
$excludes = array( 'post', 'page' );
if ( in_array( $post_type, apply_filters( 'mycred_post_type_excludes', $excludes ) ) ) return false;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment