WordPress Kitchen
v2.0.1.5
Александр Стриха
https://github.com/shtrihstr
Как опыт компании
превратился в
framework
Никто не учится
на чужих ошибках
$step1->анархия()
if(!isset($content_width))
$content_width=584;add_action('after_setup_theme','twentyeleven_s
etup');if(!function_exists('twentyeleven_setup')):function
twentyeleven_setup()
{load_theme_textdomain('twentyeleven',get_template_directory().'/
languages');add_editor_style();require(get_template_directory().'
/inc/theme-options.php');require(get_template_directory().'/inc/
widgets.php');add_theme_support('automatic-feed-
links');register_nav_menu('primary',__('Primary
Menu’,’twentyeleven’));add_theme_support('post-
formats',array('aside','link','gallery','status','quote','image')
);$theme_options=twentyeleven_get_theme_options();if('dark'==
$theme_options['color_scheme'])
$default_background_color='1d1d1d';else
$default_background_color='e2e2e2';add_theme_support('custom-
background',array('default-color'=>
$default_background_color,));add_theme_support('post-
thumbnails');$custom_header_support=array('default-text-
color'=>'000','width'=>apply_filters('twentyeleven_header_image_w
idth',
1000),'height'=>apply_filters('twentyeleven_header_image_height',
288),'flex-height'=>true,'random-default'=>true,'wp-head-
callback'=>'twentyeleven_header_style','admin-head-
callback'=>'twentyeleven_admin_header_style','admin-preview-
callback'=>'twentyeleven_admin_header_image',);add_theme_support(
'custom-header',$custom_header_support);if(! …
$step2->структура()
my-theme/
………………includes/
………………………………post-type.php
………………………………taxonomy.php
………………………………meta-box.php
………………………………hook.php
………………functions.php
………………index.php
$step3->модули()
МОДУЛЬ А
МОДУЛЬ С
МОДУЛЬ Б
МОДУЛЬ Д
МОДУЛЬ Е
МОДУЛЬ Ж
Тема 1 Тема 2
my-theme/
………………modules/
………………………………ajax-load/
………………………………catalog/
………………………………………………init.php
………………………………………………post-type.php
………………………………………………hooks.php
………………………………theme/
………………functions.php
………………index.php
$step4->framework()
Зачем?
Что уже существует?
Требования
• Строгое структурирование кода
• Ускорение разработки
• ООП
• Побуждение писать правильно
WPKit
https://github.com/REDINKno/wpkit
Post Types
use WPKitPostTypePostType;


$pt_product = new PostType( 'product', 'Product' );


$pt_product->set_menu_icon( 'dashicons-products' );


$pt_product->set_supports( ['thumbnail'] );


$pt_product->add_column_thumbnail();
Post Types
use WPKitPostTypePostType;


$pt_product = new PostType( 'product', 'Product' );


$pt_product->set_menu_icon( 'dashicons-products' );


$pt_product->set_supports( ['thumbnail'] );


$pt_product->add_column_thumbnail();
Post Types
$pt_product->add_column( 'Image', function ( $column ) {
global $post;
if ( $id = get_post_thumbnail_id( $post->ID ) ) {
echo wp_get_attachment_image( $id, [74, 60], true );
}
}, $sortable = false, $position = 1 );
$pt_product->add_column_thumbnail();
Post Types
$pt_product->add_column( 'Image', function ( $column ) {
global $post;
if ( $id = get_post_thumbnail_id( $post->ID ) ) {
echo wp_get_attachment_image( $id, [74, 60], true );
}
}, $sortable = false, $position = 1 );
$pt_product->add_column_thumbnail();
Taxonomies
use WPKitTaxonomyTaxonomy;
$tx_brand = new Taxonomy( 'brand', 'Brand' );
$tx_brand->add_post_type( $pt_product );
$tx_brand->add_custom_field( 'url', 'URL', 'Url' );
Taxonomies
use WPKitTaxonomyTaxonomy;
$tx_brand = new Taxonomy( 'brand', 'Brand' );
$tx_brand->add_post_type( $pt_product );
$tx_brand->add_custom_field( 'url', 'URL', 'Url' );
Taxonomies
use WPKitTaxonomyTaxonomy;
$tx_brand = new Taxonomy( 'brand', 'Brand' );
$tx_brand->add_post_type( $pt_product );
$tx_brand->add_custom_field( 'url', 'URL', 'Url' );
Fields
Text Textarea Hidden Email Url
Number Checkbox Color Date
File Image Map
Radio Select Select2
Video WPEditor
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', 'Price', function() {
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', 'Price', function() {
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', 'Price' );
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', ‘Price’, ‘Number’ );
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', 'Price', function() {
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
$mb_attr->add_post_type( $pt_product );
$mb_attr->add_field( 'price', 'Price', function() {
$field = new Number();
$field->set_min( 0 );
return $field;
} );
$mb_attr->add_field( 'image', 'Image', 'Image' );
Post Meta
use WPKitPostTypeMetaBox;
$mb_attr = new MetaBox( 'attributes', 'Attributes' );
use WPKitPostTypeMetaBoxRepeatable;
$mb_attr = new MetaBoxRepeatable( 'attributes',
'Attributes' );
Settings API
use WPKitOptionsOptionBox;
$ob_design = new OptionBox( 'design', 'Design' );
$ob_design->add_field( 'bg', 'Background', 'Color' );
$ob_design->set_page( 'general' );
Settings API
use WPKitOptionsOptionBox;
$ob_design = new OptionBox( 'design', 'Design' );
$ob_design->add_field( 'bg', 'Background', 'Color' );
$ob_design->set_page( 'general' );
Settings API
use WPKitOptionsOptionBox;
$ob_design = new OptionBox( 'design', 'Design' );
$ob_design->add_field( 'bg', 'Background', 'Color' );
$ob_design->set_page( 'general' );
List Table
use WPKitTableTable;
$table = new Table( 'users', 'ID' );
$table->setup_general_column( 'display_name', ‘Name' );
$table->setup_column( 'user_email', 'Email' );
$table->setup_column( 'avatar', 'Avatar',
function( $item, $key ) {
return get_avatar( $item[ 'user_email' ] );
} );
$page = new TablePage( 'clients', 'Clients' );
$page->set_table( $table );
List Table
use WPKitTableTable;
$table = new Table( 'users', 'ID' );
$table->setup_general_column( 'display_name', ‘Name' );
$table->setup_column( 'user_email', 'Email' );
$table->setup_column( 'avatar', 'Avatar',
function( $item, $key ) {
return get_avatar( $item[ 'user_email' ] );
} );
$page = new TablePage( 'clients', 'Clients' );
$page->set_table( $table );
List Table
use WPKitTableTable;
$table = new Table( 'users', 'ID' );
$table->setup_general_column( 'display_name', ‘Name' );
$table->setup_column( 'user_email', 'Email' );
$table->setup_column( 'avatar', 'Avatar',
function( $item, $key ) {
return get_avatar( $item[ 'user_email' ] );
} );
$page = new TablePage( 'clients', 'Clients' );
$page->set_table( $table );
List Table
use WPKitTableTable;
$table = new Table( 'users', 'ID' );
$table->setup_general_column( 'display_name', ‘Name' );
$table->setup_column( 'user_email', 'Email' );
$table->setup_column( 'avatar', 'Avatar',
function( $item, $key ) {
return get_avatar( $item[ 'user_email' ] );
} );
$page = new TablePage( 'clients', 'Clients' );
$page->set_table( $table );
Widgets
use WPKitWidgetsAbstractWidget;
class ImageWidget extends AbstractWidget {
protected function _get_config()
{
return ['id' => 'image', 'name' => 'Image'];
}
}
Widgets
use WPKitWidgetsAbstractWidget;
class ImageWidget extends AbstractWidget {
protected function _get_config()
{
return ['id' => 'image', 'name' => 'Image'];
}
}
Widgets
use WPKitWidgetsAbstractWidget;
class ImageWidget extends AbstractWidget {
protected function _build_fields()
{
$this->_add_field( 'image', 'Image', 'Image' );
}
}
Widgets
use WPKitWidgetsAbstractWidget;
class ImageWidget extends AbstractWidget {
protected function _render( $args, $data )
{
echo wp_get_attachment_image( $data['image'] );
}
}
Modules
my-theme/
………………modules/
………………………………ajax-load/
………………………………catalog/
………………………………………………Initialization.php
………………………………………………Functions.php
………………………………………………Ajax.php
………………………………theme/
………………functions.php
………………index.php
Initialization
namespace modulescatalog;
use WPKitModuleAbstractModuleInitialization;
class Initialization extends AbstractModuleInitialization
{
public function register_***()
{
}
}
Initialization
namespace modulescatalog;
use WPKitModuleAbstractModuleInitialization;
class Initialization extends AbstractModuleInitialization
{
public function register_post_type()
{
$pt_product = new PostType( 'product', 'Product' );
}
}
Initialization
namespace modulescatalog;
use WPKitModuleAbstractModuleInitialization;
class Initialization extends AbstractModuleInitialization
{
public function add_filter_***()
{
}
}
Initialization
namespace modulescatalog;
use WPKitModuleAbstractModuleInitialization;
class Initialization extends AbstractModuleInitialization
{
public function add_filter_the_content( $content )
{
return capital_P_dangit( $content );
}
}
Functions
namespace modulescatalog;
use WPKitModuleAbstractFunctions;
class Functions extends AbstractFunctions
{
public static function get_product_price( $id = null )
{
if( null == $id ) {
$id = get_the_ID();
}
return MetaBox::get( $id, 'attributes', 'price' );
}
}
Functions
namespace modulescatalog;
use WPKitModuleAbstractFunctions;
class Functions extends AbstractFunctions
{
public static function get_product_price( $id = null )
{
if( null == $id ) {
$id = get_the_ID();
}
return MetaBox::get( $id, 'attributes', 'price' );
}
}
Functions
<?php get_header(); ?>
<?php the_post(); ?>
<article class="product">
<h2><?php the_title(); ?></h2>
<p>
Price:
<?php echo Catalog::get_product_price(); ?>
</p>
</article>
<?php get_footer(); ?>
Ресурсы
• WPKit https://github.com/REDINKno/wpkit
• Пример https://github.com/shtrihstr/wpk15-theme-wpkit
• Wiki coming soon…
Спасибо!
Код здорового человека Код курильщика
<?php
namespace modulescatalog;
use WPKitModuleAbstractModuleInitialization;
class Initialization extends AbstractModuleInitialization
{
/**
* @var PostType
*/
protected $_post_type_product = null;
if(!isset($content_width))
$content_width=584;add_action('aft
er_setup_theme','twentyeleven_setu
p');if(!
function_exists('twentyeleven_setu
p')):function twentyeleven_setup()
{load_theme_textdomain('twentyelev
en',get_template_directory().'/
languages');add_editor_style();req
uire(get_template_directory().'/
inc/theme-options.php');

Як досвід компанії перетворився на фреймворк