Completed
Push — master ( aa4fea...197921 )
by
unknown
03:08
created

jetpack.php ➔ lsx_save_portfolio_post_meta()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 10
nop 2
dl 0
loc 29
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Jetpack Functionality
4
 *
5
 * @package lsx
6
 * @subpackage jetpack
7
 */
8
9
/*
10
 * General Jetpack Functionality
11
 */
12
13
/**
14
 * Adds portfolio and removes pages to the jetpack related posts.
15
 * 
16
 * @package lsx
17
 * @subpackage jetpack
18
 * @category general
19
 */
20
function lsx_allowed_related_post_types($allowed_post_types) {
21
	$allowed_post_types[] = 'jetpack-portfolio';
22
	foreach($allowed_post_types as $key => $value){
23
		if('page' == $value){
24
			unset($allowed_post_types[$key]);
25
		}
26
	}
27
	return $allowed_post_types;
28
}
29
30
/**
31
 * Adds the Site Title in Settings->General as a "title" attribute for the logo link.
32
 * 
33
 * @package lsx
34
 * @subpackage jetpack
35
 * @category site-logo
36
 */
37
38
function lsx_site_logo_title_tag( $html) {
39
40
	$html = str_replace('<a', '<a title="'.get_bloginfo('name').'" ', $html);
41
	return $html;
42
}
43
add_filter( 'jetpack_the_site_logo', 'lsx_site_logo_title_tag');
44
45
46
/*
47
 * Portfolio Functionality
48
 */
49
50
/**
51
 * Set the Portfolio archive slug
52
 *
53
 * @package lsx
54
 * @subpackage jetpack
55
 * @category portfolio
56
 */
57
function lsx_portfolio_infinite_scroll(){
58
	global $_wp_theme_features,$wp_query;
59
60
	if(is_post_type_archive('jetpack-portfolio') || is_tax('jetpack-portfolio-type') || is_tax('jetpack-portfolio-tag')){
61
		
62
		if(class_exists('The_Neverending_Home_Page')){
63
			$_wp_theme_features['infinite-scroll'][0]['container'] = 'portfolio-infinite-scroll-wrapper';
64
			$_wp_theme_features['infinite-scroll'][0]['posts_per_page'] = 99;
65
		}
66
	}
67
68
}
69
add_action('wp_head','lsx_portfolio_infinite_scroll',1000);
70
71
/**
72
 * Disables the infinite scroll on the portfolio archive
73
 *
74
 * @package lsx
75
 * @subpackage jetpack
76
 * @category portfolio
77
 */
78
function lsx_portfolio_infinite_scroll_disable($supported){
79
	if(is_post_type_archive('jetpack-portfolio')){
80
		$supported = false;
81
	}
82
	return $supported;
83
}
84
add_filter( 'infinite_scroll_archive_supported', 'lsx_portfolio_infinite_scroll_disable', 1, 10 );
85
86
/**
87
 * Set the Portfolio to 9 posts per page
88
 *
89
 * @package lsx
90
 * @subpackage jetpack
91
 * @category portfolio
92
*/
93
function lsx_portfolio_archive_pagination( $query ) {
94
	if(!is_admin()){
95
		if ( $query->is_post_type_archive(array('jetpack-portfolio')) && $query->is_main_query() && class_exists('The_Neverending_Home_Page')) {
96
			$query->set( 'posts_per_page', -1 );
97
		}
98
	}
99
}
100
add_action( 'pre_get_posts', 'lsx_portfolio_archive_pagination' , 100 );
101
102
/**
103
 * Remove the related posts from below the content area.
104
 * 
105
 * @package lsx
106
 * @subpackage jetpack
107
 * @category portfolio
108
 */
109 View Code Duplication
function lsx_remove_portfolio_related_posts() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
	if ( is_single() && 'jetpack-portfolio' == get_post_type() && class_exists( 'Jetpack_RelatedPosts' ) ) {
111
		$jprp = Jetpack_RelatedPosts::init();
112
		$callback = array( $jprp, 'filter_add_target_to_dom' );
113
		remove_filter( 'the_content', $callback, 40 );
114
	}
115
}
116
add_filter( 'wp', 'lsx_remove_portfolio_related_posts', 20 );
117
118
/**
119
 * Remove the related posts from below the content area.
120
 * 
121
 * @package lsx
122
 * @subpackage jetpack
123
 * @category portfolio
124
 */
125 View Code Duplication
function lsx_remove_single_related_posts() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    if ( is_single() && class_exists( 'Jetpack_RelatedPosts' ) ) {
127
        $jprp = Jetpack_RelatedPosts::init();
128
        $callback = array( $jprp, 'filter_add_target_to_dom' );
129
        remove_filter( 'the_content', $callback, 40 );
130
    }
131
}
132
add_filter( 'wp', 'lsx_remove_single_related_posts', 20 );
133
134
/**
135
 * A template tag to call the Portfolios Related posts
136
 * 
137
 * @package lsx
138
 * @subpackage jetpack
139
 * @category portfolio
140
 */
141
function lsx_portfolio_related_posts(){
142
	if(class_exists('Jetpack_RelatedPosts')){ ?>
143
		<div class="row">
144
			<div class="col-md-12">
145
				<?php echo do_shortcode('[jetpack-related-posts]'); ?>
146
			</div>
147
		</div>			
148
	<?php }
149
}
150
151
/**
152
 * Remove the sharing from below the content on single portfolio pages.
153
 *
154
 * @package lsx
155
 * @subpackage jetpack
156
 * @category portfolio
157
 */
158
function lsx_portfolio_remove_share() {
159
	if ( ( is_single() && 'jetpack-portfolio' == get_post_type() ) || is_page_template( 'page-templates/template-portfolio.php' ) ) {
160
		remove_filter( 'the_content', 'sharing_display',19 );
161
		remove_filter( 'the_excerpt', 'sharing_display',19 );
162
163
		if ( class_exists( 'Jetpack_Likes' ) ) {
164
			remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
165
		}
166
	}
167
}
168
add_action( 'loop_start', 'lsx_portfolio_remove_share' );
169
170
/**
171
 * Remove the sharing from single
172
 *
173
 * @package lsx
174
 * @subpackage jetpack
175
 * @category post
176
 */
177
function lsx_single_remove_share() {
178
	if ( is_single() ) {
179
		remove_filter( 'the_content', 'sharing_display',19 );
180
		remove_filter( 'the_excerpt', 'sharing_display',19 );
181
182
		if ( class_exists( 'Jetpack_Likes' ) ) {
183
			remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
184
		}
185
	}
186
}
187
add_action( 'loop_start', 'lsx_single_remove_share' );
188
189
/**
190
 * Redirect the template archive to our one
191
 * 
192
 * @package lsx
193
 * @subpackage jetpack
194
 * @category portfolio
195
*/
196
function lsx_portfolio_taxonomy_template( $template ) {
197
198
	if ( is_tax(array('jetpack-portfolio-type','jetpack-portfolio-tag'))  ) {
199
		$new_template = locate_template( array( 'archive-jetpack-portfolio.php' ) );
200
		if ( '' != $new_template ) {
201
			return $new_template ;
202
		}
203
	}
204
205
	return $template;
206
}
207
add_filter( 'template_include', 'lsx_portfolio_taxonomy_template', 99 );
208
209
210
/**
211
 * Save the Portfolio Post Meta Options
212
 *
213
 * @package lsx
214
 * @subpackage jetpack
215
 * @category portfolio
216
 */
217
add_action( 'add_meta_boxes', 'lsx_add_portfolio_post_meta_boxes' );
218
add_action( 'save_post', 'lsx_save_portfolio_post_meta', 100, 2 );
219
220
function lsx_save_portfolio_post_meta( $post_id, $post ) {
221
	check_admin_referer( 'lsx_save_portfolio', '_lsx_client_nonce' );
222
	check_admin_referer( 'lsx_save_portfolio', '_lsx_website_nonce' );
223
224
	$post_type = get_post_type_object( $post->post_type );
225
226
	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
227
		return $post_id;
228
229
230
	$meta_keys = array('lsx-website','lsx-client');
231
232
	foreach($meta_keys as $meta_key){
233
		$new_meta_value = sanitize_text_field( wp_unslash( $_POST[$meta_key] ) );
234
		$new_meta_value = ! empty( $new_meta_value ) ? $new_meta_value : '';
235
236
		$meta_value = get_post_meta( $post_id, $meta_key, true );
237
238
		if ( $new_meta_value && '' == $meta_value )
239
			add_post_meta( $post_id, $meta_key, $new_meta_value, true );
240
241
		elseif ( $new_meta_value && $new_meta_value != $meta_value )
242
		update_post_meta( $post_id, $meta_key, $new_meta_value );
243
244
		elseif ( '' == $new_meta_value && $meta_value )
245
		delete_post_meta( $post_id, $meta_key, $meta_value );
246
247
	}
248
}
249
250
function lsx_add_portfolio_post_meta_boxes() {
251
252
	add_meta_box(
253
	'lsx_client_meta_box',
254
	esc_html__( 'Client', 'lsx' ),
255
	'lsx_client_meta_box',
256
	'jetpack-portfolio',
257
	'side',
258
	'default'
259
			);
260
261
	add_meta_box(
262
	'lsx_website_meta_box',
263
	esc_html__( 'Website', 'lsx' ),
264
	'lsx_website_meta_box',
265
	'jetpack-portfolio',
266
	'side',
267
	'default'
268
			);
269
}
270
271 View Code Duplication
function lsx_client_meta_box( $object, $box ) { ?>
0 ignored issues
show
Unused Code introduced by
The parameter $box is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
273
  <?php wp_nonce_field( 'lsx_save_portfolio', '_lsx_client_nonce' ); ?>
274
275
  <p>
276
    <input class="widefat" type="text" name="lsx-client" id="lsx-client" value="<?php echo esc_attr( get_post_meta( $object->ID, 'lsx-client', true ) ); ?>" size="30" />
277
    <br /><br />
278
    <label for="lsx-client"><?php esc_html_e( 'Enter the name of the project client', 'lsx' ); ?></label>
279
  </p>
280
<?php }
281
282 View Code Duplication
function lsx_website_meta_box( $object, $box ) { ?>
0 ignored issues
show
Unused Code introduced by
The parameter $box is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
284
  <?php wp_nonce_field( 'lsx_save_portfolio', '_lsx_website_nonce' ); ?>
285
286
  <p>
287
    <input class="widefat" type="text" name="lsx-website" id="lsx-website" value="<?php echo esc_attr( get_post_meta( $object->ID, 'lsx-website', true ) ); ?>" size="30" />
288
    <br /><br />
289
    <label for="lsx-website"><?php esc_html_e( 'Enter the URL of the project website', 'lsx' ); ?></label>
290
  </p>
291
<?php }
292
293
294
/**
295
 * A project type filter for the portfolio template
296
 *
297
 * @package lsx
298
 * @subpackage jetpack
299
 * @category portfolio
300
 */   
301
302
function lsx_portfolio_sorter(){ ?>
303
	<ul id="filterNav" class="clearfix">
304
		<li class="allBtn"><a href="#" data-filter="*" class="selected"><?php esc_html_e( 'All', 'lsx' ); ?></a></li>
305
		<?php 
306
		$types = get_terms('jetpack-portfolio-type');
307
		
308
		if(is_array($types)){
309
			foreach ($types as $type) {
310
				$content = '<li><a href="#" data-filter=".'.$type->slug.'">';
311
		    	$content .= $type->name;					
312
				$content .= '</a></li>';
313
				echo wp_kses_post( $content );
314
				echo "\n";
315
			}
316
		}?>
317
	</ul>
318
<?php } 
319
320
/**
321
 * A project type filter for the portfolio template
322
 *
323
 * @package lsx
324
 * @subpackage jetpack
325
 * @category portfolio
326
 */
327
328
function lsx_portfolio_naviagtion_labels($labels){ 
329
	
330
	if(is_post_type_archive('jetpack-portfolio')){
331
		$labels = array(
332
				'next' 		=> '<span class="meta-nav">&larr;</span> '.__( 'Older', 'lsx' ),
333
				'previous' 	=> __( 'Newer', 'lsx' ).' <span class="meta-nav">&rarr;</span>',
334
				'title' 	=> __( 'Portfolio navigation', 'lsx' )
335
		);
336
	}
337
	return $labels;
338
}
339
add_filter('lsx_post_navigation_labels','lsx_portfolio_naviagtion_labels',1,10);
340
341
342
/*
343
 * Related Posts
344
 */
345
346
/**
347
 * Remove the Category from the Jetpack related posts.
348
 * 
349
 * @package lsx
350
 * @subpackage jetpack
351
 * @category related-posts
352
 */
353
function lsx_remove_related_post_context(){
354
	add_filter( 'jetpack_relatedposts_filter_post_context', '__return_empty_string' );
355
	add_filter( 'rest_api_allowed_post_types', 'lsx_allowed_related_post_types' );
356
}
357
add_action('init','lsx_remove_related_post_context',20);
358
359
360
/*
361
 * Infinate Scroll
362
*/
363
/**
364
 * Adds the theme_support for Jetpacks Infinite Scroll
365
 *
366
 * @package lsx
367
 * @subpackage jetpack
368
 * @category infinite scroll
369
 */
370
function lsx_jetpack_infinite_scroll_after_setup() {
371
	$infinite_scroll_args = array(
372
			'container' => 'main',
373
			'type' => 'click',
374
			'posts_per_page' => get_option('posts_per_page',10),
375
			'render'    => 'lsx_infinite_scroll_render'
376
	);
377
378
	add_theme_support( 'infinite-scroll', $infinite_scroll_args );
379
}
380
add_action( 'after_setup_theme', 'lsx_jetpack_infinite_scroll_after_setup' );
381
382
/**
383
 * Set the code to be rendered on for calling posts,
384
 * hooked to template parts when possible.
385
 * 
386
 * @package lsx
387
 * @subpackage jetpack
388
 * @category infinite scroll
389
 */
390
 function lsx_infinite_scroll_render() {
391
	global $wp_query;
392
	
393
	while(have_posts()){
394
		the_post();
395
		
396
		if('jetpack-portfolio' == get_post_type()){
397
			get_template_part( 'content', 'portfolio' );
398
		}else{
399
			get_template_part( 'content', get_post_type() );
400
		}
401
	}
402
 }
403
404
/**
405
 * Change the Related headline at the top of the Related Posts section
406
 * 
407
 * @package lsx
408
 * @subpackage jetpack
409
 * @category related posts
410
 */
411
function lsx_related_posts_headline( $headline ) {
0 ignored issues
show
Unused Code introduced by
The parameter $headline is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
412
	$headline = sprintf( '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( 'Related Posts' ) );
413
	return $headline;
414
}
415
add_filter( 'jetpack_relatedposts_filter_headline', 'lsx_related_posts_headline' );
416