Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
JPCRM_FeatureSniffer
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 4
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 sniff_for_plugin
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
156
 show_feature_alerts
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 dismiss_alert
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
3
4/**
5 *
6 * The JPCRM_FeatureSniffer class lets core detect installed
7 * plugins for which we already have integrations
8 */
9class JPCRM_FeatureSniffer {
10
11    /**
12     * An array of plugins installed.
13     *
14     * @var array
15     */
16    public $all_plugins = array();
17
18    /**
19     * An array of alerts to display to user.
20     *
21     * @var array
22     */
23    public $alerts = array();
24
25    public function __construct() {
26        if ( ! function_exists( 'get_plugins' ) ) {
27            require_once ABSPATH . 'wp-admin/includes/plugin.php';
28        }
29        $this->all_plugins = get_plugins();
30    }
31
32    /**
33     *
34     * Checks if there is an unused CRM integration
35     * with installed plugins
36     *
37     * $args may look something like this:
38     *  array(
39     *    'feature_slug'    => 'feature_slug',
40     *    'plugin_slug'     => 'plugin.php',
41     *    'more_info_link'  => 'https://kb.jetpackcrm.com/some_link_to_docs'
42     *  )
43     *
44     * @param array $args params passed to check, e.g.
45     * @param bool  $is_silent determine whether to show notices to the end user or not.
46     *
47     * @return bool
48     */
49    public function sniff_for_plugin( $args = array(), $is_silent = false ) {
50
51        if (
52            // bad params
53            empty( $args )
54            || ! isset( $args['feature_slug'] )
55            || ! isset( $args['plugin_slug'] )
56            || ! isset( $args['more_info_link'] )
57            // target plugin isn't active
58            || ! is_plugin_active( $args['plugin_slug'] )
59            // feature is already enabled
60            || zeroBSCRM_isExtensionInstalled( $args['feature_slug'] )
61        ) {
62            return false;
63        }
64
65        $is_dismissed = get_option( 'jpcrm_hide_' . $args['feature_slug'] . '_feature_alert', false );
66
67        // handle messaging if not silent
68        if ( ! $is_silent && ! $is_dismissed && current_user_can( 'activate_plugins' ) ) {
69            $plugin_details = $this->all_plugins[ $args['plugin_slug'] ];
70
71            global $zbs;
72
73            $message_template = sprintf( __( 'Jetpack CRM has an optional integration with your <code>%s</code> plugin, but the feature is not currently enabled.' ), $plugin_details['Name'] );
74            if ( isset( $args['is_module'] ) && $args['is_module'] ) {
75                $message_template .= ' ' . sprintf( __( 'If you want to use this feature, please activate it in <a href="%s">Core Modules</a>.' ), zeroBSCRM_getAdminURL( $zbs->slugs['modules'] ) );
76            }
77
78            ##WLREMOVE
79            $message_template .= '<br><br>' . sprintf( __( 'Learn more by going <a href="%s" target="_blank">here</a>.' ), $args['more_info_link'] );
80            ##/WLREMOVE
81
82            $this->alerts[] = array(
83                'feature_slug' => $args['feature_slug'],
84                'message'      => $message_template,
85            );
86        }
87
88        return true;
89    }
90
91    /**
92     *
93     * Show alert if there is a feature one might find useful
94     *
95     * Note that this will only show the first unused feature detected
96     * that has not been dismissed
97     */
98    public function show_feature_alerts() {
99        // no untapped features, so no messaging needed
100        if ( count( $this->alerts ) == 0 ) {
101            return false;
102        }
103        if ( zeroBSCRM_isAdminPage() ) {
104
105            $feature_alert_fn = function () {
106                // only show first message; no need to overload user
107                ?>
108            <div id="<?php echo $this->alerts[0]['feature_slug']; ?>_feature_alert"
109                class="ui segment jpcrm-promo notice jpcrm_feature_alert is-dismissible">
110                <div class="content">
111                    <p><strong><?php echo $this->alerts[0]['message']; ?></strong></p>
112                </div>
113            </div>
114                <?php
115            };
116            add_action( 'admin_notices', $feature_alert_fn );
117        }
118    }
119
120    /**
121     * Sets alert to be dismissed
122     * (Effectively backend variant of `jpcrm_hide_feature_alert()`)
123     */
124    public function dismiss_alert( $feature_slug ) {
125
126        if ( ! empty( $feature_slug ) ) {
127
128            update_option( 'jpcrm_hide_' . $feature_slug . '_feature_alert', 1, false );
129            return true;
130
131        }
132
133        return false;
134    }
135}
136