add to git
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 25 Nov 2014 15:06:23 +0000 (10:06 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 25 Nov 2014 15:06:23 +0000 (10:06 -0500)
controllers/admin.php [new file with mode: 0644]
controllers/front.php [new file with mode: 0644]
glm-blocks.php [new file with mode: 0644]
models/block.php [new file with mode: 0644]
views/admin/metaBoxes.php [new file with mode: 0644]
views/front/showBlocks.php [new file with mode: 0644]

diff --git a/controllers/admin.php b/controllers/admin.php
new file mode 100644 (file)
index 0000000..0742328
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+defined('ABSPATH') or die();
+
+/**
+ * AdminConroller
+ *
+ * Controller for all of the admin functionality
+ */
+class glm_blocks_admin
+{
+
+    public $pluginDirName;
+
+    /**
+     * Register Hooks and Actions
+     */
+    public function __construct($path)
+    {
+        $this->pluginDirName = $path;
+        $jobs = new glm_models_block($path);
+        add_action('admin_init', array($this, 'remove_elements'));
+    }
+
+    public function setPluginDir($dir)
+    {
+        $this->pluginDirName = $dir;
+    }
+
+
+
+    function remove_elements()
+    {
+        if(    isset($_GET['post']) && $_GET['post']
+            || (isset($_GET['post_type']) && $_GET['post_type'] == GLM_BLOCK_POST_TYPE)
+        ) {
+            $post_type = (isset($_GET['post']))
+                ? get_post_type($_GET['post'])
+                : $_GET['post_type'];
+            if($post_type == GLM_BLOCK_POST_TYPE) {
+                echo '<style>
+                    #edit-slug-box, #postbox-container-2 .inside p {display:none;}
+                    #set-ngg-post-thumbnail {display: none;}
+                    </style>';
+            }
+        }
+    }
+
+}
diff --git a/controllers/front.php b/controllers/front.php
new file mode 100644 (file)
index 0000000..ce18541
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+defined('ABSPATH') or die();
+
+/**
+ * AdminConroller
+ *
+ * Controller for all of the admin functionality
+ */
+class glm_blocks_front
+{
+
+    public $pluginDirName;
+
+    /**
+     * Register Hooks and Actions
+     */
+    public function __construct($path)
+    {
+        $this->pluginDirName = $path;
+    }
+
+    public function showBlocks()
+    {
+        $block = new glm_models_block($this->pluginDirName);
+        $block->displayBlocks();
+    }
+}
diff --git a/glm-blocks.php b/glm-blocks.php
new file mode 100644 (file)
index 0000000..675ef15
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+/*
+Plugin Name: GLM Blocks
+Version: 0.1-alpha
+Description: PLUGIN DESCRIPTION HERE
+Author: YOUR NAME HERE
+Author URI: YOUR SITE HERE
+Plugin URI: PLUGIN SITE HERE
+Text Domain: glm-blocks
+Domain Path: /languages
+*/
+defined('ABSPATH') or die();
+
+define('GLM_BLOCK_POST_TYPE', 'glm_blocks');
+
+require_once 'controllers/admin.php';
+require_once 'controllers/front.php';
+require_once 'models/block.php';
+
+/**
+ * Setup class to call in the Controllers
+ */
+class glm_blocks_setup
+{
+    public function __construct()
+    {
+        if (is_admin()) {
+            $adminController = new glm_blocks_admin(
+                plugin_dir_path(__FILE__)
+            );
+        }
+    }
+}
+
+$employmentApp = new glm_blocks_setup();
+
+if (!function_exists('glm_blocks_show')) {
+    function glm_blocks_show()
+    {
+        $frontController = new glm_blocks_front(
+            plugin_dir_path(__FILE__)
+        );
+        $frontController->showBlocks();
+    }
+}
diff --git a/models/block.php b/models/block.php
new file mode 100644 (file)
index 0000000..59c29f0
--- /dev/null
@@ -0,0 +1,169 @@
+<?php
+
+class glm_models_block
+{
+
+    public $pluginDirName;
+
+    public function __construct($path)
+    {
+        $this->pluginDirName = $path;
+        add_action('init', array($this, 'addPostTypes'));
+        add_action('add_meta_boxes', array($this, 'addMetaBoxes'));
+        add_action('save_post', array($this, 'saveMetaBox'));
+        add_action('manage_edit-glm_blocks_columns', array($this, 'addOrderColumn'));
+        add_action('manage_glm_blocks_posts_custom_column', array($this, 'showOrderColumn'));
+        add_filter('manage_edit-glm_blocks_sortable_columns', array($this, 'orderSortable'));
+        add_action('pre_get_posts', array($this, 'sortQuery'));
+    }
+
+    public function sortQuery($query)
+    {
+        if (is_admin() && !isset($_REQUEST['orderby'])) {
+            $postType = $query->query['post_type'];
+            if ($postType == GLM_BLOCK_POST_TYPE) {
+                $query->set('orderby', 'menu_order');
+                $query->set('order', 'asc');
+            }
+        }
+    }
+
+    /**
+     * Create new Post type
+     */
+    public function addPostTypes()
+    {
+        $labels = array(
+            'name'               => _x('Blocks', 'block general name'),
+            'singular_name'      => _x('Block', 'block type singular name'),
+            'add_new'            => _x('Add New', 'block'),
+            'add_new_item'       => __('Add New Block'),
+            'edit_item'          => __('Edit Block'),
+            'new_item'           => __('New Block'),
+            'all_items'          => __('All Blocks'),
+            'view_item'          => __('View Block'),
+            'search_items'       => __('Search Blocks'),
+            'not_found'          => __('No Blocks found'),
+            'not_found_in_trash' => __('No Blocks found in the Trash'),
+            'parent_item_colon'  => '',
+            'menu_name'          => 'Blocks'
+        );
+        $args   = array(
+            'label'              => __('Blocks'),
+            'labels'             => $labels,
+            'description'        => 'Stores Blocks and Block data',
+            'public'             => true,
+            'publicly_queriable' => false,
+            'rewrite'            => false,
+            'show_ui'            => true,
+            'show_in_nav_menus'  => false,
+            'can_export'         => true,
+            'menu_position'      => 5,
+            'supports'           => array('thumbnail', 'title', 'excerpt', 'page-attributes'),
+            'has_archive'        => false,
+            'menu_icon'          => 'dashicons-exerpt-view',
+            'capability_type'    => 'page',
+            'hierarchical'       => false
+        );
+        register_post_type(GLM_BLOCK_POST_TYPE, $args);
+    }
+
+    public function addMetaBoxes()
+    {
+        add_meta_box(
+            'glm_blocks_url',
+            __('URL', 'glm_blocks_textdomain'),
+            array($this, 'metaBoxCallBack'),
+            GLM_BLOCK_POST_TYPE,
+            'normal'
+        );
+    }
+
+    public function metaBoxCallBack($post)
+    {
+        $glm_blocks_url     = ($post)
+            ? get_post_meta($post->ID, 'glm_block_url', true)
+            : '';
+        $glm_blocks_ext_url = ($post)
+            ? get_post_meta($post->ID, 'glm_block_ext_url', true)
+            : '';
+        $glm_blocks_page    = ($post)
+            ? get_post_meta($post->ID, 'glm_block_page', true)
+            : '';
+        include $this->pluginDirName . 'views/admin/metaBoxes.php';
+    }
+
+    public function saveMetaBox($post_id)
+    {
+        global $post;
+        // verify this came from the our screen and with proper authorization,
+        // because save_post can be triggered at other times
+        if (isset($post) && !wp_verify_nonce($_POST['glm_block_noncename'],
+                             'glm_block_noncename')) {
+            return $post->ID;
+        }
+
+        // Is the user allowed to edit the post or page?
+        if (isset($post) && !current_user_can('edit_post', $post->ID)) {
+            return $post->ID;
+        }
+        if (!isset($post)) {
+            return;
+        }
+        // OK, we're authenticated: we need to find and save the data
+        // We'll put it into an array to make it easier to loop though.
+        update_post_meta($post->ID, 'glm_block_url', $_POST['glm_block_url']);
+        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']);
+    }
+
+    public function displayBlocks()
+    {
+        global $wpdb, $wp;
+        $args   = array(
+            'post_type' => 'glm_blocks',
+            'orderby'   => 'menu_order, post_title',
+            'order'     => 'asc'
+        );
+        $blocks = get_posts($args);
+        foreach ($blocks as $block) {
+            $custom                   = get_post_custom($block->ID);
+            if ($custom['glm_block_url'][0] == 'page') {
+                $block->url = get_permalink($custom['glm_block_page'][0]);
+            }
+            if ($custom['glm_block_url'][0] == 'url') {
+                $bloc->url = $custom['glm_block_ext_url'][0];
+            }
+            $block->thumbnail         = get_the_post_thumbnail(
+                $block->ID, array(298, 228), array('class' => 'aligncenter')
+            );
+        }
+        include $this->pluginDirName . 'views/front/showBlocks.php';
+    }
+
+    function addOrderColumn($header_text_columns)
+    {
+      $header_text_columns['menu_order'] = "Order";
+      return $header_text_columns;
+    }
+
+    function showOrderColumn($name)
+    {
+        global $post;
+        switch ($name) {
+          case 'menu_order':
+            $order = $post->menu_order;
+            echo $order;
+            break;
+         default:
+            break;
+         }
+    }
+    function orderSortable($columns)
+    {
+      $columns['menu_order'] = 'menu_order';
+      return $columns;
+    }
+
+}
diff --git a/views/admin/metaBoxes.php b/views/admin/metaBoxes.php
new file mode 100644 (file)
index 0000000..ff60d4f
--- /dev/null
@@ -0,0 +1,36 @@
+<input type="hidden" name="glm_block_noncename" id="glm_block_noncename"
+       value="<?php echo wp_create_nonce('glm_block_noncename'); ?>" />
+
+<input type="radio" name="glm_block_url" value="url"
+       <?php if ($glm_blocks_url == 'url') {
+           echo 'checked';
+       }; ?>>
+<label>
+<?php echo _e('External URL', 'glm_blocks_textdomain'); ?>
+    <input
+        type="text"
+        id="glm_blocks_ext_url"
+        name="glm_blocks_ext_url"
+        value="<?php echo esc_attr($glm_blocks_ext_url) ?>"
+        >
+</label>
+<br>
+<input type="radio" name="glm_block_url" value="page"
+<?php if ($glm_blocks_url == 'page') {
+    echo 'checked';
+}; ?>>
+<label for="my_meta_box_post_type">Post type: </label>
+<?php
+wp_dropdown_pages(
+    array(
+        'depth'            => 0,
+        'child_of'         => 0,
+        'selected'         => $glm_blocks_page,
+        'echo'             => 1,
+        'name'             => 'glm_block_page',
+        'id'               => 'glm_block_page',
+        'show_option_none' => 'None'
+    )
+);
+?>
+</select>
\ No newline at end of file
diff --git a/views/front/showBlocks.php b/views/front/showBlocks.php
new file mode 100644 (file)
index 0000000..7c6bc92
--- /dev/null
@@ -0,0 +1,7 @@
+<?php foreach ($blocks as $block):?>
+<div class="text-center small-12 medium-12 medium-centered large-uncentered large-4 columns">
+        <?php echo $block->thumbnail;?>
+        <a class="title-posts" href="<?php echo $block->url;?>"><h2><?php echo $block->post_title;?></h2></a>
+    <?php echo $block->post_excerpt;?>
+</div>
+<?php endforeach;?>
\ No newline at end of file