-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
282 lines (218 loc) · 7.93 KB
/
functions.php
File metadata and controls
282 lines (218 loc) · 7.93 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/*
Content-Type: application/json
{
"uid": "urn:uuid:1335c695-cfb8-4ebb-abbd-80da344efa6b",
"updateDate": "2016-05-23T22:34:51.0Z",
"titleText": "Amazon Developer Blog, week in review May 23rd",
"mainText": "",
"streamUrl": "https://developer.amazon.com/public/community/blog/myaudiofile.mp3",
"redirectionUrl": "https://developer.amazon.com/public/community/blog"
}
{
"uid": "urn:uuid:1335c695-cfb8-4ebb-abbd-80da344efa6b",
"updateDate": "2016-05-23T00:00:00.0Z",
"titleText": "Amazon Developer Blog, week in review May 23rd",
"mainText": "Meet Echosim. A new online community tool for developers that simulates the look and feel of an Amazon Echo.",
"redirectionUrl": "https://developer.amazon.com/public/community/blog"
}
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Invalid request.' );
}
if ( ! class_exists( 'Alexa_Reader' ) ) {
class Alexa_Reader {
/**
* Load the class instance.
*
* @var class Instance.
*/
private static $instance = null;
/**
* Set the plugin instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action( 'init', [ $this, 'create_channel_taxonomy' ], 0 );
add_action('admin_menu', [ $this, 'remove_sub_menus' ]);
add_filter( 'query_vars', [ $this, 'add_query_vars_filter' ] );
add_action( 'manage_posts_custom_column' , [ $this, 'custom_columns' ], 10, 2 );
add_action( 'manage_posts_columns' , [ $this, 'add_language_columns' ], 10, 2 );
add_filter('acf/settings/save_json', [ $this, 'acf_json_save_point' ] );
add_filter('acf/settings/load_json', [ $this, 'acf_json_load_point' ] );
}
/**
* Get recent news object.
*/
function get_recent_feed( $language = null, $size = 5 ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $size,
'orderby' => 'date',
'order' => 'DESC',
);
if ( ! empty( $language ) ) {
$args['meta_key'] = '';
$args['meta_value'] = $language;
}
$feeds = new WP_Query( $args );
return $feeds;
}
/**
* Get recent news array.
*/
function get_recent_feed_array( $language ) {
$feed_data = array();
$feeds = self::get_recent_feed( $language );
if ( $feeds->have_posts() ) {
while ( $feeds->have_posts() ) {
$feeds->the_post();
$item = array();
$item_id = wp_generate_uuid4();
$item_title = get_the_title();
$item_date = get_the_date('Y-m-d\TH:i:s');
$item_content = get_the_content();
$item_audio_type = get_field('audio_type');
$item_audio_file = get_field('audio_file');
$item_audio_url = get_field('audio_url');
$item_url = get_the_permalink();
$item_author = get_the_author();
$item_channel = wp_get_post_terms( get_the_ID(), 'channel', [ 'fields' => 'names' ] );
if ( 'en_US' === $language ) {
$by_line = 'By %1$s%2$s. %3$s';
$from_line = ' from %s';
} elseif ( 'pt_BR' === $language ) {
$by_line = 'Por %1$s%2$s. %3$s';
$from_line = ' de %s';
}
if ( is_array( $item_channel ) && ! empty( $item_channel ) ) {
$item_channel = sprintf( __( $from_line, 'wpread' ), $item_channel[0] );
} else {
$item_channel = '';
}
$item['uid'] = $item_id;
$item['updateDate'] = $item_date . '.0Z';
$item['titleText'] = $item_title;
$item['mainText'] = sprintf( __( $by_line, 'wpread' ), $item_author, $item_channel, $item_content);
if ( 'file' === $item_audio_type ) {
$item['streamUrl'] = $item_audio_file;
}
if ( 'url' === $item_audio_type ) {
$item['streamUrl'] = $item_audio_url;
}
$item['redirectionUrl'] = $item_url;
array_push( $feed_data, $item );
}
}
return $feed_data;
}
/**
* Get recent news json.
*/
function get_recent_feed_json( $language ) {
return json_encode( self::get_recent_feed_array( $language ) );
}
/**
* Create custom taxonomy for channels.
*/
function create_channel_taxonomy() {
$labels = array(
'name' => _x( 'Channels', 'channel general name' ),
'singular_name' => _x( 'Channel', 'channel singular name' ),
'search_items' => __( 'Search Channels' ),
'popular_items' => __( 'Popular Channels' ),
'all_items' => __( 'All Channels' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Channel' ),
'update_item' => __( 'Update Channel' ),
'add_new_item' => __( 'Add New Channel' ),
'new_item_name' => __( 'New Channel Name' ),
'separate_items_with_commas' => __( 'Separate channels with commas' ),
'add_or_remove_items' => __( 'Add or remove channels' ),
'choose_from_most_used' => __( 'Choose from the most used channels' ),
'menu_name' => __( 'Channels' ),
);
register_taxonomy( 'channel', 'post', array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'channel'
),
));
}
/**
* Remove default post category and tag.
*/
function remove_sub_menus() {
remove_submenu_page('edit.php', 'edit-tags.php?taxonomy=category');
remove_submenu_page('edit.php', 'edit-tags.php?taxonomy=post_tag');
}
/**
* Add query var for language.
*/
function add_query_vars_filter( $vars ){
$vars[] = "lang";
return $vars;
}
/**
* Update post list columns
*/
function add_language_columns( $columns ) {
return array(
'cb' => 'cb',
'title' => 'Title',
'author' => 'Author',
'channels' => 'Channels',
'language' => 'Language',
'date' => 'Date'
);
}
/**
* Validate custom columns value.
*/
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case 'language' :
$language = get_post_meta( $post_id, 'language', true );
echo $language;
break;
case 'channels' :
$channel = wp_get_post_terms( get_the_ID(), 'channel', [ 'fields' => 'all' ] );
if ( is_array( $channel ) && ! empty( $channel ) ) {
$channel = sprintf('<a href="%s">%s</a>', get_edit_term_link( $channel[0]->term_id ), $channel[0]->name );
} else {
$channel = '-';
}
echo $channel;
break;
}
}
/**
* Save ACF changes local
*/
function acf_json_save_point( $path ) {
$path = get_stylesheet_directory() . '/acf';
return $path;
}
/**
* Load ACF changes
*/
function acf_json_load_point( $paths ) {
unset($paths[0]);
$paths[] = get_stylesheet_directory() . '/acf';
return $paths;
}
}
Alexa_Reader::get_instance();
}