Completed
Push — add/lazy-images ( 1aef1c...4b007f )
by
unknown
08:11
created

Jetpack_PWA_Manifest::render_manifest_json()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 3
nop 0
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_PWA_Manifest {
4
	/**
5
	 * @var Jetpack_PWA_Manifest
6
	 */
7
	private static $__instance = null;
8
9
	/**
10
	 * When this query var is present, display the PWA manifest.
11
	 *
12
	 * @var string
13
	 */
14
	const PWA_MANIFEST_QUERY_VAR = 'jetpack_app_manifest';
15
16
	/**
17
	 * Singleton implementation
18
	 *
19
	 * @return Jetpack_PWA_Manifest
20
	 */
21
	public static function instance() {
22
		if ( is_null( self::$__instance ) ) {
23
			self::$__instance = new Jetpack_PWA_Manifest;
24
		}
25
26
		return self::$__instance;
27
	}
28
29
	/**
30
	 * Registers actions the first time that instance() is called.
31
	 */
32
	private function __construct() {
33
		add_action( 'wp_head', array( $this, 'render_manifest_link' ) );
34
		add_action( 'amp_post_template_head', array( $this, 'render_manifest_link' ) );
35
		add_action( 'template_redirect', array( $this, 'render_manifest_json' ), 2 );
36
	}
37
38
	function render_manifest_link() {
39
		?>
40
			<link rel="manifest" href="<?php echo esc_url_raw( $this->get_manifest_url() ); ?>">
41
			<meta name="theme-color" content="<?php echo esc_attr( Jetpack_PWA_Helpers::get_theme_color() ); ?>">
42
		<?php
43
	}
44
45
	public function get_manifest_url() {
46
		return add_query_arg(
47
			self::PWA_MANIFEST_QUERY_VAR, '1', home_url()
48
		);
49
	}
50
51
	function render_manifest_json() {
52
		// Do not load manifest in multiple locations
53
		if ( is_front_page() && isset( $_GET[ self::PWA_MANIFEST_QUERY_VAR ] ) && $_GET[ self::PWA_MANIFEST_QUERY_VAR ] ) {
54
			@ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55
56
			$theme_color = Jetpack_PWA_Helpers::get_theme_color();
57
58
			$manifest = array(
59
				'name'       => get_bloginfo( 'name' ),
60
				'start_url'  => get_home_url(),
61
				'short_name' => substr( get_bloginfo( 'name' ), 0, 12 ),
62
				'display'    => 'standalone',
63
				'background_color' => $theme_color,
64
				'theme_color'      => $theme_color,
65
			);
66
67
			if ( $description = get_bloginfo( 'description' ) ) {
68
				$manifest['description'] = $description;
69
			}
70
71
			$manifest['icons'] = array_map(
72
				array( $this, 'build_icon_object' ),
73
				Jetpack_PWA_Helpers::get_default_manifest_icon_sizes()
74
			);
75
76
			/**
77
			 * Allow overriding the manifest.
78
			 *
79
			 * @since 5.6.0
80
			 *
81
			 * @param array $manifest
82
			 */
83
			$manifest = apply_filters( 'jetpack_pwa_manifest', $manifest );
84
85
			wp_send_json( $manifest );
86
		}
87
	}
88
89
	function build_icon_object( $size ) {
90
		return array(
91
			'src' => Jetpack_PWA_Helpers::site_icon_url( $size ),
92
			'sizes' => sprintf( '%1$dx%1$d', $size ),
93
		);
94
	}
95
}
96