__('Show time spent building your aircraft'), ) ); } // widget form creation function form($instance) { if (isset($instance['title'])) { $title = $instance['title']; } else { $title = __('Aircraft Build Time'); } ?>

' . $instance['title'] . ''; echo ''; $sections = get_terms(array( 'taxonomy' => WP_Builders_Log::BUILDERS_LOG_TAXONOMY, 'hide_empty' => true )); $total_hours = 0; foreach ($sections as $section) { $min = (int) get_term_meta($section->term_id, '_build_time_total_minutes', true); $hours = ($min / 60); $total_hours += $hours; echo ''; } if (count($sections) > 1) { echo ''; } echo '
Airplane sectionBuild time (hrs)
' . $section->name . '' . $hours . '
' . __('Total') . '' . $total_hours . '
'; echo $args['after_widget']; } } class WP_Builders_Log { const BUILDERS_LOG_TAXONOMY = 'airplane_section'; static $instance = false; public static function getInstance() { if (! self::$instance) { self::$instance = new self; } return self::$instance; } private function __construct() { // Make sure custom slugs work for airplane sections add_action('admin_init', 'flush_rewrite_rules'); register_deactivation_hook(__FILE__, 'flush_rewrite_rules'); // Add a new taxonomy for airplane sections. This will be used to force selection of // only a single airplane section at a time (to simplify time tracking). // Otherwise these are pretty much the same as categories. add_action('init', array(__CLASS__, 'register_taxonomy_plane_section')); // Remove the default controls for adding an airplane section to a post. add_action('admin_menu', array(__CLASS__, 'remove_default_airplane_section_meta_box')); // Add new controls that use radio buttons rather than multi-select. add_action('add_meta_boxes_post', array(__CLASS__, 'add_airplane_section_meta_box')); // script to keep radio buttons in sync on admin meta box form add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_script')); // Allow admins to add new airplane sections from the post edit page add_action('wp_ajax_radio_tax_add_taxterm', array(__CLASS__,'ajax_add_term')); // Add a new box to the post add/edit page to record hours and minutes spent add_action('add_meta_boxes_post', array(__CLASS__, 'add_build_time_meta_box')); add_action('save_post', array($this, 'save_build_time_meta_box')); // Setup the widget add_action('widgets_init', function(){ register_widget('WP_Builders_Log_Widget'); }); } public static function remove_default_airplane_section_meta_box() { remove_meta_box('tagsdiv-' . self::BUILDERS_LOG_TAXONOMY, 'post', 'normal'); } public static function add_airplane_section_meta_box() { add_meta_box( 'tagsdiv-new-airplane_section', // div id __('Airplane Section'), // title array(__CLASS__, 'airplane_section_meta_box'), // callback 'post','normal','high' // post type, context, priority ); } public static function airplane_section_meta_box($post) { $tax_obj = get_taxonomy(self::BUILDERS_LOG_TAXONOMY); $input_name = 'tax_input[' . self::BUILDERS_LOG_TAXONOMY . ']'; $all_terms = get_terms(self::BUILDERS_LOG_TAXONOMY, array('hide_empty' => 0)); $popular_terms = get_terms(self::BUILDERS_LOG_TAXONOMY, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false )); $post_terms = get_the_terms($post->ID, self::BUILDERS_LOG_TAXONOMY); $current_term = ($post_terms ? array_pop($post_terms) : false); $current_term_id = ($current_term ? $current_term->term_id : false); ?>

ID, '_build_time_total_minutes', true); $hours = ''; $min = ''; if ($total_min) { $total_min = (int)$total_min; $hours = floor($total_min / 60); $min = $total_min % 60; } ?>

update_aircraft_section_term_meta($term); } } public static function register_taxonomy_plane_section() { $labels = [ 'name' => _x('Airplane Sections', 'taxonomy general name'), 'singular_name' => _x('Airplane Section', 'taxonomy singular name'), 'search_items' => __('Search Sections'), 'all_items' => __('All Airplane Sections'), 'parent_item' => __('Parent Airplane Section'), 'parent_item_colon' => __('Parent Airplane Section:'), 'edit_item' => __('Edit Airplane Section'), 'update_item' => __('Update Airplane Section'), 'add_new_item' => __('Add New Airplane Section'), 'new_item_name' => __('New Airplane Section Name'), 'menu_name' => __('Airplane Sections'), ]; $args = [ 'hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => ['slug' => 'airplane_section'], ]; register_taxonomy(self::BUILDERS_LOG_TAXONOMY, ['post'], $args); } public static function admin_script() { wp_register_script('airplane_section_taxonomy', plugins_url( '/js/airplane_section_taxonomy.js', __FILE__ ), array('jquery'), null, true ); // We specify true here to tell WordPress this script needs to be loaded in the footer wp_localize_script('airplane_section_taxonomy', 'airplane_section_taxonomy', array('slug'=>self::BUILDERS_LOG_TAXONOMY)); wp_enqueue_script('airplane_section_taxonomy'); } public function ajax_add_term() { $taxonomy = sanitize_text_field(!empty($_POST['taxonomy']) ? $_POST['taxonomy'] : ''); $term = sanitize_text_field(!empty($_POST['term']) ? $_POST['term'] : ''); $tax = get_taxonomy($taxonomy); check_ajax_referer('radio-tax-add-'.$taxonomy, '_wpnonce_radio-add-tag'); if(!$tax || empty($term)) exit(); if ( !current_user_can( $tax->cap->edit_terms ) ) die('-1'); $term = wp_insert_term($term, $taxonomy); if ( !$term || is_wp_error($term) || (!$term = get_term( $term['term_id'], $taxonomy )) ) { //TODO Error handling exit(); } $id = $taxonomy.'-'.$term->slug; $name = 'tax_input[' . $taxonomy . ']'; $html ='
  • '; echo json_encode(array('term'=>$term->term_id,'html'=>$html)); exit(); } // $term should be a WP_Term objection from get_terms(), // which corresponds to an airplane section. // This will add up the log time for all posts in that section, // then store it. private function update_aircraft_section_term_meta($term) { $posts = get_posts(array( 'post_type' => 'post', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id, 'include_children' => false ) ) )); $total_min = 0; foreach ($posts as $post) { $min = get_post_meta($post->ID, '_build_time_total_minutes', true); if ($min) { $total_min += (int)$min; } } update_term_meta($term->term_id, '_build_time_total_minutes', $total_min); } } $WP_Builders_Log = WP_Builders_Log::getInstance();