Skip to content

Commit ae36f4e

Browse files
authored
Merge pull request #79 from andrewlimaza/dev
Strip shortcode for users that cannot edit users.
2 parents 716190a + 511fdd8 commit ae36f4e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

pmpro-membership-maps.php

+75
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,78 @@ function pmpro_geocode_billing_address_fields_frontend( $user_id ){
931931
add_action( 'pmpro_personal_options_update', 'pmpro_geocode_billing_address_fields_frontend', 10, 1 );
932932
add_action( 'personal_options_update', 'pmpro_geocode_billing_address_fields_frontend', 10, 1 );
933933
add_action( 'edit_user_profile_update', 'pmpro_geocode_billing_address_fields_frontend', 10, 1 );
934+
935+
/**
936+
* Strip the [pmpro_membership_maps] shortcode from content if the current user can't edit users.
937+
*
938+
* @since 0.7
939+
940+
* @param string|array $content The content to strip the shortcode from.
941+
* If an array is passed in, all elements
942+
* will be filtered recursively.
943+
* Non-strings are ignored.
944+
*
945+
* @return mixed The content with the shortcode removed. Will be the same type as the input.
946+
*/
947+
function pmpromm_maybe_strip_shortcode( $content ) {
948+
// If the user can edit users, we don't need to strip the shortcode.
949+
if ( current_user_can( 'edit_users' ) ) {
950+
return $content;
951+
}
952+
953+
// If an array is passed in, filter all elements recursively.
954+
if ( is_array( $content ) ) {
955+
foreach ( $content as $key => $value ) {
956+
$content[ $key ] = pmpromm_maybe_strip_shortcode( $value );
957+
}
958+
return $content;
959+
}
960+
961+
// If we're not looking at a string, just return it.
962+
if ( ! is_string( $content ) ) {
963+
return $content;
964+
}
965+
966+
// Okay, we have a string, figure out the regex.
967+
$shortcodeRegex = get_shortcode_regex( array( 'pmpro_membership_maps' ) );
968+
969+
// Replace shortcode wrapped in block comments.
970+
$blockWrapperPattern = "/<!-- wp:shortcode -->\s*$shortcodeRegex\s*<!-- \/wp:shortcode -->/s";
971+
$content = preg_replace( $blockWrapperPattern, '', $content );
972+
973+
// Replace the shortcode by itself.
974+
$shortcodePattern = "/$shortcodeRegex/";
975+
$content = preg_replace( $shortcodePattern, '', $content );
976+
977+
return $content;
978+
}
979+
add_filter( 'content_save_pre', 'pmpromm_maybe_strip_shortcode' );
980+
add_filter( 'excerpt_save_pre', 'pmpromm_maybe_strip_shortcode' );
981+
add_filter( 'widget_update_callback', 'pmpromm_maybe_strip_shortcode' );
982+
983+
/**
984+
* Only allow those with the edit_users capability
985+
* to use the pmpro_membership_maps shortcode in post_meta.
986+
*
987+
* @since 0.7
988+
* @param int $meta_id ID of the meta data entry.
989+
* @param int $object_id ID of the object the meta is attached to.
990+
* @param string $meta_key Meta key.
991+
* @param mixed $_meta_value Meta value.
992+
* @return void
993+
*/
994+
function pmpromm_maybe_strip_shortcode_from_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
995+
// Bail if the value is not a string or array.
996+
if ( ! is_string( $_meta_value ) && ! is_array( $_meta_value ) ) {
997+
return;
998+
}
999+
1000+
// Strip the shortcode from the meta value.
1001+
$stripped_value = pmpromm_maybe_strip_shortcode( $_meta_value );
1002+
1003+
// If there was a change, save our stripped version.
1004+
if ( $stripped_value !== $_meta_value ) {
1005+
update_post_meta( $object_id, $meta_key, $stripped_value );
1006+
}
1007+
}
1008+
add_action( 'updated_post_meta', 'pmpromm_maybe_strip_shortcode_from_post_meta', 10, 4 );

0 commit comments

Comments
 (0)