forked from jacopotarantino/crowdfunding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrowdfunding.php
357 lines (296 loc) · 10.4 KB
/
crowdfunding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Plugin Name: Crowdfunding by Astoundify
* Plugin URI: https://github.com/astoundify/crowdfunding/
* Description: A crowd funding platform in the likes of Kickstarter and Indigogo
* Author: Astoundify
* Author URI: http://astoundify.com
* Version: 1.7.1
* Text Domain: atcf
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/** Check if Easy Digital Downloads is active */
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
/**
* Main Crowd Funding Class
*
* @since Astoundify Crowdfunding 0.1-alpha
*/
final class ATCF_CrowdFunding {
/**
* @var crowdfunding The one true AT_CrowdFunding
*/
private static $instance;
/**
* Main Crowd Funding Instance
*
* Ensures that only one instance of Crowd Funding exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since Astoundify Crowdfunding 0.1-alpha
*
* @return The one true Crowd Funding
*/
public static function instance() {
if ( ! isset ( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Start your engines.
*
* @since Astoundify Crowdfunding 1.5
*
* @return void
*/
public function __construct() {
$this->setup_globals();
$this->includes();
$this->setup_actions();
}
/** Private Methods *******************************************************/
/**
* Set some smart defaults to class variables. Allow some of them to be
* filtered to allow for early overriding.
*
* @since Astoundify Crowdfunding 0.1-alpha
*
* @return void
*/
private function setup_globals() {
/** Versions **********************************************************/
$this->version = '1.7.1';
$this->db_version = '1';
/** Paths *************************************************************/
$this->file = __FILE__;
$this->basename = apply_filters( 'atcf_plugin_basenname', plugin_basename( $this->file ) );
$this->plugin_dir = apply_filters( 'atcf_plugin_dir_path', plugin_dir_path( $this->file ) );
$this->plugin_url = apply_filters( 'atcf_plugin_dir_url', plugin_dir_url ( $this->file ) );
$this->template_url = apply_filters( 'atcf_plugin_template_url', 'crowdfunding/' );
// Includes
$this->includes_dir = apply_filters( 'atcf_includes_dir', trailingslashit( $this->plugin_dir . 'includes' ) );
$this->includes_url = apply_filters( 'atcf_includes_url', trailingslashit( $this->plugin_url . 'includes' ) );
// Languages
$this->lang_dir = apply_filters( 'atcf_lang_dir', trailingslashit( $this->plugin_dir . 'languages' ) );
/** Misc **************************************************************/
$this->domain = 'atcf';
}
/**
* Include required files.
*
* @since Astoundify Crowdfunding 0.1-alpha
*
* @return void
*/
private function includes() {
require( $this->includes_dir . 'class-install.php' );
require( $this->includes_dir . 'class-campaigns.php' );
require( $this->includes_dir . 'class-campaign.php' );
require( $this->includes_dir . 'class-roles.php' );
require( $this->includes_dir . 'settings.php' );
require( $this->includes_dir . 'gateways.php' );
require( $this->includes_dir . 'theme-stuff.php' );
require( $this->includes_dir . 'shipping.php' );
require( $this->includes_dir . 'logs.php' );
require( $this->includes_dir . 'export.php' );
require( $this->includes_dir . 'permalinks.php' );
require( $this->includes_dir . 'checkout.php' );
require( $this->includes_dir . 'shortcode-submit.php' );
require( $this->includes_dir . 'shortcode-profile.php' );
require( $this->includes_dir . 'shortcode-login.php' );
require( $this->includes_dir . 'shortcode-register.php' );
do_action( 'atcf_include_files' );
if ( ! is_admin() )
return;
do_action( 'atcf_include_admin_files' );
}
/**
* Setup the default hooks and actions
*
* @since Astoundify Crowdfunding 0.1-alpha
*
* @return void
*/
private function setup_actions() {
add_action( 'init', array( $this, 'is_edd_activated' ), 1 );
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) );
add_filter( 'template_include', array( $this, 'template_loader' ) );
$this->load_textdomain();
register_activation_hook( $this->file, array( 'ATCF_Install', 'init' ), 10 );
do_action( 'atcf_setup_actions' );
}
/**
* Easy Digital Downloads
*
* @since Astoundify Crowdfunding 0.2-alpha
*
* @return void
*/
function is_edd_activated() {
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
if ( is_plugin_active( $this->basename ) ) {
deactivate_plugins( $this->basename );
unset( $_GET[ 'activate' ] ); // Ghetto
add_action( 'admin_notices', array( $this, 'edd_notice' ) );
}
}
}
/**
* Admin notice.
*
* @since Astoundify Crowdfunding 0.2-alpha
*
* @return void
*/
function edd_notice() {
?>
<div class="updated">
<p><?php printf(
__( '<strong>Notice:</strong> Crowdfunding by Astoundify requires <a href="%s">Easy Digital Downloads</a> in order to function properly.', 'atcf' ),
wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=easy-digital-downloads' ), 'install-plugin_easy-digital-downloads' )
); ?></p>
</div>
<?php
}
/**
* Load a template.
*
* Handles template usage so that we can use our own templates instead of the themes.
*
* Templates are in the 'templates' folder. AT_CrowdFunding looks for theme
* overides in /theme_directory/crowdfunding/ by default
*
* @see https://github.com/woothemes/woocommerce/blob/master/woocommerce.php
*
* @access public
* @param mixed $template
* @return string $template The path of the file to include
*/
public function template_loader( $template ) {
global $wp_query;
$find = array();
$files = array();
/** Check if we are editing */
if ( isset ( $wp_query->query_vars[ 'edit' ] ) &&
is_singular( 'download' ) &&
( $wp_query->queried_object->post_author == get_current_user_id() || current_user_can( 'manage_options' ) ) &&
atcf_theme_supports( 'campaign-edit' )
) {
do_action( 'atcf_found_edit' );
$files = apply_filters( 'atcf_crowdfunding_templates_edit', array( 'single-campaign-edit.php' ) );
}
/** Check if viewing a widget */
else if ( isset ( $wp_query->query_vars[ 'widget' ] ) &&
is_singular( 'download' ) &&
atcf_theme_supports( 'campaign-widget' )
) {
do_action( 'atcf_found_widget' );
$files = apply_filters( 'atcf_crowdfunding_templates_widget', array( 'campaign-widget.php' ) );
}
/** Check if viewing standard campaign */
else if ( is_singular( 'download' ) ) {
do_action( 'atcf_found_single' );
$files = apply_filters( 'atcf_crowdfunding_templates_campaign', array( 'single-campaign.php', 'single-download.php', 'single.php' ) );
}
/** Check if viewing archives */
else if ( is_post_type_archive( 'download' ) || is_tax( 'download_category' ) ) {
do_action( 'atcf_found_archive' );
$files = apply_filters( 'atcf_crowdfunding_templates_archive', array( 'archive-campaigns.php', 'archive-download.php', 'archive.php' ) );
}
$files = apply_filters( 'atcf_template_loader', $files );
foreach ( $files as $file ) {
$find[] = $file;
$find[] = $this->template_url . $file;
}
if ( ! empty( $files ) ) {
$template = locate_template( $find );
if ( ! $template )
$template = $this->plugin_dir . 'templates/' . $file;
}
return $template;
}
/**
* Load scripts.
*
* @since Astoundify 1.6
*
* @param mixed $template
* @return string $template The path of the file to include
*/
public function frontend_scripts() {
global $edd_options;
$is_submission = is_page( $edd_options[ 'submit_page' ] ) || did_action( 'atcf_found_edit' );
$is_campaign = is_singular( 'download' ) || did_action( 'atcf_found_single' ) || apply_filters( 'atcf_is_campaign_page', false );
if ( ! ( $is_submission || $is_campaign ) )
return;
if ( $is_campaign ) {
wp_enqueue_script( 'formatCurrency', $this->plugin_url . 'assets/js/jquery.formatCurrency-1.4.0.pack.js', array( 'jquery' ) );
}
wp_enqueue_script( 'atcf-scripts', $this->plugin_url . 'assets/js/crowdfunding.js', array( 'jquery' ) );
$settings = array(
'pages' => array(
'is_submission' => $is_submission,
'is_campaign' => $is_campaign
)
);
if ( $is_submission ) {
$settings[ 'submit' ] = array(
array(
'i18n' => array(
'oneReward' => __( 'At least one reward is required.', 'atcf' )
)
)
);
}
if ( $is_campaign ) {
global $post;
$campaign = atcf_get_campaign( $post );
$settings[ 'campaign' ] = array(
'i18n' => array(),
'isDonations' => $campaign->is_donations_only(),
'currency' => array(
'thousands' => $edd_options[ 'thousands_separator' ],
'decimal' => $edd_options[ 'decimal_separator' ],
'symbol' => edd_currency_filter( '' )
)
);
}
wp_localize_script( 'atcf-scripts', 'atcfSettings', $settings );
}
/**
* Loads the plugin language files
*
* @since Astoundify Crowdfunding 0.1-alpha
*/
public function load_textdomain() {
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), $this->domain );
$mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
// Setup paths to current locale file
$mofile_local = $this->lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/' . $this->domain . '/' . $mofile;
// Look in global /wp-content/languages/atcf folder
load_textdomain( $this->domain, $mofile_global );
// Look in local /wp-content/plugins/appthemer-crowdfunding/languages/ folder
load_textdomain( $this->domain, $mofile_local );
}
}
/**
* The main function responsible for returning the one true Crowd Funding Instance
* to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $crowdfunding = crowdfunding(); ?>
*
* @since Astoundify Crowdfunding 0.1-alpha
*
* @return The one true Crowd Funding Instance
*/
function crowdfunding() {
return ATCF_CrowdFunding::instance();
}
crowdfunding();