-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
199 lines (192 loc) · 7.01 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Caff;
/*
* This example demonstrates the logic you would add to your theme's functions file to create a CaffyBlocks implementation
*/
class CaffyBlocks_Admin_Example {
/*
* This function would be the method to setup your CaffyBlocks implementation. I use the `wp_loaded` action to call
* the functionality.
*/
public static function setup() {
// only process if the CaffyBlocks plugin is present
if ( ! class_exists( 'Caff\CaffyBlocks' ) ) {
return;
}
// create the caffyblocks foundation
$foundation = \Caff\CaffyBlocks::GetInstance()->build( 'CaffyBlocks', 'CaffyBlocks' );
// create our first building, which will represent the homepage
$foundation->add_building( 'Homepage', 'homepage' )
->add_room( 'Header', 'header', array(
'container' => array(
'room-footer' => sprintf( '<p class="text-center"><a href="%s">%s</a></p>', esc_url( admin_url( 'nav-menus.php' ) ), esc_html( 'Manage Navigation' ) )
) ) )
->add_accessory( 'rss_feed', array(
'title' => 'RSS Feed:',
'display_callback' => 'Caff\CaffyBlocks\Admin\display_text_box',
'default_value' => ''
) )->room
->add_accessory( 'header-nav-menus', array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_links',
'type' => 'static',
'data' => array(
'links' => array(
'header-nav-menu' => array(
'label' => 'Manage Header Nav Menu',
'type' => 'nav_menu',
'nav_menu_location_id' => 'header'
)
)
)
) )->room->building
->add_room( 'Carousel', 'carousel' )
->add_accessory( 'carousel-items', array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_psu',
'sanitize_callbacks' => array( 'Caff\CaffyBlocks\Admin\sanitize_psu' ),
'data' => array(
'limit' => 1,
'multiple' => true,
'post_types' => array( 'post', 'page' ),
),
'delete_cache_on' => array()
) )->room->building
->add_dynamic_rooms( 'temporary-rooms', array(
'options' => array(
'ad-row' => array(
'label' => 'Ad',
'controls' => array(
'ad-room' => array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_ad'
)
)
)
)
) )->building
->add_room( 'Footer', 'footer' )
->add_accessory( 'footer-nav-menus', array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_links',
'type' => 'static',
'data' => array(
'links' => array(
'footer-nav-menu' => array(
'label' => 'Manage Footer Nav Menu',
'type' => 'nav_menu',
'nav_menu_location_id' => 'footer'
),
'sidebar' => array(
'label' => 'Sidebar',
'link' => admin_url( 'widgets.php' )
)
)
)
) );
// creating a caffyblocks page for the post types
foreach( array( 'post', 'page' ) as $post_type_slug ) {
// skip the following post types
if ( in_array( $post_type_slug, array( 'side-story' ) ) ) {
continue;
}
$post_type = get_post_type_object( $post_type_slug );
$foundation->add_building( $post_type->labels->name, $post_type_slug )
->add_room( 'Header', 'header', array(
'container' => array(
'room-footer' => sprintf( '<p class="text-center"><a href="%s">%s</a></p>', esc_url( admin_url( 'nav-menus.php' ) ), esc_html( 'Manage Navigation' ) )
) ) )
->add_accessory( 'header-nav-menus', array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_links',
'type' => 'static',
'data' => array(
'links' => array(
'header-nav-menu' => array(
'label' => 'Header Nav Menu',
'type' => 'nav_menu',
'nav_menu_location_id' => 'header'
)
)
)
) )->room->building
->add_room( 'Loop', 'loop' )
->add_accessory( 'custom_template', array(
'title' => 'Use Template',
'display_callback' => 'Caff\CaffyBlocks\Admin\display_dropdown',
'data' => array(
'options' => array(
'1-column' => '1 Column',
'2-columns' => '2 Columns',
)
),
'default_value' => '2-columns'
) )->room->building
->add_room( 'Footer', 'footer' )
->add_accessory( 'footer-nav-menus', array(
'display_callback' => 'Caff\CaffyBlocks\Admin\display_links',
'type' => 'static',
'data' => array(
'links' => array(
'footer-nav-menu' => array(
'label' => 'Manage Footer Nav Menu',
'type' => 'nav_menu',
'nav_menu_location_id' => 'footer'
),
'sidebar' => array(
'label' => 'Sidebar',
'link' => admin_url( 'widgets.php' )
)
)
)
)
);
}
}
}
/**
* Provide some examples of using CaffyBlocks in your templates
*/
class CaffyBlocks_Usage_Example {
function display() {
// Example 1
// example to get cached caffyblocks object
$homepage_header_rss_feed = \Caff\CaffyBlocks\Caching::get_object( 'homepage', 'header', 'rss_feed' );
// Example 2
// example of an output cached caffyblocks object, if the object is already is cached it is output instead of continuing with the logic
if ( ! empty( \Caff\CaffyBlocks\Caching::start_caching( 'homepage', 'carousel', 'carousel-items' ) ) ) :
// example of how to retrieve an uncached caffyblocks object
$carousel_items = \Caff\CaffyBlocks::GetInstance()->get_accessory( 'carousel-items', 'carousel', 'homepage' )->get_value();
?>
<div class="carousel">
<?php if ( ! empty( $carousel_items ) ) : ?>
<div class="carousel-items">
<?php foreach( $carousel_items as $carousel_item ) :
if ( empty( $carousel_item['post_ids'] ) ){
$carousel_item_post_type = empty( $carousel_item['post_type'] ) ? 'post' : $carousel_item['post_type'];
$args = array(
'numberposts' => 1,
'post_type' => $carousel_item_post_type,
'suppress_filter' => false,
);
$posts = get_posts( $args );
if ( ! empty( $posts ) ) {
$post = (object) array_shift( $posts );
$carousel_item_post_id = $post->ID;
} else {
continue;
}
} else {
$carousel_item_post_id = array_shift( $carousel_item['post_ids'] );
$post = get_post( $carousel_item_post_id );
$carousel_item_post_type = get_post_type( $post );
}
setup_postdata( $post );
// output some cool template
endforeach; wp_reset_postdata(); ?>
</div><!-- .carousel-items -->
<?php endif; ?>
</div><!-- .carousel -->
<?php
Curation_Caching::end_caching( 'homepage', 'carousel', 'carousel-items' );
endif;
// Example 3
// Get non-cached object
$carousel_items = \Caff\CaffyBlocks::GetInstance()->get_accessory( 'custom_template', 'loop', 'post' )->get_value();
}
}