Skip to content

Commit 971c1e5

Browse files
authored
dev: wrap global functions in function_exists() (#34)
1 parent 208455f commit 971c1e5

File tree

5 files changed

+167
-152
lines changed

5 files changed

+167
-152
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Unreleased
44
- dev: namespace Composer dependencies with Strauss.
55
- chore: update WPGraphQL Plugin Boilerplate dependency to v0.0.8.
6-
6+
- dev: wrap global functions in `function_exists()` checks.
77
## v0.0.9
88
- chore: update WPGraphQL Plugin Boilerplate dependency to v0.0.7.
99

access-functions.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,42 @@
55
* @package WPGraphQL/RankMath
66
*/
77

8-
/**
9-
* Get an option value from the plugin settings.
10-
*
11-
* @param string $option_name The key of the option to return.
12-
* @param mixed $default The default value the setting should return if no value is set.
13-
* @param string $section_name The settings group section that the option belongs to.
14-
*
15-
* @return mixed
16-
*/
17-
function graphql_seo_get_setting( string $option_name, $default = '', $section_name = 'graphql_seo_settings' ) {
18-
$section_fields = get_option( $section_name );
19-
8+
if ( ! function_exists( 'graphql_seo_get_setting' ) ) {
209
/**
21-
* Filter the section fields
10+
* Get an option value from the plugin settings.
11+
*
12+
* @param string $option_name The key of the option to return.
13+
* @param mixed $default The default value the setting should return if no value is set.
14+
* @param string $section_name The settings group section that the option belongs to.
2215
*
23-
* @param array $section_fields The values of the fields stored for the section
24-
* @param string $section_name The name of the section
25-
* @param mixed $default The default value for the option being retrieved
16+
* @return mixed
2617
*/
27-
$section_fields = apply_filters( 'graphql_seo_get_setting_section_fields', $section_fields, $section_name, $default );
18+
function graphql_seo_get_setting( string $option_name, $default = '', $section_name = 'graphql_seo_settings' ) {
19+
$section_fields = get_option( $section_name );
2820

29-
/**
30-
* Get the value from the stored data, or return the default
31-
*/
32-
$value = isset( $section_fields[ $option_name ] ) ? $section_fields[ $option_name ] : $default;
21+
/**
22+
* Filter the section fields
23+
*
24+
* @param array $section_fields The values of the fields stored for the section
25+
* @param string $section_name The name of the section
26+
* @param mixed $default The default value for the option being retrieved
27+
*/
28+
$section_fields = apply_filters( 'graphql_seo_get_setting_section_fields', $section_fields, $section_name, $default );
3329

34-
/**
35-
* Filter the value before returning it
36-
*
37-
* @param mixed $value The value of the field
38-
* @param mixed $default The default value if there is no value set
39-
* @param string $option_name The name of the option
40-
* @param array $section_fields The setting values within the section
41-
* @param string $section_name The name of the section the setting belongs to
42-
*/
43-
return apply_filters( 'graphql_seo_get_setting_section_field_value', $value, $default, $option_name, $section_fields, $section_name );
30+
/**
31+
* Get the value from the stored data, or return the default
32+
*/
33+
$value = isset( $section_fields[ $option_name ] ) ? $section_fields[ $option_name ] : $default;
34+
35+
/**
36+
* Filter the value before returning it
37+
*
38+
* @param mixed $value The value of the field
39+
* @param mixed $default The default value if there is no value set
40+
* @param string $option_name The name of the option
41+
* @param array $section_fields The setting values within the section
42+
* @param string $section_name The name of the section the setting belongs to
43+
*/
44+
return apply_filters( 'graphql_seo_get_setting_section_field_value', $value, $default, $option_name, $section_fields, $section_name );
45+
}
4446
}

activation.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
* @package WPGraphql\RankMath
66
*/
77

8-
/**
9-
* Runs when the plugin is activated.
10-
*/
11-
function graphql_seo_activation_callback() : callable {
12-
return function() : void {
13-
do_action( 'graphql_seo_activate' );
8+
if ( ! function_exists( 'graphql_seo_activation_callback' ) ) {
9+
/**
10+
* Runs when the plugin is activated.
11+
*/
12+
function graphql_seo_activation_callback() : callable {
13+
return function() : void {
14+
do_action( 'graphql_seo_activate' );
1415

15-
// store the current version of the plugin.
16-
update_option( 'wp_graphql_seo_version', WPGRAPHQL_SEO_VERSION );
17-
};
16+
// store the current version of the plugin.
17+
update_option( 'wp_graphql_seo_version', WPGRAPHQL_SEO_VERSION );
18+
};
19+
}
1820
}

deactivation.php

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,55 @@
55
* @package WPGraphql\RankMath
66
*/
77

8-
/**
9-
* Runs when WPGraphQL is de-activated.
10-
*
11-
* This cleans up data that WPGraphQL stores.
12-
*/
13-
function graphql_seo_deactivation_callback() : callable {
14-
return function() : void {
15-
16-
// Fire an action when WPGraphQL is de-activating.
17-
do_action( 'graphql_seo_deactivate' );
18-
19-
// Delete data during activation.
20-
graphql_seo_delete_data();
21-
};
8+
if ( ! function_exists( 'graphql_seo_deactivation_callback' ) ) {
9+
/**
10+
* Runs when WPGraphQL is de-activated.
11+
*
12+
* This cleans up data that WPGraphQL stores.
13+
*/
14+
function graphql_seo_deactivation_callback() : callable {
15+
return function() : void {
16+
17+
// Fire an action when WPGraphQL is de-activating.
18+
do_action( 'graphql_seo_deactivate' );
19+
20+
// Delete data during activation.
21+
graphql_seo_delete_data();
22+
};
23+
}
2224
}
2325

24-
/**
25-
* Delete data on deactivation.
26-
*/
27-
function graphql_seo_delete_data() : void {
26+
if ( ! function_exists( 'graphql_seo_delete_data' ) ) {
27+
/**
28+
* Delete data on deactivation.
29+
*/
30+
function graphql_seo_delete_data() : void {
2831

29-
// Check if the plugin is set to delete data or not.
30-
$delete_data = graphql_seo_get_setting( 'delete_data_on_deactivate' );
32+
// Check if the plugin is set to delete data or not.
33+
$delete_data = graphql_seo_get_setting( 'delete_data_on_deactivate' );
3134

32-
// Bail if not set to delete.
33-
if ( 'on' !== $delete_data ) {
34-
return;
35-
}
35+
// Bail if not set to delete.
36+
if ( 'on' !== $delete_data ) {
37+
return;
38+
}
3639

37-
// Delete plugin version.
38-
delete_option( 'wp_graphql_seo_version' );
40+
// Delete plugin version.
41+
delete_option( 'wp_graphql_seo_version' );
3942

40-
// Initialize the settings API.
41-
$settings = new \WPGraphQL\RankMath\Admin\Settings\Settings();
42-
$settings::register_settings();
43+
// Initialize the settings API.
44+
$settings = new \WPGraphQL\RankMath\Admin\Settings\Settings();
45+
$settings::register_settings();
4346

44-
// Get all the registered settings fields.
45-
$fields = $settings::get_settings_api()->get_settings_fields();
47+
// Get all the registered settings fields.
48+
$fields = $settings::get_settings_api()->get_settings_fields();
4649

47-
// Loop over the registered settings fields and delete the options.
48-
if ( ! empty( $fields ) && is_array( $fields ) ) {
49-
foreach ( $fields as $group => $fields ) {
50-
delete_option( $group );
50+
// Loop over the registered settings fields and delete the options.
51+
if ( ! empty( $fields ) && is_array( $fields ) ) {
52+
foreach ( $fields as $group => $fields ) {
53+
delete_option( $group );
54+
}
5155
}
52-
}
5356

54-
do_action( 'graphql_seo_delete_data' );
57+
do_action( 'graphql_seo_delete_data' );
58+
}
5559
}

wp-graphql-rank-math.php

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -46,87 +46,94 @@
4646
register_activation_hook( __FILE__, 'graphql_seo_deactivation_callback' );
4747
}
4848

49-
50-
/**
51-
* Define plugin constants.
52-
*/
53-
function graphql_seo_constants() : void {
54-
// Plugin version.
55-
if ( ! defined( 'WPGRAPHQL_SEO_VERSION' ) ) {
56-
define( 'WPGRAPHQL_SEO_VERSION', '0.0.9' );
57-
}
58-
59-
// Plugin Folder Path.
60-
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_DIR' ) ) {
61-
define( 'WPGRAPHQL_SEO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
62-
}
63-
64-
// Plugin Folder URL.
65-
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_URL' ) ) {
66-
define( 'WPGRAPHQL_SEO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
67-
}
68-
69-
// Plugin Root File.
70-
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_FILE' ) ) {
71-
define( 'WPGRAPHQL_SEO_PLUGIN_FILE', __FILE__ );
72-
}
73-
74-
// Whether to autoload the files or not.
75-
if ( ! defined( 'WPGRAPHQL_SEO_AUTOLOAD' ) ) {
76-
define( 'WPGRAPHQL_SEO_AUTOLOAD', true );
49+
if ( ! function_exists( 'graphql_seo_constants' ) ) {
50+
/**
51+
* Define plugin constants.
52+
*/
53+
function graphql_seo_constants() : void {
54+
// Plugin version.
55+
if ( ! defined( 'WPGRAPHQL_SEO_VERSION' ) ) {
56+
define( 'WPGRAPHQL_SEO_VERSION', '0.0.9' );
57+
}
58+
59+
// Plugin Folder Path.
60+
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_DIR' ) ) {
61+
define( 'WPGRAPHQL_SEO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
62+
}
63+
64+
// Plugin Folder URL.
65+
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_URL' ) ) {
66+
define( 'WPGRAPHQL_SEO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
67+
}
68+
69+
// Plugin Root File.
70+
if ( ! defined( 'WPGRAPHQL_SEO_PLUGIN_FILE' ) ) {
71+
define( 'WPGRAPHQL_SEO_PLUGIN_FILE', __FILE__ );
72+
}
73+
74+
// Whether to autoload the files or not.
75+
if ( ! defined( 'WPGRAPHQL_SEO_AUTOLOAD' ) ) {
76+
define( 'WPGRAPHQL_SEO_AUTOLOAD', true );
77+
}
7778
}
7879
}
7980

80-
/**
81-
* Checks if all the the required plugins are installed and activated.
82-
*/
83-
function graphql_seo_dependencies_not_ready() : array {
84-
$deps = [];
85-
86-
if ( ! class_exists( '\WPGraphQL' ) ) {
87-
$deps[] = 'WPGraphQL';
81+
if ( ! function_exists( 'graphql_seo_dependencies_not_ready' ) ) {
82+
/**
83+
* Checks if all the the required plugins are installed and activated.
84+
*/
85+
function graphql_seo_dependencies_not_ready() : array {
86+
$deps = [];
87+
88+
if ( ! class_exists( '\WPGraphQL' ) ) {
89+
$deps[] = 'WPGraphQL';
90+
}
91+
if ( ! class_exists( '\RankMath' ) ) {
92+
$deps[] = 'RankMath SEO';
93+
}
94+
95+
return $deps;
8896
}
89-
if ( ! class_exists( '\RankMath' ) ) {
90-
$deps[] = 'RankMath SEO';
91-
}
92-
93-
return $deps;
9497
}
9598

96-
/**
97-
* Initializes plugin.
98-
*/
99-
function graphql_seo_init() : void {
100-
graphql_seo_constants();
101-
102-
$not_ready = graphql_seo_dependencies_not_ready();
103-
104-
if ( empty( $not_ready ) && defined( 'WPGRAPHQL_SEO_PLUGIN_DIR' ) ) {
105-
require_once WPGRAPHQL_SEO_PLUGIN_DIR . 'src/Main.php';
106-
\WPGraphQL\RankMath\Main::instance();
107-
return;
108-
}
109-
110-
foreach ( $not_ready as $dep ) {
111-
add_action(
112-
'admin_notices',
113-
function() use ( $dep ) {
114-
?>
115-
<div class="error notice">
116-
<p>
117-
<?php
118-
printf(
119-
/* translators: dependency not ready error message */
120-
esc_html__( '%1$s must be active for WPGraphQL for Rank Math to work.', 'wp-graphql-rank-math' ),
121-
esc_html( $dep )
122-
);
123-
?>
124-
</p>
125-
</div>
126-
<?php
127-
}
128-
);
99+
if ( ! function_exists( 'graphql_seo_init' ) ) {
100+
101+
/**
102+
* Initializes plugin.
103+
*/
104+
function graphql_seo_init() : void {
105+
graphql_seo_constants();
106+
107+
$not_ready = graphql_seo_dependencies_not_ready();
108+
109+
if ( empty( $not_ready ) && defined( 'WPGRAPHQL_SEO_PLUGIN_DIR' ) ) {
110+
require_once WPGRAPHQL_SEO_PLUGIN_DIR . 'src/Main.php';
111+
\WPGraphQL\RankMath\Main::instance();
112+
return;
113+
}
114+
115+
foreach ( $not_ready as $dep ) {
116+
add_action(
117+
'admin_notices',
118+
function() use ( $dep ) {
119+
?>
120+
<div class="error notice">
121+
<p>
122+
<?php
123+
printf(
124+
/* translators: dependency not ready error message */
125+
esc_html__( '%1$s must be active for WPGraphQL for Rank Math to work.', 'wp-graphql-rank-math' ),
126+
esc_html( $dep )
127+
);
128+
?>
129+
</p>
130+
</div>
131+
<?php
132+
}
133+
);
134+
}
129135
}
130136
}
131137

138+
// Initialize the plugin.
132139
add_action( 'graphql_init', 'graphql_seo_init' );

0 commit comments

Comments
 (0)