Completed
Branch FEAT-7553-PayPal-Express-PM (ff03cf)
by
unknown
77:21 queued 61:39
created

EE_Brewing_Regular::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
use EventEspresso\core\services\database\TableAnalysis;
3
4
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
5
	exit( 'No direct script access allowed' );
6
}
7
/**
8
 * the purpose of this file is to simply contain any action/filter hook callbacks etc for specific aspects of EE
9
 * related to caffeinated (regular) use.  Before putting any code in here, First be certain that it isn't better to
10
 * define and use the hook in a specific caffeinated/whatever class or file.
11
 */
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
/**
22
 * EE_Brewing_Regular class.  Just a wrapper to help namespace activity for the functionality of this file.
23
 *
24
 * @package        Event Espresso
25
 * @subpackage     /caffeinated/brewing_regular.php
26
 * @author         Darren Ethier
27
 */
28
class EE_Brewing_Regular extends EE_BASE {
29
	
30
	/**
31
	 * @var \EventEspresso\core\services\database\TableAnalysis $table_analysis
32
	 */
33
	protected $_table_analysis;
34
35
	/**
36
	 * EE_Brewing_Regular constructor.
37
	 */
38
	public function __construct( TableAnalysis $table_analysis ) {
39
		$this->_table_analysis = $table_analysis;
40
		if ( defined( 'EE_CAFF_PATH' ) ) {
41
			// activation
42
			add_action( 'AHEE__EEH_Activation__initialize_db_content', array( $this, 'initialize_caf_db_content' ) );
43
			// load caff init
44
			add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'caffeinated_init' ) );
45
			// remove the "powered by" credit link from receipts and invoices
46
			add_filter( 'FHEE_EE_Html_messenger__add_powered_by_credit_link_to_receipt_and_invoice', '__return_false' );
47
			// add caffeinated modules
48
			add_filter(
49
				'FHEE__EE_Config__register_modules__modules_to_register',
50
				array( $this, 'caffeinated_modules_to_register' )
51
			);
52
			// load caff scripts
53
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_caffeinated_scripts' ), 10 );
54
			add_filter( 'FHEE__EE_Registry__load_helper__helper_paths', array( $this, 'caf_helper_paths' ), 10 );
55
			add_filter(
56
				'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register',
57
				array( $this, 'caf_payment_methods' )
58
			);
59
			// caffeinated constructed
60
			do_action( 'AHEE__EE_Brewing_Regular__construct__complete' );
61
			//seeing how this is caf, which isn't put on WordPress.org, we can have affiliate links without a disclaimer
62
			add_filter( 'FHEE__ee_show_affiliate_links', '__return_false' );
63
		}
64
	}
65
66
67
68
	/**
69
	 * callback for the FHEE__EE_Registry__load_helper__helper_paths filter to add the caffeinated paths
70
	 *
71
	 * @param array $paths original helper paths array
72
	 * @return array             new array of paths
73
	 */
74
	public function caf_helper_paths( $paths ) {
75
		$paths[] = EE_CAF_CORE . 'helpers' . DS;
76
		return $paths;
77
	}
78
79
80
81
	/**
82
	 * Upon brand-new activation, if this is a new activation of CAF, we want to add
83
	 * some global prices that will show off EE4's capabilities. However, if they're upgrading
84
	 * from 3.1, or simply EE4.x decaf, we assume they don't want us to suddenly introduce these extra prices.
85
	 * This action should only be called when EE 4.x.0.P is initially activated.
86
	 * Right now the only CAF content are these global prices. If there's more in the future, then
87
	 * we should probably create a caf file to contain it all instead just a function like this.
88
	 * Right now, we ASSUME the only price types in the system are default ones
89
	 *
90
	 * @global wpdb $wpdb
91
	 */
92
	public function initialize_caf_db_content(){
93
		global $wpdb;
94
		//use same method of getting creator id as the version introducing the change
95
		$default_creator_id = apply_filters('FHEE__EE_DMS_Core_4_5_0__get_default_creator_id',get_current_user_id());
96
		$price_type_table = $wpdb->prefix."esp_price_type";
97
		$price_table = $wpdb->prefix."esp_price";
98
		if ( $this->_get_table_analysis()->tableExists( $price_type_table ) ) {
99
100
			$SQL = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table . ' WHERE PBT_ID=4';//include trashed price types
101
			$tax_price_type_count = $wpdb->get_var( $SQL );
102
			if ( $tax_price_type_count <= 1 ) {
103
				$wpdb->insert(
104
					$price_type_table,
105
					array(
106
						'PRT_name'       => __( "Regional Tax", "event_espresso" ),
107
						'PBT_ID'         => 4,
108
						'PRT_is_percent' => true,
109
						'PRT_order'      => 60,
110
						'PRT_deleted'    => false,
111
						'PRT_wp_user'    => $default_creator_id,
112
					),
113
					array(
114
						'%s',//PRT_name
115
						'%d',//PBT_id
116
						'%d',//PRT_is_percent
117
						'%d',//PRT_order
118
						'%d',//PRT_deleted
119
						'%d', //PRT_wp_user
120
					)
121
				);
122
				//federal tax
123
				$result = $wpdb->insert(
124
					$price_type_table,
125
					array(
126
						'PRT_name'       => __( "Federal Tax", "event_espresso" ),
127
						'PBT_ID'         => 4,
128
						'PRT_is_percent' => true,
129
						'PRT_order'      => 70,
130
						'PRT_deleted'    => false,
131
						'PRT_wp_user'    => $default_creator_id,
132
					),
133
					array(
134
						'%s',//PRT_name
135
						'%d',//PBT_id
136
						'%d',//PRT_is_percent
137
						'%d',//PRT_order
138
						'%d',//PRT_deleted
139
						'%d' //PRT_wp_user
140
					)
141
				);
142
				if ( $result ) {
143
					$wpdb->insert(
144
						$price_table,
145
						array(
146
							'PRT_ID'         => $wpdb->insert_id,
147
							'PRC_amount'     => 15.00,
148
							'PRC_name'       => __( "Sales Tax", "event_espresso" ),
149
							'PRC_desc'       => '',
150
							'PRC_is_default' => true,
151
							'PRC_overrides'  => null,
152
							'PRC_deleted'    => false,
153
							'PRC_order'      => 50,
154
							'PRC_parent'     => null,
155
							'PRC_wp_user'    => $default_creator_id,
156
						),
157
						array(
158
							'%d',//PRT_id
159
							'%f',//PRC_amount
160
							'%s',//PRC_name
161
							'%s',//PRC_desc
162
							'%d',//PRC_is_default
163
							'%d',//PRC_overrides
164
							'%d',//PRC_deleted
165
							'%d',//PRC_order
166
							'%d',//PRC_parent
167
							'%d' //PRC_wp_user
168
						)
169
					);
170
				}
171
			}
172
		}
173
	}
174
175
176
177
	/**
178
	 *    caffeinated_modules_to_register
179
	 *
180
	 * @access public
181
	 * @param array $modules_to_register
182
	 * @return array
183
	 */
184
	public function caffeinated_modules_to_register( $modules_to_register = array() ) {
185
		if ( is_readable( EE_CAFF_PATH . 'modules' ) ) {
186
			$caffeinated_modules_to_register = glob( EE_CAFF_PATH . 'modules' . DS . '*', GLOB_ONLYDIR );
187
			if ( is_array( $caffeinated_modules_to_register ) && ! empty( $caffeinated_modules_to_register ) ) {
188
				$modules_to_register = array_merge( $modules_to_register, $caffeinated_modules_to_register );
189
			}
190
		}
191
		return $modules_to_register;
192
	}
193
194
195
196
	public function caffeinated_init() {
197
		// EE_Register_CPTs hooks
198
		add_filter( 'FHEE__EE_Register_CPTs__get_taxonomies__taxonomies', array( $this, 'filter_taxonomies' ), 10 );
199
		add_filter( 'FHEE__EE_Register_CPTs__get_CPTs__cpts', array( $this, 'filter_cpts' ), 10 );
200
		add_filter( 'FHEE__EE_Admin__get_extra_nav_menu_pages_items', array( $this, 'nav_metabox_items' ), 10 );
201
		EE_Registry::instance()->load_file( EE_CAFF_PATH, 'EE_Caf_Messages', 'class', array(), false );
202
		// caffeinated_init__complete hook
203
		do_action( 'AHEE__EE_Brewing_Regular__caffeinated_init__complete' );
204
	}
205
206
207
208
	public function enqueue_caffeinated_scripts() {
209
		// sound of crickets...
210
	}
211
212
213
214
	/**
215
	 * callbacks below here
216
	 *
217
	 * @param array $taxonomy_array
218
	 * @return array
219
	 */
220
	public function filter_taxonomies( array $taxonomy_array ) {
221
		$taxonomy_array['espresso_venue_categories']['args']['show_in_nav_menus'] = true;
222
		return $taxonomy_array;
223
	}
224
225
226
227
	/**
228
	 * @param array $cpt_array
229
	 * @return mixed
230
	 */
231
	public function filter_cpts( array $cpt_array ) {
232
		$cpt_array['espresso_venues']['args']['show_in_nav_menus'] = true;
233
		return $cpt_array;
234
	}
235
236
237
238
	/**
239
	 * @param array $menuitems
240
	 * @return array
241
	 */
242 View Code Duplication
	public function nav_metabox_items( array $menuitems ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
		$menuitems[] = array(
244
			'title'       => __( 'Venue List', 'event_espresso' ),
245
			'url'         => get_post_type_archive_link( 'espresso_venues' ),
246
			'description' => __( 'Archive page for all venues.', 'event_espresso' ),
247
		);
248
		return $menuitems;
249
	}
250
251
252
253
	/**
254
	 * Adds the payment methods in {event-espresso-core}/caffeinated/payment_methods
255
	 *
256
	 * @param array $payment_method_paths
257
	 * @return array values are folder paths to payment method folders
258
	 */
259
	public function caf_payment_methods( $payment_method_paths ) {
260
		$caf_payment_methods_paths = glob( EE_CAF_PAYMENT_METHODS . '*', GLOB_ONLYDIR );
261
		$payment_method_paths = array_merge( $payment_method_paths, $caf_payment_methods_paths );
262
		return $payment_method_paths;
263
	}
264
	/**
265
	 * Gets the injected table analyzer, or throws an exception
266
	 * @return TableAnalysis
267
	 * @throws \EE_Error
268
	 */
269 View Code Duplication
	protected function _get_table_analysis() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
		if( $this->_table_analysis instanceof TableAnalysis ) {
271
			return $this->_table_analysis;
272
		} else {
273
			throw new \EE_Error( 
274
				sprintf( 
275
					__( 'Table analysis class on class %1$s is not set properly.', 'event_espresso'), 
276
					get_class( $this ) 
277
				) 
278
			);
279
		}
280
	}
281
}
282
$brewing = new EE_Brewing_Regular(
283
	EE_Registry::instance()->create( 'TableAnalysis', array(), true )
284
);
285