Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
nfd_create_event_queue_table
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
nfd_drop_event_queue_table
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use NewfoldLabs\WP\Module\Data\Data;
4use NewfoldLabs\WP\Module\Data\EventQueue\Queues\BatchQueue;
5use NewfoldLabs\WP\Module\Data\Helpers\Encryption;
6use NewfoldLabs\WP\Module\Data\Helpers\Transient;
7use NewfoldLabs\WP\Module\Data\SiteCapabilities;
8use NewfoldLabs\WP\ModuleLoader\Container;
9use WP_Forge\UpgradeHandler\UpgradeHandler;
10use function NewfoldLabs\WP\ModuleLoader\container;
11
12use function NewfoldLabs\WP\ModuleLoader\register as registerModule;
13
14// Exit if accessed directly
15if ( ! defined( 'ABSPATH' ) ) {
16    return;
17}
18
19// Do not allow multiple copies of the module to be active
20if ( defined( 'NFD_DATA_MODULE_VERSION' ) ) {
21    return;
22}
23
24define( 'NFD_DATA_MODULE_VERSION', '2.9.4' );
25
26if ( ! function_exists( 'nfd_create_event_queue_table' ) ) {
27    /**
28     * Create the event queue table
29     */
30    function nfd_create_event_queue_table() {
31        BatchQueue::create_table();
32    }
33}
34
35if ( ! function_exists( 'nfd_drop_event_queue_table' ) ) {
36    /**
37     * Drop the event queue table
38     */
39    function nfd_drop_event_queue_table() {
40        global $wpdb;
41        $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}nfd_data_event_queue" );
42    }
43}
44
45if ( function_exists( 'is_admin' ) && is_admin() ) {
46    $upgrade_handler = new UpgradeHandler(
47        __DIR__ . '/upgrades',
48        get_option( 'nfd_data_module_version' ),
49        NFD_DATA_MODULE_VERSION
50    );
51
52    if ( $upgrade_handler->maybe_upgrade() ) {
53        // If an upgrade occurred, update the new version in the database to prevent running the routine(s) again.
54        update_option( 'nfd_data_module_version', NFD_DATA_MODULE_VERSION, true );
55    }
56}
57
58/**
59 * Register the data module
60 */
61if ( function_exists( 'add_action' ) && function_exists( 'add_filter' ) ) {
62
63    add_action(
64        'plugins_loaded',
65        function () {
66
67            registerModule(
68                array(
69                    'name'     => 'data',
70                    'label'    => __( 'Data', 'wp-module-data' ),
71                    'callback' => function ( Container $container ) {
72                        $module = new Data( $container->plugin() );
73                        $module->start();
74                    },
75                    'isActive' => true,
76                    'isHidden' => true,
77                )
78            );
79        }
80    );
81
82    // Auto-encrypt token on save.
83    add_filter(
84        'pre_update_option_nfd_data_token',
85        function ( $value ) {
86            return ( new Encryption() )->encrypt( $value );
87        }
88    );
89
90    // Auto-decrypt token when fetched
91    add_filter(
92        'option_nfd_data_token',
93        function ( $value ) {
94            return ( new Encryption() )->decrypt( $value );
95        }
96    );
97
98    // Register activation/deactivation hooks
99    add_action(
100        'newfold_container_set',
101        function ( Container $container ) {
102
103            register_activation_hook(
104                $container->plugin()->file,
105                function () use ( $container ) {
106                    nfd_create_event_queue_table();
107                    Transient::set( 'nfd_plugin_activated', $container->plugin()->basename );
108                }
109            );
110
111            register_deactivation_hook(
112                $container->plugin()->file,
113                function () use ( $container ) {
114                    delete_option( 'nfd_data_module_version' );
115                    nfd_drop_event_queue_table();
116                }
117            );
118
119            $container->set(
120                'capabilities',
121                $container->service(
122                    function () {
123                        return new SiteCapabilities();
124                    }
125                )
126            );
127        }
128    );
129
130}