/
amazin-product-box.php
142 lines (125 loc) · 5.28 KB
/
amazin-product-box.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
<?php
/**
* Plugin Name: Amazin' Product Box
* Plugin URI: http://majoh.dev
* Description: Customizable product box for Amazon products with an affiliate link
* Version: 1.0
* Author: Mandi Grant
* Author URI: http://majoh.dev
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
if ( is_admin() ){ // admin actions
add_action( 'admin_menu', 'amazin_plugin_menu' );
} else {
// non-admin enqueues, actions, and filters
}
function amazin_plugin_menu() {
add_menu_page( 'Amazin\' Product Box Management', 'Amazin\' Product Box', 'manage_options', 'amazin-product-box', 'amazin_display_management_page' );
}
function amazin_display_management_page() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
amazin_render_form();
amazin_render_table();
}
function amazin_render_form() {
?>
<h2>Amazin' Product Box</h2>
<p>Create, edit, delete and manage your product boxes here.</p>
<div class="form-wrap">
<form action="<?php echo esc_url( post_new_product_box() ); ?>" method="post">
<?php wp_nonce_field( 'nonce_action', 'amazin_nonce_field', true, true ); ?>
<!-- product box name -->
<div class="form-field">
<label for="product-box-name">Product Box name</label>
<input type="text" id="product-box-name" name="amazin-product-box-name" placeholder="Give this product box a useful name"/>
</div>
<!-- product name -->
<div class="form-field">
<label for="product-name">Product Name</label>
<input type="text" id="product-name" name="amazin-product-name" placeholder="Enter the product name here"/>
</div>
<!-- product tagline -->
<div class="form-field">
<label for="product-tagline">Product Tagline</label>
<input type="text" id="product-tagline" name="amazin-product-tagline" placeholder="Write a few words summarizing this product"/>
</div>
<!-- product description -->
<div class="form-field">
<label for="product-description">Product Description</label>
<input type="text" id="product-description" name="amazin-product-description" placeholder="Write about 100 characters explaining why this product is great."/>
</div>
<!-- product URL -->
<div class="form-field">
<label for="product-url">Affiliate link</label>
<input type="text" id="product-url" name="amazin-product-url" placeholder="http://amazon.com/affiliate-link-here"/>
</div>
<!-- Button text -->
<div class="form-field">
<label for="product-button-text">Button text</label>
<input type="text" id="product-button-text" name="amazin-product-button-text" placeholder="See XYZ product on Amazon.com"/>
</div>
<input type="submit" name="submit"/>
</form>
</div>
<?php
return;
}
function amazin_render_table() {
?>
<table>
<thead>
<tr>
<th>Shortcode</th>
<th>Product Name</th>
<th>Author</th>
<th>Last Modified</th>
<th>Manage</th>
</tr>
</thead>
<tbody>
<tr>
<!-- for loop through saved boxes -->
<td>[shortcode here]</td>
<td>Sample Name</td>
<td>Sample Author</td>
<td>Sample Date</td>
<td><button>Edit</button> <button>Delete</button></td>
</tr>
</tbody>
</table>
<?php
return;
}
function post_new_product_box() {
if ( isset( $_POST['submit'] ) ) {
//check nonce
if (! isset ( $_POST['amazin_nonce_field'] ) || ! wp_verify_nonce( $_POST['amazin_nonce_field'], 'nonce_action' ) ) {
print 'Sorry, your nonce did not verify.';
exit;
} else {
//process form data
// retrieve the form data by using the element's name attributes
// value as key $firstname = $_GET['firstname']; $lastname = $_GET['lastname'];
// display the results echo '<h3>Form GET Method</h3>'; echo 'Your name is ' . $lastname . ' ' . $firstname; exit;
$content = array(
"amazin-product-name" => $_POST['amazin-product-name'],
"amazin-product-tagline" => $_POST['amazin-product-tagline'],
"amazin-product-description" => $_POST['amazin-product-description'],
"amazin-product-url" => $_POST['amazin-product-url'],
"amazin-product-button-text" => $_POST['amazin-product-button-text']
);
$product_box = array(
'post_title' => $_REQUEST['amazin-product-box-name'],
'post_content' => wp_json_encode($content), //broke when switched this from 'none' to the content array
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database.
wp_insert_post( $product_box );
}
}
}
?>