-
Notifications
You must be signed in to change notification settings - Fork 6
/
partytown.php
138 lines (123 loc) · 3.57 KB
/
partytown.php
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
<?php
/**
* WP PartyTown
*
* @package partytown
* @subpackage Performance
* @license GPL v2 or later
*
* @wordpress-plugin
* Plugin Name: WP PartyTown
* Plugin URI: https://github.com/rtCamp/wp-partytown
* Description: Add partytown support to your WordPress site.
* Version: 1.0.0
* Requires at least: 5.1
* Requires PHP: 5.2
* Author: rtCamp, thelovekesh
* Author URI: https://rtcamp.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://github.com/rtCamp/wp-partytown
* Text Domain: partytown
* Domain Path: /languages
*/
namespace Partytown;
/**
* PartyTown Configuration
*
* @since 1.0.0
* @see https://partytown.builder.io/configuration
* @return void
*/
function partytown_configuration() {
// If partytown is not enabled, return early.
if ( ! get_option( 'partytown', 'on' ) ) {
return;
}
$config = array(
'lib' => str_replace( site_url(), '', plugin_dir_url( __FILE__ ) ) . 'includes/js/partytown/',
);
/**
* Add configuration for PartyTown.
*
* @since 1.0.0
* @param array $config Configuration for PartyTown.
* @return array
*/
$config = apply_filters( 'partytown_configuration', $config );
?>
<script>
window.partytown = <?php echo wp_json_encode( $config ); ?>;
</script>
<?php
}
add_action( 'wp_head', __NAMESPACE__ . '\partytown_configuration', 1 );
/**
* Initialize PartyTown
*
* @since 1.0.0
* @return void
*/
function partytown_init() {
// If user has enabled PartyTown, only then load the script.
if ( get_option( 'partytown', 'on' ) ) {
wp_enqueue_script(
'partytown',
plugin_dir_url( __FILE__ ) . 'includes/js/partytown/partytown.js',
array(),
'1.0.0',
false
);
}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\partytown_init', 1 );
/**
* Get all scripts tags which has `partytown` dependency.
*
* @since 1.0.0
* @return void
*/
function partytown_worker_scripts() {
global $wp_scripts;
$partytown_handles = array();
// Get all scripts which has `partytown` dependency.
foreach ( $wp_scripts->registered as $handle => $script ) {
if ( ! empty( $script->deps ) && in_array( 'partytown', $script->deps ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$partytown_handles[] = $handle;
}
}
foreach ( $partytown_handles as $partytown_handle ) {
add_filter(
'script_loader_tag',
/**
* Add type="text/partytown" to script tag.
*
* @since 1.0.0
* @param string $tag Script tag.
* @param string $handle Script handle.
* @param string $src Script source.
* @param string $partytown_handle Script handle which have `partytown` dependency.
*
* @return string $tag Script tag with type="text/partytown".
*/
function( $tag, $handle, $src ) use ( $partytown_handle ) {
if ( $handle === $partytown_handle ) {
if ( strpos( $tag, 'type="text/javascript"' ) !== false ) {
$tag = str_replace( 'type="text/javascript"', 'type="text/partytown"', $tag );
} else {
$tag = str_replace( '<script', '<script type="text/partytown"', $tag );
}
}
return $tag;
},
10,
3
);
}
}
add_action( 'wp_print_scripts', __NAMESPACE__ . '\partytown_worker_scripts' );
require_once plugin_dir_path( __FILE__ ) . 'includes/php/options-partytown.php';
// require examples.
// require_once plugin_dir_path( __FILE__ ) . 'examples/counter.php';
// require_once plugin_dir_path( __FILE__ ) . 'examples/get-request.php';
// require_once plugin_dir_path( __FILE__ ) . 'examples/post-request.php';