Skip to content

Instantly share code, notes, and snippets.

@azinkey
Created November 15, 2019 09:05
Show Gist options
  • Save azinkey/d66f9ebfe1e9680de7c2f920b2607577 to your computer and use it in GitHub Desktop.
Save azinkey/d66f9ebfe1e9680de7c2f920b2607577 to your computer and use it in GitHub Desktop.
<?php
/* Plugin Name: Hire Me
* Plugin URI: https://www.kadamtech.com
* Description: Simple and Easy plugin to manage Hire Me Content
* Version: 1.0.0
* Author: AZinkey
* Author URI: https://www.kadams.in?hero=azinkey
* License: GPLv2 or later
*/
defined('ABSPATH') or die('No script kiddies please!');
define('KDM_HIRE_VERSION', '1.0.0');
define('KDM_HIRE_LIMIT', 1000);
define('KDM_HIRE__MINIMUM_WP_VERSION', '5.0');
define('KDM_HIRE__PLUGIN_DIR', plugin_dir_path(__FILE__));
date_default_timezone_set('Asia/Kolkata');
if (!session_id()) {
session_start();
}
/**
* KDM Hire Class contains the list and manage page for the hire related tasks
*/
/*
* KDM_Hire Class
*
*
* Contains all appointment plugin tasks
*/
class KDM_Hire {
/*
* Instance: Kdm Hire Me Plugin Instance
*/
static $instance;
/*
* Constructor: init all plugin enviroments and registry
*/
function __construct() {
if (is_admin()) {
// admin actions
add_action('init', array($this, 'register_post_types'));
add_action('admin_menu', array($this, 'admin_menu'));
add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
add_action('save_post', array($this, 'save_global_hire_details_meta_box_data'));
}
add_action('wp_enqueue_scripts', array($this, 'register_assets'));
if (!is_admin()) {
add_shortcode('hire', array($this, 'display'));
}
}
public function register_assets() {
wp_enqueue_style('kdmhire-style', plugins_url('kdm_hire/assets/css/scrolltabs.css'));
wp_enqueue_script('kdmhire-script', plugins_url('kdm_hire/assets/js/jquery.scrolltabs.js'), array('jquery'));
}
/*
* load template views for this plugin
*/
public static function view($view, $data = array()) {
try {
$file = KDM_HIRE__PLUGIN_DIR . 'views/' . $view . '.php';
if (file_exists($file)) {
include( $file );
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
public function register_post_types() {
// Add new custom post type, Enquiries
register_post_type('hire', array(
'labels' => array(
'name' => __('Hire Posts', 'kdmhire'),
'singular_name' => __('Hire', 'kdmhire'),
'add_new_item' => __('Add New Hire', 'kdmhire'),
'edit_item' => __('Edit Hire', 'kdmhire'),
'new_item' => __('New Hire', 'kdmhire'),
'view_item' => __('View Hire', 'kdmhire'),
'search_items' => __('Search Hire', 'kdmhire'),
'not_found' => __('No Hire Found', 'kdmhire'),
'not_found_in_trash' => __('No Hire Post found in Trash', 'kdmhire')
),
'description' => 'Represents a single Hire.',
'hierarchical' => false,
'menu_icon' => 'dashicons-images-alt', // 'dashicons-images-alt2',
'menu_position' => 5,
'public' => true,
'rewrite' => array('slug' => 'hire', 'with_front' => false),
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'taxonomies' => array('Hire_Category'),
'supports' => array('title', 'editor', 'thumbnail') // , 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes'
));
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x('Category', 'Category', 'kdmhire'),
'singular_name' => _x('Category', 'Category', 'kdmhire'),
'search_items' => __('Search Category', 'kdmhire'),
'all_items' => __('All Category', 'kdmhire'),
'parent_item' => __('Parent Category', 'kdmhire'),
'parent_item_colon' => __('Parent Category:', 'kdmhire'),
'edit_item' => __('Edit Category', 'kdmhire'),
'update_item' => __('Update Category', 'kdmhire'),
'add_new_item' => __('Add New Category', 'kdmhire'),
'new_item_name' => __('New Category Name', 'kdmhire'),
'menu_name' => __('Category', 'kdmhire'),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'hire_category', 'with_front' => false),
);
register_taxonomy('Hire_Category', array('hire'), $args);
}
function add_meta_boxes() {
$screens = array('hire');
add_meta_box(
'hire_details', __('Details Page', 'kdmhire'), array($this, 'global_hire_details_meta_box_callback'), $screens
);
}
public function global_hire_details_meta_box_callback($post) {
global $wpdb;
wp_nonce_field('hire_details_nonce', 'hire_details_nonce');
$value = get_post_meta($post->ID, 'hire_details', true);
echo '<input type="text" name="hire_details" placeholder="http://example.com/page-slug" value="' . $value . '" />';
}
public function save_global_hire_details_meta_box_data($post_id) {
global $post;
// Check if our nonce is set.
if (!isset($_POST['hire_details_nonce'])) {
return;
}
// Verify that the nonce is valid.
if (!wp_verify_nonce($_POST['hire_details_nonce'], 'hire_details_nonce')) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
if (isset($_POST['post_type']) && 'hire' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return;
}
} else {
if (!current_user_can('edit_post', $post_id)) {
return;
}
}
$hire_details_data = sanitize_text_field($_POST['hire_details']);
update_post_meta($post_id, 'hire_details', esc_sql($hire_details_data));
}
public static function display($atts) {
global $wpdb;
$tabs = load_terms('Hire_Category');
$html = '';
if (count($tabs)) {
$html .= '<div id="tabs1" class="scroll_tabs_theme_light">';
foreach ($tabs as $tab) {
$html .= '<span class="tab-toggle" data-id="' . $tab['term_id'] . '">' . $tab['name'] . '</span>';
}
$html .= '</div>';
$i = 0;
foreach ($tabs as $tab) {
$i++;
$active = ($i == 1) ? 'active' : '';
$html .= '<div class="kdm-tab-content ' . $active . '" id="kdm-tab-' . $tab['term_id'] . '">';
$html .= '<ul>';
$results = $wpdb->get_results("SELECT object_id FROM " . $wpdb->prefix . "term_relationships WHERE `term_taxonomy_id`='" . $tab['term_id'] . "'");
foreach ($results as $object) {
$post = get_post($object->object_id);
if ($post->post_status == 'publish') {
$detail = get_post_meta($post->ID, 'hire_details', true);
$html .= '<li>';
$html .= '<a target="_blank" href="' . $detail . '"><img class="hire-thumb" src="' . get_the_post_thumbnail_url($post->ID) . '" alt="' . $post->post_title . '" /></a>';
$html .= '<h3>' . $post->post_title . '</h3>';
$html .= '<p>' . $post->post_content . '</p>';
$html .= '</li>';
}
}
$html .= '<div style="clear:both;"></div></ul>';
$html .= '</div>';
}
}
$html .= <<<HERE
<script type="text/javascript">
(function ($) {
$(document).ready(function () {
$('#tabs1').scrollTabs();
$("#tabs1 .tab-toggle").click(function () {
$(".kdm-tab-content").removeClass('active');
var id = $(this).data('id');
$("#kdm-tab-" + id).addClass('active');
});
});
})(jQuery);
</script>
HERE;
return $html;
}
}
add_action('plugins_loaded', function () {
new KDM_Hire();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment