Advertisement
designbymerovingi

myCRED Link Class

Nov 22nd, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. if ( class_exists( 'myCRED_Hook' ) ) {
  2. class myCRED_Hook_Click_Links extends myCRED_Hook {
  3. /**
  4. * Construct
  5. */
  6. function __construct( $hook_prefs ) {
  7. parent::__construct( array(
  8. 'id' => 'link_click',
  9. 'defaults' => array(
  10. 'limit_by' => 'none',
  11. 'creds' => 1,
  12. 'log' => '%plural% for clicking on link to: %url%'
  13. )
  14. ), $hook_prefs );
  15. }
  16.  
  17. /**
  18. * Run
  19. * @since 1.1
  20. * @version 1.0
  21. */
  22. public function run() {
  23. add_action( 'mycred_front_enqueue', array( $this, 'register_script' ) );
  24. add_action( 'wp_footer', array( $this, 'footer' ) );
  25.  
  26. add_shortcode( 'mycred_link', 'mycred_render_shortcode_link' );
  27. add_action( 'wp_ajax_mycred-click-points', array( $this, 'ajax_call_link_points' ) );
  28.  
  29. add_filter( 'mycred_parse_tags_link', array( $this, 'parse_custom_tags' ), 10, 2 );
  30. }
  31.  
  32. /**
  33. * Customize Limit Options
  34. * @since 1.1
  35. * @version 1.0
  36. */
  37. public function custom_limit() {
  38. return array(
  39. 'none' => __( 'No limit', 'mycred' ),
  40. 'url' => __( 'Once for each unique URL', 'mycred' ),
  41. 'id' => __( 'Once for each unique link id', 'mycred' )
  42. );
  43. }
  44.  
  45. /**
  46. * Parse Custom Tags in Log
  47. * @since 1.1
  48. * @version 1.1.1
  49. */
  50. public function parse_custom_tags( $content, $log_entry ) {
  51. $data = maybe_unserialize( $log_entry->data );
  52. $content = str_replace( '%url%', $data['link_url'], $content );
  53. $content = str_replace( '%id%', $data['link_id'], $content );
  54. if ( isset( $data['link_title'] ) )
  55. $content = str_replace( '%title%', $data['link_title'], $content );
  56.  
  57. return $content;
  58. }
  59.  
  60. /**
  61. * Register Script
  62. * @since 1.1
  63. * @version 1.0
  64. */
  65. public function register_script() {
  66. // ADJUSTMENT: Load the custom script located in our themes /js/ folder
  67. wp_register_script(
  68. 'mycred-link-points',
  69. get_template_directory_uri() . 'js/links.js',
  70. array( 'jquery' ),
  71. myCRED_VERSION . '.1',
  72. true
  73. );
  74.  
  75. global $mycred_link_points;
  76. if ( $mycred_link_points === true ) {
  77. wp_enqueue_script( 'mycred-link-points' );
  78. }
  79. }
  80.  
  81. /**
  82. * WP Fotter
  83. * @since 1.1
  84. * @version 1.0
  85. */
  86. public function footer() {
  87. global $mycred_link_points;
  88. if ( $mycred_link_points === true ) {
  89. wp_localize_script(
  90. 'mycred-link-points',
  91. 'myCREDgive',
  92. array(
  93. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  94. 'token' => wp_create_nonce( 'mycred-link-points' )
  95. )
  96. );
  97. wp_enqueue_script( 'mycred-link-points' );
  98. }
  99. }
  100.  
  101. /**
  102. * Custom Has Entry Check
  103. * @since 1.1
  104. * @version 1.1.1
  105. */
  106. public function has_entry( $action = '', $reference = '', $user_id = '', $data = '' ) {
  107. global $wpdb;
  108.  
  109. if ( $this->prefs['limit_by'] == 'url' ) {
  110. $reference = urldecode( $reference );
  111. $string = '%s:8:"link_url";s:' . strlen( $reference ) . ':"' . $reference . '";%';
  112. }
  113. elseif ( $this->prefs['limit_by'] == 'id' ) {
  114. $string = '%s:7:"link_id";s:' . strlen( $reference ) . ':"' . $reference . '";%';
  115. }
  116. else return false;
  117.  
  118. $sql = "SELECT id FROM {$this->core->log_table} WHERE ref = %s AND user_id = %d AND data LIKE %s;";
  119. $wpdb->get_results( $wpdb->prepare( $sql, $action, $user_id, $string ) );
  120. if ( $wpdb->num_rows > 0 ) return true;
  121.  
  122. return false;
  123. }
  124.  
  125. /**
  126. * AJAX Call Handler
  127. * @since 1.1
  128. * @version 1.3.1
  129. */
  130. public function ajax_call_link_points() {
  131. // We must be logged in
  132. if ( !is_user_logged_in() ) die( json_encode( 100 ) );
  133.  
  134. // Security
  135. check_ajax_referer( 'mycred-link-points', 'token' );
  136.  
  137. // ADJUSTMENT: Post Author
  138. $post = get_post( (int) $_POST['post_id'] );
  139. if ( $post === NULL ) die();
  140. $user_id = $post->post_author;
  141.  
  142. // Check if user should be excluded
  143. if ( $this->core->exclude_user( $user_id ) ) die( json_encode( 200 ) );
  144.  
  145. // Key
  146. if ( ! isset( $_POST['key'] ) ) die( json_encode( 300 ) );
  147. require_once( myCRED_INCLUDES_DIR . 'mycred-protect.php' );
  148. $protect = new myCRED_Protect();
  149. $key = explode( ':', $protect->do_decode( $_POST['key'] ) );
  150. if ( count( $key ) != 2 ) die( json_encode( $key ) );
  151.  
  152. $amount = trim( $key[0] );
  153. $id = trim( $key[1] );
  154.  
  155. // Amount
  156. if ( $amount == 0 )
  157. $amount = $this->prefs['creds'];
  158. else
  159. $amount = $this->core->number( $amount );
  160.  
  161. if ( $amount == 0 || $amount == $this->core->zero() ) die( json_encode( 400 ) );
  162.  
  163. // Limits
  164. if ( $this->prefs['limit_by'] == 'url' ) {
  165. if ( !isset( $_POST['url'] ) || empty( $_POST['url'] ) ) die( json_encode( 500 ) );
  166. if ( $this->has_entry( 'link_click', $_POST['url'], $user_id ) ) die( json_encode( 600 ) );
  167. $ref = $_POST['url'];
  168. }
  169. elseif ( $this->prefs['limit_by'] == 'id' ) {
  170. if ( $this->has_entry( 'link_click', $id, $user_id ) ) die( json_encode( 700 ) );
  171. $ref = $id;
  172. }
  173. else {
  174. $ref = $id;
  175. }
  176.  
  177. // Execute
  178. $this->core->add_creds(
  179. 'link_click',
  180. $user_id,
  181. $amount,
  182. $this->prefs['log'],
  183. '',
  184. array(
  185. 'ref_type' => 'link',
  186. 'link_url' => $_POST['url'],
  187. 'link_id' => $id,
  188. 'link_title' => ( isset( $_POST['etitle'] ) ) ? $_POST['etitle'] : ''
  189. )
  190. );
  191.  
  192. // Report the good news
  193. die( json_encode( 'done' ) );
  194. }
  195.  
  196. /**
  197. * Preference for Link Click Hook
  198. * @since 1.1
  199. * @version 1.0.2
  200. */
  201. public function preferences() {
  202. $prefs = $this->prefs; ?>
  203.  
  204. <label class="subheader"><?php echo $this->core->plural(); ?></label>
  205. <ol>
  206. <li>
  207. <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->number( $prefs['creds'] ); ?>" size="8" /></div>
  208. <span class="description"><?php _e( 'The default amount to award for clicking on links. You can override this in the shortcode.', 'mycred' ); ?></span>
  209. </li>
  210. </ol>
  211. <label class="subheader"><?php _e( 'Log Template', 'mycred' ); ?></label>
  212. <ol>
  213. <li>
  214. <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>
  215. <span class="description"><?php _e( 'Available template tags: General and custom tags: %url%, %title% or %id%.', 'mycred' ); ?></span>
  216. </li>
  217. </ol>
  218. <label class="subheader"><?php _e( 'Limits', 'mycred' ); ?></label>
  219. <ol>
  220. <li>
  221. <?php
  222. add_filter( 'mycred_hook_impose_limits', array( $this, 'custom_limit' ) );
  223. $this->impose_limits_dropdown( 'limit_by', false ); ?>
  224.  
  225. </li>
  226. <li><strong><?php _e( 'Note!', 'mycred' ); ?></strong> <?php echo $this->core->template_tags_general( __( 'If no ID is set when using the mycred_link shortcode, the shortcode will generate one automatically based on the value set under href. If you are using this feature for "sharing" content, it is recommended that you limit by ID.', 'mycred' ) ); ?></li>
  227. </ol>
  228. <?php unset( $this );
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement