Instantly share code, notes, and snippets.

Embed
What would you like to do?
Content Factory - WordPress
<?php
/**
* ContentFactory
* Classe responsável pela criação de conteúdo personalizado
*
* @author Mário Valney <mariovalney@gmail.com>.
*/
if (!defined( 'ABSPATH' )) {
exit; // Impede o acesso direto
}
if( !class_exists( 'ContentFactory' ) ) :
/**
* Classe responsável pela criação de conteúdos
* Deve ser extendida pela classe de cada tipo de conteúdo
*
* @abstract
* @version 1.0.0
*/
abstract class ContentFactory {
/**
* @var string Nome do conteúdo (até 20 caracteres em caixa baixa e sem espaços)
*/
protected $name;
/**
* @var boolean Indica se os labels devem receber artigos masculinos ou femininos
*/
protected $is_male = true;
/**
* @var array Array de argumentos como em register_post_type.
* @link https://codex.wordpress.org/Function_Reference/register_post_type register_post_type
*/
protected $post_args = array();
/**
* @var array Array de taxonomias (propriedades)
*/
protected $taxonomies = array();
/**
* A mágica começa aqui
*/
final protected function init() {
add_action( 'init', array($this, 'register_content' ), 6 );
add_filter( 'post_updated_messages', array($this, 'update_messages' ) );
add_action( 'add_meta_boxes_' . $this->name, array($this, 'add_meta_boxes' ), 5 );
add_action( 'save_post_' . $this->name, array($this, 'save_meta_boxes' ), 5 );
if($GLOBALS['is_activation_hook']) {
$this->register_content();
}
}
/**
* Faz o parse dos argumentos do post
* @param array $args
* @return array
*/
protected function parse_post_args( $args, $is_male = false ) {
$default = array(
'hierarchical' => false,
'description' => '',
'taxonomies' => $this->taxonomies,
'public' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'query_var' => true,
'can_export' => true,
'capability_type' => 'post',
'supports' => array( 'title', 'editor', 'author' ),
'has_archive' => true,
'rewrite' => array( 'slug' => $this->get_slug() ),
'labels' => array(
'name' => __( 'Plural Name', TEXT_DOMAIN ),
'singular_name' => __( 'Singular Name', TEXT_DOMAIN ),
'add_new' => sprintf( _x( 'Adicionar %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'add_new_item' => ( $is_male ) ? sprintf( __( 'Adicionar novo %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( __( 'Adicionar nova %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'edit_item' => sprintf( __( 'Editar %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'new_item' => ( $is_male ) ? sprintf( __( 'Novo %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( __( 'Nova %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'view_item' => sprintf( __( 'Ver %s', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'search_items' => sprintf( __( 'Procurar por %s', TEXT_DOMAIN ), $args['labels']['name'] ),
'not_found' => ($is_male) ? sprintf( __( 'Nenhum %s aqui', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( __( 'Nenhuma %s aqui', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'not_found_in_trash' => ($is_male) ? sprintf( __( 'Nenhum %s na lixeira', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( __( 'Nenhuma %s na lixeira', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'parent_item_colon' => sprintf( __( '%s pai:', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'menu_name' => $args['labels']['name'],
'insert_into_item' => ( $is_male ) ? sprintf( _x( 'Inserir no %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( _x( 'Inserir na %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'uploaded_to_this_item' => ( $is_male ) ? sprintf( _x( 'Anexadas ao %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['singular_name'] ) : sprintf( _x( 'Anexadas à %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['singular_name'] ),
'filter_items_list' => sprintf( _x( 'Filtrar lista de %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['name'] ),
'items_list_navigation' => sprintf( _x( 'Navegação da lista de %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['name'] ),
'items_list' => sprintf( _x( 'Lista de %s', 'Admin menu', TEXT_DOMAIN ), $args['labels']['name'] )
)
);
$return = wp_parse_args( $args, $default );
$return['labels'] = wp_parse_args( $args['labels'], $default['labels'] );
return $return;
}
/**
* Registra o conteúdo usando a função do WordPress
*/
final public function register_content() {
do_action( 'before_register_' . $this->name );
if ( ! post_type_exists( $this->name ) ) {
$this->post_args = $this->parse_post_args( $this->post_args, $this->is_male );
register_post_type( $this->name, $this->post_args );
}
do_action( 'after_register_' . $this->name );
}
/**
* Retorna o slug do conteúdo. Por padrão, o "name".
* @return string
*/
protected function get_slug() {
return $this->name;
}
/**
* Altera as mensagens mostradas na administração
*/
public function update_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$labels = $this->post_args['labels'];
$messages[$this->name] = array(
0 => '',
1 => ( $this->is_male ) ? __( $labels['singular_name'] . ' atualizado.', TEXT_DOMAIN ) : __( $labels['singular_name'] . ' atualizada.', TEXT_DOMAIN ),
2 => __( 'Campo personalizado atualizado.', TEXT_DOMAIN ),
3 => __( 'Campo personalizado apagado.', TEXT_DOMAIN ),
4 => ( $this->is_male ) ? __( $labels['singular_name'] . ' atualizado.', TEXT_DOMAIN ) : __( $labels['singular_name'] . ' atualizada.', TEXT_DOMAIN ),
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Revisão restaurada de %s', TEXT_DOMAIN ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => ( $this->is_male ) ? __( $labels['singular_name'] . ' publicado.', TEXT_DOMAIN ) : __( $labels['singular_name'] . ' publicada.', TEXT_DOMAIN ),
7 => ( $this->is_male ) ? __( $labels['singular_name'] . ' salvo.', TEXT_DOMAIN ) : __( $labels['singular_name'] . ' salva.', TEXT_DOMAIN ),
8 => ( $this->is_male ) ? __( $labels['singular_name'] . ' enviado.', TEXT_DOMAIN ) : __( $labels['singular_name'] . ' enviada.', TEXT_DOMAIN ),
9 => ( $this->is_male ) ? sprintf( __( $labels['singular_name'] . ' programado para: <strong>%1$s</strong>.', TEXT_DOMAIN ), date_i18n( __( 'd m Y @ G:i', TEXT_DOMAIN ), strtotime( $post->post_date ) ) ) : sprintf( __( $labels['singular_name'] . ' programada para: <strong>%1$s</strong>.', TEXT_DOMAIN ), date_i18n( __( 'd m Y @ G:i', TEXT_DOMAIN ), strtotime( $post->post_date ) ) ),
10 => ( $this->is_male ) ? __( 'Rascunho do ' . mb_strtolower( $labels['singular_name'] ) . ' atualizado.', TEXT_DOMAIN ) : __( 'Rascunho da ' . mb_strtolower( $labels['singular_name'] ) . ' atualizado.', TEXT_DOMAIN ),
);
if ( ( $post_type_object->publicly_queryable ) && ( $post_type == $this->name ) && ( $post_type_object->public ) ) {
$permalink = get_permalink($post->ID);
$view_link = ($this->is_male) ? sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'Ver página do ' . mb_strtolower( $labels['singular_name'] ), TEXT_DOMAIN ) ) : sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'Ver página da ' . mb_strtolower( $labels['singular_name'] ), TEXT_DOMAIN ) );
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = ($this->is_male) ? sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Pré-visualizar página do ' . mb_strtolower( $labels['singular_name'] ), TEXT_DOMAIN ) ) : sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Pré-visualizar página da ' . mb_strtolower( $labels['singular_name'] ), TEXT_DOMAIN ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
return $messages;
}
/**
* Adiciona meta boxes
*/
public function add_meta_boxes() {
}
/**
* Salva meta boxes (na action 'save_post_{post_type}')
*/
public function save_meta_boxes( $post_id ) {
}
}
endif;
@JoseCage

This comment has been minimized.

Copy link

JoseCage commented Jul 21, 2016

So nice..
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment