Jake Vanderwerf
2 hours ago a17c578433ef543e220697813a9367a260df83e1
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
 
use JVBase\managers\Cache;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
 
function ajv_prerender_core_site_logo(array $block, ?string $content):?string
{
    $open = $close = '';
 
    if (!is_home() && !is_front_page()) {
        $open = '<a href="'.get_home_url().'" rel="home">';
        $close = '</a>';
    }
    $icons = (array_key_exists('className', $block['attrs']) && str_contains($block['attrs']['className'], 'is-text')) ? jvbIcon('logo-jakevan') : jvbIcon('logo').jvbIcon('jakevan');
    return $open.$icons.$close;
}
 
function ajv_prerender_core_cover(array $block, ?string $content, ?WP_Block $parent):?string
{
    $types = array_map(function($type) { return jvbCheckBase($type); }, Registrar::getRegistered('post'));
    $taxTypes = array_map(function($type) { return jvbCheckBase($type); },  Registrar::getRegistered('term'));
 
    $type = null;
    if (is_post_type_archive($types)) {
        $obj = get_queried_object();
        $type = jvbNoBase($obj->name);
 
    } else if (is_tax($taxTypes)) {
        $obj = get_queried_object();
        $type = jvbNoBase($obj->taxonomy);
    }
    if (is_singular($types)) {
        if (!$type) {
            $type = jvbNoBase(get_queried_object()->post_type);
        }
        $registrar = Registrar::getInstance($type);
 
        if (!$registrar) {
            return JVB()->blocks()->prerender_core_cover($block, null, null);
        }
 
        $ID = get_the_ID();
        $imgID = get_post_thumbnail_id($ID);
        $img = '';
        if ($imgID && $imgID > 0) {
            $img = str_replace('<img', '<img style="object-fit:center"',jvbFormatImage($imgID, 'tiny', 'large', false)) ;
        }
 
        $singular = $registrar->getSingular();
        $header = match($type) {
            'art'       => 'Canadian made Art.<br>Made in Edmonton.',
            'design'    => 'Canadian Design in Edmonton.',
            'development'=> 'Canadian Development in Edmonton.',
            'strategy'  => 'Canadian Brand Strategy based in Edmonton, Alberta.',
            'writing'   => 'Canadian Content Writing.<br>From Edmonton, Alberta.',
            'project'   => 'Canadian-based Projects.'
        };
 
        $title = get_the_title();
 
        $title = '<small>'.$header.'</small>'.$title;
 
 
        $date = '<small><time datetime="'.get_the_date('c').'" itemprop="datePublished">'.get_the_date().'</time></small>';
 
 
        return sprintf(
            '<section class="btm right align-full cover row">
        %s
        <div class="content">
        <h1>%s</h1>
        %s
        </div>
        </section>',
            $img,
            $title,
            $date
        );
    }
    if (!$type) {
        return JVB()->blocks()->prerender_core_cover($block, null, null);
    }
    $cache = Cache::for('jakevanIcons');
 
    $icon = $cache->remember(
        $type,
        function () use ($type) {
            return file_get_contents(AJV_DIR.'/assets/icons/jakevan-does-'.$type.'.svg');
        }
    );
 
    return sprintf(
        '<section class="bottom left align-full cover alt %s row">%s</section>',
        $type,
        $icon
    );
}
 
function ajv_get_limited_posts(string $type, int $limit):array {
    $posts = new WP_Query([
        'post_type'         => jvbCheckBase($type),
        'posts_per_page'    => $limit,
        'post_status'       => 'publish',
        'fields'            => 'ids',
    ]);
    return array_map(function($item) {
        return jvbFormatImage(get_post_thumbnail_id($item), 'tiny', 'large', false);
    },$posts->posts);
}
 
function ajv_get_limited_post_images(array $args, bool $includeImages = false, int $limit = 3):array {
    $posts = new WP_Query([
        'posts_per_page'    => $limit,
        'post_status'       => 'publish',
        'fields'            => 'ids',
        ... $args
    ]);
    return array_map(function($item) use ($includeImages) {
        $aOpen = $includeImages ? sprintf('<a href="%s" class="%s">', get_the_permalink($item), jvbNoBase(get_post_type($item))) : '';
        $aClose = $includeImages ? '</a>' : '';
 
        return $aOpen.jvbFormatImage(get_post_thumbnail_id($item), 'tiny', 'large', false).$aClose;
    },$posts->posts);
}
 
function ajv_render_art_cover():string
{
    $get = 4;
    $images = ajv_get_limited_posts('art', $get);
    $out = '';
 
    $halftone = '<div class="halftone"></div>';
    for ($i=0; $i<$get; $i++) {
        $img = (array_key_exists($i, $images)) ? $images[$i] : '';
//        if ($i%4===2) {
//            $before = $halftone;
//            $after = '';
//        } elseif($i%4 === 0) {
//            $before = '';
//            $after = $halftone;
//        }
        $img = ($i%2===0) ? '<div class="img halftone">'.$img.'</div>' : $img;
        $class = ($i%2===0) ? ' class="has-img"' :'';
        $out .= '<div'.$class.'>'.$img. '</div>';
    }
 
    return $out;
}
function ajv_render_strategy_cover():string
{
    $get = 7;
    $images = ajv_get_limited_posts('strategy', $get);
    $out = '';
    for ($i=0; $i<$get; $i++) {
        $img = (array_key_exists($i, $images)) ? $images[$i] : '';
        $out .= '<div>'.$img. '</div>';
    }
    return $out;
}
function ajv_render_development_cover():string
{
    $get = 2;
    $images = ajv_get_limited_posts('development', $get);
    $out = '';
    for ($i=0; $i<$get; $i++) {
        $img = (array_key_exists($i, $images)) ? $images[$i] : '';
        $out .= '<div><div>'.$img. '</div></div>';
    }
    $get = 3;
    for ($i=0; $i<$get; $i++) {
        $out .= '<div class="radial"></div>';
    }
    return $out;
}
function ajv_render_design_cover():string
{
    $get = 4;
    $images = ajv_get_limited_posts('design', $get);
    $out = '';
    for ($i=0; $i<$get; $i++) {
       $out.= '<div><div>';
        $out .= (array_key_exists($i, $images)) ? $images[$i] : '';
        $out .= '</div></div>';
    }
    return $out;
}
function ajv_render_support_cover():string
{
    return '';
}
function ajv_render_writing_cover():string
{
    $get = 7;
    $images = ajv_get_limited_posts('writing', $get);
    $out = '';
    for ($i=0; $i<$get; $i++) {
        $img = (array_key_exists($i, $images)) ? $images[$i] : '';
        $out .= '<div>'.$img. '</div>';
    }
    return $out;
}
 
function ajv_prerender_core_post_title(array $block, ?string $content):string
{
    if (!ajv_is_singular()){
        return JVB()->blocks()->prerender_core_post_title($block, $content, null);
    }
    $ID = get_the_ID();
    $city = get_the_terms($ID, BASE.'city');
    if ($city && !is_wp_error($city)) {
        $city = $city[0]->name;
    } else {
        $city ='Edmonton';
    }
    $registrar = Registrar::getInstance(get_post_type($ID));
    if (!$registrar) {
        return JVB()->blocks()->prerender_core_post_title($block, $content, null);
    }
 
    $form = get_the_terms($ID, BASE.'form');
    if ($form && !is_wp_error($form)){
        $form = $form[0]->name.' ';
    } else {
        $form = '';
    }
 
    $title = get_the_title($ID);
 
    return sprintf(
        '<h1><small>Canadian made %s%s. Made in %s.</small>%s</h1>',
        $form,
        $registrar->getPlural(),
        $city,
        $title
    );
 
}