| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'init', 'ebook_create_post_type_series', 98); |
| 4 |
add_action('save_post', 'ebook_store_save_custom_meta_data_graduation'); |
| 5 |
add_action('ebook_store_extend_options','ebook_store_graduation_add_options'); |
| 6 |
add_action('ebook_settings_page_extend','ebook_store_graduation_options'); |
| 7 |
|
| 8 |
|
| 9 |
function ebook_create_post_type_series() { |
| 10 |
$labels = array( |
| 11 |
'name' => 'Series', |
| 12 |
'singular_name' => 'Series', |
| 13 |
'add_new' => 'Add New', |
| 14 |
'add_new_item' => 'Add New Series', |
| 15 |
'edit_item' => 'Edit Series', |
| 16 |
'new_item' => 'New Series', |
| 17 |
'all_items' => 'Series', |
| 18 |
'view_item' => 'View Series', |
| 19 |
'search_items' => 'Search Series', |
| 20 |
'not_found' => 'No Series found', |
| 21 |
'not_found_in_trash' => 'No Series found in Trash', |
| 22 |
'parent_item_colon' => '', |
| 23 |
'menu_name' => 'Series', |
| 24 |
|
| 25 |
); |
| 26 |
|
| 27 |
$args = array( |
| 28 |
'labels' => $labels, |
| 29 |
'public' => true, |
| 30 |
'publicly_queryable' => true, |
| 31 |
'show_ui' => true, |
| 32 |
'show_in_menu' => 'edit.php?post_type=ebook', |
| 33 |
'query_var' => true, |
| 34 |
'rewrite' => array( 'slug' => 'series' ), |
| 35 |
'capability_type' => 'post', |
| 36 |
'has_archive' => true, |
| 37 |
'hierarchical' => false, |
| 38 |
//'menu_position' => 5, |
| 39 |
'supports' => array( 'title', 'thumbnail','comments' ) |
| 40 |
); |
| 41 |
|
| 42 |
register_post_type( 'ebook_series', $args ); |
| 43 |
} |
| 44 |
//add_filter('ebook_store_form_extend','ebook_store_graduation_dates', 9); |
| 45 |
add_filter('ebook_store_form_extend','ebook_store_grad_date_and_series', 10); |
| 46 |
|
| 47 |
function ebook_store_graduation_dates($post_id) { |
| 48 |
$month = get_post_meta($post_id, 'ebook_store_graduation_date_month', true); |
| 49 |
$year = get_post_meta($post_id, 'ebook_store_graduation_date_year', true); |
| 50 |
$day = get_post_meta($post_id, 'ebook_store_graduation_date_day', true); |
| 51 |
|
| 52 |
$out .= 'Graduation date:<br />'; |
| 53 |
$out .= 'Month: '; |
| 54 |
$out .= '<select name="ebook_store_graduation_date_month"> '; |
| 55 |
for ($i=0; $i<12; $i++) { |
| 56 |
$k = $i+1; |
| 57 |
$out .= '<option' . ($k == $month ? ' selected="selected"' : '') . ' value="' . $k . '">'. $k . '</option>'; |
| 58 |
} |
| 59 |
$out .= '</select>'; |
| 60 |
|
| 61 |
$out .= 'Day: '; |
| 62 |
$out .= '<select name="ebook_store_graduation_date_day"> '; |
| 63 |
for ($i=0; $i<31; $i++) { |
| 64 |
$k = $i+1; |
| 65 |
$out .= '<option' . ($k == $day ? ' selected="selected"' : '') . ' value="' . $k . '">'. $k . '</option>'; |
| 66 |
} |
| 67 |
$out .= '</select>'; |
| 68 |
|
| 69 |
$out .= 'Year: '; |
| 70 |
$out .= '<select name="ebook_store_graduation_date_year"> '; |
| 71 |
for ($i=2015; $i<2100; $i++) { |
| 72 |
$k = $i+1; |
| 73 |
$out .= '<option' . ($k == $year ? ' selected="selected"' : '') . ' value="' . $k . '">'. $k . '</option>'; |
| 74 |
} |
| 75 |
$out .= '</select>'; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
$out .= '<br />'; |
| 81 |
return $out; |
| 82 |
} |
| 83 |
function ebook_store_series($post_id) { |
| 84 |
$args = array('post_type' => 'ebook_series'); |
| 85 |
$the_query = new WP_Query( $args ); |
| 86 |
$ebook_store_graduation_date_series = get_post_meta($post_id, 'ebook_store_graduation_date_series', true); |
| 87 |
// The Loop |
| 88 |
if ( $the_query->have_posts() ) { |
| 89 |
$out .= 'Series<br />'; |
| 90 |
$out .= '<select name="ebook_store_graduation_date_series">' ; |
| 91 |
while ( $the_query->have_posts() ) { |
| 92 |
$the_query->the_post(); |
| 93 |
$selected = ''; |
| 94 |
if ($ebook_store_graduation_date_series == get_the_title()) { |
| 95 |
$selected = ' selected'; |
| 96 |
} |
| 97 |
$out .= '<option' . $selected . '>' . get_the_title() . '</option>'; |
| 98 |
} |
| 99 |
$out .= '</select>'; |
| 100 |
} |
| 101 |
$out .= '<br />'; |
| 102 |
return $out; |
| 103 |
} |
| 104 |
function ebook_store_grad_date_and_series($post_id) { |
| 105 |
$post_id = $_GET['post']; |
| 106 |
$out .= ebook_store_series($post_id); |
| 107 |
$out .= ebook_store_graduation_dates($post_id); |
| 108 |
return $out; |
| 109 |
} |
| 110 |
function ebook_store_save_custom_meta_data_graduation($post_id) { |
| 111 |
update_post_meta($post_id, 'ebook_store_graduation_date_day', $_POST['ebook_store_graduation_date_day']); |
| 112 |
update_post_meta($post_id, 'ebook_store_graduation_date_month', $_POST['ebook_store_graduation_date_month']); |
| 113 |
update_post_meta($post_id, 'ebook_store_graduation_date_year', $_POST['ebook_store_graduation_date_year']); |
| 114 |
update_post_meta($post_id, 'ebook_store_graduation_date_series', $_POST['ebook_store_graduation_date_series']); |
| 115 |
// |
| 116 |
} |
| 117 |
add_shortcode('ebook_store_graduation_form', 'ebook_store_graduation_form'); |
| 118 |
function ebook_store_graduation_form() { |
| 119 |
// if (is_user_logged_in() == false) { |
| 120 |
// return 'You need to be logged in the site in order to be able to download the yearbook. Please use <a href="' . wp_login_url() . '">this</a> link to login.'; |
| 121 |
// } |
| 122 |
$args = array('post_type' => 'ebook_series'); |
| 123 |
$the_query = new WP_Query( $args ); |
| 124 |
|
| 125 |
if ( $the_query->have_posts() ) { |
| 126 |
while ( $the_query->have_posts() ) { |
| 127 |
$the_query->the_post(); |
| 128 |
$selected = ''; |
| 129 |
if ($ebook_store_graduation_date_series == get_the_title()) { |
| 130 |
$selected = ' selected'; |
| 131 |
} |
| 132 |
$series_options .= '<option' . $selected . '>' . get_the_title() . '</option>'; |
| 133 |
} |
| 134 |
$series_options .= '</select>'; |
| 135 |
} |
| 136 |
$current_user = wp_get_current_user(); |
| 137 |
|
| 138 |
for ($i=1;$i<32; $i++) @$day_options .= '<option value="' . $i . '">' . $i . '</option>'; |
| 139 |
for ($i=2016;$i<2030; $i++) @$year_options .= '<option value="' . $i . '">' . $i . '</option>'; |
| 140 |
return '<form class="form-horizontal" method="post" action=""> |
| 141 |
<input type="hidden" name="task" value="grad_submit" /> |
| 142 |
<fieldset> |
| 143 |
|
| 144 |
<!-- Form Name --> |
| 145 |
<legend>Enter your graduation information</legend> |
| 146 |
|
| 147 |
<!-- Select Basic --> |
| 148 |
<div class="form-group"> |
| 149 |
<label class="col-md-4 control-label" for="day">Day</label> |
| 150 |
<div class="col-md-4"> |
| 151 |
<select id="day" name="day" class="form-control"> |
| 152 |
' . $day_options . ' |
| 153 |
</select> |
| 154 |
</div> |
| 155 |
</div> |
| 156 |
|
| 157 |
<!-- Select Basic --> |
| 158 |
<div class="form-group"> |
| 159 |
<label class="col-md-4 control-label" for="month">Month</label> |
| 160 |
<div class="col-md-4"> |
| 161 |
<select id="month" name="month" class="form-control"> |
| 162 |
<option value="1">January</option> |
| 163 |
<option value="2">February</option> |
| 164 |
<option value="3">March</option> |
| 165 |
<option value="4">April</option> |
| 166 |
<option value="5">May</option> |
| 167 |
<option value="6">June</option> |
| 168 |
<option value="7">July</option> |
| 169 |
<option value="8">August</option> |
| 170 |
<option value="9">September</option> |
| 171 |
<option value="10">October</option> |
| 172 |
<option value="11">November</option> |
| 173 |
<option value="12">December</option> |
| 174 |
</select> |
| 175 |
</div> |
| 176 |
</div> |
| 177 |
|
| 178 |
<!-- Select Basic --> |
| 179 |
<div class="form-group"> |
| 180 |
<label class="col-md-4 control-label" for="year">Year</label> |
| 181 |
<div class="col-md-4"> |
| 182 |
<select id="year" name="year" class="form-control"> |
| 183 |
' . $year_options . ' |
| 184 |
</select> |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
|
| 188 |
<!-- Select Basic --> |
| 189 |
<div class="form-group"> |
| 190 |
<label class="col-md-4 control-label" for="series">Series</label> |
| 191 |
<div class="col-md-4"> |
| 192 |
<select id="series" name="series" class="form-control"> |
| 193 |
' . $series_options . ' |
| 194 |
</select> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
<!-- Text Basic --> |
| 198 |
<div class="form-group"> |
| 199 |
<label class="col-md-4 control-label" for="series">Personal Code</label> |
| 200 |
<div class="col-md-4"> |
| 201 |
<input type"text" name="code" class="form-control" value="" /> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
<!-- Text Basic --> |
| 205 |
<div class="form-group"> |
| 206 |
<label class="col-md-4 control-label" for="series">Email</label> |
| 207 |
<div class="col-md-4"> |
| 208 |
<input type="email" name="email" class="form-control" value="' . $current_user->user_email . '" /> |
| 209 |
</div> |
| 210 |
</div> |
| 211 |
|
| 212 |
<!-- Button --> |
| 213 |
<div class="form-group"> |
| 214 |
<label class="col-md-4 control-label" for="submit">Submit and Download</label> |
| 215 |
<div class="col-md-4"> |
| 216 |
<button id="submit" name="submit" class="btn btn-primary">Submit</button> |
| 217 |
</div> |
| 218 |
</div> |
| 219 |
|
| 220 |
</fieldset> |
| 221 |
</form> |
| 222 |
'; |
| 223 |
} |
| 224 |
add_action('init', 'grad_submit'); |
| 225 |
function grad_submit() { |
| 226 |
if (@$_POST['task'] == 'grad_submit') { |
| 227 |
$args = array('post_type' => 'ebook', |
| 228 |
'meta_query' => array( |
| 229 |
array( |
| 230 |
'key' => 'ebook_store_graduation_date_series', |
| 231 |
'value' => $_POST['series'], |
| 232 |
'compare' => '=', |
| 233 |
), |
| 234 |
array( |
| 235 |
'key' => 'ebook_store_graduation_date_year', |
| 236 |
'value' => $_POST['year'], |
| 237 |
'compare' => '=', |
| 238 |
), |
| 239 |
array( |
| 240 |
'key' => 'ebook_store_graduation_date_month', |
| 241 |
'value' => $_POST['month'], |
| 242 |
'compare' => '=', |
| 243 |
), |
| 244 |
array( |
| 245 |
'key' => 'ebook_store_graduation_date_day', |
| 246 |
'value' => $_POST['day'], |
| 247 |
'compare' => '=', |
| 248 |
), |
| 249 |
), |
| 250 |
); |
| 251 |
if (get_option('graduation_notification_email')) { |
| 252 |
$graduation_notification_email = esc_attr(get_option('graduation_notification_email')); |
| 253 |
$mail_text = esc_attr(get_option('graduation_notification_email_text')); |
| 254 |
foreach ($_POST as $pk => $pv) { |
| 255 |
$search[] = '%%' . $pk . '%%'; |
| 256 |
$replace[] = $pv; |
| 257 |
} |
| 258 |
$mail_text = str_replace($search, $replace, $mail_text); |
| 259 |
} |
| 260 |
$the_query = new WP_Query( $args ); |
| 261 |
if ( $the_query->have_posts() ) { |
| 262 |
wp_mail($graduation_notification_email, 'Notification for new download',$mail_text); |
| 263 |
|
| 264 |
while ( $the_query->have_posts() ) { |
| 265 |
//wp_die('post found!'); |
| 266 |
$the_query->the_post(); |
| 267 |
$current_user = wp_get_current_user(); |
| 268 |
$checkoutPage = esc_attr(get_option('ebook_store_checkout_page')); |
| 269 |
$order_id = ebook_store_add_order(array( |
| 270 |
'mc_gross' => 0, |
| 271 |
'payer_email' => $current_user->user_email, |
| 272 |
'first_name' => $current_user->user_firstname, |
| 273 |
'last_name' => $current_user->user_lastname, |
| 274 |
'ebook' => get_the_ID(), |
| 275 |
), true); |
| 276 |
$link = add_query_arg(array('ebook_key' => get_post_meta($order_id, 'ebook_key',true), 'action' => 'thank_you'),get_permalink($checkoutPage)); |
| 277 |
|
| 278 |
header("Location: $link"); |
| 279 |
die($link); |
| 280 |
// die(); |
| 281 |
// wp_reset_query(); |
| 282 |
return true; |
| 283 |
} |
| 284 |
} |
| 285 |
wp_die('No yearbook has been found for the selected graduation date and series, please check your information and try again.'); |
| 286 |
} |
| 287 |
} |
| 288 |
function ebook_store_graduation_add_options() { |
| 289 |
register_setting( 'ebook-settings-group', 'graduation_notification_email' ); |
| 290 |
register_setting( 'ebook-settings-group', 'graduation_notification_email_text' ); |
| 291 |
//wp_die('action started'); |
| 292 |
} |
| 293 |
|
| 294 |
function ebook_store_graduation_options() { |
| 295 |
?> |
| 296 |
<tr valign="top"> |
| 297 |
<th scope="row">Graduation notification email</th> |
| 298 |
<td><input type="text" name="graduation_notification_email" style="width:250px;" value="<?php echo get_option('graduation_notification_email',$op->graduation_notification_email); ?>" placeholder="andys@gmail.com" /></td> |
| 299 |
</tr> |
| 300 |
<tr valign="top"> |
| 301 |
<th scope="row">Notification email content</th> |
| 302 |
<td> |
| 303 |
<?php |
| 304 |
$editor_id = 'graduation_notification_email_text'; |
| 305 |
wp_editor( get_option('graduation_notification_email_text',$op->graduation_notification_email_text), $editor_id ); |
| 306 |
?></td> |
| 307 |
</tr> <?php |
| 308 |
} |