Skip to content

Instantly share code, notes, and snippets.

@marlonsabala
Created June 21, 2022 11:00
Show Gist options
  • Save marlonsabala/256393d2c3c93bafcf864a6dd71a49c4 to your computer and use it in GitHub Desktop.
Save marlonsabala/256393d2c3c93bafcf864a6dd71a49c4 to your computer and use it in GitHub Desktop.
/* adding buttons to new grid START */
function dev_card_header() {
$user_id = get_current_user_id();
$post = wpgb_get_post();
$post_id = $post->ID;
$gamipress_restrict_content_id = get_post_meta( $post_id, 'gamipress_restrict_content_id', true );
if( empty( $gamipress_restrict_content_id ) ) {
$access = true;
}
else {
$access = gamipress_restrict_content_is_user_granted_to_content( $gamipress_restrict_content_id, $user_id );
}
if( $access ) {
?>
<div class="pwc-purchased-tag">unlocked</div>
<?php
}
$wpdevlms_bb_nf_sel_menu = get_post_meta( $post_id, 'wpdevlms_bb_nf_sel_menu', true );
if( $wpdevlms_bb_nf_sel_menu ) {
if( wp_fusion()->access->user_can_access( $post_id ) && !wp_fusion()->user->has_tag( get_post_field( 'post_name', $post_id ) . '_VIEW' ) ) {
$bubble_post_ids[] = $value['post_id']; // adding accessible post id in class variable.
$wpdevlms_bb_nf_sel_menu_ids = explode( '|', $wpdevlms_bb_nf_sel_menu );
if( !empty( $wpdevlms_bb_nf_sel_menu_ids ) ) {
?>
<span class="wpdevlms-bb"></span>
<?php
}
}
}
}
function bb_nf_script_footer_pwc_gp_has_access() {
$user_id = get_current_user_id();
if( null == $all_sel_menu_ids ) {
global $wpdb;
//$query = 'SELECT post_id, meta_value FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key = \'gamipress_restrict_content_id\'';
$query = 'SELECT post_id, meta_value FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key = \'wpdevlms_bb_nf_sel_menu\'';
$results = $wpdb->get_results($query, ARRAY_A);
$all_sel_menu_ids = $results;
}
$all_meta = $all_sel_menu_ids;
if ( ! empty( $all_meta ) ) {
$all_menu_ids = array();
$bubble_post_ids = array();
foreach ($all_meta as $key => $value) {
$gamipress_restrict_content_id = get_post_meta( $value['post_id'], 'gamipress_restrict_content_id', true );
if( empty( $gamipress_restrict_content_id ) ) {
$access = true;
}
else {
$access = gamipress_restrict_content_is_user_granted_to_content( $gamipress_restrict_content_id, $user_id );
}
if( $access ) {
$bubble_post_ids[] = $value['post_id']; // adding accessible post id in class variable.
$menu_arr = explode('|', $value['meta_value']);
$all_menu_ids[] = $menu_arr[0];
if(isset($menu_arr[1])) {
// Submenu item.
$all_menu_ids[] = $menu_arr[1];
}
}
}
// Remove duplicate items.
$all_menu_ids = array_unique($all_menu_ids);
$all_menu_ids = $all_menu_ids;
$is_looped = true;
}
if(!empty($bubble_post_ids)) {
$posts_permalinks = array();
foreach ($bubble_post_ids as $post_id) {
$posts_permalinks[$post_id] = get_permalink($post_id);
}
?>
<input type="hidden" id="wpdevlms_gamipress_restrict_content_posts_href" value='<?php echo json_encode( $posts_permalinks ); ?>' />
<style type="text/css">
.wpgb-card .wpgb-card-wrapper:not(.pwc-gp-has-access) .unlocked_item_tag {
display: none;
}
</style>
<script type="text/javascript">
jQuery( document ).ready( function() {
var wpdevlms_bb_posts_href = <?php echo json_encode($posts_permalinks); ?>;
append_gamipress_restrict_content_class_on_grid();
function append_gamipress_restrict_content_class_on_grid() {
if( jQuery( '.wpgb-card' ).length ) {
jQuery.each( wpdevlms_bb_posts_href, function( post_id, permalink ) {
if( jQuery( document.body ).find( "a.wpgb-card-layer-link[href='"+permalink+"']" ).length ) {
var referencePoint = jQuery( document.body ).find( "a.wpgb-card-layer-link[href='"+permalink+"']" );
var wpgbCardWrapper = referencePoint.parents( '.wpgb-card-wrapper' );
wpgbCardWrapper.addClass( 'pwc-gp-has-access' );
}
} );
}
}
});
</script>
<?php
}
}
// add_action( 'wp_footer', 'bb_nf_script_footer', 99 );
function bb_nf_script_footer() {
if( null == $all_sel_menu_ids ) {
global $wpdb;
$query = 'SELECT post_id, meta_value FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key = \'wpdevlms_bb_nf_sel_menu\'';
$results = $wpdb->get_results($query, ARRAY_A);
$all_sel_menu_ids = $results;
}
$all_meta = $all_sel_menu_ids;
if ( ! empty( $all_meta ) ) {
$all_menu_ids = array();
$bubble_post_ids = array();
foreach ($all_meta as $key => $value) {
// Check if user has access to the post. If no, then unset element from an array.
// unset($all_meta[$key]);
$content_id = get_post_meta( $value['post_id'], '', true );
if(wp_fusion()->access->user_can_access($value['post_id']) && !wp_fusion()->user->has_tag( get_post_field( 'post_name', $value['post_id'] ) . '_VIEW')) {
$bubble_post_ids[] = $value['post_id']; // adding accessible post id in class variable.
$menu_arr = explode('|', $value['meta_value']);
$all_menu_ids[] = $menu_arr[0];
if(isset($menu_arr[1])) {
// Submenu item.
$all_menu_ids[] = $menu_arr[1];
}
}
}
// Remove duplicate items.
$all_menu_ids = array_unique($all_menu_ids);
$all_menu_ids = $all_menu_ids;
$is_looped = true;
}
if(!empty($bubble_post_ids)) {
$posts_permalinks = array();
foreach ($bubble_post_ids as $post_id) {
// echo '<br>' . get_permalink($post_id);
$posts_permalinks[$post_id] = get_permalink($post_id);
}
?>
<input type="hidden" id="wpdevlms_bb_posts_href" value='<?php echo json_encode( $posts_permalinks ); ?>' />
<style type="text/css">
.wpgb-card .wpdevlms-bb {
position: absolute;
top: -10px;
right: 20px;
padding: 12px 12px;
border-radius: 100px;
background: -webkit-linear-gradient(top, #FF6969 0%,#ff0000 100%);
z-index: 991;
}
</style>
<script type="text/javascript">
jQuery( document ).ready( function() {
var wpdevlms_bb_posts_href = <?php echo json_encode($posts_permalinks); ?>;
append_red_circle_on_grid();
function append_red_circle_on_grid() {
if( jQuery( '.wpgb-card' ).length ) {
jQuery.each( wpdevlms_bb_posts_href, function( post_id, permalink ) {
if( jQuery( document.body ).find( "a.wpgb-card-layer-link[href='"+permalink+"']" ).length ) {
var referencePoint = jQuery( document.body ).find( "a.wpgb-card-layer-link[href='"+permalink+"']" );
var wpgbCardWrapper = referencePoint.parents( '.wpgb-card-wrapper' );
wpgbCardWrapper.append( '<span class="wpdevlms-bb"></span>' );
}
} );
}
}
});
</script>
<?php
}
}
/* adding buttons to new grid END */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment