Overview

Namespaces

  • Varunsridharan
    • WordPress

Classes

  • Varunsridharan\WordPress\Review_Me
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 
<?php
/**
 * WordPress Review Me Library
 * This library provides developers to ask end user to review their product (theme / plugin)
 *
 * @author    Varun Sridharan <varunsridharan23@gmail.com>
 * @copyright 2018 Varun Sridharan
 * @license   GPLV3 Or Greater
 */

namespace Varunsridharan\WordPress;


if ( ! class_exists( '\Varunsridharan\WordPress\Review_Me' ) ) {
    /**
     * Class Review_Me
     *
     * @package Varunsridharan\WordPress
     * @author Varun Sridharan <varunsridharan23@gmail.com>
     * @since 1.0
     */
    class Review_Me {
        /**
         * Library Version
         *
         * @var string
         */
        public $version = '1.4';

        /**
         * linkid
         *
         * @var
         */
        public $linkid;

        /**
         * key
         *
         * @var string
         */
        protected $key;

        /**
         * link_id
         *
         * @var null|string
         */
        protected $link_id = null;

        /**
         * op
         *
         * @var array
         */
        protected $op = array();

        /**
         * Review_Me constructor.
         *
         * @param array $options
         */
        public function __construct( $options = array() ) {
            if ( \is_admin() || defined( 'DOING_AJAX' ) ) {
                $this->op      = \wp_parse_args( $options, $this->get_defaults() );
                $this->key     = 'vswprm_' . substr( md5( $this->op['slug'] ), 0, 20 );
                $this->link_id = 'vswprm-review-link-' . $this->key;

                if ( \get_option( $this->key . 'isdone', false ) === false ) {
                    $this->maybe_prompt();
                }

                \add_action( 'wp_ajax_vswprm_clicked_review', array( $this, 'dismiss_notice' ) );
            }
        }

        /**
         * returns defaults args
         *
         * @return array
         */
        public function get_defaults() {
            return array(
                'days_after'      => 10,
                'notice_callback' => false,
                'type'            => '',
                'slug'            => '',
                'rating'          => 5,
                'message'         => \sprintf( \esc_html__( 'Hey! It&#039;s been a little while that you&#039;ve been using this product. You might not realize it, but user reviews are such a great help to us. We would be so grateful if you could take a minute to leave a review on WordPress.org. Many thanks in advance :)', 'wp-review-me' ) ),
                'link_label'      => \esc_html__( 'Click here to leave your review', 'wp-review-me' ),
                'review_link'     => false,
                'item_id'         => false,
                'site'            => 'wordpress',
            );
        }

        /**
         * Checks And Shows Review Message
         */
        protected function maybe_prompt() {
            if ( ! $this->is_time() ) {
                return;
            }

            \add_action( 'admin_footer', array( $this, 'script' ) );
            if ( false !== $this->op['notice_callback'] ) {
                call_user_func_array( $this->op['notice_callback'], array( &$this ) );
            } else {
                \add_action( 'admin_notices', array( $this, 'add_notice' ) );
            }
        }

        /**
         * Check if it is time to ask for a review
         *
         * @return bool
         * @since 1.0
         */
        protected function is_time() {
            $installed = \get_option( $this->key, false );

            if ( false === $installed || 0 === $installed ) {
                $this->setup_date();
                $installed = time();
            }

            if ( $installed + ( $this->op['days_after'] * 86400 ) > time() ) {
                return false;
            }
            return true;
        }

        /**
         * Save the current date as the installation date
         *
         * @return void
         * @since 1.0
         */
        protected function setup_date() {
            \update_option( $this->key, time() );
        }

        /**
         * Display Admin Notice
         */
        public function add_notice() {
            echo '<div class="updated success is-dismissible"><p>' . $this->get_message() . '</p></div>';
        }

        /**
         * Get the review prompt message
         *
         * @return string
         * @since 1.0
         */
        public function get_message() {
            $message = $this->op['message'];
            $link    = $this->get_review_link_tag();
            $message = $message . ' ' . $link;
            return \wp_kses_post( $message );
        }

        /**
         * Get the complete link tag
         *
         * @return string
         * @since 1.0
         */
        protected function get_review_link_tag() {
            $link  = $this->get_review_link();
            $label = $this->op['link_label'];
            return "<a href='$link' target='_blank' id='$this->link_id'>$label</a>";
        }

        /**
         * Get the review link
         *
         * @return string
         * @since 1.0
         */
        protected function get_review_link() {
            if ( false === $this->op['review_link'] ) {
                $link = '';
                if ( 'wordpress' === $this->op['site'] ) {
                    $link = 'https://wordpress.org/support/';

                    switch ( $this->op['type'] ) {
                        case 'theme':
                            $link .= 'theme/';
                            break;
                        case 'plugin':
                            $link .= 'plugin/';
                            break;
                    }

                    $link .= $this->op['slug'] . '/reviews';

                    $link = \add_query_arg( 'rate', $this->op['rating'], $link ) . '#new-post';
                } elseif ( 'codecanyon' === $this->op['site'] ) {
                    $link = 'https://codecanyon.net/item/x/reviews/' . $this->op['item_id'] . '#rating-' . $this->op['item_id'];
                } elseif ( 'themeforest' === $this->op['site'] ) {
                    $link = 'https://themeforest.net/item/x/reviews/' . $this->op['item_id'] . '#rating-' . $this->op['item_id'];
                }

                return $link;
            }

            return $this->op['review_link'];
        }

        /**
         * Echo the JS script in the admin footer
         *
         * @return void
         * @since 1.0
         */
        public function script() { ?>

            <script>
                jQuery( document ).ready( function( $ ) {
                    $( '#<?php echo $this->link_id; ?>' ).on( 'click', wrmDismiss );

                    function wrmDismiss() {
                        var data = {
                            action: 'vswprm_clicked_review',
                            id: '<?php echo $this->link_id; ?>'
                        };
                        jQuery.ajax( {
                            type: 'POST',
                            url: ajaxurl,
                            data: data,
                        } );
                    }
                } );
            </script>

        <?php }

        /**
         * Dismiss the notice when the review link is clicked
         *
         * @return void
         * @since 1.0
         */
        public function dismiss_notice() {
            if ( empty( $_POST ) ) {
                die();
            }
            if ( ! isset( $_POST['id'] ) ) {
                die();
            }
            $id = \sanitize_text_field( $_POST['id'] );
            if ( $id !== $this->link_id ) {
                die();
            }

            \update_option( $this->key . 'isdone', 'yes' );
            die();
        }
    }
}
API documentation generated by ApiGen