Forum Replies Created

Viewing 6 replies - 16 through 21 (of 21 total)
  • Thread Starter drsdre

    (@drsdre)

    After some fiddling around, the code below (to be used as a plugin) is doing the job for me. If you implement this, you probably want to add a setting to enable it explicitly and also split the thirstyData field into specific fields.

    <?php
    /**
     * Plugin Name:     ThirstyAffiliates API
     * Plugin URI:      https://github.com/drsdre
     * Description:     Enable WP Core Rest API v2 for ThirstyAffiliates data
     * Author:          Andre Schuurman
     * Author URI:      https://github.com/drsdre
     * Text Domain:     thirstyaffiliates-api
     * Domain Path:     /languages
     * Version:         0.1.0
     *
     * @package         thirstyaffiliates-api
     */
    
    /**
     * Add REST API support to thirstylink post type.
     */
    add_action( 'init', 'thirstylink_post_enable_rest_api', 25 );
    function thirstylink_post_enable_rest_api() {
    	global $wp_post_types;
    
    	$post_type_name = 'thirstylink';
    	if ( isset( $wp_post_types[ $post_type_name ] ) ) {
    		$wp_post_types[ $post_type_name ]->show_in_rest = true;
    		$wp_post_types[ $post_type_name ]->rest_base    = $post_type_name;
    		$wp_post_types[ $post_type_name ]->supports['custom-fields'] = true;
    
    	}
    }
    
    /**
     * Add REST API support to thirstylink-category taxonomy type.
     */
    add_action( 'init', 'thirstylink_category_enable_rest_api', 25 );
    function thirstylink_category_enable_rest_api() {
    	global $wp_taxonomies;
    
    	$taxonomy_name = 'thirstylink_category';
    	if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
    		$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
    		$wp_taxonomies[ $taxonomy_name ]->rest_base    = $taxonomy_name;
    
    	}
    }
    
    /**
     * Add REST API support to thirstylink title and thirstyData fields.
     */
    add_action( 'rest_api_init', function () {
    	register_rest_field( 'thirstylink', 'title', [
    		'get_callback'    => function ( $thirstylink_data ) {
    			$thirstylink_obj = get_post( $thirstylink_data['id'] );
    
    			return $thirstylink_obj->post_title;
    		},
    		'update_callback' => function ( $title, $thirstylink_obj ) {
    			$ret = wp_update_post( [
    				'id'         => $thirstylink_obj->ID,
    				'post_title' => $title,
    			] );
    			if ( false === $ret ) {
    				return new WP_Error( 'rest_thirstylink_title_failed', __( 'Failed to update title.' ),
    					[ 'status' => 500 ] );
    			}
    
    			return true;
    		},
    		'schema'          => [
    			'description' => __( 'Thirstylink title.' ),
    			'type'        => 'string',
    		],
    	] );
    
    	register_rest_field( 'thirstylink', 'thirstyData', [
    		'get_callback'    => function ( $thirstylink_data ) {
    			$thirstylink_data_obj = get_post_meta( $thirstylink_data['id'], 'thirstyData', true );
    
    			return $thirstylink_data_obj;
    		},
    		'update_callback' => function ( $data, $thirstylink_obj ) {
    			$ret = update_post_meta( $thirstylink_obj->ID, 'thirstyData', $data);
    
    			if ( false === $ret ) {
    				return new WP_Error( 'rest_thirstylink_data_failed', __( 'Failed to update data.' ),
    					[ 'status' => 500 ] );
    			}
    
    			return true;
    		},
    		'schema'          => [
    			'description' => __( 'Thirstylink data.' ),
    			'type'        => 'string',
    		],
    	] );
    } );

    Another fix I propose, is to not register redirect stats when logged in as admin to prevent skewing the statistics. You need to add two lines of code in the same WordPressStats.php file (marked with –>):

        public function redirect()
        {
            $link = sanitize_text_field(urldecode($_GET['link']));
            $id = sanitize_text_field($_GET['id']);
    
            if (!is_admin()) {    // --> 1. Add this line
    	        WordPressDb::getInstance()->getDb()->insert(
    		        WordPressDb::getInstance()->getDb()->prefix . AEIDN_TABLE_STATS,
    		        [ 'date' => date( 'Y-m-d' ), 'product_id' => $id, 'quantity' => 1 ]
    	        )
    	        ;
            }    // --> And this line
            $link = str_replace('&', '&', $link);
    
            header('Location: ' . $link . '');
            exit();
        }
    • This reply was modified 8 years, 1 month ago by drsdre.

    Always double test…

    Solution is luckily simple. Just keep the existing line:
    add_action('wp_ajax_aeidn_redirect', [$this, 'redirect']);

    And add the new line underneath:
    add_action('wp_ajax_nopriv_aeidn_redirect', [$this, 'redirect']);

    This issue is related to the setup of the click tracker in src/Dnolbon/Wordpress/WordpressStats.php. In line 8 of this file the ajax click handler is setup as:
    add_action('wp_ajax_aeidn_redirect', [$this, 'redirect']);

    This should be changed to:
    add_action('wp_ajax_nopriv_aeidn_redirect', [$this, 'redirect']);

    This allows the ajax action also to be executed for non-logged in users.

    Andre

    Could you apply the following fix in quick-edit-template-link.php on line 99 to prevent PHP Notice errors in certain conditions:
    if (isset($options[‘qetl_checkbox_exectime’]) && $options[‘qetl_checkbox_exectime’] == 1) {

    Thread Starter drsdre

    (@drsdre)

    I’ve switched to using the subscribe widget of Sendgrid for now. I still think this plugin is relevant as the Sendgrid widget loads a bit slow.

    Andre

Viewing 6 replies - 16 through 21 (of 21 total)