From 95a6693bce7c7f1425bd392a3e27cf70dc12c470 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 25 Nov 2014 10:06:23 -0500 Subject: [PATCH 1/1] add to git --- controllers/admin.php | 49 +++++++++++ controllers/front.php | 28 ++++++ glm-blocks.php | 45 ++++++++++ models/block.php | 169 +++++++++++++++++++++++++++++++++++++ views/admin/metaBoxes.php | 36 ++++++++ views/front/showBlocks.php | 7 ++ 6 files changed, 334 insertions(+) create mode 100644 controllers/admin.php create mode 100644 controllers/front.php create mode 100644 glm-blocks.php create mode 100644 models/block.php create mode 100644 views/admin/metaBoxes.php create mode 100644 views/front/showBlocks.php diff --git a/controllers/admin.php b/controllers/admin.php new file mode 100644 index 0000000..0742328 --- /dev/null +++ b/controllers/admin.php @@ -0,0 +1,49 @@ +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 ''; + } + } + } + +} diff --git a/controllers/front.php b/controllers/front.php new file mode 100644 index 0000000..ce18541 --- /dev/null +++ b/controllers/front.php @@ -0,0 +1,28 @@ +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 index 0000000..675ef15 --- /dev/null +++ b/glm-blocks.php @@ -0,0 +1,45 @@ +showBlocks(); + } +} diff --git a/models/block.php b/models/block.php new file mode 100644 index 0000000..59c29f0 --- /dev/null +++ b/models/block.php @@ -0,0 +1,169 @@ +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 index 0000000..ff60d4f --- /dev/null +++ b/views/admin/metaBoxes.php @@ -0,0 +1,36 @@ + + +> + +
+> + + 0, + 'child_of' => 0, + 'selected' => $glm_blocks_page, + 'echo' => 1, + 'name' => 'glm_block_page', + 'id' => 'glm_block_page', + 'show_option_none' => 'None' + ) +); +?> + \ No newline at end of file diff --git a/views/front/showBlocks.php b/views/front/showBlocks.php new file mode 100644 index 0000000..7c6bc92 --- /dev/null +++ b/views/front/showBlocks.php @@ -0,0 +1,7 @@ + +
+ thumbnail;?> +

post_title;?>

+ post_excerpt;?> +
+ \ No newline at end of file -- 2.17.1