Completed
Push — sync/georgestephanis/r156697-w... ( 008e9f )
by George
11:37
created

Jetpack_Omnisearch::admin_print_styles_jetpack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
// Include this here so that other plugins can extend it if they like.
4
require_once( dirname(__FILE__) . '/omnisearch-posts.php' );
5
6
class Jetpack_Omnisearch {
7
	static $instance;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
8
	static $num_results = 5;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $num_results.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
10
	function __construct() {
11
		self::$instance = $this;
12
		add_action( 'wp_loaded',          array( $this, 'wp_loaded' ) );
13
		add_action( 'admin_init',         array( $this, 'add_providers' ) );
14
		add_action( 'jetpack_admin_menu', array( $this, 'jetpack_admin_menu' ) );
15
		add_action( 'admin_menu',         array( $this, 'admin_menu' ), 20 );
16
		if( ! jetpack_is_mobile() ) {
17
			add_action( 'admin_bar_menu', array( $this, 'admin_bar_search' ), 4 );
18
		}
19
		add_filter( 'omnisearch_num_results', array( $this, 'omnisearch_num_results' ) );
20
	}
21
22
	static function add_providers() {
23
		// omnisearch-posts.php is included above, so that other plugins can more easily extend it.
24
		new Jetpack_Omnisearch_Posts;
25
		new Jetpack_Omnisearch_Posts( 'page' );
26
27
		require_once( dirname(__FILE__) . '/omnisearch-comments.php' );
28
		new Jetpack_Omnisearch_Comments;
29
30
		if ( current_user_can( 'upload_files' ) ) {
31
			require_once( dirname(__FILE__) . '/omnisearch-media.php' );
32
			new Jetpack_Omnisearch_Media;
33
		}
34
35
		if ( current_user_can( 'install_plugins' ) ) {
36
			require_once( dirname(__FILE__) . '/omnisearch-plugins.php' );
37
			new Jetpack_Omnisearch_Plugins;
38
		}
39
40
		/**
41
		 * Fires after each default omnisearch provider has been required.
42
		 *
43
		 * Can be used to add your own Omnisearch provider.
44
		 *
45
		 * @module omnisearch
46
		 *
47
		 * @since 2.3.2
48
		 */
49
		do_action( 'omnisearch_add_providers' );
50
	}
51
52
	static function omnisearch_num_results( $num ) {
53
		return self::$num_results;
54
	}
55
56
	function wp_loaded() {
57
		$deps = null;
58
		if ( wp_style_is( 'genericons', 'registered' ) ) {
59
			$deps = array( 'genericons' );
60
		}
61
		wp_register_style( 'omnisearch-admin', plugins_url( 'omnisearch.css', __FILE__ ), $deps );
62
		wp_style_add_data( 'omnisearch-admin', 'rtl', 'replace' );
63
64
	}
65
66
	function jetpack_admin_menu() {
67
		remove_submenu_page( 'index.php', 'omnisearch' );
68
		$this->slug = add_submenu_page( null, __( 'Omnisearch', 'jetpack' ), __( 'Omnisearch', 'jetpack' ), 'edit_posts', 'omnisearch', array( $this, 'omnisearch_page' ) );
0 ignored issues
show
Bug introduced by
The property slug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
		add_action( "admin_print_styles-{$this->slug}", array( $this, 'admin_print_styles' ) );
70
	}
71
72
	function admin_menu() {
73
		$this->slug = add_dashboard_page( __( 'Omnisearch', 'jetpack' ), __( 'Omnisearch', 'jetpack' ), 'edit_posts', 'omnisearch', array( $this, 'omnisearch_page' ) );
74
		add_action( "admin_print_styles-{$this->slug}", array( $this, 'admin_print_styles' ) );
75
	}
76
77
	function admin_print_styles() {
78
		wp_enqueue_style( 'omnisearch-admin' );
79
	}
80
81
	function omnisearch_page() {
82
		$results = array();
83
		$s = isset( $_GET['s'] ) ? $_GET['s'] : '';
84
		if ( $s ) {
85
			/**
86
			 * Filter the results returned for a given Omnisearch search query.
87
			 *
88
			 * @module omnisearch
89
			 *
90
			 * @since 2.3.0
91
			 *
92
			 * @param array $results Array of Omnisearch results.
93
			 * @param string $s Search parameter.
94
			 */
95
			$results = apply_filters( 'omnisearch_results', $results, $s );
96
		}
97
		/**
98
		 * Filter the number of results displayed for each Omnisearch searched section.
99
		 *
100
		 * @module omnisearch
101
		 *
102
		 * @since 2.3.0
103
		 *
104
		 * @param int 5 Number of results displayed for each Omnisearch searched section.
105
		 */
106
		$num_results = intval( apply_filters( 'omnisearch_num_results', 5 ) );
107
		?>
108
		<div class="wrap">
109
			<h2 class="page-title"><?php esc_html_e( 'Omnisearch', 'jetpack' ); ?> <small><?php esc_html_e( 'search everything', 'jetpack' ); ?></small></h2>
110
			<br class="clear" />
111
			<?php echo self::get_omnisearch_form( array(
112
							'form_class'         => 'omnisearch-form',
113
							'search_class'       => 'omnisearch',
114
							'search_placeholder' => '',
115
							'submit_class'       => 'omnisearch-submit',
116
							'alternate_submit'   => true,
117
						) ); ?>
118
			<?php if( ! empty( $results ) ): ?>
119
				<h3 id="results-title"><?php esc_html_e( 'Results:', 'jetpack' ); ?></h3>
120
				<div class="jump-to"><strong><?php esc_html_e( 'Jump to:', 'jetpack' ); ?></strong>
121
					<?php foreach( $results as $label => $result ) : ?>
122
						<a href="#result-<?php echo sanitize_title( $label ); ?>"><?php echo esc_html( $label ); ?></a>
123
					<?php endforeach; ?>
124
				</div>
125
				<br class="clear" />
126
				<script>var search_term = '<?php echo esc_js( $s ); ?>', num_results = <?php echo $num_results; ?>;</script>
127
				<ul class="omnisearch-results">
128
					<?php foreach( $results as $label => $result ) : ?>
129
						<li id="result-<?php echo sanitize_title( $label ); ?>" data-label="<?php echo esc_attr( $label ); ?>">
130
							<?php echo $result; ?>
131
							<a class="back-to-top" href="#results-title"><?php esc_html_e( 'Back to Top &uarr;', 'jetpack' ); ?></a>
132
						</li>
133
					<?php endforeach; ?>
134
				</ul>
135
			<?php endif; ?>
136
		</div><!-- /wrap -->
137
		<?php
138
	}
139
140
	function admin_bar_search( $wp_admin_bar ) {
141
		if(
142
			! is_admin() ||
143
			! current_user_can( 'edit_posts' ) ||
144
			(
145
				function_exists( 'wpcom_use_wpadmin_flows' ) &&
146
				! wpcom_use_wpadmin_flows()
147
			)
148
		)
149
			return;
150
151
		$form = self::get_omnisearch_form( array(
152
			'form_id'      => 'adminbarsearch',
153
			'search_id'    => 'adminbar-search',
154
			'search_class' => 'adminbar-input',
155
			'submit_class' => 'adminbar-button',
156
		) );
157
158
		$form .= "<style>
159
				#adminbar-search::-webkit-input-placeholder,
160
				#adminbar-search:-moz-placeholder,
161
				#adminbar-search::-moz-placeholder,
162
				#adminbar-search:-ms-input-placeholder {
163
					text-shadow: none;
164
				}
165
			</style>";
166
167
		$wp_admin_bar->add_menu( array(
168
			'parent' => 'top-secondary',
169
			'id'     => 'search',
170
			'title'  => $form,
171
			'meta'   => array(
172
				'class'    => 'admin-bar-search',
173
				'tabindex' => -1,
174
			)
175
		) );
176
	}
177
178
	static function get_omnisearch_form( $args = array() ) {
179
		$defaults = array(
180
			'form_id'            => null,
181
			'form_class'         => null,
182
			'search_class'       => null,
183
			'search_id'          => null,
184
			'search_value'       => isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : null,
185
			'search_placeholder' => __( 'Search Everything', 'jetpack' ),
186
			'submit_class'       => 'button',
187
			'submit_value'       => __( 'Search', 'jetpack' ),
188
			'alternate_submit'   => false,
189
		);
190
		extract( array_map( 'esc_attr', wp_parse_args( $args, $defaults ) ) );
0 ignored issues
show
Bug introduced by
array_map('esc_attr', wp...args($args, $defaults)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
191
192
		$rand = rand();
193
		if( empty( $form_id ) )
194
			$form_id = "omnisearch_form_$rand";
195
		if( empty( $search_id ) )
196
			$search_id = "omnisearch_search_$rand";
197
198
		ob_start();
199
		?>
200
201
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get" class="<?php echo $form_class; ?>" id="<?php echo $form_id; ?>">
202
			<input type="hidden" name="page" value="omnisearch" />
203
			<input name="s" type="search" class="<?php echo $search_class; ?>" id="<?php echo $search_id; ?>" value="<?php echo $search_value; ?>" placeholder="<?php echo $search_placeholder; ?>" />
204
			<?php if ( $alternate_submit ) : ?>
205
				<button type="submit" class="<?php echo $submit_class; ?>"><span><?php echo $submit_value; ?></span></button>
206
			<?php else : ?>
207
				<input type="submit" class="<?php echo $submit_class; ?>" value="<?php echo $submit_value; ?>" />
208
			<?php endif; ?>
209
		</form>
210
211
		<?php
212
		/**
213
		 * Filters the Omnisearch search form output.
214
		 *
215
		 * @module omnisearch
216
		 *
217
		 * @since 2.3.0
218
		 *
219
		 * @param string ob_get_clean() Omnisearch search form output.
220
		 * @param array $args Array of arguments to pass to the form to overwrite the default form parameters.
221
		 * @param array $defaults Array of default form parameters.
222
		 */
223
		return apply_filters( 'get_omnisearch_form', ob_get_clean(), $args, $defaults );
224
	}
225
226
}
227
new Jetpack_Omnisearch;
228