Adding link to post
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 29 Oct 2015 12:53:57 +0000 (08:53 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 29 Oct 2015 12:53:57 +0000 (08:53 -0400)
Made new select list just for the post. The page one I kept because it
was showing the hierarchy for the pages. I did restrict them to only
show pages or post that are published.

js/glm-blocks.js
models/block.php
views/admin/metaBoxes.php

index 23392a6..541bdf4 100644 (file)
@@ -9,6 +9,13 @@ jQuery(function($){
             $("input[name=glm_block_url]").filter('input[value=page]').attr('checked', false);
         }
     });
+    $("#glm_block_post").change(function(){
+        if($("#glm_block_post").val()) {
+            $("input[name=glm_block_url]").filter('input[value=post]').attr('checked', true);
+        } else {
+            $("input[name=glm_block_url]").filter('input[value=post]').attr('checked', false);
+        }
+    });
     $("#glm_block_ext_url").change(function(){
         if($("#glm_block_ext_url").val()) {
             $("input[name=glm_block_url]").filter('input[value=url]').attr('checked', true);
index e613a75..f27de36 100644 (file)
@@ -148,6 +148,9 @@ class glm_models_block
         $glm_block_page    = ($post)
             ? get_post_meta($post->ID, 'glm_block_page', true)
             : '';
+        $glm_block_post    = ($post)
+            ? get_post_meta($post->ID, 'glm_block_post', true)
+            : '';
         include $this->pluginDirName . 'views/admin/metaBoxes.php';
     }
 
@@ -181,6 +184,7 @@ class glm_models_block
         update_post_meta($post->ID, 'glm_block_ext_url',
                          $_POST['glm_block_ext_url']);
         update_post_meta($post->ID, 'glm_block_page', $_POST['glm_block_page']);
+        update_post_meta($post->ID, 'glm_block_post', $_POST['glm_block_post']);
     }
 
     /**
@@ -202,16 +206,21 @@ class glm_models_block
         foreach ($blocks as $block) {
             $block->externalUrl = false;
             $custom = get_post_custom($block->ID);
-            if ($custom['glm_block_url'][0] == 'page') {
+            switch ($custom['glm_block_url'][0]) {
+            case 'page':
                 $block->url = get_permalink($custom['glm_block_page'][0]);
-            }
-            if ($custom['glm_block_url'][0] == 'url') {
+                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('/^http:\/\//', $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')
index 7631d4f..8adef7b 100644 (file)
@@ -19,7 +19,7 @@
 <?php if ($glm_block_url == 'page') {
     echo 'checked';
 }; ?>>
-<label for="my_meta_box_post_type">Internal link: </label>
+<label for="my_meta_box_post_type">Internal Page: </label>
 <?php
 wp_dropdown_pages(
     array(
@@ -29,8 +29,63 @@ wp_dropdown_pages(
         'echo'             => 1,
         'name'             => 'glm_block_page',
         'id'               => 'glm_block_page',
-        'show_option_none' => 'None'
+        'show_option_none' => 'None',
+        'post_status'      => 'publish',
+        'post_type'        => 'page'
     )
 );
 ?>
 </select>
+<br>
+<input type="radio" name="glm_block_url" value="post"
+<?php if ($glm_block_url == 'post') {
+    echo 'checked';
+}; ?>>
+<label for="my_meta_box_post_type">Internal Post: </label>
+<select id="glm_block_post" name="glm_block_post">
+<option value="">None</option>
+<?php
+$glmPosts = glm_blocks_get_posts();
+foreach ($glmPosts as $post) {
+    echo '<option value="' . $post['ID'] . '"';
+    if ($post['ID'] == $glm_block_post) {
+        echo ' selected';
+    }
+    echo '>' . $post['title']  . '</option>';
+}
+?>
+</select>
+<?php
+function glm_blocks_get_posts() {
+    $query = array(
+        'post_type'              => 'post',
+        'suppress_filters'       => true,
+        'update_post_term_cache' => false,
+        'update_post_meta_cache' => false,
+        'post_status'            => 'publish',
+        'order'                  => 'DESC',
+        'orderby'                => 'date',
+        'posts_per_page'         => -1,
+    );
+    $get_posts = new WP_Query;
+    $posts = $get_posts->query( $query );
+    $results = array();
+    if ( ! $get_posts->post_count ) {
+        return $results;
+    }
+    foreach ( $posts as $post ) {
+        if ( 'post' == $post->post_type ) {
+            $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
+        } else {
+            continue;
+        }
+        $results[] = array(
+            'ID'        => $post->ID,
+            'title'     => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
+            'permalink' => get_permalink( $post->ID ),
+            'info'      => $info,
+        );
+    }
+    return $results;
+}
+?>