-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcreate-podcast.php
159 lines (135 loc) · 4.06 KB
/
create-podcast.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
<?php
/**
* Defines class to handle the validation, sanitization
* and creation of a podcast.
*
* @package tenup_podcasting
*/
namespace tenup_podcasting;
/**
* Provides methods to create a podcast.
*/
class Create_Podcast {
/**
* Name of the podcast.
*
* @var string
*/
protected $podcast_name = '';
/**
* Name of the podcast artist.
*
* @var string
*/
protected $podcast_talent_name = '';
/**
* Summary of the podcast.
*
* @var string
*/
protected $podcast_description = '';
/**
* Podcast's primary category.
*
* @var string
*/
protected $podcast_category = '';
/**
* ID of the podcast cover.
*
* @var int
*/
protected $podcast_cover_id = 0;
/**
* Verifies nonce needed for the creation of a podcast.
*
* @return boolean|WP_Error
*/
public function verify_nonce() {
$is_nonce_set = isset( $_POST['simple-podcasting-create-show-nonce-field'] );
if ( ! $is_nonce_set ) {
return false;
}
$is_nonce_verified = wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['simple-podcasting-create-show-nonce-field'] ) ), 'simple-podcasting-create-show-action' );
if ( ! $is_nonce_verified ) {
return new \WP_Error(
'simple_podcasting_nonce_verification_failed',
esc_html__( 'Nonce verification failed.' )
);
}
return true;
}
/**
* Sanitizes the podcast fields.
*/
public function sanitize_podcast_fields() {
$this->podcast_name = isset( $_POST['podcast-name'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-name'] ) ) : null;
$this->podcast_talent_name = isset( $_POST['podcast-artist'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-artist'] ) ) : null;
$this->podcast_description = isset( $_POST['podcast-description'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-description'] ) ) : null;
$this->podcast_category = isset( $_POST['podcast-category'] ) ? sanitize_text_field( wp_unslash( $_POST['podcast-category'] ) ) : null;
$this->podcast_cover_id = isset( $_POST['podcast-cover-image-id'] ) ? absint( wp_unslash( $_POST['podcast-cover-image-id'] ) ) : null;
}
/**
* Create a podcast and saves its corressponding meta.
*
* @return boolean|WP_Error
*/
public function save_podcast_fields() {
if ( empty( $this->podcast_name ) ) {
return new \WP_Error(
'simple_podcasting_podcast_name_empty',
esc_html__( 'A podcast name is required.' )
);
}
if ( empty( $this->podcast_talent_name ) ) {
return new \WP_Error(
'simple_podcasting_podcast_artist_name_empty',
esc_html__( 'A podcast artist name is required.' )
);
}
if ( empty( $this->podcast_description ) ) {
return new \WP_Error(
'simple_podcasting_podcast_summary_empty',
esc_html__( 'A podcast summary name is required.' )
);
}
if ( empty( $this->podcast_category ) ) {
return new \WP_Error(
'simple_podcasting_podcast_category_empty',
esc_html__( 'A podcast category is required.' )
);
}
if ( empty( $this->podcast_cover_id ) ) {
return new \WP_Error(
'simple_podcasting_podcast_cover_image_empty',
esc_html__( 'A podcast cover image is required.' )
);
}
$result = wp_insert_term(
$this->podcast_name,
PODCASTING_TAXONOMY_NAME
);
if ( is_wp_error( $result ) ) {
return $result;
}
/** Add podcast talent name. */
if ( $this->podcast_talent_name ) {
update_term_meta( $result['term_id'], 'podcasting_talent_name', $this->podcast_talent_name );
}
/** Add podcast summary. */
if ( $this->podcast_description ) {
update_term_meta( $result['term_id'], 'podcasting_summary', $this->podcast_description );
}
/** Add podcast category. */
if ( $this->podcast_category ) {
update_term_meta( $result['term_id'], 'podcasting_category_1', $this->podcast_category );
}
/** Add podcast cover ID and URL. */
if ( $this->podcast_cover_id ) {
$image_url = wp_get_attachment_url( (int) $this->podcast_cover_id );
update_term_meta( $result['term_id'], 'podcasting_image', $this->podcast_cover_id );
update_term_meta( $result['term_id'], 'podcasting_image_url', $image_url );
}
return true;
}
}