eth-escape-headspace.php 5.6 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1
2
3
4
5
6
<?php
/*
Plugin Name: ETH Escape HeadSpace2
Plugin URI: https://ethitter.com/plugins/
Description: Output existing HeadSpace2 data without the original plugin. Allows HeadSpace2 (no longer maintained) to be deactivated without impactacting legacy content.
Author: Erick Hitter
Erick Hitter's avatar
Erick Hitter committed
7
Version: 0.2
Erick Hitter's avatar
Erick Hitter committed
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
Author URI: https://ethitter.com/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class ETH_Escape_HeadSpace2 {
	/**
	 * Singleton
	 */
	private static $instance = null;

	/**
	 * Instantiate singleton
	 */
	public static function get_instance() {
		if ( ! is_a( self::$instance, __CLASS__ ) ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	/**
43
44
	 * Class properties
	 */
45
	private $hs_string_keys = array(
46
47
		'_headspace_description',
		'_headspace_metakey',
48
49
50
51
52
53
		'_headspace_raw',
	);

	private $hs_array_keys = array(
		'_headspace_scripts',
		'_headspace_stylesheets',
54
55
56
	);

	private $hs_robots_keys = array(
57
		'_headspace_noindex',
58
59
60
61
		'_headspace_nofollow',
		'_headspace_noarchive',
		'_headspace_noodp',
		'_headspace_noydir',
62
63
	);

64
65
66
67
68
	private $hs_keys_to_meta_names = array(
		'_headspace_description' => 'description',
		'_headspace_metakey'     => 'keywords',
	);

69
70
	/**
	 * Register plugin's hooks
Erick Hitter's avatar
Erick Hitter committed
71
72
73
74
	 *
	 * @return null
	 */
	private function __construct() {
Erick Hitter's avatar
Erick Hitter committed
75
		add_filter( 'pre_get_document_title', array( $this, 'filter_pre_get_document_title' ) );
Erick Hitter's avatar
Erick Hitter committed
76
77
		add_filter( 'wp_title', array( $this, 'filter_wp_title' ), 10, 3 );

78
		add_action( 'wp_head', array( $this, 'action_wp_head' ) );
Erick Hitter's avatar
Erick Hitter committed
79
		add_action( 'wp_footer', array( $this, 'action_wp_footer' ) );
80
81
	}

Erick Hitter's avatar
Erick Hitter committed
82
	/**
Erick Hitter's avatar
Erick Hitter committed
83
84
	 * Filter page titles in WP 4.1+ themes
	 * add_theme_support( 'title-tag' )
Erick Hitter's avatar
Erick Hitter committed
85
86
87
88
89
90
91
92
93
94
95
96
97
	 */
	public function filter_pre_get_document_title( $title ) {
		$_title = get_post_meta( get_the_ID(), '_headspace_page_title', true );

		if ( ! empty( $_title ) ) {
			$title = esc_html( $_title );
		}

		unset( $_title );

		return $title;
	}

Erick Hitter's avatar
Erick Hitter committed
98
	/**
Erick Hitter's avatar
Erick Hitter committed
99
100
	 * Filter page titles in themes designed for < WP 4.1
	 * wp_title()
Erick Hitter's avatar
Erick Hitter committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	 */
	public function filter_wp_title( $title, $sep, $loc ) {
		$_title = get_post_meta( get_the_ID(), '_headspace_page_title', true );

		if ( ! empty( $_title ) ) {
			$_title = esc_html( $_title );

			if ( 'right' == $loc ) {
				$title = $_title . ' ' . $sep . ' ';
			} else {
				$title = ' ' . $sep . ' ' . $_title;
			}
		}

		unset( $_title );

		return $title;
	}

120
	/**
Erick Hitter's avatar
Erick Hitter committed
121
	 * Add <head> meta tags
122
123
124
125
126
127
128
129
130
131
	 */
	public function action_wp_head() {
		// Applies only to individual post objects
		if ( ! is_singular() ) {
			return;
		}

		// Check for HS data
		$hs_data = array();

132
		// Keys that only exist once per post
133
		foreach ( array_merge( $this->hs_string_keys, $this->hs_robots_keys ) as $hs_key ) {
134
135
136
137
138
139
140
			$value = get_post_meta( get_the_ID(), $hs_key, true );

			if ( ! empty( $value ) ) {
				$hs_data[ $hs_key ] = $value;
			}
		}

141
142
143
144
145
146
147
148
149
		// Keys that can exist multiple times per post
		foreach ( $this->hs_array_keys as $hs_key ) {
			$values = get_post_meta( get_the_ID(), $hs_key, false );

			if ( ! empty( $values ) ) {
				$hs_data[ $hs_key ] = $values;
			}
		}

150
151
152
153
154
		// Bail if no HS data exists for this post
		if ( empty( $hs_data ) ) {
			return;
		}

155
		// Handle basic, string-containing keys
156
157
		$output = array();

158
159
		foreach ( $hs_data as $hs_key => $hs_value ) {
			switch( $hs_key ) {
160
161
				case '_headspace_description' :
				case '_headspace_metakey' :
162
163
164
165
166
167
168
169
170
171
172
173
174
					$output[] = '<meta name="' . esc_attr( $this->hs_keys_to_meta_names[ $hs_key ] ) . '" content="' . esc_attr( $hs_value ) . '" />';
					break;

				case '_headspace_scripts' :
					foreach ( $hs_value as $_source ) {
						$output[] = '<script type="text/javascript" src="' . esc_url( $_source ) . '"></script>';
					}
					break;

				case '_headspace_stylesheets' :
					foreach ( $hs_value as $_source ) {
						$output[] = '<link rel="stylesheet" href="' . esc_url( $_source ) . '" type="text/css" />';
					}
175
176
					break;

177
178
179
180
181
				default :
					continue;
					break;
			}
		}
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

		// Handle robots key, which is build from several meta keys
		$robots = array();

		foreach ( $this->hs_robots_keys as $hs_robot_key ) {
			if ( isset( $hs_data[ $hs_robot_key] ) ) {
				$robots[] = str_replace( '_headspace_', '', $hs_robot_key );
			}
		}

		if ( ! empty( $robots ) ) {
			if ( 1 === count( $robots ) && in_array( 'noindex', $robots ) ) {
				$robots[] = 'follow';
			}

			$robots = implode( ',', $robots );

199
200
201
202
			$output[] = '<meta name="robots" content="' . esc_attr( $robots ) . '" />' . "\n";
		}

		// Raw output should follow all other output
Erick Hitter's avatar
Erick Hitter committed
203
		if ( isset( $hs_data[ '_headspace_raw' ] ) && ! empty( $hs_data[ '_headspace_raw' ] ) ) {
204
205
206
207
208
			$output[] = $hs_data[ '_headspace_raw' ];
		}

		// Output whatever we've built
		if ( ! empty( $output ) ) {
209
			echo "\n<!-- Escape HeadSpace2 by Erick Hitter; https://eth.pw/escape-hs2 -->\n" . implode( "\n", $output ) . "\n<!-- Escape HeadSpace2 -->\n";
210
		}
Erick Hitter's avatar
Erick Hitter committed
211
	}
Erick Hitter's avatar
Erick Hitter committed
212
213

	/**
Erick Hitter's avatar
Erick Hitter committed
214
	 * Add custom footer content
Erick Hitter's avatar
Erick Hitter committed
215
216
217
218
219
220
221
222
	 */
	public function action_wp_footer() {
		$output = get_post_meta( get_the_ID(), '_headspace_raw_footer', true );

		if ( ! empty( $output ) ) {
			echo $output . "\n";
		}
	}
Erick Hitter's avatar
Erick Hitter committed
223
224
225
}

ETH_Escape_HeadSpace2::get_instance();