PluginProbe
Team Member Block / trunk
Team Member Block vtrunk
trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.3.0
team-member-block / team-member-block.php

team-member-block.php in Team Member Block trunk, at team-member-block.php

268 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Team Member Block
5 * Plugin URI: https://essential-blocks.com
6 * Description: Present your team members beautifully & gain instant credibility
7 * Version: 1.3.0
8 * Author: WPDeveloper
9 * Author URI: https://wpdeveloper.net
10 * License: GPL-3.0-or-later
11 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
12 * Text Domain: team-member-block
13 * Requires PHP: 7.4
14 * Requires at least: 6.0
15 * Tested up to: 7.0
16 *
17 * @package team-member-block
18 */
19
20 // Exit if accessed directly.
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 /**
26 * Registers all block assets so that they can be enqueued through the block editor
27 * in the corresponding context.
28 *
29 * @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
30 */
31
32 define( 'TEAM_MEMBER_BLOCK_VERSION', "1.3.0" );
33 define( 'TEAM_MEMBER_BLOCK_ADMIN_URL', plugin_dir_url( __FILE__ ) );
34 define( 'TEAM_MEMBER_BLOCK_ADMIN_PATH', dirname( __FILE__ ) );
35
36 require_once __DIR__ . '/includes/font-loader.php';
37 require_once __DIR__ . '/includes/post-meta.php';
38 require_once __DIR__ . '/includes/helpers.php';
39
40 /**
41 * `lib/style-handler` ships as a git submodule. If the plugin is running from a
42 * checkout where the submodule was never initialised the directory is empty, and
43 * an unconditional require_once is a hard fatal on every request. Guard it, and
44 * surface the cause in the admin instead of taking the site down.
45 */
46 $team_member_block_style_handler = __DIR__ . '/lib/style-handler/style-handler.php';
47 if ( file_exists( $team_member_block_style_handler ) ) {
48 require_once $team_member_block_style_handler;
49 } else {
50 team_member_block_flag_missing_file( 'lib/style-handler/style-handler.php' );
51 }
52 unset( $team_member_block_style_handler );
53
54 /**
55 * Records a required file that could not be found and makes sure the admin
56 * notice is hooked exactly once.
57 *
58 * @param string $relative_path Path of the missing file, relative to the plugin root.
59 * @return array Every path recorded so far.
60 */
61 function team_member_block_flag_missing_file( $relative_path = null ) {
62 static $missing = [];
63
64 if ( null !== $relative_path && ! in_array( $relative_path, $missing, true ) ) {
65 $missing[] = $relative_path;
66 if ( ! has_action( 'admin_notices', 'team_member_block_missing_build_notice' ) ) {
67 add_action( 'admin_notices', 'team_member_block_missing_build_notice' );
68 }
69 }
70
71 return $missing;
72 }
73
74 /**
75 * Admin notice shown when a required runtime file is missing.
76 *
77 * Names the actual missing files rather than guessing at a cause: the files
78 * under lib/style-handler/ come from a git submodule and are unrelated to
79 * `npm run build`, which the previous wording incorrectly prescribed.
80 */
81 function team_member_block_missing_build_notice() {
82 if ( ! current_user_can( 'activate_plugins' ) ) {
83 return;
84 }
85 $missing = team_member_block_flag_missing_file();
86 if ( empty( $missing ) ) {
87 return;
88 }
89 echo '<div class="notice notice-error"><p>';
90 printf(
91 /* translators: %s: comma separated list of missing file paths. */
92 esc_html__( 'Team Member Block is missing required files: %s. This copy of the plugin is incomplete, so please reinstall it from an official release. If you are running it from a git checkout, run `npm run ensure-submodules` in the plugin directory.', 'team-member-block' ),
93 '<code>' . implode( '</code>, <code>', array_map( 'esc_html', $missing ) ) . '</code>'
94 );
95 echo '</p></div>';
96 }
97
98 function create_block_team_member_block_init() {
99
100 $script_asset_path = TEAM_MEMBER_BLOCK_ADMIN_PATH . "/dist/index.asset.php";
101 if ( ! file_exists( $script_asset_path ) ) {
102 team_member_block_flag_missing_file( 'dist/index.asset.php' );
103 return;
104 }
105 $script_asset = require $script_asset_path;
106 if ( ! is_array( $script_asset ) || ! isset( $script_asset['dependencies'] ) || ! is_array( $script_asset['dependencies'] ) ) {
107 $script_asset = [
108 'dependencies' => [],
109 'version' => TEAM_MEMBER_BLOCK_VERSION
110 ];
111 }
112 $all_dependencies = array_merge( $script_asset['dependencies'], [
113 'wp-blocks',
114 'wp-i18n',
115 'wp-element',
116 'wp-block-editor',
117 'team-member-block-controls-util',
118 'essential-blocks-eb-animation'
119 ] );
120
121 $index_js = TEAM_MEMBER_BLOCK_ADMIN_URL . 'dist/index.js';
122 wp_register_script(
123 'create-block-team-member-block-editor',
124 $index_js,
125 $all_dependencies,
126 isset( $script_asset['version'] ) ? $script_asset['version'] : TEAM_MEMBER_BLOCK_VERSION,
127 true
128 );
129
130 $load_animation_js = TEAM_MEMBER_BLOCK_ADMIN_URL . 'assets/js/eb-animation-load.js';
131 wp_register_script(
132 'essential-blocks-eb-animation',
133 $load_animation_js,
134 [],
135 TEAM_MEMBER_BLOCK_VERSION,
136 true
137 );
138
139 $animate_css = TEAM_MEMBER_BLOCK_ADMIN_URL . 'assets/css/animate.min.css';
140 wp_register_style(
141 'essential-blocks-animation',
142 $animate_css,
143 [],
144 TEAM_MEMBER_BLOCK_VERSION
145 );
146
147 wp_register_style(
148 'fontawesome-frontend-css',
149 TEAM_MEMBER_BLOCK_ADMIN_URL . 'assets/css/fontawesome/css/all.min.css',
150 [],
151 TEAM_MEMBER_BLOCK_VERSION,
152 "all"
153 );
154
155 wp_register_style(
156 'essential-blocks-hover-css',
157 TEAM_MEMBER_BLOCK_ADMIN_URL . 'assets/css/hover-min.css',
158 [],
159 TEAM_MEMBER_BLOCK_VERSION,
160 "all"
161 );
162
163 $style_css = TEAM_MEMBER_BLOCK_ADMIN_URL . 'dist/style.css';
164 //Frontend & Editor Style
165 wp_register_style(
166 'create-block-team-member-frontend-style',
167 $style_css,
168 [
169 'essential-blocks-hover-css',
170 'fontawesome-frontend-css',
171 'essential-blocks-animation'
172 ],
173 TEAM_MEMBER_BLOCK_VERSION
174 );
175
176 if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'essential-blocks/team-member' ) ) {
177 register_block_type(
178 Team_Member_Helper::get_block_register_path( "team-member-block/team-member-block", TEAM_MEMBER_BLOCK_ADMIN_PATH ),
179 [
180 'editor_script' => 'create-block-team-member-block-editor',
181 'editor_style' => 'create-block-team-member-frontend-style',
182 'render_callback' => function ( $attributes, $content ) {
183 if ( ! is_admin() ) {
184 wp_enqueue_style( 'create-block-team-member-frontend-style' );
185 wp_enqueue_style( 'essential-blocks-animation' );
186 wp_enqueue_script( 'essential-blocks-eb-animation' );
187 }
188 return $content;
189 }
190 ]
191 );
192 }
193 }
194
195 add_action( 'init', 'create_block_team_member_block_init', 99 );
196
197 /**
198 * Absolute URL of the Font Awesome stylesheet this plugin ships (6.5.1).
199 *
200 * @return string
201 */
202 function team_member_block_fontawesome_src() {
203 return TEAM_MEMBER_BLOCK_ADMIN_URL . 'assets/css/fontawesome/css/all.min.css';
204 }
205
206 /**
207 * Makes sure the shared `fontawesome-frontend-css` handle resolves to Font
208 * Awesome 6.5.1 rather than an older copy from another Essential Blocks plugin.
209 *
210 * Several EB plugins register this same handle from their own bundle, and
211 * wp_register_style() is a silent no-op once a handle exists, so the first
212 * plugin to run wins purely on alphabetical load order. When that winner ships
213 * Font Awesome 5 (advanced-heading ships 5.15.2), every icon added in Font
214 * Awesome 6 loses its glyph — `fa-x-twitter` among 749 others — while this
215 * plugin still believes its dependency is satisfied.
216 *
217 * Re-pointing the existing handle keeps a single Font Awesome stylesheet and a
218 * single webfont download, and leaves the other plugin's dependency graph and
219 * queue position untouched. 6.5.1 is a superset of 5.15.2 for every icon class
220 * those plugins emit, and it also declares the Font Awesome 5 family names, so
221 * older markup keeps rendering.
222 */
223 function team_member_block_ensure_fontawesome6() {
224 $handle = 'fontawesome-frontend-css';
225 $ours = team_member_block_fontawesome_src();
226 $styles = wp_styles();
227
228 if ( ! isset( $styles->registered[ $handle ] ) ) {
229 return;
230 }
231
232 $registered = $styles->registered[ $handle ];
233
234 if ( $registered->src === $ours ) {
235 return;
236 }
237
238 // Preserve everything the current owner set up, so re-registering is not
239 // observable to it beyond the file that ends up being served.
240 $deps = $registered->deps;
241 $args = null !== $registered->args ? $registered->args : 'all';
242 $extra = $registered->extra;
243 $enqueued = wp_style_is( $handle, 'enqueued' );
244
245 wp_deregister_style( $handle );
246 wp_register_style( $handle, $ours, $deps, TEAM_MEMBER_BLOCK_VERSION, $args );
247
248 foreach ( $extra as $key => $value ) {
249 wp_style_add_data( $handle, $key, $value );
250 }
251
252 if ( $enqueued ) {
253 wp_enqueue_style( $handle );
254 }
255 }
256
257 /**
258 * Run after every plugin has had its chance to register the handle.
259 *
260 * `init` priority 100 covers the plugins that register at 99 like this one.
261 * The enqueue hooks re-assert it for anything registering later, and the
262 * function is idempotent, so repeating it costs a single string comparison.
263 */
264 add_action( 'init', 'team_member_block_ensure_fontawesome6', 100 );
265 add_action( 'wp_enqueue_scripts', 'team_member_block_ensure_fontawesome6', 0 );
266 add_action( 'admin_enqueue_scripts', 'team_member_block_ensure_fontawesome6', 0 );
267 add_action( 'enqueue_block_assets', 'team_member_block_ensure_fontawesome6', 0 );
268