dimanche 16 mai 2021

How to customize a WordPress plugin (My Cred)

So I have been trying to make a new MyCred hook such that a user gets points from visiting the website only if they visit through links from different websites. This is the code:

<?php
if ( ! defined( 'myCRED_VERSION' ) ) exit;

/**
 * Hook for site visits
 * @since 1.5
 * @version 1.1
 */
// Register Hook
add_filter( 'mycred_setup_hooks', 'register_my_custom_hook' );
function register_my_custom_hook( $installed )
{
    $installed['complete_profile'] = array(
        'title'       => __( '%plural% for visits from specific sites', 'textdomain' ),
        'description' => __( 'This hook awards points for visits from specific sites.', 'textdomain' ),
        'callback'    => array( 'myCRED_Hook_Site_Visits_from_specific_site' )
    );
    return $installed;
}
add_action( 'mycred_load_hooks', 'mycred_load_site_visits_from_specific_site' );
function mycred_load_bp_site_visits_from_specific_site() {
if ( ! class_exists( 'myCRED_Hook_Site_Visits_from_specific_site' ) ) :
    class myCRED_Hook_Site_Visits_from_specific_site extends myCRED_Hook {

        /**
         * Construct
         */
        function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {

            parent::__construct( array(
                'id'       => 'site_visit_from_specific_site',
                'defaults' => array(
                    'creds'   => 1,
                    'log'     => '%plural% for site visit from specific site'
                )
            ), $hook_prefs, $type );

        }

        /**
         * Run
         * @since 1.5
         * @version 1.0.2
         */
        public function run() {

            // Make sure user is logged in. Also to prevent unneccery db queries we
            // check to make sure the user does not have the cookie.
            if ( is_user_logged_in() && ! isset( $_COOKIE['mycred_site_visit_from_specific_site'] ) )
                add_action( 'init', array( $this, 'site_visit_from_specific_site' ) );

        }
    }

        /**
         * Visit Hook
         * @since 1.5
         * @version 1.1.3
         */
        public function site_visit_from_specific_site() {

            if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
            
            $referrer = $_SERVER['HTTP_REFERER'];
            $flag = stristr( $referrer,'http://chatshop.byethost16.com/b16_28398476/subdomain2.22web.org/');
            if(!$flag) return;

            // Current User ID
            $user_id = get_current_user_id();
            $now     = current_time( 'timestamp' );
            // Make sure user is not excluded
            if ( $this->core->exclude_user( $user_id ) ) return;

            // Store todays date as an integer
            $today = (int) apply_filters( 'mycred_site_visit_from_specific_site_id', date( 'Ymd', $now ) );
            $data = '';
            // Make sure this is unique
            if ( $this->core->has_entry( 'site_visit_from_specific_site', $today, $user_id, $data, $this->mycred_type ) ) return;
            // Execute
            $this->core->add_creds(
                'site_visit_from_specific_site',
                $user_id,
                $this->prefs['creds'],
                $this->prefs['log'],
                $today,
                $data,
                $this->mycred_type
            );

        }

        /**
         * Preference for Site Visit Hook
         * @since 1.5
         * @version 1.1
         */
        public function preferences() {

            $prefs = $this->prefs;

?>
<div class="hook-instance">
    <div class="row">
        <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
            <div class="form-group">
                <label for="<?php echo $this->field_id( 'creds' ); ?>"><?php echo $this->core->plural(); ?></label>
                <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'] ); ?>" class="form-control" />
            </div>
        </div>
        <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
            <div class="form-group">
                <label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
                <input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" placeholder="<?php _e( 'required', 'mycred' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="form-control" />
                <span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
            </div>
        </div>
    </div>
</div>
<?php

        }

    }
endif;

But I am unable to get this hook to work , also this does not appear on the hooks page in the admin area of wordpress. Also please advise where the php file is to be uploaded in the file manager of cpanel for this to work.




Aucun commentaire:

Enregistrer un commentaire