adding new function to blocks that allows for slug to be passed instead of cat id
authorAnthony Talarico <talarico@gaslightmedia.com>
Fri, 30 Nov 2018 16:12:47 +0000 (11:12 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Fri, 30 Nov 2018 16:12:47 +0000 (11:12 -0500)
controllers/front.php
glm-blocks.php
models/block.php

index 124e761..6e7cdfd 100644 (file)
@@ -49,4 +49,16 @@ class glm_blocks_front
         $block = new glm_models_block($this->pluginDirName);
         return $block->fetchAllBlocks($catID);
     }
+      /**
+     * Fetches the blocks into an array
+     *
+     * Now you can control how the block is output
+     *
+     * @return array
+     */
+    public function fetch_blocks_by_slug($cat)
+    {
+        $block = new glm_models_block($this->pluginDirName);
+        return $block->fetch_blocks_by_slug($cat);
+    }
 }
index a65f534..1625e65 100644 (file)
@@ -53,14 +53,15 @@ if (!function_exists('fetch_all_glm_blocks')) {
         return $frontController->fetchAllBlocks($catId);
     }
 }
-function landing_page_blocks( $atts )
-{
+function landing_page_blocks( $atts ){
+    
     $out = '';
     extract(shortcode_atts(
         array('category' => '-1'), $atts, 'glm-blocks')
     );
     if(function_exists('fetch_all_glm_blocks')):
         $category = filter_var($atts['category'], FILTER_VALIDATE_INT);
+        
         $blocks = fetch_all_glm_blocks($category);
         $out .= '<div id="glm-blocks" class="glm-blocks-container">';
         $count = 0;
@@ -88,4 +89,50 @@ function landing_page_blocks( $atts )
     endif;
     return $out;
 }
+if (!function_exists('fetch_blocks_by_slug')) {
+    function fetch_blocks_by_slug($cat)
+    {
+        $frontController = new glm_blocks_front(
+            plugin_dir_path(__FILE__)
+        );
+        return $frontController->fetch_blocks_by_slug($cat);
+    }
+}
+function action_item_blocks( $atts ){
+    
+    $out = '';
+    extract(shortcode_atts(
+        array('category' => '-1'), $atts, 'glm-blocks')
+    );
+    if(function_exists('fetch_blocks_by_slug')):
+        $category = filter_var($atts['category'], FILTER_VALIDATE_INT);
+        fetch_blocks_by_slug($category);
+        $blocks = fetch_blocks_by_slug($category);
+        $out .= '<div id="glm-blocks" class="glm-blocks-container">';
+        $count = 0;
+        foreach ($blocks as $block):
+            $post_image_id = get_post_thumbnail_id($block->ID);
+            $thumbnail = wp_get_attachment_image_src( $post_image_id, 'landing_page');
+            $out .= '<div class="small-text-center medium-text-left glm-block">';
+            if($block->url):
+                $out .= '<a href="'.$block->url.'"'.(($block->externalUrl)? ' target="_blank"':'').'>';
+            endif;
+            $out .= '<img src="'.$thumbnail[0].'"/>';
+            $out .= '<div class="land-page-block-image" style="background: url('.$thumbnail.');"></div>';
+            $out .= '<h5 class="glm-block-title">'.$block->post_title.'</h5>';
+            if($block->url):
+                $out .= '</a>';
+            endif;
+            if($block->url):
+                $buttonText = ($count < 1) ? "SIGN UP" : "BUY NOW";
+                $out .= '<a class="sign-up-button" href="'.$block->url.'"'.(($block->externalUrl)? ' target="_blank"':'').'>' . $buttonText .'</div>';
+            endif;
+            $out .= '</div>';
+            $count++;
+        endforeach;
+        $out .= '</div>';
+    endif;
+    return $out;
+}
+add_shortcode('glm-blocks-action-items', 'action_item_blocks');
 add_shortcode('glm-blocks', 'landing_page_blocks');
\ No newline at end of file
index 3183e85..24b4672 100644 (file)
@@ -261,7 +261,69 @@ class glm_models_block
         }
         return $blocks;
     }
+    /**
+     * fetchAllBlocks
+     *
+     * @access public
+     * @return void
+     */
+    public function fetch_blocks_by_slug($cat)
+    {
+        global $wpdb, $wp;
+        $parent = array(
+            'name' => $cat,
+            'post_type'   => GLM_BLOCK_POST_TYPE,
+            'post_status' => 'publish',
+            'numberposts' => 1
+        );
+        $block_parent = get_posts($parent)[0];
 
+        $args   = array(
+            'posts_per_page' => -1,
+            'post_type'      => GLM_BLOCK_POST_TYPE,
+            'orderby'        => 'menu_order post_title',
+            'order'          => 'asc'
+        );
+       $args['post_parent'] = $block_parent->ID;
+        
+        $blocks = get_posts($args);
+        foreach ($blocks as $block) {
+            $block->externalUrl = false;
+            $custom = get_post_custom($block->ID);
+            if (isset($custom['glm_block_url'])) {
+                switch ($custom['glm_block_url'][0]) {
+                case 'event':
+                    if ( defined( 'GLM_MEMBERS_EVENTS_PLUGIN_NAME' ) ) {
+                        $event = apply_filters('glm-member-db-events-get-event-data', $custom['glm_block_event'][0]);
+                        if ( $event ) {
+                            $block->url = home_url() . '/event-detail/' . $event['name_slug'] . '/';
+                        }
+                    } else if ( defined( 'AI1EC_PATH' ) ) {
+                        $block->url = get_permalink($custom['glm_block_event'][0]);
+                    }
+                    break;
+                case 'page':
+                    $block->url = get_permalink($custom['glm_block_page'][0]);
+                    break;
+                case 'post':
+                    $block->url = get_permalink($custom['glm_block_post'][0]);
+                    break;
+                case 'url':
+                    if ($custom['glm_block_ext_url'][0] != '') {
+                        $block->url = (preg_match('/^https?:\/\//', $custom['glm_block_ext_url'][0]))
+                            ? $custom['glm_block_ext_url'][0]
+                            : 'http://' . $custom['glm_block_ext_url'][0];
+                        $block->externalUrl = true;
+                    }
+                    break;
+                }
+            }
+            $block->thumbnail = get_the_post_thumbnail(
+                $block->ID, GLM_BLOCK_POST_TYPE, array('class' => 'aligncenter')
+            );
+        }
+        return $blocks;
+    }
     /**
      * displayBlocks
      *