Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 174
0.00% covered (danger)
0.00%
0 / 13
CRAP
n/a
0 / 0
jpcrm_activation_checks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
jpcrm_check_min_php_version
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
jpcrm_is_devmode_override
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jpcrm_check_api_connector
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
jpcrm_do_critical_prerun_checks
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
jpcrm_register_admin_notice
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
jpcrm_show_admin_notice
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
zeroBSCRM_legacySupport
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
_we
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
__w
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
zeroBSCRM_init_perfTest
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
jpcrm_plugin_activate
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
jpcrm_plugin_redirect
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3 * Plugin Name: Jetpack CRM
4 * Plugin URI: https://jetpackcrm.com
5 * Description: Jetpack CRM is the simplest CRM for WordPress. Self host your own Customer Relationship Manager using WP.
6 * Version: 6.7.2
7 * Author: Automattic - Jetpack CRM team
8 * Author URI: https://jetpackcrm.com
9 * Text Domain: zero-bs-crm
10 * Requires at least: 6.0
11 * Requires PHP: 7.4
12 * License: GPLv2
13 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
14 */
15
16/*
17======================================================
18    Breaking Checks
19    ====================================================== */
20
21// stops direct access
22if ( ! defined( 'ABSPATH' ) ) {
23    exit( 0 );
24}
25
26// Enabling THIS will start LOGGING PERFORMANCE TESTS
27// NOTE: This will do for EVERY page load, so just add temporarily else adds rolling DRAIN on sys
28// define( 'ZBSPERFTEST', 1 );
29
30if ( ! defined( 'ZBS_ROOTFILE' ) ) {
31    define( 'ZBS_ROOTFILE', __FILE__ );
32    define( 'ZBS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
33    define( 'ZBS_ROOTDIR', basename( __DIR__ ) );
34    define( 'ZBS_ROOTPLUGIN', ZBS_ROOTDIR . '/' . basename( ZBS_ROOTFILE ) );
35    define( 'ZBS_LANG_DIR', basename( __DIR__ ) . '/languages' );
36}
37
38/**
39 * Jetpack Autoloader.
40 */
41$jetpack_autoloader = ZBS_PLUGIN_DIR . 'vendor/autoload_packages.php';
42if ( is_readable( $jetpack_autoloader ) ) {
43    require_once $jetpack_autoloader;
44    if ( method_exists( \Automattic\Jetpack\Assets::class, 'alias_textdomains_from_file' ) ) {
45        \Automattic\Jetpack\Assets::alias_textdomains_from_file( ZBS_PLUGIN_DIR . 'jetpack_vendor/i18n-map.php' );
46    }
47} else {
48    // Something very unexpected. Error out gently with an admin_notice and exit loading.
49    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
50        error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
51            __( 'Error loading autoloader file for Jetpack CRM plugin', 'zero-bs-crm' )
52        );
53    }
54
55    // Add a red bubble notification to My Jetpack if the installation is bad.
56    add_filter(
57        'my_jetpack_red_bubble_notification_slugs',
58        function ( $slugs ) {
59            $slugs['jetpack-crm-plugin-bad-installation'] = array(
60                'data' => array(
61                    'plugin' => 'Jetpack CRM',
62                ),
63            );
64
65            return $slugs;
66        }
67    );
68
69    add_action(
70        'admin_notices',
71        function () {
72            if ( get_current_screen()->id !== 'plugins' ) {
73                return;
74            }
75            $message = sprintf(
76                wp_kses(
77                    /* translators: Placeholder is a link to a support document. */
78                    __( 'Your installation of Jetpack CRM is incomplete. If you installed Jetpack CRM from GitHub, please refer to <a href="%1$s" target="_blank" rel="noopener noreferrer">this document</a> to set up your development environment. Jetpack CRM must have Composer dependencies installed and built via the build command.', 'zero-bs-crm' ),
79                    array(
80                        'a' => array(
81                            'href'   => array(),
82                            'target' => array(),
83                            'rel'    => array(),
84                        ),
85                    )
86                ),
87                'https://github.com/Automattic/jetpack/blob/trunk/docs/development-environment.md#building-your-project'
88            );
89            wp_admin_notice(
90                $message,
91                array(
92                    'type'        => 'error',
93                    'dismissible' => true,
94                )
95            );
96        }
97    );
98
99    return;
100}
101
102/**
103 * Performs some checks before the plugin activation
104 *
105 * @param $plugin
106 */
107function jpcrm_activation_checks( $plugin ) {
108    jpcrm_check_api_connector();
109}
110add_action( 'activate_plugin', 'jpcrm_activation_checks', 10, 2 );
111
112/**
113 * Check for the minimum PHP version
114 *
115 * @return bool
116 */
117function jpcrm_check_min_php_version() {
118    $min_php_version = '7.4';
119
120    if ( version_compare( PHP_VERSION, $min_php_version, '<' ) ) {
121        /* translators: %1$s: Minimum PHP version, %2$s: Current PHP version */
122        $error_message  = sprintf( __( 'Jetpack CRM requires PHP version %1$s or greater. Older versions of PHP are no longer supported. Your current version of PHP is %2$s.', 'zero-bs-crm' ), $min_php_version, PHP_VERSION );
123        $error_message .= '<br><a href="https://kb.jetpackcrm.com/knowledge-base/php-version-jetpack-crm/" target="_blank">' . __( 'Click here for more information', 'zero-bs-crm' ) . '</a>';
124        jpcrm_register_admin_notice( 'error', $error_message );
125        return false;
126    }
127
128    return true;
129}
130
131/**
132 * Determines if dev mode is enabled with JPCRM_DEVOVERRIDE constant
133 *
134 * @return bool
135 */
136function jpcrm_is_devmode_override() {
137    if ( defined( 'JPCRM_DEVOVERRIDE' ) ) {
138        return true;
139    }
140    return false;
141}
142
143/**
144 * Check if the plugin API Connector is installed
145 */
146function jpcrm_check_api_connector() {
147    global $zbs;
148    if ( class_exists( 'ZeroBSCRM_APIConnector' ) ) {
149        $error_msg  = '<b>' . __( 'API Connector is currently installed and activated on this site.', 'zero-bs-crm' ) . '</b>';
150        $error_msg .= '<br>' . __( 'API Connector is a plugin used to connect external websites to your CRM. You should not install it on the same website as your CRM. Please deactivate it first if you plan to install the CRM on this site.', 'zero-bs-crm' );
151        $error_msg .= '<br><br><a href="' . $zbs->urls['kbapi'] . '">' . __( 'Learn more', 'zero-bs-crm' ) . '</a>';
152        wp_die( $error_msg );
153    }
154}
155
156/**
157 * Verifies various critical checks pass:
158 *   - PHP version
159 */
160function jpcrm_do_critical_prerun_checks() {
161    $all_clear = true;
162    if ( ! jpcrm_check_min_php_version() ) {
163        $all_clear = false;
164    }
165
166    // yay, all clear
167    if ( $all_clear ) {
168        return true;
169    }
170
171    // at this point something failed, so disable this plugin
172
173    $error_message = __( 'Jetpack CRM was unable to load and has been deactivated.', 'zero-bs-crm' );
174    jpcrm_register_admin_notice( 'error', $error_message );
175
176    // this file isn't always available, so include it in case
177    require_once ABSPATH . 'wp-admin/includes/plugin.php';
178
179    deactivate_plugins( __FILE__ );
180
181    // if the plugin was just activated, this keeps it from saying "plugin activated"
182    if ( isset( $_GET['activate'] ) ) {
183        unset( $_GET['activate'] );
184    }
185
186    return false;
187}
188
189/**
190 * Show admin notice with `admin_notices` action
191 *
192 * @param string $notice_class e.g. "error"|"notice"
193 * @param string $message
194 */
195function jpcrm_register_admin_notice( $notice_class, $message ) {
196    $admin_notice_fn = function () use ( $notice_class, $message ) {
197        jpcrm_show_admin_notice( $notice_class, $message );
198    };
199    add_action( 'admin_notices', $admin_notice_fn );
200}
201
202/**
203 * Show admin notice
204 *
205 * @param string $notice_class e.g. "error"|"notice"
206 * @param string $message
207 */
208function jpcrm_show_admin_notice( $notice_class, $message ) {
209    ?>
210    <div class="<?php echo esc_attr( $notice_class ); ?>">
211        <p>
212            <?php echo $message; ?>
213        </p>
214    </div>
215    <?php
216}
217
218// ====================================================================
219// =================  Legacy (pre v2.53) Support ======================
220
221    // LEGACY SUPPORT - all ext settings
222    global $zbsLegacySupport;
223    $zbsLegacySupport = array( 'extsettingspostinit' => array() );
224
225    // support for old - to be removed in time.
226    global $zeroBSCRM_Settings;
227
228    // this gets run post init :)
229function zeroBSCRM_legacySupport() {
230
231    // map old global, for NOW... remove once all ext's removed
232    // only needed in old wh.config.lib's, which we can replace now :)
233    global $zeroBSCRM_Settings, $zbs, $zbsLegacySupport;
234    $zeroBSCRM_Settings = $zbs->settings;
235
236    if ( count( $zbsLegacySupport ) > 0 ) {
237        foreach ( $zbsLegacySupport as $key => $defaultConfig ) {
238
239            // init with a throwaway var (just get)
240
241            // this checks is the setting is accessible, and if not (fresh installs) then uses the caught defaultConfig from wh.config.lib legacy support
242            $existingSettings = $zbs->settings->dmzGetConfig( $key );
243            // Create if not existing
244            if ( ! is_array( $existingSettings ) ) {
245
246                // init
247                $zbs->settings->dmzUpdateConfig( $key, $defaultConfig );
248
249            }
250        } // / foreach loaded with legacy support
251    }
252}
253
254    // legacy support for removal of _we() - to be fixed in ext
255    // still used in a few plugins as of 1922-07-28
256if ( ! function_exists( '_we' ) ) {
257    function _we( $str, $domain = 'zero-bs-crm' ) {
258        _e( $str, $domain );
259    }
260    function __w( $str, $domain = 'zero-bs-crm' ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore -- legacy
261        return __( $str, $domain );
262    }
263}
264
265// ================ / Legacy (pre v2.53) Support ======================
266// ====================================================================
267
268// ====================================================================
269// ==================== General Perf Testing ==========================
270
271function zeroBSCRM_init_perfTest() {
272    if ( defined( 'ZBSPERFTEST' ) && zeroBSCRM_isWPAdmin() ) {
273
274        // retrieve our global (may have had any number of test res added)
275        global $zbsPerfTest;
276
277        // If admin, clear any prev perf test ifset
278        if ( isset( $_GET['delperftest'] ) ) {
279            delete_option( 'zbs-global-perf-test' );
280        }
281
282        // retrieve opt + add to it (up to 50 tests)
283        $zbsPerfTestOpt = get_option( 'zbs-global-perf-test', array() );
284        if ( is_array( $zbsPerfTestOpt ) && count( $zbsPerfTestOpt ) < 50 ) {
285
286            // add
287            $zbsPerfTestOpt[] = $zbsPerfTest;
288
289            // save
290            update_option( 'zbs-global-perf-test', $zbsPerfTestOpt, false );
291        }
292    }
293}
294
295// ====================================================================
296// ==================== Welcome Wizard Simple Redirect ================
297// ====================================================================
298
299/**
300 * Runs on plugin activation.
301 *
302 * @return void
303 */
304function jpcrm_plugin_activate() {
305
306    // run the install functions.
307    include_once __DIR__ . '/includes/ZeroBSCRM.Core.php';
308    $zbs = ZeroBSCRM::instance();
309    $zbs->install();
310
311    // Skip redirect if it's a bulk activation with more than one plugin.
312    if (
313        // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing -- This is safe.
314        isset( $_POST['action'] )
315        && $_POST['action'] === 'activate-selected'
316        && isset( $_POST['checked'] )
317        && count( $_POST['checked'] ) > 1
318        // phpcs:enable WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing -- This is safe.
319    ) {
320        return;
321    }
322    add_option( 'jpcrm_do_redirect', true );
323}
324register_activation_hook( __FILE__, 'jpcrm_plugin_activate' );
325
326/**
327 * Redirects user after plugin activation.
328 *
329 * @return void
330 */
331function jpcrm_plugin_redirect() {
332    if ( ! get_option( 'jpcrm_do_redirect' ) ) {
333        return;
334    }
335
336    // Skip redirect if it's a JSON/AJAX request or via WP-CLI
337    if ( wp_doing_ajax() || wp_is_json_request() || ( defined( 'WP_CLI' ) && WP_CLI ) || wp_is_xml_request() ) {
338        return;
339    }
340
341    // Clean up option on the first normal request.
342    delete_option( 'jpcrm_do_redirect' );
343
344    // Skip redirect if it's a plugin upgrade process.
345    // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing -- This is safe.
346    if ( isset( $_GET['action'] ) && $_GET['action'] === 'upgrade-plugin' ) {
347        return;
348    }
349
350    $redirect_url = admin_url( 'admin.php?page=zerobscrm-dash' );
351    // Only force wizard if it hasn't been run before
352    if ( ! get_option( 'jpcrm_wizard_completed', false ) ) {
353        $redirect_url = add_query_arg( 'jpcrm_force_wizard', '1', $redirect_url );
354    }
355    wp_safe_redirect( $redirect_url );
356    exit;
357}
358add_action( 'admin_init', 'jpcrm_plugin_redirect' );
359
360// =================== / General Perf Testing =========================
361// ====================================================================
362
363// ====================================================================
364// =================  Main Include ====================================
365if ( jpcrm_do_critical_prerun_checks() ) {
366
367    // full perf test mode
368    if ( defined( 'ZBSPERFTEST' ) ) {
369
370        // store a global arr for this "test"
371        global $zbsPerfTest;
372        $zbsPerfTest = array(
373            'init'    => time(),
374            'get'     => $_GET,
375            'results' => array(),
376        );
377
378        // include if not already
379        if ( ! function_exists( 'zeroBSCRM_performanceTest_finishTimer' ) ) {
380                include_once __DIR__ . '/includes/ZeroBSCRM.PerformanceTesting.php';
381        }
382
383        // start timer
384        zeroBSCRM_performanceTest_startTimer( 'plugin-load' );
385    }
386
387    // Include the main Jetpack CRM class.
388    if ( ! class_exists( 'ZeroBSCRM' ) ) {
389        include_once __DIR__ . '/includes/ZeroBSCRM.Core.php';
390    }
391
392    // init hook
393    add_action(
394        'init',
395        function () {
396            if ( ! class_exists( 'ZeroBSCRM' ) ) {
397                include_once __DIR__ . '/includes/ZeroBSCRM.Core.php';
398            }
399            global $zbs;
400            $zbs = ZeroBSCRM::instance();
401        },
402        0
403    );
404
405    add_action(
406        'plugins_loaded',
407        function () {
408            load_plugin_textdomain( 'zero-bs-crm', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
409        },
410        0
411    );
412
413    // close timer (at this point we'll have perf library)
414    if ( defined( 'ZBSPERFTEST' ) ) {
415
416        // retrieve our global (may have had any number of test res added)
417        global $zbsPerfTest;
418
419        // close it
420        zeroBSCRM_performanceTest_finishTimer( 'plugin-load' );
421
422        // store in perf-reports
423        $zbsPerfTest['results']['plugin-load'] = zeroBSCRM_performanceTest_results( 'plugin-load' );
424
425        // here we basically wait for init so we can check user is wp admin
426        // ... only saving perf logs if defined + wp admin
427        add_action( 'shutdown', 'zeroBSCRM_init_perfTest' );
428    }
429}
430
431// ================ / Main Include ====================================
432// ====================================================================