Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
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.8.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', 'newfold-data-module' ),
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    // Temporary filter, as the migrate capability isn't working as expected.
99    add_filter(
100        'pre_set_transient_nfd_site_capabilities',
101        function ( $transient ) {
102            if ( empty( $transient ) && 'bluehost' === container()->plugin()->brand ) {
103                return array(
104                    'canMigrateSite' => true,
105                    'hasAISiteGen'   => true,
106                );
107            }
108            return $transient;
109        },
110        10,
111        2
112    );
113
114    // Register activation/deactivation hooks
115    add_action(
116        'newfold_container_set',
117        function ( Container $container ) {
118
119            register_activation_hook(
120                $container->plugin()->file,
121                function () use ( $container ) {
122                    nfd_create_event_queue_table();
123                    Transient::set( 'nfd_plugin_activated', $container->plugin()->basename );
124                }
125            );
126
127            register_deactivation_hook(
128                $container->plugin()->file,
129                function () use ( $container ) {
130                    delete_option( 'nfd_data_module_version' );
131                    nfd_drop_event_queue_table();
132                }
133            );
134
135            $container->set(
136                'capabilities',
137                $container->service(
138                    function () {
139                        return new SiteCapabilities();
140                    }
141                )
142            );
143        }
144    );
145
146}