-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiya-poll.php
93 lines (83 loc) · 3.61 KB
/
piya-poll.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
<?php
/*
Plugin Name: Piya Poll
Plugin URI: http://www.iampiya.com
Description: esey to make poll for post
Author: Nattawud Sinprasert (PiYA)
Author URI: http://www.iampiya.com
Version: 0.0.1
tags: poll, survey
*/
// Initialize the metabox class
add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
if ( file_exists( dirname( __FILE__ ) . '/lib/cmb2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/lib/cmb2/init.php';
} elseif ( file_exists( dirname( __FILE__ ) . '/lib/CMB2/init.php' ) ) {
require_once dirname( __FILE__ ) . '/lib/CMB2/init.php';
}
if ( !class_exists( 'poll' ) ) {
require_once( 'models/poll.php' );
}
}
add_filter( 'cmb2_init', 'be_sample_metaboxes' );
function be_sample_metaboxes( $meta_boxes ) {
$prefix = 'piya_'; // Prefix for all fields
$poll_metabox = new_cmb2_box( array(
'id' => $prefix . 'poll_metabox',
'title' => __( 'Poll (iampiya.com)', 'cmb2' ),
'object_types' => array( 'post', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // true to keep the metabox closed by default
) );
$poll_metabox->add_field( array(
'name' => __( 'Poll ID', 'cmb2' ),
// 'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'poll_id',
'type' => 'hidden',
// 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
// 'on_front' => false, // Optionally designate a field to wp-admin only
// 'repeatable' => true,
) );
$poll_metabox->add_field( array(
'name' => __( 'Topic', 'cmb2' ),
// 'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'topic',
'type' => 'text',
// 'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
// 'on_front' => false, // Optionally designate a field to wp-admin only
// 'repeatable' => true,
) );
$choice_group_id = $poll_metabox->add_field( array(
'id' => $prefix . 'choice_group',
'type' => 'group',
// 'description' => __( 'Generates reusable form entries', 'cmb2' ),
'options' => array(
'group_title' => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Choice', 'cmb2' ),
'remove_button' => __( 'Remove Choice', 'cmb2' ),
'sortable' => true, // beta
),
) );
$poll_metabox->add_group_field( $choice_group_id, array(
'name' => __( 'Choice', 'cmb2' ),
'id' => 'choice',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
return $meta_boxes;
}
function prfx_meta_save( $post_id, $post, $update ) {
$poll = new Poll($_POST);
$response_object = json_decode( $poll->send_poll() );
$old_response_id = get_post_meta( $post_id, 'piya_poll_id', true );
update_post_meta( $post_id, 'piya_poll_id', $response_object->key_id, $old_response_id );
}
add_action( 'publish_post', 'prfx_meta_save', 10, 3 );