PluginProbe
oik / 4.16.0
oik v4.16.0
1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.2 1.3 1.3.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.0.1 2.1 2.2 2.3 2.4 2.5 2.5.1 All 71 releases
oik / oik.php

oik.php in oik 4.16.0, at oik.php

992 lines 33.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: oik
4 Plugin URI: https://www.oik-plugins.com/oik-plugins/oik
5 Description: OIK Information Kit - Over 80 lazy smart shortcodes for displaying WordPress content
6 Version: 4.16.0
7 Author: bobbingwide
8 Author URI: https://bobbingwide.com/about-bobbing-wide
9 Text Domain: oik
10 Domain Path: /languages/
11 License: GPLv2 or later
12 License URI: https://www.gnu.org/licenses/gpl-2.0.html
13
14 Copyright 2010-2026 Bobbing Wide (email : herb@bobbingwide.com )
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License version 2,
18 as published by the Free Software Foundation.
19
20 You may NOT assume that you can use any other version of the GPL.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 The license for this software can likely be found here:
28 http://www.gnu.org/licenses/gpl-2.0.html
29
30 */
31
32 /**
33 * Return the oik_version
34 *
35 * @returns string oik base plugin version number - from the WordPress plugin template
36 */
37 function oik_version() {
38 return bw_oik_version();
39 }
40
41 /**
42 * Function to invoke when the file has been loaded
43 *
44 * All of the oik plugins and many of the common functions, include calls to bw_trace(), bw_trace2() or bw_backtrace() so we need to include bwtrace.inc
45 * As of oik v2.3 the "init" action is invoked after most other plugins.
46 * This allows oik-fields and oik-types to define overrides to registered post types and taxonomies.
47 *
48 * In oik 2.4 I tried not calling oik_main_init() for AJAX requests but this was more troublesome than beneficial since
49 * there are many vanilla WordPress functions that can fail if post types are not registered.
50 *
51 * In oik 3.0.3 I increased the priority to 20, to allow oik-types to apply overrides to the Genesis theme framework's definitions.
52 */
53 function oik_plugin_file_loaded() {
54 require_once( "libs/oik_boot.php" );
55 oik_lib_fallback( dirname( __FILE__ ) . '/libs' );
56 add_filter( "oik_query_libs", "oik_query_libs_query_libs" );
57 add_action( "oik_lib_loaded", "oik_oik_lib_loaded" );
58 oik_require_lib( "bwtrace" );
59 oik_require_lib_wrapper( "bobbfunc" );
60 oik_require_lib_wrapper( "class-BW-" );
61 //require_once( "oik-add-shortcodes.php" );
62 oik_require_lib_wrapper( 'oik-shortcodes' );
63 require_once( "includes/bobbcomp.php" );
64
65 if ( defined('DOING_AJAX') && DOING_AJAX ) {
66 oik_require( "includes/oik-ajax.php" );
67 oik_ajax_lazy_init();
68 } else {
69 add_action('wp_enqueue_scripts', 'oik_enqueue_stylesheets', 11);
70 add_action( 'admin_enqueue_scripts', 'add_thickbox' );
71 add_action( 'admin_enqueue_scripts', "oik_admin_enqueue_scripts");
72 }
73 add_action( 'init', 'oik_main_init', 20 );
74 add_action( 'init', 'oik_register_dynamic_blocks', 21 );
75 add_action( 'rest_api_init', 'oik_rest_api_init', 20 );
76
77 add_filter( "attachment_fields_to_edit", "oik_attachment_fields_to_edit", null, 2 );
78 add_filter( "attachment_fields_to_save", "oik_attachment_fields_to_save", null, 2 );
79 add_filter( 'oembed_remote_get_args', 'oik_oembed_remote_get_args', 10, 2 );
80 //remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
81 }
82
83 /**
84 * Implement 'wp_enqueue_scripts' action to enqueue the oik.css and $customCSS stylesheets as required
85 *
86 * oik.css contains styles for oik shortcodes. It is embedded if not specifically excluded.
87 * $customCSS is embedded only if selected on oik options
88 * If you want some of oik.css then copy the contents into custom.css and exclude oik.css
89 *
90 * Note: bwlink.css contains styles for the bobbingwide and oik branding colours
91 * It is now only enqueued by the oik-bob-bing-wide plugin
92 *
93 */
94 function oik_enqueue_stylesheets() {
95 $oikCSS = bw_get_option( 'oikCSS' );
96 // bw_trace2( $oikCSS, "oikCSS" );
97 if ( !$oikCSS ) {
98 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
99 $version = oik_version();
100 } else {
101 $version = false;
102 }
103 $url = plugins_url( "/oik/oik.css" );
104 bw_trace2( $url, "oikCSS URL", false, BW_TRACE_VERBOSE );
105 wp_enqueue_style( 'oikCSS', $url, array(), $version );
106 }
107 $customCSS = bw_get_option( 'customCSS' );
108 if ( !empty( $customCSS) ) {
109 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
110 $timestamp = oik_query_timestamp( get_stylesheet_directory(), $customCSS );
111 } else {
112 $timestamp = false;
113 }
114 if ( $timestamp !== null ) {
115 $customCSSurl = get_stylesheet_directory_uri() . '/' . $customCSS;
116 // bw_trace( $customCSSurl, __FUNCTION__, __LINE__, __FILE__, "customCSSurl");
117 wp_register_style( 'customCSS', $customCSSurl, array(), $timestamp );
118 wp_enqueue_style( 'customCSS' );
119 }
120 }
121 }
122
123 /**
124 * Registers scripts for oik-admin.
125 */
126 function oik_admin_enqueue_scripts() {
127 wp_register_script( 'disable_postbox_save', oik_url( 'admin/oik_disable_postbox_save.js') , 'postbox', '0.0.1', true );
128 //wp_enqueue_script( 'disable_postbox_save' );
129 }
130 /**
131 * Query the timestamp for a file
132 *
133 * We need to wrap filemtime() to avoid 'Warning: filemtime(): stat failed for' messages.
134 *
135 * @param string $path the first part of the path to the file ( not terminated with a '/' )
136 * @param string $file the rest of the path to the file ( not prefixed with a '/' )
137 * @return integer/null the timestamp of the file
138 */
139 function oik_query_timestamp( $path, $file ) {
140 $full_file = $path . '/' . $file;
141 $timestamp = null;
142 if ( file_exists( $full_file ) ) {
143 $timestamp = filemtime( $full_file );
144 if ( $timestamp === false ) {
145 $timestamp = null;
146 }
147 }
148 return( $timestamp );
149 }
150
151 /**
152 * Implement the 'init' action
153 *
154 * start oik and let oik dependent plugins know it's OK to use the oik API
155 */
156 function oik_main_init() {
157 add_action( 'admin_menu', 'oik_admin_menu' );
158 add_action( 'network_admin_menu', "oik_network_admin_menu" );
159 add_action( "network_admin_notices", "oik_network_admin_menu" );
160 add_action( "admin_bar_menu", "oik_admin_bar_menu", 20 );
161 add_action( 'login_head', 'oik_login_head');
162 add_action( 'admin_notices', "oik_admin_notices", 9 );
163 add_action( "oik_add_shortcodes", "bw_oik_add_shortcodes" );
164
165 add_filter( "_sc__help", "oik_oik_sc__help", 10, 2 );
166 $bobbfunc = oik_require_lib_wrapper( "bobbfunc" );
167 if ( $bobbfunc && !is_wp_error( $bobbfunc ) ) {
168 bw_load_plugin_textdomain();
169 /**
170 * Tell plugins that oik has been loaded.
171 *
172 * This allows plugins which are dependent upon oik to start using the oik APIs
173 * It doesn't mean that all the APIs are available.
174 */
175 do_action( 'oik_loaded' );
176 bw_block_theme_extras();
177 } else {
178 bw_trace2( $bobbfunc, "bobbfunc?", false, BW_TRACE_ERROR );
179 }
180 }
181
182 /**
183 * Add extras for block themes.
184 *
185 * If it's a block theme we need to run `oik_add_shortcodes`
186 * to ensure that oik based shortcodes are registered for use throughout
187 * the whole processing of the theme.
188 *
189 * @return void
190 */
191 function bw_block_theme_extras() {
192 $theme =wp_get_theme();
193 $is_block_theme=$theme->is_block_theme();
194 if ( $is_block_theme ) {
195 do_action( "oik_add_shortcodes" );
196 }
197 add_action( 'enqueue_block_assets', 'oik_enqueue_block_assets' );
198 add_action( 'enqueue_block_editor_assets', 'oik_enqueue_block_editor_assets' );
199 }
200
201 /**
202 * Enqueues block assets for both editor and front-end.
203 * @return void
204 */
205 function oik_enqueue_block_assets() {
206 //bw_backtrace();
207 oik_register_genericons();
208 }
209
210 /**
211 * Enqueues block assets for the editor.
212 *
213 * @return void
214 */
215 function oik_enqueue_block_editor_assets() {
216 //bw_backtrace();
217 oik_register_genericons();
218 }
219
220 /**
221 * Registers the genericons style.
222 *
223 * The oik/follow-me block is dependent upon this style for theme=genericons.
224 * We only need to register the genericons style.
225 * It's not necessary to enqueue it until the block is actually used.
226 * This is done automatically due to the dependency of the follow-me block's styles on genericons.
227 * See the block.json file.
228 * @return void
229 */
230 function oik_register_genericons() {
231 if ( !wp_style_is( 'genericons', 'registered' ) ) {
232 wp_register_style( 'genericons', oik_url( 'css/genericons/genericons.css' ), false );
233 }
234 //wp_enqueue_style( 'genericons' );
235 }
236
237 /**
238 * Implement the 'admin_menu' action
239 *
240 * Note: This action comes before 'admin_init' and after '_admin_menu'
241 */
242 function oik_admin_menu() {
243 oik_require_lib_wrapper( "bobbforms" );
244 oik_require_lib_wrapper( "oik-admin" );
245 oik_require_lib_wrapper( "class-bobbcomp" );
246 oik_require_lib_wrapper( "oik-l10n" );
247 oik_require_lib_wrapper( "class-BW-" );
248 oik_require_lib_wrapper( "class-oik-update" );
249 require_once( 'admin/oik-admin.php' );
250 oik_options_add_page();
251 add_action( 'admin_init', 'oik_admin_init' );
252 do_action( 'oik_admin_menu' );
253 add_action('admin_enqueue_scripts', 'oik_enqueue_stylesheets', 11 );
254
255 add_action( "activate_plugin", "oik_load_plugins" );
256 }
257
258 /**
259 * Implement the 'network_admin_menu'/'network_admin_notices' action for multisite
260 *
261 * network_admin_menu is used to determine if plugins need updating
262 * network_admin_notices is used in plugin dependency checking
263 */
264 function oik_network_admin_menu() {
265 static $actioned = null;
266 if ( !$actioned ) {
267 $actioned = current_filter();
268 oik_require_lib_wrapper( "bobbforms" );
269 oik_require_lib_wrapper( "oik-admin" );
270 oik_require_lib_wrapper( "class-bobbcomp" );
271 oik_require_lib_wrapper( "oik-l10n" );
272 oik_require_lib_wrapper( "class-BW-" );
273 oik_require_lib_wrapper( "class-oik-update" );
274 require_once( 'admin/oik-admin.php' );
275 oik_options_add_page();
276 add_action('admin_init', 'oik_admin_init' );
277 do_action( 'oik_admin_menu' );
278 } else {
279 bw_trace2( $actioned, "actioned", false, BW_TRACE_INFO );
280 }
281 }
282
283 /**
284 * Implement 'admin_init' action
285 */
286 function oik_admin_init() {
287 oik_options_init();
288 }
289
290 /**
291 * Implement 'oik_add_shortcodes' action for oik
292 *
293 * Loads oik shortcodes and registers them
294 */
295 function bw_oik_add_shortcodes() {
296 $oik_shortcodes_path = oik_path( 'includes/oik-shortcodes.php');
297 if ( file_exists( $oik_shortcodes_path ) ) {
298 oik_require( "includes/oik-shortcodes.php" );
299 bw_oik_lazy_add_shortcodes();
300 }
301 }
302
303 /**
304 * Add the custom image link using the same method as the Portfolio slideshow plugin which used the method documented here:
305 * @link http://wpengineer.com/2076/add-custom-field-attachment-in-wordpress/
306 *
307 * This is the method that adds fields to the form. Paired with 'attachment_fields_to_save'
308 *
309 * Note: Although this filter is only invoked during image editing
310 * ( in admin processing ) it can't be registered during admin_init / admin_menu
311 * since it's not invoked that way
312 * i.e. Don't move the add_filter() logic for this filter.
313 */
314 function oik_attachment_fields_to_edit( $form_fields, $post) {
315 $form_fields['bw_image_link'] = array(
316 "label" => __( "oik custom image link URL", "oik" ),
317 "input" => "text",
318 "value" => get_post_meta( $post->ID, "_bw_image_link", true )
319 );
320 // This doesn't work since the url uses the [html] field instead of [value]
321 // $form_fields['url']['value'] = get_post_meta( $post->ID, "_oik_nivo_image_link", true );
322 return $form_fields;
323 }
324
325 /**
326 * Save the "oik custom image link URL"
327 *
328 * We save the value even if it's blanked out.
329 * Note: The custom meta field is prefixed with an underscore but the field name is not.
330 * Paired with 'attachment_fields_to_edit'
331 *
332 * See also comments for oik_attachment_fields_to_edit()
333 */
334 function oik_attachment_fields_to_save( $post, $attachment) {
335 $link = bw_array_get( $attachment, "bw_image_link", null ) ;
336 update_post_meta( $post['ID'], '_bw_image_link', $link );
337 return $post;
338 }
339
340 /**
341 * Filters `oembed_remote_get_args to set sslverify=true for local requests.
342 *
343 * Uses a method in the oik_remote class from the class-oik-remote shared library.
344 *
345 * @param array $args Array of arguments for the remote request
346 * @param string $url the target URL
347 * @return array
348 */
349 function oik_oembed_remote_get_args( $args, $url ) {
350 if ( !class_exists( 'oik_remote') ) {
351 oik_require_lib( 'class-oik-remote');
352 }
353 $args = oik_remote::bw_adjust_args( $args, $url );
354 return $args;
355 }
356
357 /**
358 * Implements "admin_bar_menu" action for oik
359 *
360 * We implement this first for the whole site...
361 * @TODO but allow each user to override it in oik-user?
362 *
363 * - This action hook runs after other action hooks to alter the "Howdy," prefix for 'my-account'.
364 * - Most howdy replace plugins look for the string "Howdy," so the code won't work if the string has already been translated.
365 * - We look for the translated version, which includes the user's display name.
366 * - So this logic should work for localized versions.
367 * - However, WordPress keeps changing the code.
368 * - Which has caused it to stop working in both 4.7 and 4.8.
369 * - See WordPress TRACs 37794 and 40342.
370 *
371 * The structure we're changing is the node for 'my-account'. e.g.
372 * `
373 [id] => my-account
374 [parent] => top-secondary
375 [title] => Howdy, <span class="display-name">vsgloik</span><img alt='' onerror='this.src="http://qw/wordpress/wp-content/themes/rngs0721/images/no-avatar.jpg"' src='http://1.gravatar.com/avatar/1c32865f0cfb495334dacb5680181f2d?s=26&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D26&amp;r=G' class='avatar avatar-26 photo' height='26' width='26' />
376 [href] => http://qw/wordpress/wp-admin/profile.php
377 [meta] => Array
378 (
379 [class] => with-avatar
380 [title] => My Account
381 )
382 * `
383 *
384 * @param WP_Admin_Bar $wp_admin_bar - the WP_Admin_Bar object
385 */
386 function oik_admin_bar_menu( &$wp_admin_bar ) {
387 $replace = bw_get_option( "howdy" );
388 if ( $replace ) {
389 $node = $wp_admin_bar->get_node( 'my-account' );
390 $current_user = wp_get_current_user();
391 /* translators: %s current user display name */
392 $howdy = sprintf( __('Howdy, %s'), '<span class="display-name">' . $current_user->display_name . '</span>' );
393 //bw_trace2( $node, "node: $howdy" );
394 $replace = $replace . " " . '<span class="display-name">' . $current_user->display_name . '</span>';
395 if ( $node && $node->title ) {
396 $node->title = str_replace( $howdy, $replace, $node->title );
397 $wp_admin_bar->add_node( $node );
398 }
399 }
400 }
401
402 /**
403 * Implement "login_head" action for oik
404 *
405 * Will use the user defined logo image as the login logo, if required
406 *
407 */
408 function oik_login_head() {
409 oik_require( "shortcodes/oik-logo.php" );
410 oik_lazy_login_head();
411 }
412
413 /**
414 * Implement "oik_query_libs" for oik
415 *
416 * When the oik-lib shared library plugin is active we can register the libraries
417 * that we 'Provide' by responding to this filter.
418 *
419 * The libraries should be defined with their dependencies.
420 * There is no need to define oik_boot nor bwtrace as these are pre-requisite libraries.
421 *
422 * The libraries are NOT loaded at this time, just checked and registered
423 * using oik_lib_check_libs() to build the OIK_lib objects for each library that actually exists.
424 *
425 * @param array $libraries array of OIK_libs
426 * @return array updated array of OIK_libs
427 */
428 function oik_query_libs_query_libs( $libraries ) {
429 $libs = array( "bobbforms" => "bobbfunc"
430 , "oik-admin" => "bobbforms"
431 , "oik-sc-help" => "class-BW-"
432 , "oik-activation" => "oik-depends"
433 , "oik-depends" => null
434 , "oik_plugins" => null
435 , "oik_themes" => null
436 , "bobbfunc" => null
437 , "oik-autoload" => null
438 , "oik-honeypot" => "bobbforms"
439 , "class-oik-remote" => null
440 , "class-oik-update" => "class-oik-remote"
441 , "class-bobbcomp" => null
442 , "class-dependencies-cache" => null
443 , "class-BW-" => null
444 , "oik-l10n" => null
445 , "bw_fields" => null
446 , 'oik-shortcodes' => null
447 );
448 $new_libraries = oik_lib_check_libs( $libraries, $libs, "oik" );
449
450 // @TODO Replace this temporary fiddle of the version of bobbfunc with something more acceptable
451 //$last = end( $new_libraries );
452 //$last->version = "3.0.0";
453
454 //$versions = array( "bobbfunc" => "3.0.0" );
455 //$new_libraries = oik_lib_set_lib_versions( $libraries, $libs, $versions, "oik" );
456 bw_trace2( $new_libraries, "new libraries", true, BW_TRACE_VERBOSE );
457
458 return( $new_libraries );
459 }
460
461 /**
462 * Implement "oik_lib_loaded" for oik
463 *
464 * We might decide that we want to register some more libraries
465 * when a particular one has been loaded.
466 *
467 * Note: Each new library is added to the list of registered libraries
468 * It doesn't replace any entry that's already there.
469 */
470 function oik_oik_lib_loaded( $lib ) {
471 //bw_trace2();
472 //if ( $lib->library == "bwtrace" ) {
473 // oik_register_lib( "bobbfunc", oik_path( "libs/bobbfunc.php" ), null, "3.0.0" );
474 //}
475 }
476
477 /**
478 *
479 */
480 if ( !function_exists( "oik_lib_set_lib_versions" ) ) {
481 function oik_lib_set_lib_versions( $libraries, $libs, $versions, $plugin ) {
482 $lib_args = array();
483 foreach ( $libs as $library => $depends ) {
484 $src = oik_path( "libs/$library.php", $plugin );
485 //if ( file_exists( $src ) ) {
486 $lib_args['library'] = $library;
487 $lib_args['src'] = $src;
488 $lib_args['deps'] = $depends;
489 $lib_args['version'] = bw_array_get( $versions, $library, null );
490 $lib = new OIK_lib( $lib_args );
491 $libraries[] = $lib;
492 }
493 return( $libraries );
494 }
495 }
496
497 /**
498 * Implement "admin_notices" for oik
499 *
500 * Load admin/oik-activation.php before any other plugins which may be dependent upon it.
501 * This saves them from loading their own version of the same code.
502 *
503 * Note: If oik-lib is not loaded then the dependency checking will not be performed
504 * so we need to ensure "oik-depends" is loaded prior to "oik-activation".
505 *
506 *
507 *
508 */
509 function oik_admin_notices() {
510 oik_require_lib_wrapper( "oik-depends" );
511 $loaded = oik_require_lib_wrapper( "oik-activation" );
512 bw_trace2( $loaded, "oik-activation loaded?", false, BW_TRACE_DEBUG );
513
514 }
515
516 /**
517 * Wraps oik_require_lib
518 *
519 * oik_require_lib() has a number of possible outcomes
520 * It can be invoked before "oik_query_libs" has been run.
521 * It may need to use fallback logic to load library files, even when oik-lib is activated.
522 * oik-lib itself also needs to implement fallback logic.
523 *
524 */
525 function oik_require_lib_wrapper( $lib ) {
526 $loaded = oik_require_lib( $lib );
527 bw_trace2( $loaded, "$lib loaded?", false );
528 if ( is_object( $loaded ) ) {
529 if ( is_wp_error( $loaded ) ) {
530 bw_trace2( $loaded, "Library failed to load." );
531 }
532 }
533 return $loaded;
534 }
535
536 /**
537 * Implements "_sc__help" for oik
538 *
539 * @param array $help array of translated help keyed by shortcode
540 * @param string $shortcode
541 * @return array updated help array
542 */
543 function oik_oik_sc__help( $help, $shortcode ) {
544 oik_require( "includes/oik-sc-help.php" );
545 $help = oik_lazy_sc__help( $help, $shortcode );
546 return $help;
547 }
548
549
550 /**
551 * Implements 'rest_api_init'
552 *
553 * Disables the_content and get_the_excerpt processing to save time when the request probably doesn't need it.
554 *
555 * e.g.
556 * `/wp-json/wp/v2/pages?per_page=100&exclude=20770&parent_exclude=20770&orderby=menu_order&order=asc&context=edit&_locale=user`
557 * @TODO Do we need to perform a sanity check against the HTTP_REFERER
558 * `[HTTP_REFERER] => https://s.b/hm/wp-admin/post.php?post=20770&action=edit&classic-editor__forget`
559 */
560 function oik_rest_api_init() {
561
562 $context = bw_array_get( $_REQUEST, 'context', null );
563 bw_trace2( $context, "context", false );
564 $_locale = bw_array_get( $_REQUEST, '_locale', null );
565 bw_trace2( $_locale, '_locale', false );
566 if ( $context === 'edit' || $_locale === 'user') {
567 //bw_trace_attached_hooks( 'the_content');
568 if ( function_exists( 'bw_trace_get_attached_hooks')) {
569 $hooks = bw_trace_get_attached_hooks( 'the_content' );
570 bw_trace2( $hooks, 'the_content', false, BW_TRACE_ALWAYS );
571 $hooks = bw_trace_get_attached_hooks( 'get_the_excerpt' );
572 bw_trace2( $hooks, 'get_the_excerpt', false, BW_TRACE_ALWAYS );
573 }
574 remove_all_filters("the_content");
575 remove_all_filters( 'get_the_excerpt');
576 }
577
578 }
579
580 function oik_is_rest() {
581 $is_rest = defined( 'REST_REQUEST' ) && REST_REQUEST;
582 return $is_rest;
583 }
584
585 function oik_is_block_editor() {
586 $is_block_editor = false;
587 if ( function_exists( "get_current_screen" ) ) {
588 $current_screen = get_current_screen();
589 bw_trace2( $current_screen, "current_screen" );
590 $is_block_editor = $current_screen && $current_screen->is_block_editor();
591 } else {
592 bw_backtrace();
593 bw_trace2("get_current_screen", "function does not exist", null, BW_TRACE_DEBUG);
594 }
595 return $is_block_editor;
596 }
597
598 function oik_is_block_renderer( $renderer=null ) {
599 static $is_block_renderer = false;
600 if ( $renderer !== null ) {
601 $is_block_renderer = $renderer;
602 }
603 return $is_block_renderer;
604 }
605
606
607 /**
608 * Determines if shortcode expansion is necessary
609 *
610 * @TODO This is prototype code. Is it necessary?
611 *
612 * REST API | block renderer | Necessary?
613 * -------- | -------------- | ----------
614 * N | n/a | true
615 * Y | false | false - don't expand shortcodes
616 * Y | true | true - expand shortcodes when the block's being rendered
617
618 *
619 * @return bool
620 */
621
622 function oik_is_shortcode_expansion_necessary() {
623 $shortcode_expansion_necessary = true;
624 if ( oik_is_rest() ) {
625 if ( oik_is_block_renderer() ) {
626 // We need to do this
627 } else {
628 $shortcode_expansion_necessary = false;
629 }
630 }
631 return $shortcode_expansion_necessary;
632 }
633
634 /**
635 * Registers blocks on the server.
636 *
637 * Gives the block editor less to do.
638 */
639 function oik_register_dynamic_blocks() {
640 $library_file = oik_require_lib( 'oik-blocks');
641 add_filter( 'block_type_metadata', 'oik_block_type_metadata', 10 );
642
643 $args = [ 'render_callback' => 'oik_dynamic_block_address' ];
644 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-address', $args );
645 $args = [ 'render_callback' => 'oik_dynamic_block_contact_form' ];
646 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-contact-form', $args );
647 $args = [ 'render_callback' => 'oik_dynamic_block_content_block' ];
648 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-content', $args );
649 $args = [ 'render_callback' => 'oik_dynamic_block_countdown' ];
650 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-countdown', $args );
651 $args = [ 'render_callback' => 'oik_dynamic_block_follow_me' ];
652 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-follow-me', $args );
653 // The Googlemap block is not server side rendered.
654 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-googlemap' );
655 $args = [ 'render_callback' => 'oik_dynamic_block_paypal' ];
656 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-paypal', $args );
657 $args = [ 'render_callback' => 'oik_dynamic_block_shortcode_block' ];
658 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-shortcode', $args );
659 $args = [ 'render_callback' => 'oik_dynamic_block_contact_field' ];
660 $registered = register_block_type_from_metadata( __DIR__ .'/src/oik-contact-field', $args );
661
662
663 /**
664 * Localise the script by loading the required strings for the build/index.js file
665 * from the locale specific .json file in the languages folder.
666 */
667 $ok = wp_set_script_translations( 'oik-address-editor-script', 'oik' , __DIR__ .'/languages' );
668 $ok = wp_set_script_translations( 'oik-contact-form-editor-script', 'oik' , __DIR__ .'/languages' );
669 $ok = wp_set_script_translations( 'oik-content-block-editor-script', 'oik' , __DIR__ .'/languages' );
670 $ok = wp_set_script_translations( 'oik-countdown-editor-script', 'oik' , __DIR__ .'/languages' );
671 $ok = wp_set_script_translations( 'oik-follow-me-editor-script', 'oik' , __DIR__ .'/languages' );
672 $ok = wp_set_script_translations( 'oik-googlemap-editor-script', 'oik' , __DIR__ .'/languages' );
673 $ok = wp_set_script_translations( 'oik-paypal-editor-script', 'oik' , __DIR__ .'/languages' );
674 $ok = wp_set_script_translations( 'oik-shortcode-block-editor-script', 'oik' , __DIR__ .'/languages' );
675 $ok = wp_set_script_translations( 'oik-contact-field-editor-script', 'oik' , __DIR__ .'/languages' );
676
677
678 add_filter( 'load_script_textdomain_relative_path', 'oik_load_script_textdomain_relative_path', 10, 2);
679
680 oik_register_block_styles();
681 }
682
683
684 /**
685 * Filters $relative so that md5's match what's expected.
686 *
687 * Depending on how it was built the `build/index.js` may be preceded by `./` or `src/block-name/../../`.
688 * In either of these situations we want the $relative value to be returned as `build/index.js`.
689 * This then produces the correct md5 value and the .json file is found.
690 *
691 * @param $relative
692 * @param $src
693 *
694 * @return mixed
695 */
696 function oik_load_script_textdomain_relative_path( $relative, $src ) {
697
698 if ( false !== strpos( $src, '/oik/src/' )) {
699 $relative = 'build/' . basename( $relative );
700 }
701 //bw_trace2( $relative, "relative");
702 return $relative;
703 }
704
705 /**
706 * Implements block_type_metadata filter
707 *
708 * - to set the textdomain if not set, for blocks with a prefix of oik.
709 * - to set defaults for the PayPal block
710 * - to set defaults for the Contact Form block
711 *
712
713 *
714 * @param $metadata
715 * @return mixed
716 */
717 function oik_block_type_metadata( $metadata ) {
718 if ( !isset( $metadata['textdomain']) ) {
719 $name = $metadata['name'];
720 $name_parts = explode( '/', $name );
721 $textdomain = $name_parts[0];
722 if ( 'oik' === $textdomain ) {
723 $metadata['textdomain'] = $textdomain;
724 }
725 }
726
727 if ( $metadata['name'] === 'oik/paypal') {
728 $metadata=oik_block_type_metadata_paypal_default_values( $metadata );
729 }
730
731 if ( $metadata['name'] === 'oik/contact-form') {
732 $metadata = oik_block_type_metadata_contact_form_default_values( $metadata );
733 }
734
735 return $metadata;
736 }
737
738 /**
739 * Set default values from oik options.
740 *
741 * If the option values aren't set then default values aren't set either.
742 *
743 * @param $metadata
744 * @return array
745 */
746 function oik_block_type_metadata_paypal_default_values( $metadata ) {
747 //bw_trace2();
748 // We only need to do this when it's the block editor
749 if ( is_admin() ) {
750 $metadata['attributes']['email']['default'] =bw_get_option( 'paypal-email' );
751 $metadata['attributes']['location']['default']=bw_get_option( 'paypal-country' );
752 $metadata['attributes']['currency']['default']=bw_get_option( 'paypal-currency' );
753 }
754 return $metadata;
755 }
756
757 /**
758 * Set default values for Contact Form
759 *
760 * If the option values aren't set then default values aren't set either.
761 *
762 * @param $metadata
763 * @return array
764 */
765 function oik_block_type_metadata_contact_form_default_values( $metadata ) {
766 // We only need to do this when it's the block editor
767 // not the site / template editor.
768 if ( is_admin() ) {
769
770 $id = oik_maybe_get_current_post_author();
771 if ( null === $id ) {
772 $email = bw_get_option_arr( "email");
773 $me = bw_get_me( [] );
774 } else {
775 $email = bw_get_user_option($id, "email");
776 $me = bw_get_user_option( $id, "display_name");
777 }
778 $metadata['attributes']['email']['default'] = $email;
779
780 /* translators: %s: name to contact */
781 $text = sprintf( __( "Contact %s" ), $me );
782 $metadata['attributes']['contact']['default'] = $text;
783 }
784 return $metadata;
785 }
786
787 /**
788 * Returns the ID of the current post's author.
789 *
790 * This is run when the request is:
791 * - to edit a post or page template ( URL post.php?post=post-ID&action=edit )
792 * - to edit a page template
793 *
794 * It's not run:
795 * - in the Site Editor ( URL = site-editor.php?postType=wp_template&postId=theme-name )
796 * - for a new post ( URL = post-new.php ) where it could default to the current user
797 * -
798 * @return int|string|null
799 */
800 function oik_maybe_get_current_post_author() {
801 $id = null;
802 if ( function_exists( 'oiku_loaded')) {
803 $post = bw_array_get($_REQUEST, "post", null);
804 $action = bw_array_get($_REQUEST, "action", null);
805 if ('edit' === $action && $post) {
806 $post = get_post($post);
807 //bw_trace2($post);
808 if ($post) {
809 $id = $post->post_author;
810 }
811 }
812 }
813 return $id;
814 }
815
816 /**
817 * Server rendering dynamic address block.
818 *
819 * @param array $attributes array of block attributes.
820 * @return string generated HTML
821 */
822 function oik_dynamic_block_address( $attributes ) {
823 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-address.php', 'oik', 'bw_address' );
824 if ( ! $html ) {
825 $attributes['tag'] = bw_array_get( $attributes, 'tag', 'div' );
826 $attributes['alt'] = bw_array_get( $attributes, 'alt', '0' );
827 $html = bw_address( $attributes, null, null );
828 }
829 return $html;
830 }
831
832 /**
833 * Server rendering contact-form block.
834 *
835 * @param array $attributes Attributes to the block.
836 * @return string generated HTML
837 */
838 function oik_dynamic_block_contact_form( $attributes, $content, $block ) {
839 bw_trace2();
840 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-contact-form.php', 'oik', 'bw_contact_form_block' );
841 if ( ! $html ) {
842 if ( did_action( "oik_loaded" ) ) {
843 bw_add_shortcode( "bw_contact_field", "bw_contact_field", oik_path( "shortcodes/oik-contact-field.php" ), false );
844
845 $html = bw_contact_form_block( $attributes, $content, $block );
846 } else {
847 $html="The Contact form block requires the oik plugin.";
848 }
849 }
850 return $html;
851 }
852
853 /**
854 * Server rendering contact-field block.
855 *
856 * @param array $attributes Attributes to the block.
857 * @return string generated HTML
858 */
859 function oik_dynamic_block_contact_field( $attributes ) {
860 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-contact-field.php', 'oik', 'bw_contact_field' );
861 if ( ! $html ) {
862 if ( did_action( "oik_loaded" ) ) {
863 bw_add_shortcode( "bw_contact_field", "bw_contact_field", oik_path( "shortcodes/oik-contact-field.php" ), false );
864 //$html = bw_contact_field( $attributes, null, null );
865
866 } else {
867 $html="The Contact field block requires the oik plugin.";
868 }
869 }
870 return $html;
871 }
872
873 /**
874 * Server rendering countdown block.
875 *
876 * This block is a dynamic block but it cannot be Server Side Rendered since it generates inline jQuery code.
877 *
878 * @param array $attributes Attributes to the block.
879 * @return string generated HTML
880 */
881 function oik_dynamic_block_countdown( $attributes ) {
882 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-countdown.php', 'oik', 'bw_countdown' );
883 if ( ! $html ) {
884 $attributes = bw_countdown_attributes( $attributes );
885 $html = bw_countdown( $attributes );
886 }
887 return $html;
888 }
889
890 /**
891 * Server rendering follow-me block.
892 *
893 * @param array $attributes Attributes to the block.
894 * @return string generated HTML
895 */
896 function oik_dynamic_block_follow_me( $attributes ) {
897 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-follow.php', 'oik', 'bw_follow_me' );
898 if ( ! $html ) {
899 bw_trace2();
900 $attributes = oik_follow_me_attributes( $attributes );
901 $html = bw_follow_me( $attributes );
902 }
903 return $html;
904
905 }
906
907 /**
908 * Server side rendering dynamic shortcode block
909 *
910 * Dynamically loads oik_shortcode_block to expand the shortcode
911 *
912 * @param array $attributes
913 * @return string generated HTML
914 */
915 function oik_dynamic_block_shortcode_block( $attributes ) {
916 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-shortcode.php', 'oik', 'oik_shortcode_block' );
917 if ( ! $html ) {
918 $html = oik_shortcode_block( $attributes );
919 }
920 //oik_require( "shortcodes/oik-shortcode.php", "oik-blocks" );
921
922 return $html;
923
924 }
925
926 /**
927 * Server rendering dynamic content block
928 *
929 * @param array $attributes
930 * @return string generated HTML
931 */
932 function oik_dynamic_block_content_block( $attributes ) {
933 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-content.php', 'oik', 'oik_content_block' );
934 if ( ! $html ) {
935 $html = oik_content_block( $attributes );
936 }
937 //oik_require( "shortcodes/oik-shortcode.php", "oik-blocks" );
938 return $html;
939 }
940
941 /**
942 * Server rendering PayPal block.
943 *
944 * @param array $attributes array of block attributes.
945 * @return string generated HTML
946 */
947 function oik_dynamic_block_paypal( $attributes ) {
948 $html = \oik\oik_blocks\oik_blocks_check_server_func( 'shortcodes/oik-paypal.php', 'oik', 'bw_pp_shortcodes' );
949 if ( ! $html ) {
950 $attributes['type'] = bw_array_get( $attributes, 'type', 'donate' );
951 $attributes['amount'] = bw_array_get( $attributes, 'amount', '5.00' );
952 $html = bw_pp_shortcodes( $attributes, null, null );
953 $html = oik_server_side_wrapper( $attributes, $html );
954 }
955 return $html;
956 }
957
958 /**
959 * Implements wrapper for Server Side Rendered blocks.
960 *
961 * @param $attributes
962 * @param $html
963 * @return string
964 */
965 function oik_server_side_wrapper( $attributes, $html ) {
966 $align_class_name=empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
967 $extra_attributes =[ 'class'=>$align_class_name ];
968 $wrapper_attributes = get_block_wrapper_attributes( $extra_attributes );
969 $html=sprintf(
970 '<div %1$s>%2$s</div>',
971 $wrapper_attributes,
972 $html
973 );
974 return $html;
975 }
976
977 /**
978 * Register styles for oik blocks.
979 *
980 */
981
982 function oik_register_block_styles() {
983 register_block_style( 'oik/contact-field', [ 'name' => 'grid', 'label' => __( 'Grid', 'oik'), 'is_default' => true ]);
984 register_block_style( 'oik/contact-field', [ 'name' => 'above', 'label' => __( 'Label above', 'oik') ]);
985 register_block_style( 'oik/contact-field', [ 'name' => 'reversed', 'label' => __( 'Reversed', 'oik')]);
986
987 }
988
989 /**
990 * Initiate oik processing
991 */
992 oik_plugin_file_loaded();