-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclass-cc-site.php
167 lines (166 loc) · 4.57 KB
/
class-cc-site.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* CC Site
*
* This file contains a mix of useful functions for several uses.
*
* @link https://github.com/creativecommons/creativecommons-base
*
* @package WordPress
* @subpackage creativecommons-base
* @since 2020.04.1
*/
/**
* CC Site
*
* A set of useful functions.
*
* @since 2020.04.1
*/
class CC_Site {
/**
* Return the term list of a certain taxonomy related to an entry
*
* @param int $post_id : entry ID.
* @param string $term : term slug.
* @return array array of Term objects
*/
public static function get_terms_list( $post_id, $term = 'category' ) {
$terms = wp_get_post_terms( $post_id, $term );
return $terms;
}
/**
* Return a list of button links to each category of a certain post
*
* @param int $post_id : entry ID.
* @return string List of button links
*/
public static function show_categories( $post_id ) {
$categories = self::get_terms_list( $post_id );
if ( ! empty( $categories ) ) {
$out = '';
foreach ( $categories as $category ) {
$category_link = get_term_link( $category, 'category' );
$out .= Components::button( $category->name, $category_link, 'tiny', '' );
}
return $out;
}
}
/**
* Return a list of button links to each tag of a certain post
*
* @param int $post_id : entry ID.
* @return string List of button links
*/
public static function show_tags( $post_id ) {
$tags = self::get_terms_list( $post_id, 'post_tag' );
if ( ! empty( $tags ) ) {
$out = '';
foreach ( $tags as $tag ) {
$tag_link = get_term_link( $tag, 'post_tag' );
$out .= Components::button( $tag->name, $tag_link, 'tag', '' );
}
return $out;
}
}
public static function get_highlighted_posts( $current_object, $size = 10, $category = 'highlight' ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $size,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $current_object->slug, $category ),
),
),
);
$query = new WP_Query( $args );
return $query;
}
/**
* Return a share url with entry url parameters to share in social media
*
* @param string $which : Which social media facebook|twitter|linkedin.
* @param int $post_id : entry ID.
* @return string url with parameters to share.
*/
public static function social_share( $which, $post_id ) {
if ( ! empty( $post_id ) ) {
$url = get_permalink( $post_id );
$title = get_the_title( $post_id );
switch ( $which ) {
case 'facebook':
return 'https://www.facebook.com/sharer.php?u=' . urlencode( $url );
break;
case 'linkedin':
return 'https://www.linkedin.com/sharing/share-offsite/?url=' . urlencode( $url );
break;
case 'twitter':
return 'https://twitter.com/intent/tweet?via=creativecommons&text=' . urlencode( $title ) . '&url=' . urlencode( $url );
break;
default:
return '';
break;
}
}
}
/**
* Current page title
* Display the title of the current page checking the context
*
* @return string current page title
*/
static function page_title() {
$get = get_queried_object();
if ( is_post_type_archive() ) {
return 'Archive: ' . $get->labels->name;
} elseif ( is_category() ) {
return 'Category ' . $get->name;
} elseif ( is_tag() ) {
return 'Tag "' . $get->name . '"';
} elseif ( is_tax() ) {
return 'Archive ' . $get->name;
} if ( is_search() ) {
return 'Search for: “' . get_search_query() . '”';
} elseif ( is_404() ) {
return 'Content not found';
} elseif ( is_author() ) {
return 'Author: ' . $get->display_name;
} elseif ( is_home() ) {
return 'Blog';
} else {
the_title();
}
}
/**
* Set the default website logo and allows to filter it in child themes
*
* @return void
*/
public static function get_current_website_logo() {
$default_logo = 'cc/logomark.svg#logomark';
$current_main_logo = apply_filters( 'cc_theme_base_set_default_logo', $default_logo );
return Components::cc_logos( $current_main_logo, false );
}
/**
* Get the top parent page of the current navigation level
*
* @return int $post_id : top level parent page
*/
public static function get_parent_page() {
global $post;
if ( $post->post_parent ) {
$ancestors = get_post_ancestors( $post->ID );
$root = count( $ancestors ) - 1;
return $ancestors[ $root ];
} else {
return $post->ID;
}
}
public static function get_date_format() {
$date_format = apply_filters( 'cc_theme_base_date_format', get_option( 'date_format' ) );
return $date_format;
}
}