-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacyt-settings.class.php
186 lines (155 loc) · 4.51 KB
/
acyt-settings.class.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
class ACYTSettingsPage {
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'update_option', array( $this, 'acyt_validate_channelid' ) );
}
/**
* Add options page
*/
public function add_plugin_page() {
// This page will be under "Settings"
add_options_page(
'ACYT Settings',
'AC YouTube Settings',
'manage_options',
'acyt-settings',
array( $this, 'create_admin_page' ) );
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property
$this->options = get_option( 'acyt-yt-id' );
// submit_button("text", "secondary","fetch-yt");
$htmloutput = '<div class="wrap"><h1>ACYT Settings</h1><form method="post" action="options.php">';
echo $htmloutput;
// This prints out all hidden setting fields
settings_fields( 'acyt-settings' );
do_settings_sections( 'acyt-setting-admin' );
submit_button();
$closehtml = '</form></div>';
echo $closehtml;
}
/**
* Register and add settings
*/
public function page_init() {
register_setting(
// Option group
'acyt-settings',
// Option name
'acyt-yt-id',
// Sanitize
array( $this, 'sanitize' ) );
add_settings_section(
'acyt_setting_section_id',
'Settings voor ACYT Plugin',
array( $this, 'print_section_info' ),
'acyt-setting-admin' );
add_settings_field(
'acyt-yt-id',
'YouTube ID',
array( $this, 'title_callback' ),
'acyt-setting-admin',
'acyt_setting_section_id' );
}
/**
* Sanitize each setting field as needed
*
* @param array $input
* Contains all settings fields as array keys
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['acyt-yt-id'] ) ) {
$new_input['acyt-yt-id'] = sanitize_text_field( $input['acyt-yt-id'] );
}
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info() {
print 'Zoek ff het ID van je kanaal:';
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback() {
printf(
'<input type="text" id="acyt-yt-id" name="acyt-yt-id[acyt-yt-id]" value="%s" />',
isset( $this->options['acyt-yt-id'] ) ? esc_attr( $this->options['acyt-yt-id'] ) : '' );
}
function acyt_validate_channelid() {
$channelid = sanitize_text_field( $_POST['acyt-yt-id']['acyt-yt-id'] );
if ( isset( $channelid ) ) {
$channelid = esc_attr( $channelid );
$url = 'https://www.youtube.com/channel/' . $channelid;
// var_dump( $url );
$response = wp_remote_get( $url );
if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$status = $response['response']['code']; // array of http header lines
// var_dump($status);
if ( $status == '200' ) {
$feed = fetch_feed( $url );
$items = $feed->get_items();
global $wpdb;
$paginaIds = $wpdb->get_col("SELECT pm.meta_value AS youtube_id FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_ID
WHERE p.post_type = 'youtube' AND pm.meta_key = '_acyt-yt-videoid'");
foreach ( $items as $item ) {
$titel = $item->get_title();
$videoId = '';
$text = '';
$thumbUrl = '';
$local_date = $item->get_local_date( "%F %T" );
if ( preg_match( '![?&]{1}v=([^&]+)!', $item->get_permalink() . '&', $m2 ) ) {
$videoId = $m2[1];
}
$enclosure = $item->get_enclosure();
if ( $enclosure ) {
$thumbUrl = $enclosure->get_thumbnail();
$text = $enclosure->get_description();
}
// var_dump( $titel );
// var_dump( $videoId );
// var_dump( $thumbUrl );
// var_dump( $text );
// var_dump($local_date);
//
// var_dump( "--------- Next! ---------" );
if(in_array($videoId, $paginaIds))
{
continue;
}
$postid = wp_insert_post(
array(
'post_title' => $titel,
'post_date' => $local_date,
'post_type' => 'youtube',
'post_status' => 'published',
'post_content' => $text,
'meta_input' => array( '_acyt-yt-videoid' => $videoId )
) );
// gelijk post updaten zodat het wel een draft is, maar dan in ieder geval de date goed staat
wp_update_post(
array(
'ID' => $postid,
'post_status' => 'draft'
) );
}
}
}
}
}
}