|
10 | 10 |
|
11 | 11 | // check if php version is >= 5.3 |
12 | 12 | // version is required by the mf2 parser |
13 | | -function uf2_feed_activation() |
14 | | -{ |
15 | | - if (version_compare(phpversion(), 5.3, '<')) { |
16 | | - die("The minimum PHP version required for this plugin is 5.3"); |
17 | | - } |
| 13 | +function uf2_feed_activation() { |
| 14 | + if ( version_compare( phpversion(), 5.3, '<' ) ) { |
| 15 | + die( 'The minimum PHP version required for this plugin is 5.3' ); |
| 16 | + } |
18 | 17 | } |
19 | | -register_activation_hook(__FILE__, 'uf2_feed_activation'); |
| 18 | +register_activation_hook( __FILE__, 'uf2_feed_activation' ); |
20 | 19 |
|
21 | | -if (!class_exists("Mf2\Parser")) { |
22 | | - require_once 'Mf2/Parser.php'; |
| 20 | +if ( ! class_exists( 'Mf2\Parser' ) ) { |
| 21 | + require_once 'Mf2/Parser.php'; |
23 | 22 | } |
24 | 23 |
|
25 | 24 | use Mf2\Parser; |
26 | 25 |
|
27 | | -add_action('init', array('Uf2Feed', 'init')); |
28 | | -register_activation_hook(__FILE__, array('Uf2Feed', 'flush_rewrite_rules')); |
29 | | -register_deactivation_hook(__FILE__, array('Uf2Feed', 'flush_rewrite_rules')); |
| 26 | +add_action( 'init', array( 'Uf2Feed', 'init' ) ); |
| 27 | +register_activation_hook( __FILE__, array( 'Uf2Feed', 'flush_rewrite_rules' ) ); |
| 28 | +register_deactivation_hook( __FILE__, array( 'Uf2Feed', 'flush_rewrite_rules' ) ); |
30 | 29 |
|
31 | 30 | /** |
| 31 | + * Uf2Feed class |
32 | 32 | * |
| 33 | + * @author Matthias Pfefferle |
33 | 34 | */ |
34 | | -class Uf2Feed |
35 | | -{ |
36 | | - /** |
37 | | - * init function |
38 | | - */ |
39 | | - public static function init() |
40 | | - { |
41 | | - // add 'json' as feed |
42 | | - add_action('do_feed_uf2', array('Uf2Feed', 'do_feed_uf2'), 10, 1); |
43 | | - // add 'json' as feed |
44 | | - add_action('do_feed_mf2', array('Uf2Feed', 'do_feed_uf2'), 10, 1); |
45 | | - // add the as1 feed |
46 | | - add_feed('uf2', array('Uf2Feed', 'do_feed_uf2')); |
47 | | - // add the as2 feed |
48 | | - add_feed('mf2', array('Uf2Feed', 'do_feed_uf2')); |
49 | | - |
50 | | - add_filter('query_vars', array('Uf2Feed', 'query_vars')); |
51 | | - add_filter('feed_content_type', array('Uf2Feed', 'feed_content_type'), 10, 2); |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * adds an UF2 JSON feed |
56 | | - * |
57 | | - * @param boolean $for_comments true if it is a comment-feed |
58 | | - */ |
59 | | - public static function do_feed_uf2($for_comments) |
60 | | - { |
61 | | - global $wp; |
62 | | - |
63 | | - // get query vars as array |
64 | | - $params = $wp->query_vars; |
65 | | - |
66 | | - // remove feed param |
67 | | - if (isset($params["feed"])) { |
68 | | - unset($params["feed"]); |
69 | | - } |
70 | | - |
71 | | - $current_url = add_query_arg($params, site_url()); |
72 | | - |
73 | | - // filter feed URL (add url params for example) |
74 | | - $current_url = apply_filters("uf2_feed_url", $current_url); |
75 | | - |
76 | | - // get HTML content |
77 | | - $response = wp_remote_retrieve_body(wp_remote_get($current_url, array('timeout' => 100))); |
78 | | - |
79 | | - // parse source html |
80 | | - $parser = new Parser($response, $current_url); |
81 | | - |
82 | | - // also support uf1? |
83 | | - $parseUf1 = apply_filters("uf2_feed_support_uf1", false); |
84 | | - |
85 | | - $mf_array = $parser->parse($parseUf1); |
86 | | - |
87 | | - // filter output |
88 | | - $json = apply_filters("uf2_feed_array", $mf_array); |
89 | | - |
90 | | - header("Content-Type: " . feed_content_type("uf2") . "; charset=" . get_option("blog_charset"), true); |
91 | | - |
92 | | - if (version_compare(phpversion(), "5.3.0", "<")) { |
93 | | - // json_encode() options added in PHP 5.3 |
94 | | - $json_str = json_encode($json); |
95 | | - } else { |
96 | | - $options = 0; |
97 | | - // JSON_PRETTY_PRINT added in PHP 5.4 |
98 | | - if (get_query_var("pretty") && version_compare(phpversion(), "5.4.0", ">=")) { |
99 | | - $options |= JSON_PRETTY_PRINT; |
100 | | - } |
101 | | - |
102 | | - /* |
103 | | - * Options to be passed to json_encode() |
104 | | - * |
105 | | - * @param int $options The current options flags |
106 | | - */ |
107 | | - $options = apply_filters("uf2_feed_options", $options); |
108 | | - $json_str = json_encode($json, $options); |
109 | | - } |
110 | | - |
111 | | - echo $json_str; |
112 | | - } |
113 | | - |
114 | | - /** |
115 | | - * adds "uf2" content-type |
116 | | - * |
117 | | - * @param string $content_type the default content-type |
118 | | - * @param string $type the feed-type |
119 | | - * @return string the as1 content-type |
120 | | - */ |
121 | | - public static function feed_content_type($content_type, $type) |
122 | | - { |
123 | | - if ($type == "uf2" || $type == "mf2") { |
124 | | - return apply_filters("uf2_feed_content_type", "application/json"); |
125 | | - } |
126 | | - |
127 | | - return $content_type; |
128 | | - } |
129 | | - |
130 | | - /** |
131 | | - * add 'feed' and 'pretty' as a valid query variables. |
132 | | - * |
133 | | - * @param array $vars |
134 | | - * @return array |
135 | | - */ |
136 | | - public static function query_vars($vars) |
137 | | - { |
138 | | - $vars[] = 'feed'; |
139 | | - $vars[] = 'pretty'; |
140 | | - |
141 | | - return $vars; |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * reset rewrite rules |
146 | | - */ |
147 | | - public static function flush_rewrite_rules() |
148 | | - { |
149 | | - global $wp_rewrite; |
150 | | - |
151 | | - $wp_rewrite->flush_rules(); |
152 | | - } |
| 35 | +class Uf2Feed { |
| 36 | + /** |
| 37 | + * init function |
| 38 | + */ |
| 39 | + public static function init() { |
| 40 | + // add 'json' as feed |
| 41 | + add_action( 'do_feed_uf2', array( 'Uf2Feed', 'do_feed_uf2' ), 10, 1 ); |
| 42 | + // add 'json' as feed |
| 43 | + add_action( 'do_feed_mf2', array( 'Uf2Feed', 'do_feed_uf2' ), 10, 1 ); |
| 44 | + // add the as1 feed |
| 45 | + add_feed( 'uf2', array( 'Uf2Feed', 'do_feed_uf2' ) ); |
| 46 | + // add the as2 feed |
| 47 | + add_feed( 'mf2', array( 'Uf2Feed', 'do_feed_uf2' ) ); |
| 48 | + |
| 49 | + add_filter( 'query_vars', array( 'Uf2Feed', 'query_vars' ) ); |
| 50 | + add_filter( 'feed_content_type', array( 'Uf2Feed', 'feed_content_type' ), 10, 2 ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * adds an UF2 JSON feed |
| 55 | + * |
| 56 | + * @param boolean $for_comments true if it is a comment-feed |
| 57 | + */ |
| 58 | + public static function do_feed_uf2( $for_comments ) { |
| 59 | + global $wp; |
| 60 | + |
| 61 | + // get query vars as array |
| 62 | + $params = $wp->query_vars; |
| 63 | + |
| 64 | + // remove feed param |
| 65 | + if ( isset( $params['feed'] ) ) { |
| 66 | + unset( $params['feed'] ); |
| 67 | + } |
| 68 | + |
| 69 | + $current_url = add_query_arg( $params, site_url() ); |
| 70 | + |
| 71 | + // filter feed URL (add url params for example) |
| 72 | + $current_url = apply_filters( 'uf2_feed_url', $current_url ); |
| 73 | + |
| 74 | + // get HTML content |
| 75 | + $response = wp_remote_retrieve_body( wp_remote_get( $current_url, array( 'timeout' => 100 ) ) ); |
| 76 | + |
| 77 | + // parse source html |
| 78 | + $parser = new Parser( $response, $current_url ); |
| 79 | + |
| 80 | + // also support uf1? |
| 81 | + $parseUf1 = apply_filters( 'uf2_feed_support_uf1', false ); |
| 82 | + |
| 83 | + $mf_array = $parser->parse( $parseUf1 ); |
| 84 | + |
| 85 | + // filter output |
| 86 | + $json = apply_filters( 'uf2_feed_array', $mf_array ); |
| 87 | + |
| 88 | + header( 'Content-Type: ' . feed_content_type( 'uf2' ) . '; charset=' . get_option( 'blog_charset' ), true ); |
| 89 | + |
| 90 | + if ( version_compare( phpversion(), '5.3.0', '<' ) ) { |
| 91 | + // json_encode() options added in PHP 5.3 |
| 92 | + $json_str = json_encode( $json ); |
| 93 | + } else { |
| 94 | + $options = 0; |
| 95 | + // JSON_PRETTY_PRINT added in PHP 5.4 |
| 96 | + if ( get_query_var( 'pretty' ) && version_compare( phpversion(), '5.4.0', '>=' ) ) { |
| 97 | + $options |= JSON_PRETTY_PRINT; |
| 98 | + } |
| 99 | + |
| 100 | + /* |
| 101 | + * Options to be passed to json_encode() |
| 102 | + * |
| 103 | + * @param int $options The current options flags |
| 104 | + */ |
| 105 | + $options = apply_filters( 'uf2_feed_options', $options ); |
| 106 | + $json_str = json_encode( $json, $options ); |
| 107 | + } |
| 108 | + |
| 109 | + echo $json_str; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * adds "uf2" content-type |
| 114 | + * |
| 115 | + * @param string $content_type the default content-type |
| 116 | + * @param string $type the feed-type |
| 117 | + * @return string the as1 content-type |
| 118 | + */ |
| 119 | + public static function feed_content_type( $content_type, $type ) { |
| 120 | + if ( 'uf2' == $type || 'mf2' == $type ) { |
| 121 | + return apply_filters( 'uf2_feed_content_type', 'application/json' ); |
| 122 | + } |
| 123 | + |
| 124 | + return $content_type; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * add 'feed' and 'pretty' as a valid query variables. |
| 129 | + * |
| 130 | + * @param array $vars |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + public static function query_vars( $vars ) { |
| 134 | + $vars[] = 'feed'; |
| 135 | + $vars[] = 'pretty'; |
| 136 | + |
| 137 | + return $vars; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * reset rewrite rules |
| 142 | + */ |
| 143 | + public static function flush_rewrite_rules() { |
| 144 | + global $wp_rewrite; |
| 145 | + |
| 146 | + $wp_rewrite->flush_rules(); |
| 147 | + } |
153 | 148 | } |
0 commit comments