summaryrefslogtreecommitdiff
blob: fb8587180a26a320fa2d26c18712f778c49daa26 (plain)
1
2
3
4
5
6
7
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
 * Content Options.
 *
 * This feature will only be activated for themes that declare their support.
 * This can be done by adding code similar to the following during the
 * 'after_setup_theme' action:
 *
	add_theme_support( 'jetpack-content-options', array(
		'blog-display'       => 'content', // the default setting of the theme: 'content', 'excerpt' or array( 'content', 'excerpt' ) for themes mixing both display.
		'author-bio'         => true, // display or not the author bio: true or false.
		'author-bio-default' => false, // the default setting of the author bio, if it's being displayed or not: true or false (only required if false).
		'masonry'            => '.site-main', // a CSS selector matching the elements that triggers a masonry refresh if the theme is using a masonry layout.
		'post-details'       => array(
			'stylesheet'        => 'themeslug-style', // name of the theme's stylesheet.
			'date'              => '.posted-on', // a CSS selector matching the elements that display the post date.
			'categories'        => '.cat-links', // a CSS selector matching the elements that display the post categories.
			'tags'              => '.tags-links', // a CSS selector matching the elements that display the post tags.
			'author'            => '.byline', // a CSS selector matching the elements that display the post author.
			'comment'           => '.comments-link', // a CSS selector matching the elements that display the comment link.
		),
		'featured-images'    => array(
			'archive'           => true, // enable or not the featured image check for archive pages: true or false.
			'archive-default'   => false, // the default setting of the featured image on archive pages, if it's being displayed or not: true or false (only required if false).
			'post'              => true, // enable or not the featured image check for single posts: true or false.
			'post-default'      => false, // the default setting of the featured image on single posts, if it's being displayed or not: true or false (only required if false).
			'page'              => true, // enable or not the featured image check for single pages: true or false.
			'page-default'      => false, // the default setting of the featured image on single pages, if it's being displayed or not: true or false (only required if false).
			'portfolio'         => true, // enable or not the featured image check for single projects: true or false.
			'portfolio-default' => false, // the default setting of the featured image on single projects, if it's being displayed or not: true or false (only required if false).
			'fallback'          => true, // enable or not the featured image fallback: true or false.
			'fallback-default'  => true, // the default setting for featured image fallbacks: true or false (only required if false)
		),
	) );
 *
 */

/**
 * Activate the Content Options plugin.
 *
 * @uses current_theme_supports()
 */
function jetpack_content_options_init() {
	// If the theme doesn't support 'jetpack-content-options', don't continue.
	if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
		return;
	}

	// Load the Customizer options.
	require( dirname( __FILE__ ) . '/content-options/customizer.php' );

	// Load Blog Display function.
	require( dirname( __FILE__ ) . '/content-options/blog-display.php' );

	// Load Author Bio function.
	require( dirname( __FILE__ ) . '/content-options/author-bio.php' );

	// Load Post Details function.
	require( dirname( __FILE__ ) . '/content-options/post-details.php' );

	// Load Featured Images function.
	if ( jetpack_featured_images_should_load() ) {
		require( dirname( __FILE__ ) . '/content-options/featured-images.php' );
	}

	// Load Featured Images Fallback function.
	if ( jetpack_featured_images_fallback_should_load() ) {
		require( dirname( __FILE__ ) . '/content-options/featured-images-fallback.php' );
	}
}
add_action( 'init', 'jetpack_content_options_init' );

function jetpack_featured_images_get_settings() {
	$options         = get_theme_support( 'jetpack-content-options' );

	$featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;

	$settings        = array(
		'archive'           => ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null,
		'post'              => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null,
		'page'              => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null,
		'portfolio'         => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null,
		'archive-default'   => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1,
		'post-default'      => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1,
		'page-default'      => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1,
		'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1,
		'fallback'          => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null,
		'fallback-default'  => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1,
	);

	$settings        = array_merge( $settings, array(
		'archive-option'   => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ),
		'post-option'      => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ),
		'page-option'      => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ),
		'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ),
		'fallback-option'  => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ),
	) );

	return $settings;
}

function jetpack_featured_images_should_load() {
	// If the theme doesn't support post thumbnails, don't continue.
	if ( ! current_theme_supports( 'post-thumbnails' ) ) {
		return false;
	}

	$opts = jetpack_featured_images_get_settings();

	// If the theme doesn't support archive, post and page or if all the options are ticked and we aren't in the customizer, don't continue.
	if (
		( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] )
		|| ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() )
	) {
		return false;
	}

	return true;
}

function jetpack_featured_images_fallback_should_load() {
	// If the theme doesn't support post thumbnails, don't continue.
	if ( ! current_theme_supports( 'post-thumbnails' ) ) {
		return false;
	}

	$opts = jetpack_featured_images_get_settings();

	// If the theme doesn't support fallback, don't continue.
	if ( true !== $opts['fallback'] ) {
		return false;
	}

	return true;
}