Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( class_exists( 'myCRED_Hook' ) ) {
- class myCRED_Hook_Click_Links extends myCRED_Hook {
- /**
- * Construct
- */
- function __construct( $hook_prefs ) {
- parent::__construct( array(
- 'id' => 'link_click',
- 'defaults' => array(
- 'limit_by' => 'none',
- 'creds' => 1,
- 'log' => '%plural% for clicking on link to: %url%'
- )
- ), $hook_prefs );
- }
- /**
- * Run
- * @since 1.1
- * @version 1.0
- */
- public function run() {
- add_action( 'mycred_front_enqueue', array( $this, 'register_script' ) );
- add_action( 'wp_footer', array( $this, 'footer' ) );
- add_shortcode( 'mycred_link', 'mycred_render_shortcode_link' );
- add_action( 'wp_ajax_mycred-click-points', array( $this, 'ajax_call_link_points' ) );
- add_filter( 'mycred_parse_tags_link', array( $this, 'parse_custom_tags' ), 10, 2 );
- }
- /**
- * Customize Limit Options
- * @since 1.1
- * @version 1.0
- */
- public function custom_limit() {
- return array(
- 'none' => __( 'No limit', 'mycred' ),
- 'url' => __( 'Once for each unique URL', 'mycred' ),
- 'id' => __( 'Once for each unique link id', 'mycred' )
- );
- }
- /**
- * Parse Custom Tags in Log
- * @since 1.1
- * @version 1.1.1
- */
- public function parse_custom_tags( $content, $log_entry ) {
- $data = maybe_unserialize( $log_entry->data );
- $content = str_replace( '%url%', $data['link_url'], $content );
- $content = str_replace( '%id%', $data['link_id'], $content );
- if ( isset( $data['link_title'] ) )
- $content = str_replace( '%title%', $data['link_title'], $content );
- return $content;
- }
- /**
- * Register Script
- * @since 1.1
- * @version 1.0
- */
- public function register_script() {
- // ADJUSTMENT: Load the custom script located in our themes /js/ folder
- wp_register_script(
- 'mycred-link-points',
- get_template_directory_uri() . 'js/links.js',
- array( 'jquery' ),
- myCRED_VERSION . '.1',
- true
- );
- global $mycred_link_points;
- if ( $mycred_link_points === true ) {
- wp_enqueue_script( 'mycred-link-points' );
- }
- }
- /**
- * WP Fotter
- * @since 1.1
- * @version 1.0
- */
- public function footer() {
- global $mycred_link_points;
- if ( $mycred_link_points === true ) {
- wp_localize_script(
- 'mycred-link-points',
- 'myCREDgive',
- array(
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
- 'token' => wp_create_nonce( 'mycred-link-points' )
- )
- );
- wp_enqueue_script( 'mycred-link-points' );
- }
- }
- /**
- * Custom Has Entry Check
- * @since 1.1
- * @version 1.1.1
- */
- public function has_entry( $action = '', $reference = '', $user_id = '', $data = '' ) {
- global $wpdb;
- if ( $this->prefs['limit_by'] == 'url' ) {
- $reference = urldecode( $reference );
- $string = '%s:8:"link_url";s:' . strlen( $reference ) . ':"' . $reference . '";%';
- }
- elseif ( $this->prefs['limit_by'] == 'id' ) {
- $string = '%s:7:"link_id";s:' . strlen( $reference ) . ':"' . $reference . '";%';
- }
- else return false;
- $sql = "SELECT id FROM {$this->core->log_table} WHERE ref = %s AND user_id = %d AND data LIKE %s;";
- $wpdb->get_results( $wpdb->prepare( $sql, $action, $user_id, $string ) );
- if ( $wpdb->num_rows > 0 ) return true;
- return false;
- }
- /**
- * AJAX Call Handler
- * @since 1.1
- * @version 1.3.1
- */
- public function ajax_call_link_points() {
- // We must be logged in
- if ( !is_user_logged_in() ) die( json_encode( 100 ) );
- // Security
- check_ajax_referer( 'mycred-link-points', 'token' );
- // ADJUSTMENT: Post Author
- $post = get_post( (int) $_POST['post_id'] );
- if ( $post === NULL ) die();
- $user_id = $post->post_author;
- // Check if user should be excluded
- if ( $this->core->exclude_user( $user_id ) ) die( json_encode( 200 ) );
- // Key
- if ( ! isset( $_POST['key'] ) ) die( json_encode( 300 ) );
- require_once( myCRED_INCLUDES_DIR . 'mycred-protect.php' );
- $protect = new myCRED_Protect();
- $key = explode( ':', $protect->do_decode( $_POST['key'] ) );
- if ( count( $key ) != 2 ) die( json_encode( $key ) );
- $amount = trim( $key[0] );
- $id = trim( $key[1] );
- // Amount
- if ( $amount == 0 )
- $amount = $this->prefs['creds'];
- else
- $amount = $this->core->number( $amount );
- if ( $amount == 0 || $amount == $this->core->zero() ) die( json_encode( 400 ) );
- // Limits
- if ( $this->prefs['limit_by'] == 'url' ) {
- if ( !isset( $_POST['url'] ) || empty( $_POST['url'] ) ) die( json_encode( 500 ) );
- if ( $this->has_entry( 'link_click', $_POST['url'], $user_id ) ) die( json_encode( 600 ) );
- $ref = $_POST['url'];
- }
- elseif ( $this->prefs['limit_by'] == 'id' ) {
- if ( $this->has_entry( 'link_click', $id, $user_id ) ) die( json_encode( 700 ) );
- $ref = $id;
- }
- else {
- $ref = $id;
- }
- // Execute
- $this->core->add_creds(
- 'link_click',
- $user_id,
- $amount,
- $this->prefs['log'],
- '',
- array(
- 'ref_type' => 'link',
- 'link_url' => $_POST['url'],
- 'link_id' => $id,
- 'link_title' => ( isset( $_POST['etitle'] ) ) ? $_POST['etitle'] : ''
- )
- );
- // Report the good news
- die( json_encode( 'done' ) );
- }
- /**
- * Preference for Link Click Hook
- * @since 1.1
- * @version 1.0.2
- */
- public function preferences() {
- $prefs = $this->prefs; ?>
- <label class="subheader"><?php echo $this->core->plural(); ?></label>
- <ol>
- <li>
- <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>
- <span class="description"><?php _e( 'The default amount to award for clicking on links. You can override this in the shortcode.', 'mycred' ); ?></span>
- </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( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" class="long" /></div>
- <span class="description"><?php _e( 'Available template tags: General and custom tags: %url%, %title% or %id%.', 'mycred' ); ?></span>
- </li>
- </ol>
- <label class="subheader"><?php _e( 'Limits', 'mycred' ); ?></label>
- <ol>
- <li>
- <?php
- add_filter( 'mycred_hook_impose_limits', array( $this, 'custom_limit' ) );
- $this->impose_limits_dropdown( 'limit_by', false ); ?>
- </li>
- <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>
- </ol>
- <?php unset( $this );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement