· 4 years ago · Jun 11, 2021, 07:44 PM
1<?php if(!defined('ABSPATH')) { die(); } // Include in all php files, to prevent direct execution
2
3/************ membership account page ***************/
4
5// display logged in username with shortcode [current_user] aniketos in title
6add_filter( 'the_title', 'do_shortcode' );
7
8function custom_shortcode_func() {
9 ob_start();
10 $current_user = wp_get_current_user();
11
12 if ( is_user_logged_in() ) {
13 echo '' . $current_user->display_name . "";
14 }
15 else {
16 wp_loginout();
17 }
18
19 return ob_get_clean();
20}
21
22add_shortcode('current_user', 'custom_shortcode_func');
23
24// woo - display woocommerce orders shortcode
25function shortcode_my_orders( $atts ) {
26 extract( shortcode_atts( array(
27 'order_count' => -1
28 ), $atts ) );
29
30 ob_start();
31 wc_get_template( 'myaccount/my-orders.php', array(
32 'current_user' => get_user_by( 'id', get_current_user_id() ),
33 'order_count' => $order_count
34 ) );
35 return ob_get_clean();
36}
37add_shortcode('my_orders', 'shortcode_my_orders');
38
39// woo - display woocommerce downloads shortcode
40function shortcode_my_downloads( $atts ) {
41 extract( shortcode_atts( array(
42 'get_downloadable_products' => -1
43 ), $atts ) );
44
45 ob_start();
46 wc_get_template( 'myaccount/downloads.php', array(
47 'current_user' => get_user_by( 'id', get_current_user_id() ),
48 ) );
49 return ob_get_clean();
50}
51add_shortcode('downloads', 'shortcode_my_downloads');
52
53// woo - member acct page remove cancelled orders
54add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_orders' );
55function custom_my_orders( $args ) {
56
57 if (($key = array_search('wc-cancelled', $args['post_status'])) !== false) {
58 unset($args['post_status'][$key]);
59 }
60 return $args;
61}
62
63/********************** automate freeconference call rss to posts ***************************/
64
65// assign Sacred Space Recordings category to Conference Recording and publish posts Aniketos
66function assign_categories_for_title( $post_id, $post, $update = false ) {
67 // Skip update saves?
68 if( $update )
69 return;
70
71 // Skip revision saves
72 if( wp_is_post_revision( $post_id ) )
73 return;
74
75 // Mapping of *lowercase* title fragments to desired category IDs. Can look up ID with `get_cat_ID( 'Cat Name' )`
76 $title_cats = array(
77 'sacred space' => get_cat_ID( 'Sacred Space Recordings' ),
78 );
79
80 $post_title = strtolower( $post->post_title ); // Convert the title to lowercase for a case-insensitive check.
81 $add_cats = array(); // Categories to add to the post.
82
83 // See if the post title has starts with any of the mapped title fragments, and push the cat ID to be added if so.
84 foreach( $title_cats as $title_fragment => $cat_id ) {
85 if( strpos( $post_title, $title_fragment ) === 0 )
86 $add_cats[] = $cat_id;
87 }
88
89 if( count( $add_cats ) > 0 ) {
90 // If we've categories to add, add them.
91 wp_set_post_categories( $post_id, $add_cats, true );
92
93 // If categories were added and the post_status is draft, publish it.
94 if( $post->post_status === 'draft' ) {
95 remove_action( 'save_post', 'assign_categories_for_title', 10, 3 );
96
97 // coordinates server time with publsih time in wp
98 wp_update_post(
99 array(
100 'ID' => $post_id,
101 'post_status' => 'publish',
102 'post_date' => current_time('mysql'),
103 'post_date_gmt' => get_gmt_from_date( $time )
104 )
105 );
106
107
108 // If posts were deleted/borked, reset publish dates
109 wp_update_post(
110 array(
111 'ID' => $post_id,
112 'post_status' => 'publish',
113 )
114 );
115
116 add_action( 'transition_post_status', 'assign_categories_for_title', 10, 3 );
117 }
118 }
119}
120
121add_action( 'save_post', 'assign_categories_for_title', 10, 3 );
122
123
124function assign_post_title_categories_retroactively() {
125
126 // The total pool of posts is small - we'll just loop through all of them. This would be not great on a big site.
127 $query = new WP_Query(
128 array(
129 'post_type' => 'post',
130 'posts_per_page' => -1
131 )
132 );
133 foreach( $query->posts as $post )
134 assign_categories_for_title( $post->ID, $post );
135}
136add_action( 'init', 'assign_post_title_categories_retroactively' );
137
138
139
140/************ mailchimp / pmpro ***************/
141
142/**
143 * Send users' next payment date to Mailchimp.
144 **/
145function my_pmpromc_update_on_subscription_payment_completed( $morder ) {
146 if ( ! function_exists( 'pmpromc_getAPI' ) ) {
147 // PMPro Mailchimp is not active.
148 return;
149 }
150
151 $user = get_userdata( $morder->user_id );
152
153 // Get API and bail if we can't set it.
154 $api = pmpromc_getAPI();
155 if ( empty( $api ) || empty( $user ) ) {
156 return;
157 }
158
159 // Get all audiences.
160 $audiences = $api->get_all_lists();
161
162 if ( ! empty( $audiences ) ) {
163 foreach ( $audiences as $audience ) {
164 // Check for member.
165 $member = $api->get_listinfo_for_member( $audience->id, $user );
166 if ( isset( $member->status ) ) {
167 // Update the user's merge fields.
168 pmpromc_add_audience_member_update( $user->ID, $audience->id, $member->status );
169 }
170 }
171 }
172}
173add_action( 'pmpro_subscription_payment_completed', 'my_pmpromc_update_on_subscription_payment_completed' );
174
175function my_pmpro_mailchimp_listsubscribe_fields_nextpayment( $fields, $user ) {
176 // Get user's next payment date.
177 $next_payment_date = pmpro_next_payment( $user->ID );
178
179 if ( ! empty( $next_payment_date ) ) {
180 $fields['NEXTPAYMNT'] = date( 'M', $next_payment_date );
181 } else {
182 $fields['NEXTPAYMNT'] = 'None';
183 }
184
185 return $fields;
186}
187add_action( 'pmpro_mailchimp_listsubscribe_fields', 'my_pmpro_mailchimp_listsubscribe_fields_nextpayment', 10, 2 );
188
189/*
190 * Creates an NEXTPAYMNT merge field in MailChimp.
191 */
192function my_pmpro_mailchimp_merge_field_nextpayment( $merge_fields ) {
193 $merge_fields[] = array(
194 'name' => 'NEXTPAYMNT',
195 'type' => 'text'
196 );
197 return $merge_fields;
198}
199add_filter( 'pmpro_mailchimp_merge_fields', 'my_pmpro_mailchimp_merge_field_nextpayment' );
200
201
202/**
203 * Change text string on cart opt in.
204 *
205 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
206 */
207function my_text_strings( $translated_text, $text, $domain ) {
208 switch ( $translated_text ) {
209 case 'Subscribe to our newsletter' :
210 $translated_text = __( 'I understand this content will arrive by email.', 'woocommerce' );
211 break;
212 }
213 return $translated_text;
214}
215add_filter( 'gettext', 'my_text_strings', 20, 3 );
216
217
218/************************ PAID MEMBERSHIPS PRO ***************************/
219
220/**
221 * This recipe Hide Previous Content From New Members EXCEPTION for Addon Packages.
222 *
223 * https://www.paidmembershipspro.com/add-ons/pmpro-purchase-access-to-a-single-page/
224 * based on this post: https://www.paidmembershipspro.com/hide-previous-content-from-new-members/
225 *
226 * You can add this recipe to your site by creating a custom plugin
227 * or using the Code Snippets plugin available for free in the WordPress repository.
228 * Read this companion article for step-by-step directions on either method.
229 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
230 */
231
232add_filter("pmpro_has_membership_access_filter", "hide_old_posts_from_members", 10, 4);
233function hide_old_posts_from_members($hasaccess, $thepost, $theuser, $post_membership_levels)
234{
235 global $wpdb;
236 //if PMPro says false already, return false
237 if(!$hasaccess)
238 return false;
239 //if the post doesn't require membership, allow access
240 if(!$post_membership_levels)
241 return true;
242 //okay, this post requires membership. start by getting the user's startdate
243 $startdate = pmpro_getMemberStartdate($theuser->ID);
244 //no startdate? return false
245 if(empty($startdate))
246 return false;
247 //if the startdate is before the post date, return true
248 if($startdate < strtotime($thepost->post_date))
249 return true;
250 else
251 {
252 //in this case we want to also tweak the message shown
253 add_filter("pmpro_non_member_text_filter", "swap_old_posts_member_text");
254 return false;
255 }
256}
257function swap_old_posts_member_text($s)
258{
259 $s = "This content was published before your membership started.";
260 return $s;
261}
262/**
263 * Filter pmpro_has_membership_access based on addon access.
264 */
265function my_pmproap_pmpro_has_membership_access_filter( $hasaccess, $mypost, $myuser, $post_membership_levels ) {
266
267 remove_filter( 'pmpro_has_membership_access_filter', 'pmproap_pmpro_has_membership_access_filter', 10, 4 );
268 // If the user doesn't have access already, we won't change that. So only check if they already have access.
269 if ( $hasaccess ) {
270
271 // is this post locked down via an addon?
272 if ( pmproap_isPostLocked( $mypost->ID ) ) {
273 // okay check if the user has access
274 if ( pmproap_hasAccess( $myuser->ID, $mypost->ID ) ) {
275 remove_filter("pmpro_has_membership_access_filter", "hide_old_posts_from_members", 10, 4);
276
277 $hasaccess = true;
278 } else {
279 $hasaccess = false;
280 }
281 }
282 }
283 return $hasaccess;
284}
285add_filter( 'pmpro_has_membership_access_filter', 'my_pmproap_pmpro_has_membership_access_filter', 8, 4 );
286
287/**
288 * When a Member cancles or expires, they will get assigned a free level
289 * change line 13 to your freelevel id number
290 * You can add this recipe to your site by creating a custom plugin
291 * or using the Code Snippets plugin available for free in the WordPress repository.
292 * Read this companion article for step-by-step directions on either method.
293 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
294 */
295
296function my_pmpro_after_change_membership_level( $level_id, $user_id ) {
297 // TODO: Replace '1' with level id of free level
298 $free_level = 1;
299
300 if ( $level_id == 0 && empty( $_REQUEST['delete_account'] ) ) {
301 //cancelling, give them level 1 instead
302 pmpro_changeMembershipLevel( $free_level, $user_id );
303 }
304}
305add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
306add_action( 'pmpro_membership_post_membership_expiry', 'my_pmpro_after_change_membership_level', 10, 2 );
307
308
309
310?>