1
|
|
|
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* Components Library |
4
|
|
|
* |
5
|
|
|
* Load and display a pre-rendered component |
6
|
|
|
*/ |
7
|
|
|
class Jetpack_Components { |
8
|
|
|
/** |
9
|
|
|
* Load and display a pre-rendered component |
10
|
|
|
* |
11
|
|
|
* @since 7.7.0 |
12
|
|
|
* |
13
|
|
|
* @param string $name Component name. |
14
|
|
|
* @param array $props Component properties. |
15
|
|
|
* |
16
|
|
|
* @return string The component markup |
17
|
|
|
*/ |
18
|
|
|
public static function render_component( $name, $props ) { |
19
|
|
|
|
20
|
|
|
$rtl = is_rtl() ? '.rtl' : ''; |
21
|
|
|
wp_enqueue_style( 'jetpack-components', plugins_url( "_inc/blocks/components{$rtl}.css", JETPACK__PLUGIN_FILE ), array( 'wp-components' ), JETPACK__VERSION ); |
22
|
|
|
|
23
|
|
|
ob_start(); |
24
|
|
|
// `include` fails gracefully and throws a warning, but doesn't halt execution. |
25
|
|
|
include JETPACK__PLUGIN_DIR . "_inc/blocks/$name.html"; |
26
|
|
|
$markup = ob_get_clean(); |
27
|
|
|
|
28
|
|
|
foreach ( $props as $key => $value ) { |
29
|
|
|
$markup = str_replace( |
30
|
|
|
"#$key#", |
31
|
|
|
$value, |
32
|
|
|
$markup |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
// Workaround, required to replace strings in `sprintf`-expressions. |
36
|
|
|
// See extensions/i18n-to-php.js for more information. |
37
|
|
|
$markup = str_replace( |
38
|
|
|
"%($key)s", |
39
|
|
|
$value, |
40
|
|
|
$markup |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $markup; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Load and display a pre-rendered component |
49
|
|
|
* |
50
|
|
|
* @since 7.7.0 |
51
|
|
|
* |
52
|
|
|
* @param array $props Component properties. |
53
|
|
|
* |
54
|
|
|
* @return string The component markup |
55
|
|
|
*/ |
56
|
|
|
public static function render_upgrade_nudge( $props ) { |
57
|
|
|
$plan_slug = $props['plan']; |
58
|
|
|
jetpack_require_lib( 'plans' ); |
59
|
|
|
$plan = Jetpack_Plans::get_plan( $plan_slug ); |
60
|
|
|
|
61
|
|
|
if ( ! $plan ) { |
62
|
|
|
return self::render_component( |
63
|
|
|
'upgrade-nudge', |
64
|
|
|
array( |
65
|
|
|
'planName' => __( 'a paid plan', 'jetpack' ), |
66
|
|
|
'upgradeUrl' => '', |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't |
72
|
|
|
// For Jetpack, we thus use the plan slug with the 'jetpack_' prefix removed. |
73
|
|
|
$plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' ) |
74
|
|
|
? substr( $plan_slug, strlen( 'jetpack_' ) ) |
75
|
|
|
: $plan->path_slug; |
76
|
|
|
|
77
|
|
|
$post_id = get_the_ID(); |
78
|
|
|
$post_type = get_post_type(); |
79
|
|
|
|
80
|
|
|
// The editor for CPTs has an `edit/` route fragment prefixed. |
81
|
|
|
$post_type_editor_route_prefix = in_array( $post_type, array( 'page', 'post' ), true ) ? '' : 'edit'; |
82
|
|
|
|
83
|
|
|
if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
84
|
|
|
$site_slug = Jetpack::build_raw_urls( home_url() ); |
85
|
|
|
} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
86
|
|
|
$site_slug = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Post-checkout: redirect back to the editor. |
90
|
|
|
$redirect_to = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
91
|
|
|
? '/' . implode( '/', array_filter( array( $post_type_editor_route_prefix, $post_type, $site_slug, $post_id ) ) ) |
|
|
|
|
92
|
|
|
: add_query_arg( |
93
|
|
|
array( |
94
|
|
|
'action' => 'edit', |
95
|
|
|
'post' => $post_id, |
96
|
|
|
), |
97
|
|
|
admin_url( 'post.php' ) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$upgrade_url = |
101
|
|
|
$plan_path_slug |
102
|
|
|
? add_query_arg( |
103
|
|
|
'redirect_to', |
104
|
|
|
$redirect_to, |
105
|
|
|
"https://wordpress.com/checkout/${site_slug}/${plan_path_slug}" |
106
|
|
|
) : ''; |
107
|
|
|
|
108
|
|
|
return self::render_component( |
109
|
|
|
'upgrade-nudge', |
110
|
|
|
array( |
111
|
|
|
'planName' => $plan->product_name, |
112
|
|
|
'upgradeUrl' => $upgrade_url, |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: