Completed
Push — master ( 72c4b0...e2176a )
by
unknown
03:35
created

jetpack.php ➔ lsx_save_portfolio_post_meta()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 21
nc 12
nop 2
dl 0
loc 36
rs 5.1234
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
	if ( 'jetpack-portfolio' != $post->post_type ) {
222
		return;
223
	}
224
225
	if ( ! isset( $_POST['lsx-website'] ) && ! isset( $_POST['lsx-client'] ) ) {
226
		return;
227
	}
228
229
	$post_type = get_post_type_object( $post->post_type );
230
231
	if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
232
		return;
233
	}
234
235
	check_admin_referer( 'lsx_save_portfolio', '_lsx_client_nonce' );
236
	check_admin_referer( 'lsx_save_portfolio', '_lsx_website_nonce' );
237
238
	$meta_keys = array('lsx-website','lsx-client');
239
240
	foreach($meta_keys as $meta_key){
241
		$new_meta_value = sanitize_text_field( wp_unslash( $_POST[$meta_key] ) );
242
		$new_meta_value = ! empty( $new_meta_value ) ? $new_meta_value : '';
243
244
		$meta_value = get_post_meta( $post_id, $meta_key, true );
245
246
		if ( $new_meta_value && '' == $meta_value )
247
			add_post_meta( $post_id, $meta_key, $new_meta_value, true );
248
249
		elseif ( $new_meta_value && $new_meta_value != $meta_value )
250
		update_post_meta( $post_id, $meta_key, $new_meta_value );
251
252
		elseif ( '' == $new_meta_value && $meta_value )
253
		delete_post_meta( $post_id, $meta_key, $meta_value );
254
	}
255
}
256
257
function lsx_add_portfolio_post_meta_boxes() {
258
259
	add_meta_box(
260
	'lsx_client_meta_box',
261
	esc_html__( 'Client', 'lsx' ),
262
	'lsx_client_meta_box',
263
	'jetpack-portfolio',
264
	'side',
265
	'default'
266
			);
267
268
	add_meta_box(
269
	'lsx_website_meta_box',
270
	esc_html__( 'Website', 'lsx' ),
271
	'lsx_website_meta_box',
272
	'jetpack-portfolio',
273
	'side',
274
	'default'
275
			);
276
}
277
278 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...
279
280
  <?php wp_nonce_field( 'lsx_save_portfolio', '_lsx_client_nonce' ); ?>
281
282
  <p>
283
    <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" />
284
    <br /><br />
285
    <label for="lsx-client"><?php esc_html_e( 'Enter the name of the project client', 'lsx' ); ?></label>
286
  </p>
287
<?php }
288
289 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...
290
291
  <?php wp_nonce_field( 'lsx_save_portfolio', '_lsx_website_nonce' ); ?>
292
293
  <p>
294
    <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" />
295
    <br /><br />
296
    <label for="lsx-website"><?php esc_html_e( 'Enter the URL of the project website', 'lsx' ); ?></label>
297
  </p>
298
<?php }
299
300
301
/**
302
 * A project type filter for the portfolio template
303
 *
304
 * @package lsx
305
 * @subpackage jetpack
306
 * @category portfolio
307
 */   
308
309
function lsx_portfolio_sorter(){ ?>
310
	<ul id="filterNav" class="clearfix">
311
		<li class="allBtn"><a href="#" data-filter="*" class="selected"><?php esc_html_e( 'All', 'lsx' ); ?></a></li>
312
		<?php 
313
		$types = get_terms('jetpack-portfolio-type');
314
		
315
		if(is_array($types)){
316
			foreach ($types as $type) {
317
				$content = '<li><a href="#" data-filter=".'.$type->slug.'">';
318
		    	$content .= $type->name;					
319
				$content .= '</a></li>';
320
				echo wp_kses_post( $content );
321
				echo "\n";
322
			}
323
		}?>
324
	</ul>
325
<?php } 
326
327
/**
328
 * A project type filter for the portfolio template
329
 *
330
 * @package lsx
331
 * @subpackage jetpack
332
 * @category portfolio
333
 */
334
335
function lsx_portfolio_naviagtion_labels($labels){ 
336
	
337
	if(is_post_type_archive('jetpack-portfolio')){
338
		$labels = array(
339
				'next' 		=> '<span class="meta-nav">&larr;</span> '.__( 'Older', 'lsx' ),
340
				'previous' 	=> __( 'Newer', 'lsx' ).' <span class="meta-nav">&rarr;</span>',
341
				'title' 	=> __( 'Portfolio navigation', 'lsx' )
342
		);
343
	}
344
	return $labels;
345
}
346
add_filter('lsx_post_navigation_labels','lsx_portfolio_naviagtion_labels',1,10);
347
348
349
/*
350
 * Related Posts
351
 */
352
353
/**
354
 * Remove the Category from the Jetpack related posts.
355
 * 
356
 * @package lsx
357
 * @subpackage jetpack
358
 * @category related-posts
359
 */
360
function lsx_remove_related_post_context(){
361
	add_filter( 'jetpack_relatedposts_filter_post_context', '__return_empty_string' );
362
	add_filter( 'rest_api_allowed_post_types', 'lsx_allowed_related_post_types' );
363
}
364
add_action('init','lsx_remove_related_post_context',20);
365
366
367
/*
368
 * Infinate Scroll
369
*/
370
/**
371
 * Adds the theme_support for Jetpacks Infinite Scroll
372
 *
373
 * @package lsx
374
 * @subpackage jetpack
375
 * @category infinite scroll
376
 */
377
function lsx_jetpack_infinite_scroll_after_setup() {
378
	$infinite_scroll_args = array(
379
			'container' => 'main',
380
			'type' => 'click',
381
			'posts_per_page' => get_option('posts_per_page',10),
382
			'render'    => 'lsx_infinite_scroll_render'
383
	);
384
385
	add_theme_support( 'infinite-scroll', $infinite_scroll_args );
386
}
387
add_action( 'after_setup_theme', 'lsx_jetpack_infinite_scroll_after_setup' );
388
389
/**
390
 * Set the code to be rendered on for calling posts,
391
 * hooked to template parts when possible.
392
 * 
393
 * @package lsx
394
 * @subpackage jetpack
395
 * @category infinite scroll
396
 */
397
 function lsx_infinite_scroll_render() {
398
	global $wp_query;
399
	
400
	while(have_posts()){
401
		the_post();
402
		
403
		if('jetpack-portfolio' == get_post_type()){
404
			get_template_part( 'content', 'portfolio' );
405
		}else{
406
			get_template_part( 'content', get_post_type() );
407
		}
408
	}
409
 }
410
411
/**
412
 * Change the Related headline at the top of the Related Posts section
413
 * 
414
 * @package lsx
415
 * @subpackage jetpack
416
 * @category related posts
417
 */
418
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...
419
	$headline = sprintf( '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( 'Related Posts' ) );
420
	return $headline;
421
}
422
add_filter( 'jetpack_relatedposts_filter_headline', 'lsx_related_posts_headline' );
423