Repository navigation
Expand file tree
/
Copy pathghc-e3-workshops.php
More file actions
339 lines (299 loc) · 11.2 KB
/
Copy pathghc-e3-workshops.php
File metadata and controls
339 lines (299 loc) · 11.2 KB
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
<?php
/**
* Plugin Name: GHC E3 Bundle Workshopzs
* Plugin URI: https://github.com/greathomeschoolconventions/ghc-functionality
* Description: Add streaming-only workshop features
* Version: 1.0.0
* Author: AndrewRMinion Design
* Author URI: https://andrewrminion.com
* Copyright: 2017 AndrewRMinion Design
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package GHC_Functionality_Plugin
*/
if ( ! function_exists( 'add_filter' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
if ( ! function_exists( 'ghc_admin_options' ) ) {
include 'inc/acf.php';
}
/**
* Flush rewrite rules on (de)activation
*/
function ghc_workshops_activate() {
ghc_e3_cpts();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'ghc_workshops_activate' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
/**
* Register CPTs
*/
function ghc_e3_cpts() {
$workshops_labels = array(
'name' => 'E3 Workshops',
'singular_name' => 'E3 Workshop',
'menu_name' => 'E3 Workshops',
'name_admin_bar' => 'E3 Workshop',
'parent_item_colon' => 'Parent E3 Workshop:',
'all_items' => 'All E3 Workshops',
'add_new_item' => 'Add New E3 Workshop',
'add_new' => 'Add New',
'new_item' => 'New E3 Workshop',
'edit_item' => 'Edit E3 Workshop',
'update_item' => 'Update E3 Workshop',
'view_item' => 'View E3 Workshop',
'search_items' => 'Search E3 Workshop',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$workshops_rewrite = array(
'slug' => 'e3-workshops',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$workshops_args = array(
'label' => __( 'workshop', 'GHC' ),
'description' => __( 'Workshops', 'GHC' ),
'labels' => $workshops_labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'page-attributes' ),
'taxonomies' => array(),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 28,
'menu_icon' => 'dashicons-welcome-learn-more',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'my-account/e3-workshops',
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $workshops_rewrite,
'capability_type' => 'page',
);
register_post_type( 'e3_workshop', $workshops_args );
}
add_action( 'init', 'ghc_e3_cpts' );
/**
* Modify workshops query
*
* @param object $query WP_Query object.
* @return object modified WP_Query object
*/
function ghc_e3_post_order( $query ) {
if ( ! is_admin() && $query->is_archive && 'e3_workshop' === $query->get( 'post_type' ) ) {
$query->set( 'orderby', 'post_title' );
$query->set( 'order', 'ASC' );
$query->set( 'posts_per_page', '-1' );
}
return $query;
}
add_action( 'pre_get_posts', 'ghc_e3_post_order' );
/**
* Add media player and speaker bio to E3 workshop content
*
* @param string $content HTML content.
* @return string modified HTML content
*/
function ghc_e3_content( $content ) {
$new_content = '';
if ( 'e3_workshop' === get_post_type() ) {
$speaker_bio = get_field( 'e3_speaker_biography' );
$speaker_bio_content = '<a class="button expand-trigger">About ' . $speaker_name . ' <span class="dashicons dashicons-arrow-down-alt2"></span></a><div class="click-to-expand">' . $speaker_bio . '</div>';
if ( is_singular() ) {
$new_content .= '
<audio class="wp-audio-shortcode" controls="controls" preload="none" style="width: 100%">
<source type="audio/mpeg" src="' . ghc_e3_get_signed_url( get_field( 'e3_workshop_media_url' ) ) . '" />
</audio>';
if ( $speaker_bio ) {
$content .= '<p>' . $speaker_bio_content . '</p>';
}
wp_enqueue_style( 'wp-mediaelement' );
wp_enqueue_script( 'wp-mediaelement' );
} else {
$content .= '<p><a class="button" href="' . get_permalink() . '">Listen Now <span class="dashicons dashicons-arrow-right-alt2"></span></a>' . ( $speaker_bio ? $speaker_bio_content : '' ) . '</p>';
}
}
return $new_content . $content;
}
add_filter( 'the_content', 'ghc_e3_content' );
add_filter( 'the_excerpt', 'ghc_e3_content' );
/**
* Add speaker info to post thumbnail
*
* @param string $html featured image HTML.
* @param integer $post_id WP post ID.
* @param integer $post_thumbnail_id WP media post ID.
* @param string/array $size image size.
* @param array $attr query string of attributes.
* @return string featured image HTML
*/
function ghc_e3_thumbnail_content( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( 'e3_workshop' === get_post_type( $post_id ) ) {
$speaker_name = get_field( 'e3_speaker_name' );
$speaker_company = get_field( 'e3_speaker_company' );
$speaker_company_url = get_field( 'e3_speaker_company_url' );
$html .= '<p class="entry-meta"><span class="speaker-name">' . $speaker_name . '</span>';
if ( $speaker_company ) {
$html .= '<br/>';
if ( $speaker_company_url && is_user_logged_in() ) {
$html .= '<a class="speaker-company" target="_blank" rel="noopener noreferrer" href="' . $speaker_company_url . '">' . $speaker_company . '</a>';
} else {
$html .= '<span class="speaker-company">' . $speaker_company . '</span>';
}
}
$html .= '</p>';
}
return $html;
}
add_filter( 'post_thumbnail_html', 'ghc_e3_thumbnail_content', 10, 5 );
/**
* Generate signed URL
*
* @param string $resource resource URL.
* @return string signed resource URL
*/
function ghc_e3_get_signed_url( $resource ) {
$key_pair_id = get_field( 'aws_cloudfront_key_pair_id', 'option' );
$private_key = get_field( 'aws_cloudfront_private_key', 'option' );
$aws_s3_domain = get_field( 'aws_s3_domain', 'option' );
$aws_cloudfront_domain = get_field( 'aws_cloudfront_domain', 'option' );
// Use CloudFront domain instead of S3 domain.
$resource = str_replace( $aws_s3_domain, $aws_cloudfront_domain, $resource );
// Get expiration time.
$expires = time() + get_field( 'aws_cloudfront_signed_url_lifetime', 'option' );
$json = '{"Statement":[{"Resource":"' . $resource . '","Condition":{"DateLessThan":{"AWS:EpochTime":' . $expires . '}}}]}';
// Get private key.
$key = openssl_get_privatekey( $private_key );
if ( ! $key ) {
error_log( 'Failed to read private key: ' . openssl_error_string() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
return $resource;
}
// Sign the policy with the private key.
if ( ! openssl_sign( $json, $signed_policy, $key ) ) {
error_log( 'Failed to sign url: ' . openssl_error_string() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
return $resource;
}
// Create signature.
$base64_signed_policy = base64_encode( $signed_policy ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$signature = str_replace( array( '+', '=', '/' ), array( '-', '_', '~' ), $base64_signed_policy );
// Construct the URL.
$url = $resource . '?Expires=' . $expires . '&Signature=' . $signature . '&Key-Pair-Id=' . $key_pair_id;
return $url;
}
/**
* Tweak archive title
*
* @param string $title HTML archive title.
* @return string modified HTML archive title
*/
function ghc_e3_archive_title( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>';
} elseif ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
} elseif ( is_tax() ) {
$title = single_term_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'ghc_e3_archive_title' );
/**
* Redirect to Checkout page when product is added to cart
*
* @return string checkout URL
*/
function ghc_e3_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'ghc_e3_add_to_cart_redirect' );
/**
* Change text on add-to-cart buttons
*
* @return string button label
*/
function ghc_e3_cart_button_text() {
return __( 'Buy Now', 'woocommerce' );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'ghc_e3_cart_button_text' );
/**
* Add shortcode for purchase page
*
* @param array $attributes shortcode attributes.
* @return string HTML content
*/
function ghc_e3_workshop_promo_list( $attributes ) {
$shortcode_attributes = shortcode_atts(
array(
'posts_per_page' => -1,
'offset' => null,
), $attributes
);
ob_start();
$shortcode_query_args = array(
'post_type' => 'e3_workshop',
'posts_per_page' => $shortcode_attributes['posts_per_page'],
'offset' => $shortcode_attributes['offset'],
);
$shortcode_query = new WP_Query( $shortcode_query_args );
if ( $shortcode_query->have_posts() ) {
echo '<section class="e3-shortcode-container">';
while ( $shortcode_query->have_posts() ) {
$shortcode_query->the_post();
echo '<article class="' . esc_attr( implode( ' ', get_post_class() ) ) . '">';
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'speaker_xs', array( 'class' => '' ) );
}
echo '<h3>' . esc_attr( get_the_title() ) . '</h3>
<p><a class="workshop-description expand-trigger dashicons-after">Workshop Description <span class="dashicons dashicons-arrow-down-alt2"></span></a></p>
<div class="excerpt click-to-expand">' . wp_kses_post( get_the_content() ) . '</div>
</article>';
}
echo '</section>';
}
return ob_get_clean();
}
add_shortcode( 'e3_workshop_promo_list', 'ghc_e3_workshop_promo_list' );
/**
* Add shortcode for buy now button
*
* @param array $attributes shortcode attributes.
* @return string HTML content
*/
function ghc_e3_buy_now_button( $attributes ) {
$shortcode_attributes = shortcode_atts(
array(
'product_id' => null,
'button_text' => 'Buy Now',
), $attributes
);
ob_start();
if ( $shortcode_attributes['product_id'] ) {
echo '<section class="buy-now">
<p><a class="button" rel="nofollow" href="' . esc_url( home_url() ) . '/checkout/?add-to-cart=' . esc_attr( $shortcode_attributes['product_id'] ) . '">' . esc_attr( $shortcode_attributes['button_text'] ) . '</a><br/>
<img src="' . esc_url( plugin_dir_url( __FILE__ ) ) . 'dist/images/svg/credit-cards.svg" alt="credit card icons" /></p>
</section>';
}
return ob_get_clean();
}
add_shortcode( 'e3_buy_now', 'ghc_e3_buy_now_button' );