Skip to content

Commit

Permalink
Merge pull request #310 from matheusgimenez/master
Browse files Browse the repository at this point in the history
Adicionando classe de Post Status
  • Loading branch information
fdaciuk committed Jul 12, 2015
2 parents cdbb53c + 74fac05 commit f6d6d7d
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
41 changes: 41 additions & 0 deletions core/assets/js/admin-custom-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(function ( $ ) {
'use strict';

/**
* Custom post status
*/
$( window ).load( function() {
var html = '';
var label = false;
var $odinMeta = document.querySelectorAll( 'meta.odin-custom-status-meta' );

Array.prototype.forEach.call( $odinMeta, function( item ) {
var $meta = $( item );
var args = $.parseJSON( $meta.attr( 'value' ) );

if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) {
var select = '';
if( args.select ) {
select = 'selected="selected"';
label = '<span id="post-status-display">&nbsp;' + $.trim( args.appliedLabel ) + '</span>';
}
html += '<option value="' + $.trim( args.slug ) + '" ' + $.trim( select ) + '>' + $.trim( args.appliedLabel ) + '</option>';
}
if( $( document.body ).hasClass( 'edit-php' ) ) {
html += '<option value="' + $.trim( args.slug ) + '">' + $.trim( args.appliedLabel ) + '</option>';
}
});
if( $( document.body ).hasClass( 'post-php' ) || $( document.body ).hasClass( 'post-new-php' ) ) {
$( '#post_status ').append( html );
if( label ) {
$( 'label[for="post_status"]' ).append( label );
}
}
if( $( document.body ).hasClass( 'edit-php' ) ) {
var $inlineStatus = document.querySelectorAll( '.inline-edit-status select' );
Array.prototype.forEach.call( $inlineStatus, function( item ) {
$( item ).append( html );
});
}
});
}( jQuery ));
132 changes: 132 additions & 0 deletions core/classes/class-post-status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Odin_Post_Status Class.
*
* Build Custom Post Status
*
* @package Odin
* @category Metabox
* @author WPBrasil
* @version 2.1.4
*/
class Odin_Post_Status {

/**
* The name of Custom Post Status.
*
* @var string
**/
protected $post_status;

/**
* Array of Post Types will applied to.
*
* @var arrays
**/
protected $post_types = array();

/**
* Text used to display the custom post status
* when it can be applied to a post.
*
* @var string
**/
protected $action_label;

/**
* Text used to display the custom post status
* when it has been applied to a post.
*
* @var string
**/
protected $applied_label;

/**
* Array of arguments to pass register_post_status();
*
* @var array
**/
protected $args = array();

/**
* Constructor
*
* @param string $post_status Name of the Custom Post Status
* @param array $post_types Array of Posts Types to apply the Custom Post Status
* @param array $args Array of arguments to pass register_post_status() function
*
* @return void
**/
public function __construct( $post_status, $post_types, $args ) {
$this->post_status = $post_status;
$this->post_types = $post_types;
$this->action_label = isset( $args['label'] ) ? $args['label'] : $post_status;
$this->applied_label = isset( $args['applied_label'] ) ? $args['applied_label'] : $this->action_label;
$this->args = $args;

// Removes the arguments that do not belong to register_post_type
unset( $this->args['applied_label'] );

if( ! isset( $this->args['label_count'] ) ) {
$this->args['label_count'] = _n_noop( $this->applied_label . '&nbsp;<span class="count">(%s)</span>', $this->applied_label . '&nbsp;<span class="count">(%s)</span>' );
}

// Register post status
add_action( 'init', array( $this, 'register_post_status' ) );

// Add meta tags to pass args
add_action( 'admin_head', array( $this, 'meta_tags' ) );

// Load scripts
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );

}

/**
* Register the Custom Post Status with Wordpress ;)
*
* @param string $post_status The name of Custom Post Status.
* @param array $args Array of arguments to pass register_post_status()
*
* @return void
**/
public function register_post_status() {
register_post_status( $this->post_status, $this->args );
}

/**
* Add meta tags to JS
*
* @return void
*/
public function meta_tags() {
$screen = get_current_screen();
if( ! in_array( $screen->post_type, $this->post_types ) ) {
return;
}

$args = array(
'postTypes' => $this->post_types,
'appliedLabel' => $this->applied_label,
'slug' => $this->post_status,
);
if( $screen->base === 'post' ) {
global $post;
if( is_object( $post ) && $post->post_status === $this->post_status ) {
$args['select'] = true;
}
}
printf( '<meta class="odin-custom-status-meta" value="%s" />', esc_attr( json_encode( $args ) ) );
}

/**
* Load post status scripts and inject JS vars
*
* @return void
*/
public function scripts() {
// Load admin JS
wp_enqueue_script( 'odin-custom-status', get_template_directory_uri() . '/core/assets/js/admin-custom-status.js', array( 'jquery' ), null, true );

}
}
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// require_once get_template_directory() . '/core/classes/class-contact-form.php';
// require_once get_template_directory() . '/core/classes/class-post-form.php';
// require_once get_template_directory() . '/core/classes/class-user-meta.php';
// require_once get_template_directory() . '/core/classes/class-post-status.php';

/**
* Odin Widgets.
Expand Down

0 comments on commit f6d6d7d

Please sign in to comment.