Guest User

Untitled

a guest
Oct 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. /**
  2. * Register Custom Hook
  3. * @since 1.0
  4. * @version 1.0
  5. */
  6. add_filter( 'mycred_setup_hooks', 'mycred_pro_register_publish_content_category_hook' );
  7. function mycred_pro_register_publish_content_category_hook( $installed ) {
  8.  
  9. $installed['publish_category_content'] = array(
  10. 'title' => __( '%plural% for Publishing Content (Categories)', 'mycred' ),
  11. 'description' => __( 'Award users points based on the category a post is published in.', 'mycred' ),
  12. 'callback' => array( 'myCRED_Publish_Content_Category' )
  13. );
  14.  
  15. return $installed;
  16.  
  17. }
  18.  
  19. /**
  20. * Load Custom Hook
  21. * @version 1.0
  22. */
  23. add_action( 'mycred_load_hooks', 'mycred_pro_load_publish_content_category_hook' );
  24. function mycred_pro_load_publish_content_category_hook() {
  25.  
  26. class myCRED_Publish_Content_Category extends myCRED_Hook {
  27.  
  28. public $categories = array();
  29.  
  30. /**
  31. * Construct
  32. */
  33. function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
  34.  
  35. $this->categories = get_categories( array( 'hide_empty' => false ) );
  36.  
  37. $defaults = array();
  38. foreach ( $this->categories as $cat )
  39. $defaults[ $cat->slug ] = array(
  40. 'creds' => 0,
  41. 'log' => '%plural% for publishing content',
  42. 'limit' => '0/x'
  43. );
  44.  
  45. if ( isset( $hook_prefs['publish_category_content'] ) )
  46. $defaults = $hook_prefs['publish_category_content'];
  47.  
  48. parent::__construct( array(
  49. 'id' => 'publish_category_content',
  50. 'defaults' => $defaults
  51. ), $hook_prefs, $type );
  52.  
  53. }
  54.  
  55. /**
  56. * Run
  57. * @version 1.0
  58. */
  59. public function run() {
  60.  
  61. add_action( 'transition_post_status', array( $this, 'maybe_publish' ), 10, 3 );
  62. add_filter( 'mycred_all_references', array( $this, 'add_badge_support' ) );
  63.  
  64. }
  65.  
  66. /**
  67. * Add Badge Support
  68. * @version 1.0
  69. */
  70. public function add_badge_support( $references ) {
  71.  
  72. foreach ( $this->categories as $cat ) {
  73.  
  74. $ref = 'published_' . $cat->slug;
  75. if ( ! array_key_exists( $ref, $references ) )
  76. $references[ $ref ] = sprintf( 'Published content in %s', $cat->name );
  77.  
  78. }
  79.  
  80. return $references;
  81.  
  82. }
  83.  
  84. /**
  85. * Maybe Publish
  86. * @version 1.0
  87. */
  88. public function maybe_publish( $new_status, $old_status, $post ) {
  89.  
  90. // Check for exclusions
  91. if ( $this->core->exclude_user( $post->post_author ) === true ) return;
  92.  
  93. // We want to fire when content get published or when it gets privatly published
  94. $status = apply_filters( 'mycred_publish_hook_old', array( 'new', 'auto-draft', 'draft', 'private', 'pending' ) );
  95. $publish_status = apply_filters( 'mycred_publish_hook_new', array( 'publish', 'private' ) );
  96.  
  97. // Make sure this is the right transition
  98. if ( in_array( $old_status, $status ) && in_array( $new_status, $publish_status ) ) {
  99.  
  100. // Get all the categories the post belongs to in case we have more than one
  101. $assigned_categories = get_the_category( $post->ID );
  102.  
  103. // Loop through all assigned categories
  104. if ( ! empty( $assigned_categories ) ) {
  105. foreach ( $assigned_categories as $category ) {
  106.  
  107. // If we set a vale of zero, skip
  108. if ( ! array_key_exists( $category->slug, $this->prefs ) || $this->prefs[ $category->slug ]['creds'] == 0 ) continue;
  109.  
  110. // Add this under a custom reference
  111. $reference = 'published_' . $category->slug;
  112. $data = array( 'ref_type' => 'post' );
  113.  
  114. if ( ! $this->over_hook_limit( $category->slug, $reference, $post->post_author ) )
  115. $this->core->add_creds(
  116. $reference,
  117. $post->post_author,
  118. $this->prefs[ $category->slug ]['creds'],
  119. $this->prefs[ $category->slug ]['log'],
  120. $post->ID,
  121. $data,
  122. $this->mycred_type
  123. );
  124.  
  125. }
  126. }
  127.  
  128. }
  129.  
  130. }
  131.  
  132. /**
  133. * Preference for Viewing Content Hook
  134. * @version 1.0
  135. */
  136. public function preferences() {
  137.  
  138. foreach ( $this->categories as $cat ) {
  139.  
  140. $prefs = shortcode_atts( array(
  141. 'creds' => 0,
  142. 'log' => '%plural% for publishing content',
  143. 'limit' => '0/x'
  144. ), ( ( array_key_exists( $cat->slug, $this->prefs ) ) ? $this->prefs[ $cat->slug ] : array() ) );
  145.  
  146. ?>
  147. <div class="hook-instance">
  148. <h3><?php printf( 'Published in: %s', $cat->name ); ?></h3>
  149. <div class="row">
  150. <div class="col-lg-2 col-md-6 col-sm-12 col-xs-12">
  151. <div class="form-group">
  152. <label for="<?php echo $this->field_id( array( $cat->slug => 'creds' ) ); ?>"><?php _e( 'Amount', 'mycred' ); ?></label>
  153. <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" />
  154. </div>
  155. </div>
  156. <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
  157. <div class="form-group">
  158. <label for="<?php echo $this->field_id( array( $cat->slug, 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
  159. <?php echo $this->hook_limit_setting( $this->field_name( array( $cat->slug, 'limit' ) ), $this->field_id( array( $cat->slug, 'limit' ) ), $prefs['limit'] ); ?>
  160. </div>
  161. </div>
  162. <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">
  163. <div class="form-group">
  164. <label for="<?php echo $this->field_id( array( $cat->slug => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
  165. <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" />
  166. <span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. <?php
  172.  
  173. }
  174.  
  175. }
  176.  
  177. /**
  178. * Sanitise Preferences
  179. * @version 1.1
  180. */
  181. public function sanitise_preferences( $data ) {
  182.  
  183. foreach ( $this->categories as $category ) {
  184.  
  185. if ( isset( $data[ $category->slug ]['limit'] ) && isset( $data[ $category->slug ]['limit_by'] ) ) {
  186. $limit = sanitize_text_field( $data[ $category->slug ]['limit'] );
  187. if ( $limit == '' ) $limit = 0;
  188. $data[ $category->slug ]['limit'] = $limit . '/' . $data[ $category->slug ]['limit_by'];
  189. unset( $data[ $category->slug ]['limit_by'] );
  190. }
  191.  
  192. }
  193.  
  194. return $data;
  195.  
  196. }
  197.  
  198. }
  199.  
  200. }
Add Comment
Please, Sign In to add comment