--- /dev/null
+<?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;
+ }
+
+}