Plugin Directory

source: fleet/tags/2.1.7/includes/iworks/rate/rate.php @ 3105465

Last change on this file since 3105465 was 3105465, checked in by iworks, 6 weeks ago

2.1.7

File size: 14.5 KB
Line 
1<?php
2defined( 'ABSPATH' ) || exit; // Exit if accessed directly
3/**
4 * iWorks_Rate - Dashboard Notification module.
5 *
6 * @version 2.1.9
7 * @author  iworks (Marcin Pietrzak)
8 *
9 */
10if ( ! class_exists( 'iworks_rate' ) ) {
11        class iworks_rate {
12
13                /**
14                 * This class version.
15                 *
16                 * @since 1.0.1
17                 * @var   string
18                 */
19                private $version = '2.1.9';
20
21                /**
22                 * $wpdb->options field name.
23                 *
24                 * @since 1.0.0
25                 * @var   string
26                 */
27                protected $option_name = 'iworks_rates';
28
29                /**
30                 * List of all registered plugins.
31                 *
32                 * @since 1.0.0
33                 * @var   array
34                 */
35                protected $plugins = array();
36
37                /**
38                 * Module options that are stored in database.
39                 * Timestamps are stored here.
40                 *
41                 * Note that this option is stored in site-meta for multisite installs.
42                 *
43                 * @since 1.0.0
44                 * @var   array
45                 */
46                protected $stored = array();
47
48                /**
49                 * The Plugin ID
50                 *
51                 * @since 2.1.4
52                 * @var   string
53                 */
54                private $plugin_id;
55
56                /**
57                 * Initializes and returns the singleton instance.
58                 *
59                 * @since  1.0.0
60                 */
61                static public function instance() {
62                        static $Inst = null;
63                        if ( null === $Inst ) {
64                                $Inst = new iworks_rate();
65                        }
66                        return $Inst;
67                }
68
69                /**
70                 * Set up the iworks_rate module. Private singleton constructor.
71                 *
72                 * @since  1.0.0
73                 */
74                private function __construct() {
75                        /**
76                         * settings
77                         */
78                        $this->stored = wp_parse_args(
79                                get_site_option( $this->option_name, false ),
80                                array()
81                        );
82                        /**
83                         * actions
84                         */
85                        add_action( 'load-index.php', array( $this, 'load' ) );
86                        add_action( 'iworks-register-plugin', array( $this, 'register' ), 5, 3 );
87                        add_action( 'wp_ajax_iworks_rate_button', array( $this, 'ajax_button' ) );
88                        add_action( 'admin_init', array( $this, 'admin_init' ) );
89                        /**
90                         * own hooks
91                         */
92                        add_filter( 'iworks_rate_assistance', array( $this, 'filter_get_assistance_widget' ), 10, 2 );
93                        add_filter( 'iworks_rate_love', array( $this, 'filter_get_love_widget' ), 10, 2 );
94                        /**
95                         * advertising
96                         *
97                         * @since 2.1.0
98                         */
99                        add_filter( 'iworks_rate_advertising_og', array( $this, 'filter_get_advertising_og' ) );
100                }
101
102                /**
103                 * Inicialize admin area
104                 *
105                 * @since 2.0.2
106                 */
107                public function admin_init() {
108                        foreach ( $this->plugins as $plugin_file => $plugin ) {
109                                add_filter( 'plugin_action_links_' . $plugin_file, array( $this, 'add_donate_link' ), 10, 4 );
110                        }
111                }
112
113                /**
114                 * Add donate link to plugin_row_meta.
115                 *
116                 * @since 2.0.2
117                 *
118                 * @param array  $actions An array of the plugin's metadata, including the version, author, author URI, and plugin URI.
119                 */
120                public function add_donate_link( $actions, $plugin_file, $plugin_data, $context ) {
121                        $slug = 'iworks';
122                        if (
123                                isset( $this->plugins[ $plugin_file ] )
124                                && isset( $this->plugins[ $plugin_file ]['slug'] )
125                        ) {
126                                $slug = $this->plugins[ $plugin_file ]['slug'];
127                        }
128                        $settings_page_url = apply_filters( 'iworks_rate_settings_page_url_' . $slug, null );
129                        if ( ! empty( $settings_page_url ) ) {
130                                $actions['settings'] = sprintf(
131                                        '<a href="%s">%s</a>',
132                                        esc_url( $settings_page_url ),
133                                        esc_html__( 'Settings', 'fleet' )
134                                );
135                        }
136                        $actions['donate'] = sprintf(
137                                '<a href="%s" target="_blank">%s</a>',
138                                esc_url(
139                                        add_query_arg(
140                                                array(
141                                                        'utm_source' => $slug,
142                                                        'utm_medium' => 'plugin-links',
143                                                ),
144                                                'https://ko-fi.com/iworks'
145                                        )
146                                ),
147                                esc_html__( 'Provide us a coffee', 'fleet' )
148                        );
149                        return $actions;
150                }
151
152                public function load() {
153                        $plugin_id = $this->choose_plugin();
154                        if ( empty( $plugin_id ) ) {
155                                return;
156                        }
157                        $this->plugin_id = $plugin_id;
158                        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
159                        add_action( 'admin_notices', array( $this, 'show' ) );
160                }
161
162                /**
163                 * Save persistent module-data to the WP database.
164                 *
165                 * @since  1.0.0
166                 */
167                protected function store_data() {
168                        update_site_option( $this->option_name, $this->stored );
169                }
170
171                /**
172                 * Action handler for 'iworks-register-plugin'
173                 * Register an active plugin.
174                 *
175                 * @since  1.0.0
176                 * @param  string $plugin_id WordPress plugin-ID (see: plugin_basename).
177                 * @param  string $title Plugin name for display.
178                 * @param  string $slug the plugin slug on wp.org
179                 */
180                public function register( $plugin_id, $title, $slug ) {
181                        // Ignore incorrectly registered plugins to avoid errors later.
182                        if ( empty( $plugin_id ) || empty( $title ) || empty( $slug ) ) {
183                                return;
184                        }
185                        /**
186                         * collect data
187                         */
188                        $data = array(
189                                'title' => $title,
190                                'slug'  => $slug,
191                        );
192                        /**
193                         * add dat to plugins array
194                         */
195                        $this->plugins[ $plugin_id ] = $data;
196                        /**
197                         * check for option update
198                         *
199                         * @since 2.0.6
200                         *
201                         */
202                        $update = false;
203                        /*
204                         * When the plugin is registered the first time we store some infos
205                         * in the persistent module-data that help us later to find out
206                         * if/which message should be displayed.
207                         */
208                        if (��empty( $this->stored[ $plugin_id ] ) ) {
209                                $this->stored[ $plugin_id ] = wp_parse_args(
210                                        array(
211                                                'registered' => time(),
212                                                'show_at'    => $this->get_random_future_timestamp( 7, 14 ),
213                                                'rated'      => 0,
214                                                'hide'       => 0,
215                                        ),
216                                        $data
217                                );
218                                $update                     = true;
219                        }
220                        /**
221                         * check slug & mark for update if needed
222                         *
223                         * @since 2.0.6
224                         */
225                        if ( $this->stored[ $plugin_id ]['slug'] !== $slug ) {
226                                $this->stored[ $plugin_id ]['slug'] = $slug;
227                                $update                             = true;
228                        }
229                        /**
230                         * check title - can be diferent due language
231                         *
232                         * @since 2.0.6
233                         */
234                        $this->stored[ $plugin_id ]['title'] = $title;
235                        /**
236                         * Finally save the details if it is needed
237                         */
238                        if ( $update ) {
239                                $this->store_data();
240                        }
241                }
242
243                /**
244                 * Ajax handler called when the user chooses the CTA button.
245                 *
246                 * @since  1.0.0
247                 */
248                public function ajax_button() {
249                        /**
250                         * Chekc nonce
251                         *
252                         * @since 2.1.4
253                         */
254                        if ( ! check_ajax_referer( 'iworks-rate' ) ) {
255                                wp_send_json_error();
256                        }
257                        /**
258                         * get plugin ID
259                         */
260                        $nonce_value = filter_input( INPUT_POST, '_wpnonce', FILTER_DEFAULT );
261                        if ( ! wp_verify_nonce( $nonce_value, 'iworks-rate' ) ) {
262                                wp_send_json_error();
263                        }
264                        $plugin_id = filter_input( INPUT_POST, 'plugin_id', FILTER_DEFAULT );
265                        if ( empty( $plugin_id ) ) {
266                                wp_send_json_error();
267                        }
268                        /**
269                         * sanitize plugin_id
270                         *
271                         * @since 2.1.3
272                         */
273                        $plugin_id = sanitize_text_field( $plugin_id );
274                        if ( ! isset( $this->plugins[ $plugin_id ] ) ) {
275                                wp_send_json_error();
276                        }
277                        /**
278                         * sanitize button value
279                         *
280                         * @since 2.1.3
281                         */
282                        $value = '';
283                        if ( isset( $_POST['button'] ) ) {
284                                $value = sanitize_text_field( filter_input( INPUT_POST, 'button', FILTER_DEFAULT ) );
285                        }
286                        switch ( $value ) {
287                                case '':
288                                case 'add-review':
289                                        $this->add_weeks( $plugin_id );
290                                        wp_send_json_success();
291                                case 'hide':
292                                        $this->add_weeks( $plugin_id );
293                                        $this->hide( $plugin_id );
294                                        wp_send_json_success();
295                                case 'donate':
296                                        $this->add_months( $plugin_id );
297                                        wp_send_json_success();
298                        }
299                        wp_send_json_success();
300                }
301
302                public function hide( $plugin_id ) {
303                        if ( ! isset( $this->stored[ $plugin_id ] ) ) {
304                                return;
305                        }
306                        $this->stored[ $plugin_id ]['rated'] = time();
307                        $this->store_data();
308                }
309
310                private function add_weeks( $plugin_id ) {
311                        if ( ! isset( $this->stored[ $plugin_id ] ) ) {
312                                return;
313                        }
314                        $this->stored[ $plugin_id ]['show_at'] = $this->get_random_future_timestamp( 0, 7, 4, 6 );
315                        $this->store_data();
316                }
317
318                private function add_months( $plugin_id ) {
319                        if ( ! isset( $this->stored[ $plugin_id ] ) ) {
320                                return;
321                        }
322                        $this->stored[ $plugin_id ]['show_at'] = $this->get_random_future_timestamp( 0, 14, 15, 30 );
323                        $this->store_data();
324                }
325
326                /**
327                 * Ajax handler called when the user chooses the dismiss button.
328                 *
329                 * @since  1.0.0
330                 */
331                public function dismiss() {
332                        $plugin = $this->get_plugin_from_post();
333                        if ( is_wp_error( $plugin ) ) {
334                                wp_send_json_error();
335                        }
336                        wp_send_json_success();
337                }
338
339                /**
340                 * Action handler for 'load-index.php'
341                 * Set-up the Dashboard notification.
342                 *
343                 * @since  1.0.0
344                 */
345                public function enqueue() {
346                        wp_enqueue_style(
347                                __CLASS__,
348                                plugin_dir_url( __FILE__ ) . 'admin.css',
349                                array(),
350                                $this->version
351                        );
352                        wp_enqueue_script(
353                                __CLASS__,
354                                plugin_dir_url( __FILE__ ) . 'admin.js',
355                                array(),
356                                $this->version,
357                                true
358                        );
359                }
360
361                /**
362                 * Action handler for 'admin_notices'
363                 * Display the Dashboard notification.
364                 *
365                 * @since  1.0.0
366                 */
367                public function show() {
368                        $this->render_message( $this->plugin_id );
369                }
370
371                /**
372                 * Check to see if there is a pending message to display and returns
373                 * the message details if there is.
374                 *
375                 * Note that this function is only called on the main Dashboard screen
376                 * and only when logged in as super-admin.
377                 *
378                 * @since  1.0.0
379                 * @return object|false
380                 *         string $plugin WordPress plugin ID?
381                 */
382                protected function choose_plugin() {
383                        if ( wp_is_mobile() ) {
384                                return false;
385                        }
386                        /**
387                         * list
388                         */
389                        $choosen = array();
390                        /**
391                         * change time by filter
392                         */
393                        $now = apply_filters( 'iworks_rate_set_custom_time', time() );
394                        foreach ( $this->stored as $plugin_id => $item ) {
395                                if ( ! isset( $this->plugins[ $plugin_id ] ) ) {
396                                        if ( isset( $this->stored[ $plugin_id ] ) ) {
397                                                unset( $this->stored[ $plugin_id ] );
398                                                $this->store_data();
399                                        }
400                                        continue;
401                                }
402                                if ( intval( $item['show_at'] ) > $now ) {
403                                        continue;
404                                }
405                                $choosen[] = $plugin_id;
406                        }
407                        if ( empty( $choosen ) ) {
408                                return false;
409                        }
410                        return $choosen[ array_rand( $choosen ) ];
411                }
412
413                /**
414                 * Renders the actual Notification message.
415                 *
416                 * @since  1.0.0
417                 */
418                protected function render_message( $plugin_id ) {
419                        $file   = $this->get_file( 'thanks' );
420                        $plugin = $this->get_plugin_data_by_plugin_id( $plugin_id );
421                        load_template( $file, true, $plugin );
422                }
423
424                /**
425                 * @since 2.0.1
426                 */
427                private function get_file( $file, $group = '' ) {
428                        return sprintf(
429                                '%s/templates/%s%s%s.php',
430                                dirname( __FILE__ ),
431                                $group,
432                                '' === $group ? '' : '/',
433                                sanitize_title( $file )
434                        );
435                }
436
437                /**
438                 * @since 2.0.1
439                 */
440                private function get_plugin_data_by_plugin_id( $plugin_id ) {
441                        $plugin              = wp_parse_args(
442                                $this->plugins[ $plugin_id ],
443                                $this->stored[ $plugin_id ]
444                        );
445                        $plugin['plugin_id'] = $plugin_id;
446                        $plugin['logo']      = apply_filters( 'iworks_rate_notice_logo_style', '', $plugin );
447                        $plugin['ajax_url']  = admin_url( 'admin-ajax.php' );
448                        $plugin['classes']   = array(
449                                'iworks-rate',
450                                'iworks-rate-' . $plugin['slug'],
451                                'iworks-rate-notice',
452                        );
453                        if ( ! empty( $plugin['logo'] ) ) {
454                                $plugin['classes'][] = 'has-logo';
455                        }
456                        $plugin['url'] = esc_url(
457                                sprintf(
458                                /* translators: %s: plugin slug */
459                                        _x( 'https://wordpress.org/plugins/%s', 'plugins home', 'fleet' ),
460                                        $plugin['slug']
461                                )
462                        );
463                        $plugin['support_url'] = esc_url(
464                                sprintf(
465                                        /* translators: %s: plugin slug */
466                                        _x( 'https://wordpress.org/support/plugin/%s', 'plugins support home', 'fleet' ),
467                                        $plugin['slug']
468                                )
469                        );
470                        /**
471                         * Change plugin data.
472                         *
473                         * Allows to change generated plugin data.
474                         *
475                         * @since 2.1.4
476                         *
477                         * @param array $plugin {
478                         *     Plugin data.
479                         *
480                         *     @type string $plugin_id Plugin ID
481                         *     @type string $logo Logo URL
482                         *     @type string $ajax_url Admin AJAX URl
483                         *     @type array $classes CSS classes
484                         *     @type string $url Plugin repository URL
485                         *     @type string $support_url Plugin support URL
486                         * }
487                         */
488                        return apply_filters( 'iworks_rate_plugin_data', $plugin );
489                }
490
491                /**
492                 * @since 2.0.1
493                 */
494                private function get_plugin_id_by_slug( $slug ) {
495                        foreach ( $this->stored as $plugin_id => $plugin ) {
496                                if ( $slug === $plugin['slug'] ) {
497                                        return $plugin_id;
498                                }
499                        }
500                        return new WP_Error();
501                }
502
503                /**
504                 * @since 2.0.1
505                 */
506                public function filter_get_assistance_widget( $content, $slug ) {
507                        $plugin_id = $this->get_plugin_id_by_slug( $slug );
508                        if ( is_wp_error( $plugin_id ) ) {
509                                return $content;
510                        }
511                        $this->enqueue();
512                        $plugin = $this->get_plugin_data_by_plugin_id( $plugin_id );
513                        $file   = $this->get_file( 'support', 'widgets' );
514                        ob_start();
515                        load_template( $file, true, $plugin );
516                        $content = ob_get_contents();
517                        ob_end_clean();
518                        return $content;
519                }
520
521                /**
522                 * @since 2.0.1
523                 */
524                public function filter_get_love_widget( $content, $slug ) {
525                        $plugin_id = $this->get_plugin_id_by_slug( $slug );
526                        if ( is_wp_error( $plugin_id ) ) {
527                                return $content;
528                        }
529                        $this->enqueue();
530                        $plugin = $this->get_plugin_data_by_plugin_id( $plugin_id );
531                        $file   = $this->get_file( 'donate', 'widgets' );
532                        ob_start();
533                        load_template( $file, true, $plugin );
534                        $content = ob_get_contents();
535                        ob_end_clean();
536                        return $content;
537                }
538
539                /**
540                 * Get advertising for "OG — Better Share on Social Media" plugin.
541                 *
542                 * @since 2.1.0
543                 */
544                public function filter_get_advertising_og( $data ) {
545                        return array(
546                                'iworks-adverting-og' => array(
547                                        'title'    => __( 'OpenGraph', 'fleet' ),
548                                        'callback' => array( $this, 'get_advertising_og_content' ),
549                                        'context'  => 'side',
550                                        'priority' => 'low',
551                                ),
552                        );
553                }
554
555                /**
556                 * Advertising content for "OG — Better Share on Social Media" plugin.
557                 *
558                 * @since 2.1.0
559                 */
560                public function get_advertising_og_content() {
561                        $args = array(
562                                'install_plugin_url' => $this->get_install_plugin_url( 'og' ),
563                                'plugin_name'        => __( 'OG — Better Share on Social Media', 'fleet' ),
564                                'plugin_wp_home'     => __( 'https://wordpress.org/plugins/og/', 'fleet' ),
565                        );
566                        $file = $this->get_file( 'og', 'plugins' );
567                        load_template( $file, true, $args );
568                }
569
570                /**
571                 * get admin plugin install url
572                 *
573                 * @since 2.1.0
574                 */
575                private function get_install_plugin_url( $slug ) {
576                        return wp_nonce_url(
577                                self_admin_url(
578                                        add_query_arg(
579                                                array(
580                                                        'action' => 'install-plugin',
581                                                        'plugin' => $slug,
582                                                ),
583                                                'update.php'
584                                        )
585                                ),
586                                'install-plugin_' . $slug
587                        );
588                }
589
590                /**
591                 * Get random future timstamp
592                 *
593                 * @since 2.1.5
594                 */
595                private function get_random_future_timestamp( $day_min = 0, $day_max = 0, $week_min = 0, $week_max = 0 ) {
596                        $time = time();
597                        /**
598                         * DAY_IN_SECONDS
599                         */
600                        $days = 0;
601                        if ( 0 < $day_max ) {
602                                $days = wp_rand( $day_min, $day_max );
603                        }
604                        $time += $days * DAY_IN_SECONDS;
605                        /**
606                         * WEEK_IN_SECONDS
607                         */
608                        $weeks = 0;
609                        if ( 0 < $week_max ) {
610                                $weeks = wp_rand( $week_min, $week_max );
611                        }
612                        $time += $weeks * WEEK_IN_SECONDS;
613                        /**
614                         * returns
615                         */
616                        return $time;
617                }
618        }
619
620        // Initialize the module.
621        iworks_rate::instance();
622}
Note: See TracBrowser for help on using the repository browser.