PluginProbe
Page & Post Notes / trunk
Page & Post Notes vtrunk
trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5
page-post-notes / include / insert-to-db.php

insert-to-db.php in Page & Post Notes trunk, at include/insert-to-db.php

116 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
2 <?php
3
4 if( isset($_POST['yydev_notes_nonce']) ) {
5
6 if( wp_verify_nonce($_POST['yydev_notes_nonce'], 'yydev_notes_action') ) {
7
8 // Add capability check - user must be able to edit posts
9 if ( ! current_user_can( 'edit_posts' ) ) {
10 return;
11 }
12
13 include('settings.php');
14
15 // ====================================================
16 // Checking if this is a category/tags post or regular page/post
17 // ====================================================
18
19 if( isset($_POST['yydev_notes_page_id']) ) {
20 $page_id = intval($_POST['yydev_notes_page_id']);
21
22 // Additional check: verify user can edit this specific post
23 if ( $page_id > 0 && ! current_user_can( 'edit_post', $page_id ) ) {
24 return;
25 }
26 } // if( isset($_POST['yydev_notes_page_id']) ) {
27
28 // ====================================================
29 // Add seo data to the database
30 // ====================================================
31
32 if( isset($page_id) ) {
33
34 // Checking if the data id exists
35 global $wpdb;
36 $check_database_exists = $wpdb->query("SELECT * FROM " . $table_name . " WHERE page_post_id = $page_id ");
37
38 // sanitize the data
39 $class_direction = $_POST['yydev_direction_class'];
40 $table_notes_post = $_POST['yydev_notes'];
41
42 $notes_count = count($table_notes_post);
43 $table_notes_data = '';
44 $table_notes_comain = '';
45
46 // converting the data and combine it
47 for ($i = 0; $i <= $notes_count; $i++) {
48
49 if( isset($table_notes_post[$i]) ) {
50
51 $table_notes_data_note = $table_notes_post[$i];
52 $table_notes_data_direction = $class_direction[$i];
53 $table_notes_comain .= $table_notes_data_note;
54
55 if( !empty($table_notes_data_note) ) {
56
57 if($i > 0) {
58 $table_notes_data .= "###";
59 } // if($i > 0) {
60
61 $table_notes_data .= $table_notes_data_note . "^^" . $table_notes_data_direction;
62 } // if( !empty($table_notes_data_note) ) {
63
64 } // if( isset($table_notes_post[$i]) ) {
65
66 } // for ($i = 0; $i <= $notes_count; $i++) {
67
68 $table_notes = sanitize_textarea_field($table_notes_data);
69 $notes_id = intval($_POST['yydev_notes_id']);
70
71 // checking if the notes are empty or not
72 if( empty($table_notes_comain) ) {
73 $there_is_data_here = 1;
74 } // if( empty($table_notes_comain) ) {
75
76 // adding the data into the database
77 if( $check_database_exists == 0 ) {
78
79 // if the post doesn't exists in the database we will
80 // make sure there is data and then we will insert it
81 if( !isset($there_is_data_here) ) {
82
83 // if the data not exists on the database insert new one
84 $wpdb->insert( $table_name,
85 array('page_post_id'=>$page_id,
86 'notes'=>$table_notes,
87 ), array('%d', '%s') );
88
89 } // if( !isset($there_is_data_here) ) {
90
91 } else { // if( $check_database_exists == 0 ) {
92
93 // If the current page data exists in the database we will update it
94 if( !isset($there_is_data_here) ) {
95
96 $wpdb->update( $table_name,
97 array('page_post_id'=>$page_id,
98 'notes'=>$table_notes,
99 ), array('page_post_id'=>$page_id), array('%d', '%s') );
100
101 } else { // if( !isset($there_is_data_here) ) {
102
103 // if there are no data at all (in notes and date as well) we will remove the line from the database
104 $wpdb->delete( $table_name, array('id'=>$notes_id) );
105
106 } // if( !isset($there_is_data_here) ) {
107
108 } // } else { // if($check_database_exists == 0 ) {
109
110 } // if( isset($page_id) ) {
111
112 } // if( wp_verify_nonce($_POST['yydev_notes_nonce'], 'yydev_notes_action') ) {
113
114 } // if( isset($_POST['yydev_notes_nonce']) ) {
115
116 ?>