Completed
Branch FET/add-step-bubble-nav-compon... (ad6e27)
by
unknown
44:32 queued 35:49
created

EE_Brewing_Regular::setSwitchHooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
use EventEspresso\core\interfaces\InterminableInterface;
6
use EventEspresso\core\services\database\TableAnalysis;
7
8
/**
9
 * the purpose of this file is to simply contain any action/filter hook callbacks etc for specific aspects of EE
10
 * related to caffeinated (regular) use.  Before putting any code in here, First be certain that it isn't better to
11
 * define and use the hook in a specific caffeinated/whatever class or file.
12
 */
13
// defined some new constants related to caffeinated folder
14
define('EE_CAF_URL', EE_PLUGIN_DIR_URL . 'caffeinated/');
15
define('EE_CAF_CORE', EE_CAFF_PATH . 'core' . DS);
16
define('EE_CAF_LIBRARIES', EE_CAF_CORE . 'libraries' . DS);
17
define('EE_CAF_PAYMENT_METHODS', EE_CAFF_PATH . 'payment_methods' . DS);
18
19
20
/**
21
 * EE_Brewing_Regular class.  Just a wrapper to help namespace activity for the functionality of this file.
22
 *
23
 * @package        Event Espresso
24
 * @subpackage     /caffeinated/brewing_regular.php
25
 * @author         Darren Ethier
26
 */
27
class EE_Brewing_Regular extends EE_BASE implements InterminableInterface
28
{
29
30
    /**
31
     * @var TableAnalysis $table_analysis
32
     */
33
    protected $_table_analysis;
34
35
36
    /**
37
     * EE_Brewing_Regular constructor.
38
     *
39
     * @param TableAnalysis $table_analysis
40
     */
41
    public function __construct(TableAnalysis $table_analysis)
42
    {
43
        $this->_table_analysis = $table_analysis;
44
        if (defined('EE_CAFF_PATH')) {
45
            $this->setInitializationHooks();
46
            $this->setApiRegistrationHooks();
47
            $this->setSwitchHooks();
48
            $this->setDefaultFilterHooks();
49
            // caffeinated constructed
50
            do_action('AHEE__EE_Brewing_Regular__construct__complete');
51
        }
52
    }
53
54
55
    /**
56
     * Various hooks used for extending features via registration of modules or extensions.
57
     */
58
    private function setApiRegistrationHooks()
59
    {
60
        add_filter(
61
            'FHEE__EE_Config__register_modules__modules_to_register',
62
            array($this, 'caffeinated_modules_to_register')
63
        );
64
        add_filter('FHEE__EE_Registry__load_helper__helper_paths', array($this, 'caf_helper_paths'), 10);
65
        add_filter(
66
            'AHEE__EE_System__load_core_configuration__complete',
67
            function () {
68
                EE_Register_Payment_Method::register(
69
                    'caffeinated_payment_methods',
70
                    array(
71
                        'payment_method_paths' => glob(EE_CAF_PAYMENT_METHODS . '*', GLOB_ONLYDIR),
72
                    )
73
                );
74
            }
75
        );
76
    }
77
78
79
    /**
80
     * Various hooks used for modifying initialization or activation processes.
81
     */
82
    private function setInitializationHooks()
83
    {
84
        // activation
85
        add_action('AHEE__EEH_Activation__initialize_db_content', array($this, 'initialize_caf_db_content'));
86
        // load caff init
87
        add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'caffeinated_init'));
88
        // load caff scripts
89
        add_action('wp_enqueue_scripts', array($this, 'enqueue_caffeinated_scripts'), 10);
90
    }
91
92
93
    /**
94
     * Various hooks used for switch (on/off) type filters.
95
     */
96
    private function setSwitchHooks()
97
    {
98
        // remove the "powered by" credit link from receipts and invoices
99
        add_filter('FHEE_EE_Html_messenger__add_powered_by_credit_link_to_receipt_and_invoice', '__return_false');
100
        // seeing how this is caf, which isn't put on WordPress.org, we can have affiliate links without a disclaimer
101
        add_filter('FHEE__ee_show_affiliate_links', '__return_false');
102
    }
103
104
105
    /**
106
     * Various filters for affecting default configuration values in the caffeinated
107
     * context.
108
     */
109
    private function setDefaultFilterHooks()
110
    {
111
        add_filter(
112
            'FHEE__EE_Admin_Config__show_reg_footer__default',
113
            '__return_true'
114
        );
115
    }
116
117
118
    /**
119
     * callback for the FHEE__EE_Registry__load_helper__helper_paths filter to add the caffeinated paths
120
     *
121
     * @param array $paths original helper paths array
122
     * @return array             new array of paths
123
     */
124
    public function caf_helper_paths($paths)
125
    {
126
        $paths[] = EE_CAF_CORE . 'helpers' . DS;
127
        return $paths;
128
    }
129
130
131
    /**
132
     * Upon brand-new activation, if this is a new activation of CAF, we want to add
133
     * some global prices that will show off EE4's capabilities. However, if they're upgrading
134
     * from 3.1, or simply EE4.x decaf, we assume they don't want us to suddenly introduce these extra prices.
135
     * This action should only be called when EE 4.x.0.P is initially activated.
136
     * Right now the only CAF content are these global prices. If there's more in the future, then
137
     * we should probably create a caf file to contain it all instead just a function like this.
138
     * Right now, we ASSUME the only price types in the system are default ones
139
     *
140
     * @global wpdb $wpdb
141
     */
142
    public function initialize_caf_db_content()
143
    {
144
        global $wpdb;
145
        // use same method of getting creator id as the version introducing the change
146
        $default_creator_id = apply_filters('FHEE__EE_DMS_Core_4_5_0__get_default_creator_id', get_current_user_id());
147
        $price_type_table = $wpdb->prefix . "esp_price_type";
148
        $price_table = $wpdb->prefix . "esp_price";
149
        if ($this->_get_table_analysis()->tableExists($price_type_table)) {
150
            $SQL = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table . ' WHERE PBT_ID=4';// include trashed price types
151
            $tax_price_type_count = $wpdb->get_var($SQL);
152
            if ($tax_price_type_count <= 1) {
153
                $wpdb->insert(
154
                    $price_type_table,
155
                    array(
156
                        'PRT_name'       => __("Regional Tax", "event_espresso"),
157
                        'PBT_ID'         => 4,
158
                        'PRT_is_percent' => true,
159
                        'PRT_order'      => 60,
160
                        'PRT_deleted'    => false,
161
                        'PRT_wp_user'    => $default_creator_id,
162
                    ),
163
                    array(
164
                        '%s',// PRT_name
165
                        '%d',// PBT_id
166
                        '%d',// PRT_is_percent
167
                        '%d',// PRT_order
168
                        '%d',// PRT_deleted
169
                        '%d', // PRT_wp_user
170
                    )
171
                );
172
                // federal tax
173
                $result = $wpdb->insert(
174
                    $price_type_table,
175
                    array(
176
                        'PRT_name'       => __("Federal Tax", "event_espresso"),
177
                        'PBT_ID'         => 4,
178
                        'PRT_is_percent' => true,
179
                        'PRT_order'      => 70,
180
                        'PRT_deleted'    => false,
181
                        'PRT_wp_user'    => $default_creator_id,
182
                    ),
183
                    array(
184
                        '%s',// PRT_name
185
                        '%d',// PBT_id
186
                        '%d',// PRT_is_percent
187
                        '%d',// PRT_order
188
                        '%d',// PRT_deleted
189
                        '%d' // PRT_wp_user
190
                    )
191
                );
192
                if ($result) {
193
                    $wpdb->insert(
194
                        $price_table,
195
                        array(
196
                            'PRT_ID'         => $wpdb->insert_id,
197
                            'PRC_amount'     => 15.00,
198
                            'PRC_name'       => __("Sales Tax", "event_espresso"),
199
                            'PRC_desc'       => '',
200
                            'PRC_is_default' => true,
201
                            'PRC_overrides'  => null,
202
                            'PRC_deleted'    => false,
203
                            'PRC_order'      => 50,
204
                            'PRC_parent'     => null,
205
                            'PRC_wp_user'    => $default_creator_id,
206
                        ),
207
                        array(
208
                            '%d',// PRT_id
209
                            '%f',// PRC_amount
210
                            '%s',// PRC_name
211
                            '%s',// PRC_desc
212
                            '%d',// PRC_is_default
213
                            '%d',// PRC_overrides
214
                            '%d',// PRC_deleted
215
                            '%d',// PRC_order
216
                            '%d',// PRC_parent
217
                            '%d' // PRC_wp_user
218
                        )
219
                    );
220
                }
221
            }
222
        }
223
    }
224
225
226
    /**
227
     *    caffeinated_modules_to_register
228
     *
229
     * @access public
230
     * @param array $modules_to_register
231
     * @return array
232
     */
233
    public function caffeinated_modules_to_register($modules_to_register = array())
234
    {
235
        if (is_readable(EE_CAFF_PATH . 'modules')) {
236
            $caffeinated_modules_to_register = glob(EE_CAFF_PATH . 'modules' . DS . '*', GLOB_ONLYDIR);
237
            if (is_array($caffeinated_modules_to_register) && ! empty($caffeinated_modules_to_register)) {
238
                $modules_to_register = array_merge($modules_to_register, $caffeinated_modules_to_register);
239
            }
240
        }
241
        return $modules_to_register;
242
    }
243
244
245
    /**
246
     * @throws EE_Error
247
     * @throws InvalidArgumentException
248
     * @throws ReflectionException
249
     * @throws InvalidDataTypeException
250
     * @throws InvalidInterfaceException
251
     */
252
    public function caffeinated_init()
253
    {
254
        // Custom Post Type hooks
255
        add_filter(
256
            'FHEE__EventEspresso_core_domain_entities_custom_post_types_TaxonomyDefinitions__getTaxonomies',
257
            array($this, 'filter_taxonomies')
258
        );
259
        add_filter(
260
            'FHEE__EventEspresso_core_domain_entities_custom_post_types_CustomPostTypeDefinitions__getCustomPostTypes',
261
            array($this, 'filter_cpts')
262
        );
263
        add_filter(
264
            'FHEE__EE_Admin__get_extra_nav_menu_pages_items',
265
            array($this, 'nav_metabox_items')
266
        );
267
        EE_Registry::instance()->load_file(
268
            EE_CAFF_PATH,
269
            'EE_Caf_Messages',
270
            'class',
271
            array(),
272
            false
273
        );
274
        // caffeinated_init__complete hook
275
        do_action('AHEE__EE_Brewing_Regular__caffeinated_init__complete');
276
    }
277
278
279
    public function enqueue_caffeinated_scripts()
280
    {
281
        // sound of crickets...
282
    }
283
284
285
    /**
286
     * callbacks below here
287
     *
288
     * @param array $taxonomy_array
289
     * @return array
290
     */
291
    public function filter_taxonomies(array $taxonomy_array)
292
    {
293
        $taxonomy_array['espresso_venue_categories']['args']['show_in_nav_menus'] = true;
294
        return $taxonomy_array;
295
    }
296
297
298
    /**
299
     * @param array $cpt_array
300
     * @return mixed
301
     */
302
    public function filter_cpts(array $cpt_array)
303
    {
304
        $cpt_array['espresso_venues']['args']['show_in_nav_menus'] = true;
305
        return $cpt_array;
306
    }
307
308
309
    /**
310
     * @param array $menuitems
311
     * @return array
312
     */
313
    public function nav_metabox_items(array $menuitems)
314
    {
315
        $menuitems[] = array(
316
            'title'       => __('Venue List', 'event_espresso'),
317
            'url'         => get_post_type_archive_link('espresso_venues'),
318
            'description' => __('Archive page for all venues.', 'event_espresso'),
319
        );
320
        return $menuitems;
321
    }
322
323
324
    /**
325
     * Gets the injected table analyzer, or throws an exception
326
     *
327
     * @return TableAnalysis
328
     * @throws \EE_Error
329
     */
330
    protected function _get_table_analysis()
331
    {
332
        if ($this->_table_analysis instanceof TableAnalysis) {
333
            return $this->_table_analysis;
334
        } else {
335
            throw new \EE_Error(
336
                sprintf(
337
                    __('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
338
                    get_class($this)
339
                )
340
            );
341
        }
342
    }
343
}
344
345
346
$brewing = new EE_Brewing_Regular(
347
    EE_Registry::instance()->create('TableAnalysis', array(), true)
348
);
349