-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_forms.php
More file actions
96 lines (73 loc) · 3.22 KB
/
Copy path_forms.php
File metadata and controls
96 lines (73 loc) · 3.22 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
<?php
// The poorest reccomended security I ever saw. But let it be here.
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
* Form handlers and related logic.
*/
function simple_lineup__create_new_showboxes($post_id, $post, $wpdb){
if(
!(
// All key must be present in order to update.
array_key_exists('simple_lineup__start', $_POST) &&
array_key_exists('simple_lineup__duration', $_POST) &&
array_key_exists('simple_lineup__show_on_program_page', $_POST)
)
){return;}
foreach($_POST['simple_lineup__start'] as $key => $value){
// If any show is valid, it must have synced keys with
// `simple_lineup__duration`, `simple_lineup__show_on_program_page.`
if(
!(
array_key_exists($key, $_POST['simple_lineup__duration']) &&
array_key_exists($key, $_POST['simple_lineup__show_on_program_page'])
)
){
error_log('Sheduling keys do not correspond when creating new show record.');
continue;
}
$update_data = [
'start' => $_POST['simple_lineup__start'][$key],
'duration' => $_POST['simple_lineup__duration'][$key],
'show_on_program_page' => $_POST['simple_lineup__show_on_program_page'][$key],
'node_id' => $post_id,
'node_type' => $post->post_type
];
$wpdb->insert(simple_lineup__get_db_table_name(), $update_data);
}
}
function simple_lineup__delete_unused_shoboxes($post_id, $post, $wpdb){
$db_table = simple_lineup__get_db_table_name();
$prepared_sql = $wpdb->prepare(
"SELECT id FROM {$db_table} WHERE node_id = %d AND node_type = %d", $post_id, $post->post_type
);
$old_ids = $wpdb->get_col($prepared_sql);
// If there are any old IDs, they must be also sent fron frontend.
// If not, delete all old IDs which are not sent back from user's request.
if(empty($old_ids)){
return;
}
$ids_to_keep = $_POST['simple_lineup__id'];
$sql_delete_statement = "DELETE FROM {$db_table} WHERE";
if(!empty($ids_to_keep)){
$sql_ids = implode(', ', $ids_to_keep);
$sql_delete_statement .= " id NOT IN ({$sql_ids}) AND";
}
$sql_delete_statement .= " node_id = %d AND node_type = %s;";
$prepared_sql = $wpdb->prepare($sql_delete_statement, $post_id, $post->post_type);
$old_ids = $wpdb->query($prepared_sql);
}
function simple_lineup__set_post_scheduling($post_id, $post, $update){
global $wpdb;
if(empty($_POST)){
// checking empty $_POST is a must. For some wierd reason, wordpress
// makes double saving. This prevents for accidental show removal.
return;
}
$sheduled_content_types = json_decode(get_option(SIMPLE_LINEUP__CONTENT_TYPES_FIELD));
if((wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) || !in_array($post->post_type, $sheduled_content_types)){
// Save sheduling only in case, content_type is configured.
}
simple_lineup__delete_unused_shoboxes($post_id, $post, $wpdb);
simple_lineup__create_new_showboxes($post_id, $post, $wpdb);
}
add_action('post_updated', 'simple_lineup__set_post_scheduling', 1, 4);