function wp_sweat_works_page() { global $wpdb; $draftname = sanit_title($_POST); $draftid = sanit_id($_POST); if ($_POST && $_POST['submit']) { if (!empty($draftname)) { switch ($_POST['action']) { // add case 'add': $wpdb->insert($wpdb->posts, array('post_title' => $draftname, 'post_status' => 'draft'), array('%s', '%s')); break; // edit // edit case 'edit': $wpdb->update($wpdb->posts, array('post_title' => $draftname), array('ID' => $draftid)); break; // delete // delete case 'delete': $wpdb->delete($wpdb->posts, array('ID' => $draftid)); break; default: // nothing break; } } } ob_start(); ?> <script type="text/javascript"> function edit(id, name) { document.getElementById('action').value = 'edit'; document.getElementById('draftid').value = id; document.getElementById('draftname').value = name; document.getElementById('submit').value = 'Edit'; document.getElementById('cancel').style.visibility = "visible"; } function dlte(id, name) { document.getElementById('action').value = 'delete'; document.getElementById('draftid').value = id; document.getElementById('draftname').value = name; document.getElementById('submit').value = 'Delete'; document.getElementById('cancel').style.visibility = "visible"; } function cancel(){ document.getElementById('action').value = 'add'; document.getElementById('draftid').value = ''; document.getElementById('draftname').value = ''; document.getElementById('submit').value = 'Save Changes'; document.getElementById('cancel').style.visibility = "hidden"; } </script> <div class="wrap"> <h1>WP Sweat Works - Quick Draft</h1> <form action="" method="POST"> <input type="hidden" value="add" name="action" id="action"> <input type="hidden" value="" name="draftid" id="draftid"> <table class="form-table"> <tbody> <tr> <th scope="row"> <label for="createdraft">Draft Title</label> </th> <td> <input type="text" name="draftname" id="draftname" value=""> <span id="cancel" style="visibility:hidden"> <a onclick="cancel();">X</a> </span> </td> </tr> </tbody> </table> <p class="submit"> <input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"> </p> </form> <?php $data = $wpdb->get_results("SELECT ID, post_title\n FROM {$wpdb->posts}\n WHERE post_status = 'draft'\n "); ?> <table class="widefat"> <thead> <tr> <th>ID</th> <th>Post Title</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($data as $row) { ?> <tr> <td><?php echo $row->ID; ?> </td> <td><?php echo $row->post_title; ?> </td> <td> <a id="edit" onclick="edit('<?php echo $row->ID; ?> ', '<?php echo $row->post_title; ?> ')"> Edit </a> | <a id="delete" onclick="dlte('<?php echo $row->ID; ?> ', '<?php echo $row->post_title; ?> ')"> Delete </a> </td> </tr> <?php } ?> </tbody> </table> </div> <?php echo ob_get_clean(); }
/** * @dataProvider testSanitIdProvider */ function test_sanit_id($a, $b) { $this->assertEquals(sanit_id($a), $b); }