Skip to content

Instantly share code, notes, and snippets.

@dnaber-de
Created March 19, 2012 11:45
Show Gist options
  • Save dnaber-de/2108813 to your computer and use it in GitHub Desktop.
Save dnaber-de/2108813 to your computer and use it in GitHub Desktop.
Codesnippets for sharing
/**
* forks the taxonom-edit links for
* custom taxonomies on attachments
*/
( function( $ ) {
$( document ).ready(
function() {
//replace the href attributes
console.log( pfdTax.adminURL );
$( 'a[href^="upload.php"]' ).each(
function() {
var self = $( this );
var href = self.attr( 'href' ).match( /^upload.php\?page\=(\w+)$/ );
if ( ! href )
return;
self.attr( 'href', pfdTax.EditTagsURL + '?taxonomy=' + href[ 1 ] );
self.addClass( 'custom-tax-link' ).addClass( 'custom-tax-' + href[ 1 ] );
}
);
if ( 'edit-tags.php' == pfdTax.Pagenow ) {
//set the current active menu to 'media'
var tax = document.URL.match( /(?:[?|&])taxonomy\=(\w+)/ )[ 1 ];
if ( -1 < $.inArray( tax, pfdTax.Taxonomies ) ) {
$( 'a.wp-has-current-submenu' ).parent( 'li' )
.removeClass( 'wp-has-current-submenu' )
.removeClass( 'wp-menu-open' )
.addClass( 'wp-not-current-submenu' );
$( 'a.wp-has-current-submenu' ).removeClass( 'wp-has-current-submenu' ).removeClass( 'wp-menu-open' );
$( 'a[href="upload.php"]' ).each(
function() {
var self = $( this );
if ( self.hasClass( 'wp-has-submenu' ) && self.hasClass( 'wp-not-current-submenu' ) ) {
self.removeClass( 'wp-not-current-submenu' );
self.addClass( 'wp-has-current-submenu' ).addClass( 'wp-menu-open' );
self.parent( 'li' )
.removeClass( 'wp-not-current-submenu' )
.addClass( 'wp-has-current-submenu' )
.addClass( 'wp-menu-open' );
}
}
);
$( 'a.custom-tax-link' ).each(
function() {
console.log ( $( this ).hasClass( 'custom-tax-' + tax ) );
if ( $( this ).hasClass( 'custom-tax-' + tax ) ) {
$( this ).addClass( 'current' );
$( this ).parent( 'li' ).addClass( 'current' );
}
}
);
/**
* replace the parent select-box on 'turnier' term edit page
*/
if ( 'turnier' == tax ) {
$.post(
pfdTax.adminURL,
{
action : 'replace_parent_select',
term_id : ( null != document.URL.match( /(?:[?|&])tag_ID\=(\d+)/ ) ? document.URL.match( /(?:[?|&])tag_ID\=(\d+)/ )[ 1 ] : null )
},
function ( data ) {
console.log( data );
$( '#parent' ).replaceWith( data );
}
);
}
}
}
}
);
} )( jQuery );
<?php
class Pfd_Post_Types {
/**
* VERSION
*
* @const string
*/
const VERSION = '0.1';
/**
* url to this plugin
*
* @access public
* @static
* @var string
*/
public static $uri = '';
/**
* Taxonomies
*
* @var array
*/
protected $post_types = array();
/**
* labels
*
* @var array
*/
protected $labels = array();
/**
* parameters
*
* @var array
*/
protected $parameters = array();
public function init() {
self::$uri = plugins_url( '', __FILE__ );
new self;
}
/**
* constructor
*/
public function __construct() {
$this->load_textdomain();
add_action( 'admin_enqueue_scripts', array( $this, 'styles_and_scripts' ) );
# metabox
add_action( 'add_meta_boxes', array( $this, 'post_metabox' ) );
add_action( 'save_post', array( $this, 'save_details' ) );
$this->post_types = array(
'turniere'
);
$this->labels = array(
'turniere' => array(
'name' => __( 'Turniere', 'pfd-pt' ),
'singular_name' => __( 'Turnier', 'pfd-pt' ),
'add_new' => __( 'Turnier hinzufügen', 'pfd-pt' ),
'all_items' => __( 'Alle Turniere', 'pfd-pt' ),
'add_new_item' => __( 'Neues Turnier anlegen', 'pfd-pt' ),
'edit_item' => __( 'Turnier bearbeiten', 'pfd-pt' ),
'new_item' => __( 'Neues Turnier', 'pfd-pt' ),
'view_item' => __( 'Turnier ansehen', 'pfd-pt' ),
'search_items' => __( 'Turniere durchsuchen', 'pfd-pt' ),
'not_found' => __( 'Kein Turnier gefunden', 'pfd-pt' ),
'not_found_in_trash' => __( 'Kein Turnier im Papierkorb gefunden', 'pfd-pt' ),
'parent_item_colon' => __( 'Übergeordnetest Turnier', 'pfd-pt' ),
'menu_name' => __( 'Turniere', 'pfd-pt' ),
)
);
$this->parameters = array(
'turniere' => array(
'label' => __( 'Turniere', 'pfd-pt' ),
'labels' => $this->labels[ 'turnier' ],
'description' => __( 'Turniere', 'pfd-pt' ),
'public' => TRUE,
'publicly_queryable' => TRUE,
'exclude_from_search' => FALSE,
'show_ui ' => TRUE,
'show_in_menu' => TRUE,
'menu_position' => 5,
'menu_icon' => plugins_url( '/img/horse-icon-26.png', __FILE__ ),
#'capability_type' => '',
#'capabilities' => array(),
#'map_meta_cap' => '',
'hierarchical' => FALSE,
'supports' => array(
'title',
'editor',
#'author',
'excerpt',
'post-thumbnail',
'custom-fields',
'revisions'
),
'permalink_epmask' => EP_PERMALINK,
'has_archive' => TRUE,
'rewrite' => array(
'slug' => 'turnieruebersicht',
'with_front' => TRUE
),
'query_var' => 'turnieruebersicht',
'can_export' => TRUE,
'show_in_nav_menus' => TRUE,
'register_meta_box_cb' => array( $this, 'post_metabox' ),
)
);
foreach ( $this->post_types as $p ) {
register_post_type( $p, $this->parameters[ $p ] );
}
}
/**
* styles & scripts
*
* @access public
* @return void
*/
public function styles_and_scripts() {
wp_enqueue_style(
'pfd-post-type-admin-styles',
self::$uri . '/css/admin.css',
self::VERSION
);
}
/**
* metabox
*
* @access public
* @return void
*/
public function post_metabox() {
add_meta_box(
'pfd-turnier-details',
__( 'Turnierdetails', 'pfd-pt' ),
array( $this, 'details_metabox' ),
$this->post_types[ 0 ],
'side', # context
'core' # priority
);
add_meta_box(
'pfd-related-term',
__( 'Verknüpfter Term', 'pfd-pt' ),
array( $this, 'related_term_metabox' ),
$this->post_types[ 0 ],
'advanced', # context
'core' # priority
);
}
/**
* post-type details metabox
*
* @access public
* @return
*/
public function details_metabox() {
$id = get_the_ID();
$date = esc_attr( get_post_meta( $id, '_pfd_comp_date', TRUE ) );
$host = esc_attr( get_post_meta( $id, '_pfd_comp_host', TRUE ) );
$location = esc_attr( get_post_meta( $id, '_pfd_comp_location', TRUE ) );
?>
<input type="hidden" name="pfd-comp[nonce]" value="<?php echo wp_create_nonce( 'pfd-comp' ); ?>" />
<div class="inner">
<p>
<label for="pfd-comp-date"><?php _e( 'Datum', 'pfd-pt' ); ?></label><br />
<input type="text" name="pfd-comp[date]" value="<?php echo $date; ?>" id="pfd-comp-date" />
</p>
<p>
<label for="pfd-comp-location"><?php _e( 'Ort', 'pfd-pt' ); ?></label><br />
<input type="text" name="pfd-comp[location]" value="<?php echo $location; ?>" id="pfd-comp-location" />
</p>
<p>
<label for="pfd-comp-host"><?php _e( 'Veranstalter', 'pfd-pt' ); ?></label><br />
<input type="text" name="pfd-comp[host]" value="<?php echo $host; ?>" id="pfd-comp-host" />
</p>
</div>
<?php
}
/**
* metabox to add a related term
*
* @access public
* @return
*/
public function related_term_metabox() {
$id = get_the_ID();
$related_term = esc_attr( get_post_meta( $id, '_pfd_related_term', TRUE ) );
?>
<div class="inner">
<p>
<input class="large-text" type="text" name="pfd-comp[related-term]" value="<?php echo $related_term; ?>" id="pfd-related-term" />
</p>
</div>
<?php
}
/**
* save post-type details
*
* @access public
* @param int $id (Post->ID)
* @return void
*/
public function save_details( $id ) {
$data = isset( $_POST[ 'pfd-comp' ] ) ? $_POST[ 'pfd-comp' ] : NULL;
#var_dump( $data );exit;
if (
empty( $data )
|| ! wp_verify_nonce( $data[ 'nonce' ], 'pfd-comp' )
|| ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
) return;
if ( ! empty( $data[ 'date' ] ) )
update_post_meta( $id, '_pfd_comp_date', $data[ 'date' ] );
else
delete_post_meta( $id, '_pfd_comp_date' );
if ( ! empty( $data[ 'location' ] ) )
update_post_meta( $id, '_pfd_comp_location', $data[ 'location' ]);
else
delete_post_meta( $id, '_pfd_comp_location' );
if ( ! empty( $data[ 'host' ] ) )
update_post_meta( $id, '_pfd_comp_host', $data[ 'host' ] );
else
delete_post_meta( $id, '_pfd_comp_host' );
if ( ! empty( $data[ 'related-term' ] ) )
update_post_meta( $id, '_pfd_related_term', $data[ 'related-term' ] );
else
delete_post_meta( $id, '_pfd_related_term' );
}
/**
* load the language files
*
* @access protected
* @return void
*/
protected function load_textdomain() {
load_plugin_textdomain( 'pfd-pt', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/l10n' );
}
}
<?php
class Pfd_Taxonomies {
/**
* version
*
* @const string
*/
const VERSION = '0.1';
/**
* uri to this plugin
*
* @access public
* @static
* @var string
*/
public static $uri = '';
/**
* Taxonomies
*
* @var array
*/
protected $taxonomies = array();
/**
* labels
*
* @var array
*/
protected $labels = array();
/**
* the currently processed term_id
*
* @var int
*/
protected $processing_term_id = NULL;
/**
* the currently processed term
*
* @var stdClass
*/
protected $processing_term = NULL;
/**
* parameters
*
* @var array
*/
protected $parameters = array();
public function init() {
self::$uri = plugins_url( '', __FILE__ );
require_once( dirname( __FILE__ ) . '/php/class-Pfd_Walker_Tax_Select.php' );
new self;
}
/**
* constructor
*/
public function __construct() {
$this->load_textdomain();
# check for these taxonomies anywhere
add_filter( 'pfd_is_tax', array( $this, 'is_tax' ), 10, 1 );
add_action( 'wp_dashboard_setup', array( $this, 'dashboard_widgets' ) );
add_action( 'restrict_manage_posts', array( $this, 'attachment_list_filters' ) );
add_filter( 'attachment_fields_to_edit', array( $this, 'select_taxonomy_fields' ), 10, 2 );
add_filter( 'attachment_fields_to_save', array( $this, 'update_attachment' ), 10, 2 );
# wp doesn't support taxonomy-edit-links on attachment-taxonomys. so we fix it manually
add_action( 'admin_menu', array( $this, 'admin_menu_links' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'scripts_and_styles' ) );
# attachment upload form fields
add_action( 'post-upload-ui', array( $this, 'upload_ui_fields' ) );
# ajax hooks
add_action( 'wp_ajax_get_child_term_select', array( $this, 'taxonomy_select' ) );
add_action( 'wp_ajax_replace_parent_select', array( $this, 'get_top_level_parent_select' ) );
# term inster/edit formular
add_action( 'turnier_add_form_fields', array( $this, 'term_add_form' ) );
add_action( 'turnier_edit_form_fields', array( $this, 'term_edit_form' ) );
# functions to evaluate custom term field: wp_insert_term/wp_update_term
add_action( 'edited_terms', array( $this, 'store_term_id' ), 10, 1 );
add_action( 'edit_term_taxonomy', array( $this, 'store_term' ), 10, 2 );
add_action( 'edited_term', array( $this, 'additional_term_fields' ), 10, 3 );
add_action( 'created_term', array( $this, 'additional_term_fields' ), 10, 3 );
add_filter( 'get_term', array( $this, 'parse_term_description' ), 10, 2 );
add_filter( 'get_terms', array( $this, 'parse_terms' ), 10, 3 );
# create a new post when a term is created
add_action( 'created_term', array( $this, 'create_related_post' ), 10, 3 );
# shortcode to display a list of child-terms
add_shortcode( 'aufgaben', array( $this, 'child_term_shortcode' ) );
$this->taxonomies = array(
'turnier',
'galerie'
);
$this->labels = array(
'turnier' => array(
'name' => __( 'Turniere', 'pfd-tax' ),
'search' => __( 'In Turnieren suchen', 'pfd-tax' ),
'edit_item' => __( 'Turnier bearbeiten', 'pfd-tax' ),
'all_items' => __( 'Alle Turniere', 'pfd-tax' ),
'view_item' => __( 'Turnier ansehen', 'pfd-tax' ),
'update_item' => __( 'Turnier aktualisieren', 'pfd-tax' ),
'search_items' => __( 'Turnier suchen', 'pfd-tax' ),
'add_new_item' => __( 'Neues Turnier erstellen', 'pfd-tax' ),
'singular_name' => __( 'Turnier', 'pfd-tax' ),
'popular_items' => __( 'Beliebte Turniere', 'pfd-tax' ),
'new_item_name' => __( 'Neues Turnier', 'pfd-tax' ),
'add_or_remove_items' => __( 'Turnier hinzufügen oder entfernen', 'pfd-tax' ),
'choose_from_most_used' => __( 'Wähle aus häufig genutzten Turnieren', 'pfd-tax' ),
'separate_items_with_commas' => __( 'Trenne die Turniere durch Kommas', 'pfd-tax' ),
),
'galerie' => array(
'name' => __( 'Galerien', 'pfd-tax' ),
'search' => __( 'In Galerien suchen', 'pfd-tax' ),
'edit_item' => __( 'Galerie bearbeiten', 'pfd-tax' ),
'all_items' => __( 'Alle Galerien', 'pfd-tax' ),
'view_item' => __( 'Galerie ansehen', 'pfd-tax' ),
'update_item' => __( 'Galerie aktualisieren', 'pfd-tax' ),
'add_new_item' => __( 'Neues Galerie erstellen', 'pfd-tax' ),
'search_items' => __( 'Galerie suchen', 'pfd-tax' ),
'singular_name' => __( 'Galerie', 'pfd-tax' ),
'popular_items' => __( 'Beliebte Galerie', 'pfd-tax' ),
'new_item_name' => __( 'Neue Galerie', 'pfd-tax' ),
'add_or_remove_items' => __( 'Galerie hinzufügen oder entfernen', 'pfd-tax' ),
'choose_from_most_used' => __( 'Wähle aus häufig genutzten Galerien', 'pfd-tax' ),
'separate_items_with_commas' => __( 'Trenne die Galerien durch Kommas', 'pfd-tax' ),
)
);
$this->parameters = array(
'turnier' => array(
'label' => __( 'Turniere', 'pfd-tax' ),
'labels' => $this->labels[ 'turnier' ],
'public' => TRUE,
'show_ui' => TRUE,
'rewrite' => array( 'slug' => 'turnier', 'hierarchical' => TRUE ),
'_builtin' => FALSE,
'query_var' => 'turnier',
'hierarchical' => TRUE,
'capabilities' => array(),
'show_tagcloud' => TRUE,
'show_in_nav_menus' => TRUE,
'update_count_callback' => '_update_generic_term_count', #workaround to trigger always the right function
),
'galerie' => array(
'label' => __( 'Galerien', 'pfd-tax' ),
'labels' => $this->labels[ 'topic' ],
'public' => TRUE,
'show_ui' => TRUE,
'rewrite' => array( 'slug' => 'galerie', 'hierarchical' => TRUE ),
'_builtin' => FALSE,
'query_var' => 'galerie',
'hierarchical' => TRUE,
'capabilities' => array(),
'show_tagcloud' => TRUE,
'show_in_nav_menus' => TRUE,
'update_count_callback' => '_update_generic_term_count',
)
);
foreach ( $this->taxonomies as $t ) {
register_taxonomy( $t, 'attachment', $this->parameters[ $t ] );
}
}
/**
* enqueue scripts and styles
*
* @access public
* @return void
*/
public function scripts_and_styles() {
$this->js_globals();
# admin menu ui
wp_enqueue_script(
'pfd-tax-admin-menu-ui',
self::$uri . '/js/admin-menu.js',
self::VERSION,
array( 'jquery' )
);
wp_localize_script(
'pfd-tax-admin-menu-ui',
'pfdTax',
$this->js_globals[ 'admin_menu_ui' ]
);
#upload ui
wp_register_script(
'pfd-tax-upload-ui',
self::$uri . '/js/upload-ui.js',
self::VERSION,
array( 'jquery' ),
TRUE
);
wp_localize_script(
'pfd-tax-upload-ui',
'pfdTaxUpload',
$this->js_globals[ 'upload_ui' ]
);
}
/**
* sets js globals
*
* @access protected
* @global $pagenow
* @return void
*/
protected function js_globals() {
global $pagenow;
$this->js_globals = array(
'admin_menu_ui' => array(
'adminURL' => admin_url( 'admin-ajax.php' ),
'Taxonomies' => $this->taxonomies,
'EditTagsURL' => admin_url( 'edit-tags.php' ),
'Pagenow' => $pagenow
),
'upload_ui' => array(
'MultitaxNonce' => wp_create_nonce( 'pfd-multitax-nonce' ),
'adminURL' => admin_url( 'admin-ajax.php' ),
'childTermAction' => 'get_child_term_select',
)
);
}
/**
* put the links to the admin menu
*
* @access public
* @return void
*/
public function admin_menu_links() {
foreach ( $this->taxonomies as $t ) {
add_submenu_page(
'upload.php',
$this->labels[ $t ][ 'name' ],
$this->labels[ $t ][ 'name' ],
'edit_posts',
$t,
array( $this, 'tax_edit_link' )
);
}
}
/**
* place a link in the content, if the javascript fails
*
* @access public
* @return void
*/
public function tax_edit_link() {
?>
<div class="inside">
<p>
<a href="<?php echo admin_url( 'edit-tags.php' ); ?>?taxonomy=<?php echo esc_attr( $_GET[ 'page' ] ); ?>"><?php
_e( 'Hier entlang', 'pfd-tax' ); ?></a></p>
</div>
<?php
}
/**
* extend the term edit formular
*
* @access public
* @return void
*/
public function term_edit_form() {
$term_id = (int) $_REQUEST[ 'tag_ID' ];
$term = get_term( $term_id, 'turnier', OBJECT, 'edit' );
$date = isset( $term->pfd_date ) ? $term->pfd_date : '';
?>
<tr class="form-field">
<td><label for="pfd-term-date">Datum</label></td>
<td><input type="datetime" name="pfd[term-date]" id="pfd-term-date" value="<?php echo $date; ?>" /></td>
</tr>
<?php
}
/**
* extend the term insert formular
*
* @access public
* @return void
*/
public function term_add_form() {
?>
<div class="form-field">
<label for="pfd-term-date">Datum</label>
<input type="datetime" name="pfd[term-date]" id="pfd-term-date" value="" />
</div>
<?php
}
/**
* places extra fields into the term description after
* they where inserted/updated (use POST request input)
*
* @access public
* @global $wpdb
* @param int $term_id
* @param int $term_tax_id
* @param $taxonomy
* @return void
*/
public function additional_term_fields( $term_id, $term_taxonomy_id, $taxonomy ) {
$date = $desc = NULL;
if (
'turnier' !== $taxonomy
|| (
! isset( $_POST[ 'pfd' ][ 'term-date' ] )
&& NULL === $this->processing_term
)
)
return;
$term = get_term_by( 'id', $term_id, $taxonomy );
$desc = $term->description;
if ( ! empty( $_POST[ 'pfd' ][ 'term-date' ] ) ) {
# parse the current description
list( $date, $desc ) = explode( "\t", $term->description );
# still no date?
if ( 0 !== strlen( $desc ) )
$desc = $date;
else
$desc = ' ';
$date = $this->sanitize_datetime( $_POST[ 'pfd' ][ 'term-date' ] );
$desc = $date . "\t" . $desc;
}
elseif ( ! empty( $this->processing_term->pfd_date ) ) {
$date = $this->processing_term->pfd_date;
# in this case the date was striped from the description so
# we can simply add it
if ( ! empty( $term->description ) )
$desc = $date . "\t" . $term->description;
else
$desc = $date ."\t "; # the space is important!
}
# prevent infinite loop
unset( $_POST[ 'pfd' ][ 'term-date' ] );
$this->processing_term = NULL;
remove_action( 'edited_terms', array( $this, 'store_term_id' ), 10, 1 );
remove_action( 'edit_term_taxonomy', array( $this, 'store_term' ), 10, 2 );
#echo '<pre>'; var_dump( $desc ); exit;
wp_update_term(
$term->term_id,
$taxonomy,
array(
'description' => $desc,
'parent' => $term->parent,
'slug' => $term->slug,
)
);
}
/**
* store the term_id druing the process of wp_update_term()
* this is called on 'edited_terms' action
*
* @access public
* @param int $id
* @return void
*/
public function store_term_id( $term_id ) {
$this->processing_term_id = $term_id;
}
/**
* store the term (at this point the taxonomy is available)
*
* @access public
* @param int $term_taxonomy_id
* @param string $taxonomy
* @return void
*/
public function store_term( $term_taxonomy_id, $taxonomy ) {
if ( 'turnier' !== $taxonomy
|| empty( $this->processing_term_id )
) {
$this->processing_term_id = NULL;
return;
}
$this->processing_term = get_term( $this->processing_term_id, $taxonomy );
}
/**
* extract the datetime from the term-description
*
* @access public
* @param strClass $term
* @param string $taxonomy
* @return stdClass
*/
public function parse_term_description( $term, $taxonomy ) {
if ( 'turnier' !== $taxonomy )
return $term;
list( $date, $desc ) = explode( "\t", $term->description );
if ( ! $desc )
return $term;
$term->description = trim( $desc );
$term->pfd_date = $date;
return $term;
}
/**
* parse each term description on 'get_terms'
*
* @access public
* @param array $terms
* @param array $taxonomies
* @param $args
* @return $array
*/
public function parse_terms( $terms, $taxonomies, $args ) {
if ( ! in_array( 'turnier', $taxonomies ) ) {
return $terms;
}
foreach ( $terms as &$t ) {
$t = $this->parse_term_description( $t, 'turnier' );
}
return $terms;
}
/**
* create a related post
*
* @access public
* @param int $term_id
* @param int $term_taxonomy_id
* @param string $taxonomy
* @return void
*/
public function create_related_post( $term_id, $term_taxonomy_id, $taxonomy ) {
if ( 'turnier' !== $taxonomy )
return;
$term = get_term( $term_id, $taxonomy );
# only top level terms are interesting
if ( 0 < ( int ) $term->parent )
return;
# a post with the slug does alreay exists?
$post = get_posts(
array(
'post_type' => 'turnier',
'meta_query' => array(
array(
'key' => '_pfd_related_term',
'value' => $term->slug,
'compare' => '='
)
)
)
);
if ( $posts && 0 < count( $posts ) )
return;
$args = array(
'author' => get_current_user_id(),
'post_type' => 'turnier',
'post_content' => '[aufgaben]',
'post_title' => $term->name,
'post_status' => 'draft',
'post_name' => $term->slug
);
$id = wp_insert_post( $args );
add_post_meta( $id, '_pfd_related_term', $term->slug, TRUE );
}
/**
* sanitize datetime yyyy-mm-dd HH:ii
*
* @access public
* @param string
* @return string
*/
public function sanitize_datetime( $string ) {
$date = $time = NULL;
list( $date, $time ) = explode( ' ', $string );
if ( $time ) {
$time = explode( ':', $time );
# hours
$time[ 0 ] = empty( $time[ 0 ] ) ? 0 : ( int ) $time[ 0 ];
# minutes
$time[ 1 ] = empty( $time[ 1 ] ) ? 0 : ( int ) $time[ 1 ];
if ( 0 <= $time[ 0 ] && $time[ 0 ] <=23 ) {
if ( 0 === $time[ 0 ] )
$time[ 0 ] = '00';
else
$time[ 0 ] = ( string ) $time[ 0 ];
if ( 1 === strlen( $time[ 0 ] ) )
$time[ 0 ] = '0' . $time[ 0 ];
}
else
$time[ 0 ] = '00';
if ( 0 <= $time[ 1 ] && $time[ 1 ] <=59 ) {
if ( 0 === $time[ 1 ] )
$time[ 1 ] = '00';
else
$time[ 1 ] = ( string ) $time[ 1 ];
if ( 1 === strlen( $time[ 1 ] ) )
$time[ 1 ] = '0' . $time[ 1 ];
}
else
$time[ 1 ] = '00';
$time = $time[ 0 ] . ':' . $time[ 1 ];
}
else
$time = '00:00';
if ( $date ) {
$date = explode( '-', $date );
#year
$date[ 0 ] = empty( $date[ 0 ] ) ? 0 : ( int ) $date[ 0 ];
#month
$date[ 1 ] = empty( $date[ 1 ] ) ? 0 : ( int ) $date[ 1 ];
#day
$date[ 2 ] = empty( $date[ 2 ] ) ? 0 : ( int ) $date[ 2 ];
if ( 1000 <= $date[ 0 ] && $date[ 0 ] <= 2999 )
$date[ 0 ] = ( string ) $date[ 0 ];
else
$date[ 0 ] = date( 'Y' );
if ( 1 <= $date[ 1 ] && $date[ 1 ] <= 12 ) {
$date[ 1 ] = ( string ) $date[ 1 ];
if ( 1 === strlen( $date[ 1 ] ) )
$date[ 1 ] = '0' . $date[ 1 ];
}
else
$date[ 1 ] = date( 'm' );
if ( 1 <= $date[ 2 ] && $date[ 2 ] <= 31 ) {
$date[ 2 ] = ( string ) $date[ 2 ];
if ( 1 === strlen( $date[ 2 ] ) )
$date[ 2 ] = '0' . $date[ 2 ];
}
else
$date[ 2 ] = date( 'd' );
$date = implode( '-', $date );
}
else
$date = date( 'Y-m-d' );
return $date . ' ' . $time;
}
/**
* get datetime in format
*
* @access public
* @param string $datetime
* @param string $format
* @return string
*
* @todo Find the correct format-string for Wekdays. 'D' get only the abbravitions
*/
public function get_date_formated( $datetime, $format = 'Y-m-d H:i' ) {
if ( empty( $format ) )
return $datetime;
$ts = strtotime( $datetime );
if ( FALSE === strpos( $format, 'D' )
&& FALSE === strpos( $format, 'l' )
)
return date( $format, $ts );
$translate =
array(
'Monday' => __( 'Montag', 'pfd-tax' ),
'Tuesday' => __( 'Dienstag', 'pfd-tax' ),
'Wednesday' => __( 'Mittwoch', 'pfd-tax' ),
'Thursday' => __( 'Donnerstag', 'pfd-tax' ),
'Friday' => __( 'Freitag', 'pfd-tax' ),
'Saturday' => __( 'Sonnabend', 'pfd-tax' ),
'Sunday' => __( 'Sonntag', 'pfd-tax' ),
'Mon' => __( 'Mo', 'pfd-tax' ),
'Tue' => __( 'Di', 'pfd-tax' ),
'Wed' => __( 'Mi', 'pfd-tax' ),
'Thu' => __( 'Do', 'pfd-tax' ),
'Fri' => __( 'Fr', 'pfd-tax' ),
'Sat' => __( 'Sa', 'pfd-tax' ),
'Sun' => __( 'So', 'pfd-tax' ),
);
return str_replace(
array_keys( $translate),
array_values( $translate ),
date( $format, $ts )
);
}
/**
* selectfields in the attachment formular
*
* hooked to 'attachment_fields_to_edit'
*
* @access public
* @param array $fields
* @param object $post
* @return array $fields
*/
public function select_taxonomy_fields( $fields, $post ) {
foreach( $this->taxonomies as $t ) {
$tax = get_terms( $t, array( 'taxonomy' => $t, 'hide_empty' => 0 ) );
if ( empty( $tax ) )
continue;
#remove default custom-tax field
unset( $fields[ $t ] );
$post_tax = wp_get_object_terms( $post->ID, $t );
$post_terms = array();
foreach( $post_tax as $term ) {
$post_terms[] = $term->term_id;
}
$selected = isset( $post_terms[ 0 ] ) ? $post_terms[ 0 ] : -1;
$field = array();
$field[ 'label' ] = $this->labels[ $t ][ 'name' ];
$field[ 'input' ] = 'html';
$field[ 'html' ] = wp_dropdown_categories(
array(
'echo' => 0,
'id' => 'attachments[' . $post->ID . '][pfd_' . $t . ']',
'name' => 'attachments[' . $post->ID . '][pfd_' . $t . ']',
'taxonomy' => $t,
'hierarchical' => 1,
'hide_empty' => 0,
'show_option_none' => __( '(keine)', 'pfd-tax' ),
'show_count' => 1,
'selected' => ( int ) $selected,
)
);
$field[ 'html' ] .= $this->get_hidden_taxonomy_fields( $t, $post->ID );
$fields[ 'pfd_' . $t ] = $field;
}
return $fields;
}
/**
* get a hidden field which is meant to be filled by javascript
* this is used on multiple uploads to attach all images to a taxonomy
*
* @access protected
* @param string $taxonomy
* @param int $post_id
* @return string
*/
protected function get_hidden_taxonomy_fields( $taxonomy, $post_id ) {
$field =
'<input'
. ' type="hidden"'
. ' name="attachments[' . $post_id . '][pfd_multitax][' . $taxonomy . ']"'
. ' class="pfd_multitax_input_' . $taxonomy . '"'
. ' value=""'
. ' />';
return $field;
}
/**
* link or unlink an attachment to the taxonomy on saving
*
* hooked to 'attachment_fields_to_save'
*
* @access public
* @param array $post
* @param array $fields Equals $_POST[ 'attachment' ][ {PostID} ]
* @return array $post
*/
public function update_attachment( $post, $fields ) {
$id = $post[ 'ID' ];
foreach( $this->taxonomies as $t ) {
# multiupload taxonomy?
$term = isset( $fields[ 'pfd_multitax' ][ $t ] )
? ( int ) $fields[ 'pfd_multitax' ][ $t ]
: NULL;
# the selection for the single post owerrides the multi-image selection
$term = isset( $fields[ 'pfd_' . $t ] ) && 0 < ( int ) $fields[ 'pfd_' . $t ]
? ( int ) $fields[ 'pfd_' . $t ]
: $term;
if ( -1 === $term )
$term = NULL;
wp_set_object_terms( $id, $term, $t );
}
return $post;
}
/**
* provide filters to the post overview in backend
*
* attachment list filters
*
* hooked to action 'restrict_manage_posts';
*
* @access public
* @return void
*/
public function attachment_list_filters() {
if ( 'attachment' != get_query_var( 'post_type' ) )
return;
foreach ( $this->taxonomies as $t ) {
/**
* wp_dropdown_categories() seems not to work here, because the values
* for query_var '{taxonomy}' must be the slug, not the id!
*/
?>
<select name="<?php echo $this->parameters[ $t ][ 'query_var' ]; ?>">
<option value=""><?php echo $this->labels[ $t ][ 'all_items' ]; ?></option>
<?php
wp_list_categories(
array(
'list' => '',
'walker' => new Pfd_Walker_Tax_Select,
'title_li' => '',
'selected' => get_query_var( $this->parameters[ $t ][ 'query_var' ] ),
'taxonomy' => $t,
'hide_empty' => 0,
)
);
?>
</select>
<?php
}
}
/**
* link to the edit page of each taxonomy in a dashboard widget
*
* @access public
* @global $wp_metaboxes
* @return void
*/
public function dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget(
'pfd_taxonomy_management',
__( 'Galerieverwaltung', 'pfd-tax' ),
array( $this, 'taxonomy_dashboard_widget' )
);
}
/**
* dashboard widget to link to the taxonomy edit pages
*
* @access public
* @return void
*/
public function taxonomy_dashboard_widget() {
?>
<div>
<?php foreach ( $this->taxonomies as $t ) : ?>
<p>
<a href="<?php echo get_admin_url( NULL, '/edit-tags.php?taxonomy=' . $t );?>"><?php echo $this->labels[ $t ][ 'edit_item' ]; ?></a>
</p>
<?php endforeach; ?>
</div>
<?php
}
/**
* conditional template tag
*
* @access public
* @param bool $default
* @return bool
*/
public function is_tax( $default = FALSE ) {
foreach ( $this->taxonomies as $t ) {
if ( TRUE === is_tax( $t ) )
return TRUE;
}
return $default;
}
/**
* place select-elements to the upload screen to attach multiple
* images on a taxonomy
*
* @access public
* @return void
*/
public function upload_ui_fields() {
?><div class="inside">
<p><?php _e( 'Alle Bilder folgenden Galerien/Turnieren zuweisen', 'pfd-tax' ); ?></p>
<p><?php
$this->taxonomy_select( $this->taxonomies[ 1 ] );
$this->taxonomy_select( $this->taxonomies[ 0 ], NULL, array( 'hierarchical' => FALSE, 'parent' => 0, 'class' => 'pfd-multitax-pre-select' ) );
?></p>
</div><?php
# get the script
wp_enqueue_script( 'pfd-tax-upload-ui' );
}
/**
* print the galery-tax select-element
*
* @access public
* @param int $selected
* @return void
*/
public function taxonomy_select( $tax = '', $selected = NULL, $args = array() ) {
$depth = isset( $args[ 'depth' ] ) ? $args[ 'depth' ] : 0;
$parent = isset( $args[ 'parent' ] ) ? $args[ 'parent' ] : 0;
$class = isset( $args[ 'class' ] ) ? $args[ 'class' ] : 'pfd-multitax-select';
$hierarchical = isset( $args[ 'hierarchical' ] ) ? $args[ 'hierarchical' ] : 1;
$ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? TRUE : FALSE;
if ( $ajax ) {
if ( ! wp_verify_nonce( $_POST[ 'pfd_multitax_nonce' ], 'pfd-multitax-nonce' ) )
exit( 'Nope' );
$parent = ( int ) $_POST[ 'pfd_parent_term_id' ];
$tax = $_POST[ 'pfd_taxonomy' ];
}
if ( ! $ajax && ! in_array( $tax, $this->taxonomies ) )
return;
wp_dropdown_categories(
array(
'echo' => 1,
'id' => 'pfd-multitax-' . $tax . ( 0 !== $parent ? '-1' : '' ),
'name' => '',
'taxonomy' => $tax,
'hierarchical' => $hierarchical,
'depth' => $depth,
'parent' => $parent,
'hide_empty' => 0,
'show_option_none' => sprintf( __( '(keine) %s', 'pfd-tax' ), $this->labels[ $tax ][ 'name' ] ),
'show_count' => 0,
'selected' => ( int ) $selected,
'class' => $class
)
);
if ( $ajax )
exit;
}
/**
* prints a select element of top-level terms
* ajax-response function
*
* @access public
* @return void
*/
public function get_top_level_parent_select() {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )
return;
$term_id = empty( $_POST[ 'term_id' ] ) ? NULL : ( int ) $_POST[ 'term_id' ];
$parent = 0;
if ( $term_id ) {
$term = get_term( $term_id, 'turnier' );
if ( $term )
$parent = $term->parent;
}
wp_dropdown_categories(
array(
'taxonomy' => 'turnier',
'hide_empty' => 0,
'depth' => 1,
'name' => 'parent',
'oderby' => 'description',
'order' => 'DESC',
'child_of' => 0,
'hierarchical' => TRUE,
'selected' => $parent,
'show_option_none' => __( 'keine', 'pfd-tax' )
)
);
exit;
}
/**
* shortcode to list child terms
*
* @access public
* @global $wpdb, $post
* @param array $atts
* @return string
*/
public function child_term_shortcode( $atts ) {
global $wpdb, $post;
$current_day = NULL;
$atts = shortcode_atts(
array(
'id' => ''
),
$atts
);
if ( ! empty( $atts[ 'id' ] ) )
$term = get_term( $atts[ 'id' ], $this->taxonomies[ 0 ] );
else {
$term = get_term_by(
'slug',
$post->post_name,
$this->taxonomies[ 0 ]
);
}
if ( ! $term || $term instanceof WP_Error )
return '';
add_filter( 'get_terms_orderby', array( $this, 'order_terms_by_description' ) );
$childs = get_terms(
$this->taxonomies[ 0 ],
array(
'child_of' => $term->term_id,
'hide_empty' => 0,
'order' => 'ASC',
'orderby' => 'count' # important to use count!
)
);
remove_filter( 'get_terms_orderby', array( $this, 'order_terms_by_description' ) );
#echo '<pre>'; var_dump( $term, $childs ); exit;
if ( ! $childs )
return;
$current_day = $this->get_date_formated( $childs[ 0 ]->pfd_date, 'l, d.m.' );
?>
<h3 class="indent"><?php echo $current_day; ?></h3>
<ul class="eventlist"><?php
foreach ( $childs as $c ) {
if ( $current_day !== $this->get_date_formated( $c->pfd_date, 'l, d.m.' ) ) {
$current_day = $this->get_date_formated( $c->pfd_date, 'l, d.m.' ); ?>
</ul>
<h3 class="indent"><?php echo $current_day; ?></h3>
<ul class="eventlist">
<?php
}
?>
<li>
<a href="<?php echo get_term_link( $c ); ?>">
<span class="date cell"><?php echo $this->get_date_formated( $c->pfd_date, 'H:i' ); ?></span>
<span class="title cell"><?php echo $c->name; ?></span>
</a>
</li>
<?php
}
?>
</ul>
<?php
}
/**
* sql-orderby clause to sort terms by description
*
* @access public
* @param string $orderby
* @return string
*/
public function order_terms_by_description( $orderby ) {
return str_replace( '.count', '.description', $orderby );
}
/**
* load the language files
*
* @access protected
* @return void
*/
protected function load_textdomain() {
load_plugin_textdomain( 'pfd-tax', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/l10n' );
}
}
<!DOCTYPE html>
<!--
- some strange padding occurs on li-elements in firefox due to
- list-style-position: inside
-->
<html lang="en">
<meta charset="utf-8" />
<title>Floated list elements</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
article {
width: 50em;
margin: 10px auto;
padding: 10px;
background: #ddd;
}
h1, ul {
float:left;
}
ul {
list-style-type: none;
list-style-position: inside;
}
li {
float: left;
outline: 1px solid #000;
border-right: 1px dotted #f00;
padding-right: 0;
margin-right: 0;
}
li span {
background: #aaf;
padding: 3px;
}
p:after {
content:".";
clear:left;
display:block;
wilih:0;
height:0;
text-indent: -9999px;
}
</style>
<article>
<h1>List-style-possiton</h1>
<ul>
<li><span>one</span></li>
<li><span>two</span></li>
<li><span>foo</span></li>
</ul>
<p>Lorem Ipsum</p>
</article>
</html>
~(box(?:-[0-9]+)?\.(?:jpe?g|png))~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment