Posts 2 Posts - Version 0.2

Version Description

  • added p2p_list_connected() template tag
  • UI that supports multiple related posts. props Patrik Bn
  • more info
Download this release

Release Info

Developer scribu
Plugin Icon wp plugin Posts 2 Posts
Version 0.2
Comparing to
See all releases

Code changes from version 0.1 to 0.2

admin.php DELETED
@@ -1,72 +0,0 @@
1
- <?php
2
-
3
- class P2P_Box {
4
-
5
- function init() {
6
- add_action('add_meta_boxes', array(__CLASS__, 'register'));
7
- add_action('save_post', array(__CLASS__, 'save'), 10, 2);
8
- }
9
-
10
- function save($post_a, $post) {
11
- if ( defined('DOING_AJAX') || defined('DOING_CRON') || empty($_POST) || 'revision' == $post->post_type )
12
- return;
13
-
14
- $connections = p2p_get_connected('from', $post->ID);
15
-
16
- foreach ( p2p_get_connection_types($post->post_type) as $post_type ) {
17
- if ( !isset($_POST['p2p'][$post_type]) )
18
- continue;
19
-
20
- foreach ( $connections[$post_type] as $post_b )
21
- p2p_disconnect($post_a, $post_b);
22
-
23
- if ( $post_b = absint($_POST['p2p'][$post_type]) )
24
- p2p_connect($post_a, $post_b);
25
- }
26
- }
27
-
28
- function register($post_type) {
29
- $connection_types = p2p_get_connection_types($post_type);
30
-
31
- if ( empty($connection_types) )
32
- return;
33
-
34
- add_meta_box('p2p-connections', __('Connections', 'p2p-textdomain'), array(__CLASS__, 'box'), $post_type, 'side');
35
- }
36
-
37
- function box($post) {
38
- $connections = p2p_get_connected('from', $post->ID);
39
-
40
- $out = '';
41
- foreach ( p2p_get_connection_types($post->post_type) as $post_type ) {
42
- $posts = self::get_post_list($post_type);
43
- $selected = reset(array_intersect((array) @$connections[$post_type], array_keys($posts)));
44
-
45
- $out .=
46
- html('li',
47
- get_post_type_object($post_type)->singular_label . ' '
48
- .scbForms::input(array(
49
- 'type' => 'select',
50
- 'name' => "p2p[$post_type]",
51
- 'values' => self::get_post_list($post_type),
52
- 'selected' => $selected,
53
- ))
54
- );
55
- }
56
-
57
- echo html('ul', $out);
58
- }
59
-
60
- private function get_post_list($post_type) {
61
- $args = array(
62
- 'post_type' => $post_type,
63
- 'post_status' => 'any',
64
- 'nopaging' => true,
65
- 'cache_results' => false,
66
- );
67
-
68
- return scbUtil::objects_to_assoc(get_posts($args), 'ID', 'post_title');
69
- }
70
- }
71
- P2P_Box::init();
72
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
admin/admin.js ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function($) {
2
+ var update_input = function($metabox) {
3
+ $metabox.find('.p2p_connected .howto').remove();
4
+
5
+ var ids = [];
6
+ $metabox.find('.p2p_connected input:checked').each(function() {
7
+ ids.push($(this).val());
8
+ });
9
+ $metabox.find('.p2p_connected_ids').val(ids.join(','));
10
+ };
11
+
12
+ $('.p2p_connected').delegate('input', 'change', function() {
13
+ update_input($(this).parents('.p2p_metabox'));
14
+ });
15
+
16
+ $('.p2p_results').delegate('a', 'click', function() {
17
+ var $self = $(this);
18
+ $metabox = $self.parents('.p2p_metabox'),
19
+ $list = $metabox.find('.p2p_connected');
20
+
21
+ if ( !$list.find('input[value=' + $self.attr('name') + ']').length ) {
22
+ $list
23
+ .append($('<li>')
24
+ .append($('<input>').attr({
25
+ 'type': 'checkbox',
26
+ 'checked': 'checked',
27
+ 'id': 'p2p_checkbox_' + $self.attr('name'),
28
+ 'value': $self.attr('name'),
29
+ 'autocomplete': 'off'
30
+ }))
31
+ .append($('<label>').attr({
32
+ 'for': 'p2p_checkbox_' + $self.attr('name')
33
+ }).html($self.html()))
34
+ );
35
+ }
36
+
37
+ update_input($metabox);
38
+
39
+ return false;
40
+ });
41
+
42
+ var delayed = undefined;
43
+
44
+ $('.p2p_search :text').keyup(function() {
45
+
46
+ if ( delayed != undefined )
47
+ clearTimeout(delayed);
48
+
49
+ var $self = $(this);
50
+ $metabox = $self.parents('.p2p_metabox'),
51
+ $results = $metabox.find('.p2p_results'),
52
+ post_type = $self.attr('name').split('_')[2],
53
+ old_value = '',
54
+ $spinner = $metabox.find('.waiting');
55
+
56
+ var delayed = setTimeout(function() {
57
+ if ( !$self.val().length ) {
58
+ $results.html('');
59
+ return;
60
+ }
61
+
62
+ if ( $self.val() == old_value )
63
+ return;
64
+ old_value = $self.val();
65
+
66
+ $spinner.show();
67
+ $.getJSON(ajaxurl, {action: 'p2p_search', q: $self.val(), post_type: post_type}, function(data) {
68
+ $spinner.hide();
69
+
70
+ $results.html('');
71
+
72
+ $.each(data, function(id, title) {
73
+ $results.append('<li><a href="#" name="' + id + '">' + title + '</a></li>');
74
+ });
75
+ });
76
+ }, 400);
77
+ });
78
+ });
79
+
admin/admin.php ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class P2P_Box {
4
+
5
+ function init( $file ) {
6
+ add_action( 'admin_print_styles-post.php', array( __CLASS__, 'scripts' ) );
7
+ add_action( 'admin_print_styles-post-new.php', array( __CLASS__, 'scripts' ) );
8
+
9
+ add_action( 'add_meta_boxes', array( __CLASS__, 'register' ) );
10
+
11
+ add_action( 'save_post', array( __CLASS__, 'save' ), 10, 2 );
12
+ add_action( 'wp_ajax_p2p_search', array( __CLASS__, 'ajax_search' ) );
13
+
14
+ scbUtil::add_uninstall_hook( $file, array( __CLASS__, 'uninstall' ) );
15
+ }
16
+
17
+ function scripts() {
18
+ wp_enqueue_script( 'p2p-admin-js', plugins_url( 'admin.js', __FILE__ ), array( 'jquery' ), '0.2', true );
19
+
20
+ ?>
21
+ <style type="text/css">
22
+ .p2p_connected {margin: 10px 4px}
23
+ .p2p_results {margin: -5px 6px 10px}
24
+ .p2p_metabox .waiting {vertical-align: -.4em}
25
+ </style>
26
+ <?php
27
+ }
28
+
29
+ function save( $post_a, $post ) {
30
+ if ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) || empty( $_POST ) || 'revision' == $post->post_type )
31
+ return;
32
+
33
+ $connections = p2p_get_connected( 'any', 'from', $post_a, true );
34
+
35
+ foreach ( p2p_get_connection_types( $post->post_type ) as $post_type ) {
36
+ if ( !isset( $_POST['p2p_connected_ids_' . $post_type] ) )
37
+ continue;
38
+
39
+ $old_connections = (array) $connections[ $post_type ];
40
+ $new_connections = explode( ',', $_POST[ 'p2p_connected_ids_' . $post_type ] );
41
+
42
+ foreach ( array_diff( $old_connections, $new_connections ) as $post_b )
43
+ p2p_disconnect( $post_a, $post_b );
44
+
45
+ foreach ( array_diff( $new_connections, $old_connections ) as $post_b )
46
+ p2p_connect( $post_a, $post_b );
47
+ }
48
+ }
49
+
50
+ function register( $post_type ) {
51
+ foreach ( p2p_get_connection_types( $post_type ) as $type ) {
52
+ add_meta_box(
53
+ 'p2p-connections-' . $type,
54
+ __( 'Connected', 'posts-to-posts' ) . ' ' . get_post_type_object( $type )->labels->name,
55
+ array( __CLASS__, 'box' ),
56
+ $post_type,
57
+ 'side',
58
+ 'default',
59
+ $type
60
+ );
61
+ }
62
+ }
63
+
64
+ function box( $post, $args ) {
65
+ $post_type = $args['args'];
66
+ $connected_ids = p2p_get_connected( $post_type, 'from', $post->ID );
67
+ ?>
68
+
69
+ <div class="p2p_metabox">
70
+ <div class="hide-if-no-js checkboxes">
71
+ <ul class="p2p_connected">
72
+ <?php if ( empty( $connected_ids ) ) { ?>
73
+ <li class="howto"><?php _e( 'No connections.', 'posts-to-posts' ); ?></li>
74
+ <?php } else { ?>
75
+ <?php foreach ( $connected_ids as $id ) {
76
+ echo html( 'li', scbForms::input( array(
77
+ 'type' => 'checkbox',
78
+ 'name' => "p2p_checkbox_$id",
79
+ 'value' => $id,
80
+ 'checked' => true,
81
+ 'desc' => get_the_title( $id ),
82
+ 'extra' => array( 'autocomplete' => 'off' ),
83
+ ) ) );
84
+ } ?>
85
+ <?php } ?>
86
+ </ul>
87
+
88
+ <?php echo html( 'p class="p2p_search"',
89
+ scbForms::input( array(
90
+ 'type' => 'text',
91
+ 'name' => 'p2p_search_' . $post_type,
92
+ 'desc' => __( 'Search', 'posts-to-posts' ) . ':',
93
+ 'desc_pos' => 'before',
94
+ 'extra' => array( 'autocomplete' => 'off' ),
95
+ ) )
96
+ . '<img alt="" src="' . admin_url( 'images/wpspin_light.gif' ) . '" class="waiting" style="display: none;">'
97
+ ); ?>
98
+
99
+ <ul class="p2p_results"></ul>
100
+ <p class="howto"><?php _e( 'Start typing name of connected post type and click on it if you want to connect it.', 'posts-to-posts' ); ?></p>
101
+ </div>
102
+
103
+ <div class="hide-if-js">
104
+ <?php echo scbForms::input( array(
105
+ 'type' => 'text',
106
+ 'name' => 'p2p_connected_ids_' . $post_type,
107
+ 'value' => implode( ',', $connected_ids ),
108
+ 'extra' => array( 'class' => 'p2p_connected_ids' ),
109
+ ) ); ?>
110
+ <p class="howto"><?php _e( 'Enter IDs of connected post types separated by commas, or turn on JavaScript!', 'posts-to-posts' ); ?></p>
111
+ </div>
112
+ </div>
113
+ <?php
114
+ }
115
+
116
+ function ajax_search() {
117
+ $posts = new WP_Query( array(
118
+ 'posts_per_page' => 5,
119
+ 's' => $_GET['q'],
120
+ 'post_type' => $_GET['post_type']
121
+ ) );
122
+
123
+ $results = array();
124
+ while ( $posts->have_posts() ) {
125
+ $posts->the_post();
126
+ $results[ get_the_ID() ] = get_the_title();
127
+ }
128
+
129
+ die( json_encode( $results ) );
130
+ }
131
+
132
+ private function get_post_list( $post_type ) {
133
+ $args = array(
134
+ 'post_type' => $post_type,
135
+ 'post_status' => 'any',
136
+ 'nopaging' => true,
137
+ 'cache_results' => false,
138
+ );
139
+
140
+ return scbUtil::objects_to_assoc( get_posts( $args ), 'ID', 'post_title' );
141
+ }
142
+
143
+ function uninstall() {
144
+ global $wpdb;
145
+
146
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '" . P2P_META_KEY . "'" );
147
+ }
148
+ }
149
+
api.php CHANGED
@@ -1,47 +1,108 @@
1
  <?php
2
 
3
- function p2p_register_connection_type($post_type_a, $post_type_b, $bydirectional = false) {
4
- if ( !$ptype = get_post_type_object($post_type_a) )
5
- return false;
 
 
 
 
 
 
 
 
6
 
7
- $ptype->can_connect_to[] = $post_type_b;
8
- $ptype->can_connect_to = array_unique($ptype->can_connect_to);
9
 
10
- if ( $bydirectional && $post_type_a != $post_type_b )
11
- p2p_register_connection_type($post_type_b, $post_type_a, false);
12
 
13
- return true;
14
- }
15
 
16
- function p2p_get_connection_types($post_type_a) {
17
- return (array) @get_post_type_object($post_type_a)->can_connect_to;
 
 
 
18
  }
19
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- function p2p_connect($post_a, $post_b, $bydirectional = false) {
22
- add_post_meta($post_a, P2P_META_KEY, $post_b, true);
 
 
 
 
 
 
 
23
 
24
  if ( $bydirectional )
25
- add_post_meta($post_b, P2P_META_KEY, $post_a, true);
26
  }
27
 
28
- function p2p_disconnect($post_a, $post_b, $bydirectional = false) {
29
- delete_post_meta($post_a, P2P_META_KEY, $post_b);
 
 
 
 
 
 
 
30
 
31
  if ( $bydirectional )
32
- delete_post_meta($post_b, P2P_META_KEY, $post_a);
33
  }
34
 
35
- function p2p_is_connected($post_a, $post_b, $bydirectional = false) {
36
- $r = (bool) get_post_meta($post_b, P2P_META_KEY, $post_a, true);
 
 
 
 
 
 
 
 
 
37
 
38
  if ( $bydirectional )
39
- $r = $r && p2p_is_connected($post_b, $post_a);
40
 
41
  return $r;
42
  }
43
 
44
- function p2p_get_connected($direction, $post_id, $post_type = '') {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  if ( 'to' == $direction ) {
46
  $col_a = 'post_id';
47
  $col_b = 'meta_value';
@@ -50,44 +111,96 @@ function p2p_get_connected($direction, $post_id, $post_type = '') {
50
  $col_a = 'meta_value';
51
  }
52
 
53
- $post_id = absint($post_id);
54
-
55
- if ( !$post_id || (!empty($post_type) && !is_post_type($post_type)) )
56
- return false;
57
-
58
- global $wpdb;
59
-
60
- if ( !empty($post_type) ) {
61
- $query = $wpdb->prepare("
62
- SELECT $col_a
63
  FROM $wpdb->postmeta
64
  WHERE meta_key = '" . P2P_META_KEY . "'
65
  AND $col_b = $post_id
66
- AND $col_a IN (
67
- SELECT ID
68
- FROM $wpdb->posts
69
- WHERE post_type = %s
70
- )
71
- ", $post_type);
72
 
73
- return $wpdb->get_col($query);
74
  }
75
 
76
- $query = "
77
- SELECT $col_a AS post_id, (
78
- SELECT post_type
79
- FROM $wpdb->posts
80
- WHERE $wpdb->posts.ID = $col_a
81
- ) AS type
82
- FROM $wpdb->postmeta
83
  WHERE meta_key = '" . P2P_META_KEY . "'
84
  AND $col_b = $post_id
85
  ";
86
 
87
- $connections = array();
88
- foreach ( $wpdb->get_results($query) as $row )
89
- $connections[$row->type][] = $row->post_id;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  return $connections;
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ /**
4
+ * Register a connection between two post types.
5
+ * This creates the appropriate meta box in the admin edit screen
6
+ *
7
+ * @param string $post_type_a The first end of the connection
8
+ * @param string $post_type_a The second end of the connection
9
+ * @param bool $bydirectional Wether the connection should be bydirectional
10
+ */
11
+ function p2p_register_connection_type( $post_type_a, $post_type_b, $bydirectional = false ) {
12
+ if ( !$ptype = get_post_type_object( $post_type_a ) )
13
+ return;
14
 
15
+ if ( empty( $post_type_b ) )
16
+ return;
17
 
18
+ if ( empty( $ptype->can_connect_to ) )
19
+ $ptype->can_connect_to = array();
20
 
21
+ $post_type_b = (array) $post_type_b;
 
22
 
23
+ $ptype->can_connect_to = array_merge( $ptype->can_connect_to, $post_type_b );
24
+
25
+ if ( $bydirectional )
26
+ foreach ( $post_type_b as $ptype_b )
27
+ p2p_register_connection_type( $ptype_b, $post_type_a, false );
28
  }
29
 
30
+ /**
31
+ * Get the registered connection types for a certain post type
32
+ *
33
+ * @param string $post_type_a The first end of the connection
34
+ *
35
+ * @return array[string] A list of post types
36
+ */
37
+ function p2p_get_connection_types( $post_type_a ) {
38
+ return (array) @get_post_type_object( $post_type_a )->can_connect_to;
39
+ }
40
 
41
+ /**
42
+ * Connect a post to another one
43
+ *
44
+ * @param int $post_a The first end of the connection
45
+ * @param int $post_b The second end of the connection
46
+ * @param bool $bydirectional Wether the connection should be bydirectional
47
+ */
48
+ function p2p_connect( $post_a, $post_b, $bydirectional = false ) {
49
+ add_post_meta( $post_a, P2P_META_KEY, $post_b );
50
 
51
  if ( $bydirectional )
52
+ add_post_meta( $post_b, P2P_META_KEY, $post_a );
53
  }
54
 
55
+ /**
56
+ * Disconnect a post from another one
57
+ *
58
+ * @param int $post_a The first end of the connection
59
+ * @param int $post_b The second end of the connection
60
+ * @param bool $bydirectional Wether the connection should be bydirectional
61
+ */
62
+ function p2p_disconnect( $post_a, $post_b, $bydirectional = false ) {
63
+ delete_post_meta( $post_a, P2P_META_KEY, $post_b );
64
 
65
  if ( $bydirectional )
66
+ delete_post_meta( $post_b, P2P_META_KEY, $post_a );
67
  }
68
 
69
+ /**
70
+ * See if a certain post is connected to another one
71
+ *
72
+ * @param int $post_a The first end of the connection
73
+ * @param int $post_b The second end of the connection
74
+ * @param bool $bydirectional Wether the connection should be bydirectional
75
+ *
76
+ * @return bool True if the connection exists, false otherwise
77
+ */
78
+ function p2p_is_connected( $post_a, $post_b, $bydirectional = false ) {
79
+ $r = (bool) get_post_meta( $post_b, P2P_META_KEY, $post_a, true );
80
 
81
  if ( $bydirectional )
82
+ $r = $r && p2p_is_connected( $post_b, $post_a );
83
 
84
  return $r;
85
  }
86
 
87
+ /**
88
+ * Get the list of connected posts
89
+ *
90
+ * @param string $post_type The post type of the connected posts.
91
+ * @param string $direction The direction of the connection. Can be 'to' or 'from'
92
+ * @param int $post_id One end of the connection
93
+ * @param bool $grouped Wether the results should be grouped by post type
94
+ *
95
+ * @return array[int] if $grouped is True
96
+ * @return array[string => array[int]] if $grouped is False
97
+ */
98
+ function p2p_get_connected( $post_type, $direction, $post_id, $grouped = false ) {
99
+ global $wpdb;
100
+
101
+ $post_id = absint( $post_id );
102
+
103
+ if ( !$post_id || ( 'any' != $post_type && !post_type_exists( $post_type ) ) )
104
+ return false;
105
+
106
  if ( 'to' == $direction ) {
107
  $col_a = 'post_id';
108
  $col_b = 'meta_value';
111
  $col_a = 'meta_value';
112
  }
113
 
114
+ if ( 'any' == $post_type && $grouped ) {
115
+ $query = "
116
+ SELECT $col_a AS post_id, (
117
+ SELECT post_type
118
+ FROM $wpdb->posts
119
+ WHERE $wpdb->posts.ID = $col_a
120
+ ) AS type
 
 
 
121
  FROM $wpdb->postmeta
122
  WHERE meta_key = '" . P2P_META_KEY . "'
123
  AND $col_b = $post_id
124
+ ";
125
+
126
+ $connections = array();
127
+ foreach ( $wpdb->get_results( $query ) as $row )
128
+ $connections[$row->type][] = $row->post_id;
 
129
 
130
+ return $connections;
131
  }
132
 
133
+ $where = "
 
 
 
 
 
 
134
  WHERE meta_key = '" . P2P_META_KEY . "'
135
  AND $col_b = $post_id
136
  ";
137
 
138
+ if ( 'any' != $post_type )
139
+ $where .= $wpdb->prepare( "
140
+ AND $col_a IN (
141
+ SELECT ID
142
+ FROM $wpdb->posts
143
+ WHERE post_type = %s
144
+ )
145
+ ", $post_type );
146
+
147
+ $connections = $wpdb->get_col( "
148
+ SELECT $col_a
149
+ FROM $wpdb->postmeta
150
+ $where
151
+ " );
152
+
153
+ if ( $grouped )
154
+ return array( $post_type => $connections );
155
 
156
  return $connections;
157
  }
158
 
159
+ /**
160
+ * Display the list of connected posts
161
+ *
162
+ * @param string $post_type The post type of the connected posts.
163
+ * @param string $direction The direction of the connection. Can be 'to' or 'from'
164
+ * @param int $post_id One end of the connection
165
+ * @param callback(WP_Query) $callback the function used to do the actual displaying
166
+ */
167
+ function p2p_list_connected( $post_type = 'any', $direction = 'from', $post_id = '', $callback = '' ) {
168
+ if ( !$post_id )
169
+ $post_id = get_the_ID();
170
+
171
+ $connected_post_ids = p2p_get_connected( $post_type, $direction, $post_id );
172
+
173
+ if ( empty( $connected_post_ids ) )
174
+ return;
175
+
176
+ $args = array(
177
+ 'post__in' => $connected_post_ids,
178
+ 'post_type'=> $post_type,
179
+ 'nopaging' => true,
180
+ );
181
+ $query = new WP_Query( $args );
182
+
183
+ if ( empty( $callback ) )
184
+ $callback = '_p2p_list_connected';
185
+
186
+ call_user_func( $callback, $query );
187
+
188
+ wp_reset_postdata();
189
+ }
190
+
191
+ /**
192
+ * The default callback for p2p_list_connected()
193
+ * Lists the posts as an unordered list
194
+ *
195
+ * @param WP_Query
196
+ */
197
+ function _p2p_list_connected( $query ) {
198
+ if ( $query->have_posts() ) :
199
+ echo '<ul>';
200
+ while ( $query->have_posts() ) : $query->the_post();
201
+ echo html( 'li', html_link( get_permalink( get_the_ID() ), get_the_title() ) );
202
+ endwhile;
203
+ echo '</ul>';
204
+ endif;
205
+ }
206
+
lang/posts-to-posts.pot ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Translation of the WordPress plugin Posts 2 Posts 0.2-alpha2 by scribu.
2
+ # Copyright (C) 2010 scribu
3
+ # This file is distributed under the same license as the Posts 2 Posts package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5
+ #
6
+ #, fuzzy
7
+ msgid ""
8
+ msgstr ""
9
+ "Project-Id-Version: Posts 2 Posts 0.2-alpha2\n"
10
+ "Report-Msgid-Bugs-To: http://wordpress.org/tag/posts-to-posts\n"
11
+ "POT-Creation-Date: 2010-07-15 18:49+0300\n"
12
+ "PO-Revision-Date: 2010-MO-DA HO:MI+ZONE\n"
13
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
+ "Language-Team: LANGUAGE <LL@li.org>\n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=utf-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+
19
+ #: admin/admin.php:54
20
+ msgid "Connected"
21
+ msgstr ""
22
+
23
+ #: admin/admin.php:73
24
+ msgid "No connections."
25
+ msgstr ""
26
+
27
+ #: admin/admin.php:92
28
+ msgid "Search"
29
+ msgstr ""
30
+
31
+ #: admin/admin.php:100
32
+ msgid ""
33
+ "Start typing name of connected post type and click on it if you want to "
34
+ "connect it."
35
+ msgstr ""
36
+
37
+ #: admin/admin.php:110
38
+ msgid ""
39
+ "Enter IDs of connected post types separated by commas, or turn on JavaScript!"
40
+ msgstr ""
41
+
42
+ #: scb/AdminPage.php:167
43
+ msgid "Settings <strong>saved</strong>."
44
+ msgstr ""
45
+
46
+ #: scb/AdminPage.php:179 scb/AdminPage.php:189
47
+ msgid "Save Changes"
48
+ msgstr ""
49
+
50
+ #: scb/AdminPage.php:371
51
+ msgid "Settings"
52
+ msgstr ""
53
+
54
+ #. Plugin Name of the plugin/theme
55
+ msgid "Posts 2 Posts"
56
+ msgstr ""
57
+
58
+ #. Plugin URI of the plugin/theme
59
+ msgid "http://scribu.net/wordpress/posts-to-posts"
60
+ msgstr ""
61
+
62
+ #. Description of the plugin/theme
63
+ msgid "Create connections between posts of different types"
64
+ msgstr ""
65
+
66
+ #. Author of the plugin/theme
67
+ msgid "scribu"
68
+ msgstr ""
69
+
70
+ #. Author URI of the plugin/theme
71
+ msgid "http://scribu.net/"
72
+ msgstr ""
posts2posts.php → posts-to-posts.php RENAMED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  Plugin Name: Posts 2 Posts
4
- Version: 0.1
5
  Plugin Author: scribu
6
  Description: Create connections between posts of different types
7
  Author URI: http://scribu.net/
@@ -10,12 +10,12 @@ Text Domain: posts-to-posts
10
  Domain Path: /lang
11
 
12
 
13
- Copyright (C) 2010 scribu.net (scribu AT gmail DOT com)
14
 
15
  This program is free software; you can redistribute it and/or modify
16
  it under the terms of the GNU General Public License as published by
17
  the Free Software Foundation; either version 3 of the License, or
18
- (at your option) any later version.
19
 
20
  This program is distributed in the hope that it will be useful,
21
  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,16 +26,17 @@ You should have received a copy of the GNU General Public License
26
  along with this program. If not, see <http://www.gnu.org/licenses/>.
27
  */
28
 
29
- define('P2P_META_KEY', '_p2p');
30
 
31
- function _p2p_init() {
32
- require dirname(__FILE__) . '/scb/load.php';
33
 
34
- require dirname(__FILE__) . '/api.php';
 
35
 
36
  if ( is_admin() ) {
37
- require dirname(__FILE__) . '/admin.php';
 
38
  }
39
  }
40
- _p2p_init();
41
 
1
  <?php
2
  /*
3
  Plugin Name: Posts 2 Posts
4
+ Version: 0.2
5
  Plugin Author: scribu
6
  Description: Create connections between posts of different types
7
  Author URI: http://scribu.net/
10
  Domain Path: /lang
11
 
12
 
13
+ Copyright ( C ) 2010 scribu.net ( scribu AT gmail DOT com )
14
 
15
  This program is free software; you can redistribute it and/or modify
16
  it under the terms of the GNU General Public License as published by
17
  the Free Software Foundation; either version 3 of the License, or
18
+ ( at your option ) any later version.
19
 
20
  This program is distributed in the hope that it will be useful,
21
  but WITHOUT ANY WARRANTY; without even the implied warranty of
26
  along with this program. If not, see <http://www.gnu.org/licenses/>.
27
  */
28
 
29
+ define( 'P2P_META_KEY', '_p2p' );
30
 
31
+ require dirname( __FILE__ ) . '/scb/load.php';
 
32
 
33
+ function _p2p_init() {
34
+ require dirname( __FILE__ ) . '/api.php';
35
 
36
  if ( is_admin() ) {
37
+ require dirname( __FILE__ ) . '/admin/admin.php';
38
+ P2P_Box::init( __FILE__ );
39
  }
40
  }
41
+ scb_init( '_p2p_init' );
42
 
readme.txt CHANGED
@@ -1,7 +1,7 @@
1
  === Posts 2 Posts ===
2
  Contributors: scribu
3
  Donate link: http://scribu.net/paypal
4
- Tags: cms, custom post types, relationships, graph, many-to-many
5
  Requires at least: 3.0
6
  Tested up to: 3.0
7
  Stable tag: 0.1
@@ -12,7 +12,7 @@ Create connections between posts
12
 
13
  This plugin allows you to create relationships between posts of different types. The relationships are stored in the postmeta table.
14
 
15
- To register a connection type, write:
16
 
17
  `
18
  function my_connection_types() {
@@ -22,7 +22,7 @@ add_action('init', 'my_connection_types', 100);
22
  `
23
  <br>
24
 
25
- See [available functions](http://plugins.trac.wordpress.org/browser/posts-to-posts/trunk/api.php).
26
 
27
 
28
  == Installation ==
@@ -41,9 +41,17 @@ Make sure your host is running PHP 5. The only foolproof way to do this is to ad
41
  `var_dump(PHP_VERSION);`
42
  <br>
43
 
 
 
 
44
 
45
  == Changelog ==
46
 
 
 
 
 
 
47
  = 0.1 =
48
  * initial release
49
  * [more info](http://scribu.net/wordpress/posts-to-posts/p2p-0-1.html)
1
  === Posts 2 Posts ===
2
  Contributors: scribu
3
  Donate link: http://scribu.net/paypal
4
+ Tags: cms, custom post types, relationships, many-to-many
5
  Requires at least: 3.0
6
  Tested up to: 3.0
7
  Stable tag: 0.1
12
 
13
  This plugin allows you to create relationships between posts of different types. The relationships are stored in the postmeta table.
14
 
15
+ To register a connection between two post types, write:
16
 
17
  `
18
  function my_connection_types() {
22
  `
23
  <br>
24
 
25
+ See [available functions](http://plugins.trac.wordpress.org/browser/posts-to-posts/tags/0.2/api.php).
26
 
27
 
28
  == Installation ==
41
  `var_dump(PHP_VERSION);`
42
  <br>
43
 
44
+ == Screenshots ==
45
+
46
+ 1. The metabox on the post editing screen
47
 
48
  == Changelog ==
49
 
50
+ = 0.2 =
51
+ * added p2p_list_connected() template tag
52
+ * UI that supports multiple related posts. props [Patrik Bón](http://www.mrhead.sk/)
53
+ * [more info](http://scribu.net/wordpress/posts-to-posts/p2p-0-2.html)
54
+
55
  = 0.1 =
56
  * initial release
57
  * [more info](http://scribu.net/wordpress/posts-to-posts/p2p-0-1.html)
scb/AdminPage.php ADDED
@@ -0,0 +1,461 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ Creates an admin page
5
+
6
+ You must set $this->args and define the page_content() method
7
+ */
8
+
9
+ abstract class scbAdminPage {
10
+ /** Page args
11
+ * $toplevel string If not empty, will create a new top level menu
12
+ * $icon string Path to an icon for the top level menu
13
+ * $parent string ( default: options-general.php )
14
+ * $capability string ( default: 'manage_options' )
15
+ * $page_title string ( mandatory )
16
+ * $menu_title string ( default: $page_title )
17
+ * $page_slug string ( default: sanitized $page_title )
18
+ * $nonce string ( default: $page_slug )
19
+ * $action_link string|bool Text of the action link on the Plugins page ( default: 'Settings' )
20
+ */
21
+ protected $args;
22
+
23
+ // URL to the current plugin directory.
24
+ // Useful for adding css and js files
25
+ protected $plugin_url;
26
+
27
+ // Created at page init
28
+ protected $pagehook;
29
+
30
+ // scbOptions object holder
31
+ // Normally, it's used for storing formdata
32
+ protected $options;
33
+ protected $option_name;
34
+
35
+ // l10n
36
+ protected $textdomain;
37
+
38
+ // Formdata used for filling the form elements
39
+ protected $formdata = array();
40
+
41
+
42
+ // ____________REGISTRATION COMPONENT____________
43
+
44
+
45
+ private static $registered = array();
46
+
47
+ static function register( $class, $file, $options = null ) {
48
+ if ( isset( self::$registered[$class] ) )
49
+ return false;
50
+
51
+ self::$registered[$class] = array( $file, $options );
52
+
53
+ add_action( '_admin_menu', array( __CLASS__, '_pages_init' ) );
54
+
55
+ return true;
56
+ }
57
+
58
+ static function replace( $old_class, $new_class ) {
59
+ if ( ! isset( self::$registered[$old_class] ) )
60
+ return false;
61
+
62
+ self::$registered[$new_class] = self::$registered[$old_class];
63
+ unset( self::$registered[$old_class] );
64
+
65
+ return true;
66
+ }
67
+
68
+ static function remove( $class ) {
69
+ if ( ! isset( self::$registered[$class] ) )
70
+ return false;
71
+
72
+ unset( self::$registered[$class] );
73
+
74
+ return true;
75
+ }
76
+
77
+ static function _pages_init() {
78
+ foreach ( self::$registered as $class => $args )
79
+ new $class( $args[0], $args[1] );
80
+ }
81
+
82
+
83
+ // ____________MAIN METHODS____________
84
+
85
+
86
+ // Constructor
87
+ function __construct( $file, $options = NULL ) {
88
+ if ( NULL !== $options ) {
89
+ $this->options = $options;
90
+ $this->formdata = $this->options->get();
91
+ }
92
+
93
+ $this->file = $file;
94
+ $this->plugin_url = plugin_dir_url( $file );
95
+
96
+ $this->setup();
97
+ $this->check_args();
98
+
99
+ if ( isset( $this->option_name ) ) {
100
+ add_action( 'admin_init', array( $this, 'option_init' ) );
101
+ if ( function_exists( 'settings_errors' ) )
102
+ add_action( 'admin_notices', 'settings_errors' );
103
+ }
104
+
105
+ add_action( 'admin_menu', array( $this, 'page_init' ) );
106
+ add_filter( 'contextual_help', array( $this, '_contextual_help' ), 10, 2 );
107
+
108
+ if ( $this->args['action_link'] )
109
+ add_filter( 'plugin_action_links_' . plugin_basename( $file ), array( $this, '_action_link' ) );
110
+ }
111
+
112
+ // This is where all the page args can be set
113
+ function setup(){}
114
+
115
+ // This is where the css and js go
116
+ // Both wp_enqueue_*() and inline code can be added
117
+ function page_head(){}
118
+
119
+ // This is where the contextual help goes
120
+ // @return string
121
+ function page_help(){}
122
+
123
+ // A generic page header
124
+ function page_header() {
125
+ echo "<div class='wrap'>\n";
126
+ screen_icon();
127
+ echo "<h2>" . $this->args['page_title'] . "</h2>\n";
128
+ }
129
+
130
+ // This is where the page content goes
131
+ abstract function page_content();
132
+
133
+ // A generic page footer
134
+ function page_footer() {
135
+ echo "</div>\n";
136
+ }
137
+
138
+ // This is where the form data should be validated
139
+ function validate( $new_data, $old_data ) {
140
+ return $new_data;
141
+ }
142
+
143
+ // Manually handle option saving ( use Settings API instead )
144
+ function form_handler() {
145
+ if ( empty( $_POST['action'] ) )
146
+ return false;
147
+
148
+ check_admin_referer( $this->nonce );
149
+
150
+ $new_data = array();
151
+ foreach ( array_keys( $this->formdata ) as $key )
152
+ $new_data[$key] = @$_POST[$key];
153
+
154
+ $new_data = stripslashes_deep( $new_data );
155
+
156
+ $this->formdata = $this->validate( $new_data, $this->formdata );
157
+
158
+ if ( isset( $this->options ) )
159
+ $this->options->set( $this->formdata );
160
+
161
+ $this->admin_msg();
162
+ }
163
+
164
+ // Manually generate a standard admin notice ( use Settings API instead )
165
+ function admin_msg( $msg = '', $class = "updated" ) {
166
+ if ( empty( $msg ) )
167
+ $msg = __( 'Settings <strong>saved</strong>.', $this->textdomain );
168
+
169
+ echo "<div class='$class fade'><p>$msg</p></div>\n";
170
+ }
171
+
172
+
173
+ // ____________UTILITIES____________
174
+
175
+
176
+ // Generates a form submit button
177
+ function submit_button( $value = '', $action = 'action', $class = "button" ) {
178
+ if ( is_array( $value ) ) {
179
+ extract( wp_parse_args( $value, array( 'value' => __( 'Save Changes', $this->textdomain ),
180
+ 'action' => 'action',
181
+ 'class' => 'button',
182
+ 'ajax' => true ) ) );
183
+
184
+ if ( ! $ajax )
185
+ $class .= ' no-ajax';
186
+ }
187
+ else {
188
+ if ( empty( $value ) )
189
+ $value = __( 'Save Changes', $this->textdomain );
190
+ }
191
+
192
+ $input_args = array( 'type' => 'submit',
193
+ 'names' => $action,
194
+ 'values' => $value,
195
+ 'extra' => '',
196
+ 'desc' => false );
197
+
198
+ if ( ! empty( $class ) )
199
+ $input_args['extra'] = "class='{$class}'";
200
+
201
+ $output = "<p class='submit'>\n" . scbForms::input( $input_args ) . "</p>\n";
202
+
203
+ return $output;
204
+ }
205
+
206
+ /*
207
+ Mimics scbForms::form_wrap()
208
+
209
+ $this->form_wrap( $content ); // generates a form with a default submit button
210
+
211
+ $this->form_wrap( $content, false ); // generates a form with no submit button
212
+
213
+ // the second argument is sent to submit_button()
214
+ $this->form_wrap( $content, array( 'text' => 'Save changes',
215
+ 'name' => 'action',
216
+ 'ajax' => true,
217
+ ) );
218
+ */
219
+ function form_wrap( $content, $submit_button = true ) {
220
+ if ( is_array( $submit_button ) ) {
221
+ $content .= call_user_func( array( $this, 'submit_button' ), $submit_button );
222
+ } elseif ( true === $submit_button ) {
223
+ $content .= $this->submit_button();
224
+ } elseif ( false !== strpos( $submit_button, '<input' ) ) {
225
+ $content .= $submit_button;
226
+ } elseif ( false !== $submit_button ) {
227
+ $button_args = array_slice( func_get_args(), 1 );
228
+ $content .= call_user_func_array( array( $this, 'submit_button' ), $button_args );
229
+ }
230
+
231
+ return scbForms::form_wrap( $content, $this->nonce );
232
+ }
233
+
234
+ // See scbForms::form()
235
+ function form( $rows, $formdata = array() ) {
236
+ return scbForms::form( $rows, $formdata, $this->nonce );
237
+ }
238
+
239
+ // Generates a table wrapped in a form
240
+ function form_table( $rows, $formdata = array() ) {
241
+ $output = '';
242
+ foreach ( $rows as $row )
243
+ $output .= $this->table_row( $row, $formdata );
244
+
245
+ $output = $this->form_table_wrap( $output );
246
+
247
+ return $output;
248
+ }
249
+
250
+ // Wraps the given content in a <form><table>
251
+ function form_table_wrap( $content ) {
252
+ $output = $this->table_wrap( $content );
253
+ $output = $this->form_wrap( $output, $this->nonce );
254
+
255
+ return $output;
256
+ }
257
+
258
+ // Generates a form table
259
+ function table( $rows, $formdata = array() ) {
260
+ $output = '';
261
+ foreach ( $rows as $row )
262
+ $output .= $this->table_row( $row, $formdata );
263
+
264
+ $output = $this->table_wrap( $output );
265
+
266
+ return $output;
267
+ }
268
+
269
+ // Generates a table row
270
+ function table_row( $args, $formdata = array() ) {
271
+ return $this->row_wrap( $args['title'], $this->input( $args, $formdata ) );
272
+ }
273
+
274
+ // Wraps the given content in a <table>
275
+ function table_wrap( $content ) {
276
+ return
277
+ html( 'table class="form-table"', $content );
278
+ }
279
+
280
+ // Wraps the given content in a <tr><td>
281
+ function row_wrap( $title, $content ) {
282
+ return
283
+ html( 'tr',
284
+ html( 'th scope="row"', $title )
285
+ .html( 'td', $content ) );
286
+ }
287
+
288
+ function input( $args, $formdata = array() ) {
289
+ if ( empty( $formdata ) )
290
+ $formdata = $this->formdata;
291
+
292
+ if ( isset( $args['name_tree'] ) ) {
293
+ $tree = ( array ) $args['name_tree'];
294
+ unset( $args['name_tree'] );
295
+
296
+ $value = $formdata;
297
+ $name = $this->option_name;
298
+ foreach ( $tree as $key ) {
299
+ $value = $value[$key];
300
+ $name .= '[' . $key . ']';
301
+ }
302
+
303
+ $args['name'] = $name;
304
+ unset( $args['names'] );
305
+
306
+ unset( $args['values'] );
307
+
308
+ $formdata = array( $name => $value );
309
+ }
310
+
311
+ return scbForms::input( $args, $formdata );
312
+ }
313
+
314
+ // Mimic scbForms inheritance
315
+ function __call( $method, $args ) {
316
+ return call_user_func_array( array( 'scbForms', $method ), $args );
317
+ }
318
+
319
+ // Wraps a string in a <script> tag
320
+ function js_wrap( $string ) {
321
+ return "\n<script type='text/javascript'>\n" . $string . "\n</script>\n";
322
+ }
323
+
324
+ // Wraps a string in a <style> tag
325
+ function css_wrap( $string ) {
326
+ return "\n<style type='text/css'>\n" . $string . "\n</style>\n";
327
+ }
328
+
329
+
330
+ // ____________INTERNAL METHODS____________
331
+
332
+
333
+ // Registers a page
334
+ function page_init() {
335
+ extract( $this->args );
336
+
337
+ if ( ! $toplevel ) {
338
+ $this->pagehook = add_submenu_page( $parent, $page_title, $menu_title, $capability, $page_slug, array( $this, '_page_content_hook' ) );
339
+ } else {
340
+ $func = 'add_' . $toplevel . '_page';
341
+ $this->pagehook = $func( $page_title, $menu_title, $capability, $page_slug, array( $this, '_page_content_hook' ), $icon_url );
342
+ }
343
+
344
+ if ( ! $this->pagehook )
345
+ return;
346
+
347
+ if ( $ajax_submit ) {
348
+ $this->ajax_response();
349
+ add_action( 'admin_footer', array( $this, 'ajax_submit' ), 20 );
350
+ }
351
+
352
+ add_action( 'admin_print_styles-' . $this->pagehook, array( $this, 'page_head' ) );
353
+ }
354
+
355
+ function option_init() {
356
+ register_setting( $this->option_name, $this->option_name, array( $this, 'validate' ) );
357
+ }
358
+
359
+ private function check_args() {
360
+ if ( empty( $this->args['page_title'] ) )
361
+ trigger_error( 'Page title cannot be empty', E_USER_WARNING );
362
+
363
+ $this->args = wp_parse_args( $this->args, array(
364
+ 'toplevel' => '',
365
+ 'icon' => '',
366
+ 'parent' => 'options-general.php',
367
+ 'capability' => 'manage_options',
368
+ 'menu_title' => $this->args['page_title'],
369
+ 'page_slug' => '',
370
+ 'nonce' => '',
371
+ 'action_link' => __( 'Settings', $this->textdomain ),
372
+ 'ajax_submit' => false,
373
+ ) );
374
+
375
+ if ( empty( $this->args['page_slug'] ) )
376
+ $this->args['page_slug'] = sanitize_title_with_dashes( $this->args['menu_title'] );
377
+
378
+ if ( empty( $this->args['nonce'] ) )
379
+ $this->nonce = $this->args['page_slug'];
380
+ }
381
+
382
+ function _contextual_help( $help, $screen ) {
383
+ if ( is_object( $screen ) )
384
+ $screen = $screen->id;
385
+
386
+ if ( $screen == $this->pagehook && $actual_help = $this->page_help() )
387
+ return $actual_help;
388
+
389
+ return $help;
390
+ }
391
+
392
+ function ajax_response() {
393
+ if ( ! isset( $_POST['_ajax_submit'] ) || $_POST['_ajax_submit'] != $this->pagehook )
394
+ return;
395
+
396
+ $this->form_handler();
397
+ die;
398
+ }
399
+
400
+ function ajax_submit() {
401
+ global $page_hook;
402
+
403
+ if ( $page_hook != $this->pagehook )
404
+ return;
405
+ ?>
406
+ <script type="text/javascript">
407
+ jQuery( document ).ready( function( $ ){
408
+ var $spinner = $( new Image() ).attr( 'src', '<?php echo admin_url( "images/wpspin_light.gif" ); ?>' );
409
+
410
+ $( ':submit' ).click( function( ev ){
411
+ var $submit = $( this );
412
+ var $form = $submit.parents( 'form' );
413
+
414
+ if ( $submit.hasClass( 'no-ajax' ) || $form.attr( 'method' ).toLowerCase() != 'post' )
415
+ return true;
416
+
417
+ var $this_spinner = $spinner.clone();
418
+
419
+ $submit.before( $this_spinner ).hide();
420
+
421
+ var data = $form.serializeArray();
422
+ data.push( {name: $submit.attr( 'name' ), value: $submit.val()} );
423
+ data.push( {name: '_ajax_submit', value: '<?php echo $this->pagehook; ?>'} );
424
+
425
+ $.post( location.href, data, function( response ){
426
+ var $prev = $( '.wrap > .updated, .wrap > .error' );
427
+ var $msg = $( response ).hide().insertAfter( $( '.wrap h2' ) );
428
+ if ( $prev.length > 0 )
429
+ $prev.fadeOut( 'slow', function(){ $msg.fadeIn( 'slow' ); } );
430
+ else
431
+ $msg.fadeIn( 'slow' );
432
+
433
+ $this_spinner.hide();
434
+ $submit.show();
435
+ } );
436
+
437
+ ev.stopPropagation();
438
+ ev.preventDefault();
439
+ } );
440
+ } );
441
+ </script>
442
+ <?php
443
+ }
444
+
445
+ function _page_content_hook() {
446
+ $this->form_handler();
447
+
448
+ $this->page_header();
449
+ $this->page_content();
450
+ $this->page_footer();
451
+ }
452
+
453
+ function _action_link( $links ) {
454
+ $url = add_query_arg( 'page', $this->args['page_slug'], admin_url( $this->args['parent'] ) );
455
+
456
+ $links[] = html_link( $url, $this->args['action_link'] );
457
+
458
+ return $links;
459
+ }
460
+ }
461
+
scb/BoxesPage.php ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ Creates an admin page with widgets, similar to the dashboard
5
+
6
+ For example, if you defined the boxes like this:
7
+
8
+ $this->boxes = array( array( 'settings', 'Settings box', 'normal' )
9
+ ... );
10
+
11
+ You must also define two methods in your class for each box:
12
+
13
+ function settings_box() - this is where the box content is echoed
14
+ function settings_handler() - this is where the box settings are saved
15
+ ...
16
+ */
17
+ abstract class scbBoxesPage extends scbAdminPage {
18
+ /*
19
+ A box definition looks like this:
20
+ array( $slug, $title, $column );
21
+
22
+ Available columns: normal, side, column3, column4
23
+ */
24
+ protected $boxes = array();
25
+
26
+ function __construct( $file, $options = null ) {
27
+ parent::__construct( $file, $options );
28
+
29
+ // too late
30
+ scbUtil::add_uninstall_hook( $this->file, array( $this, 'uninstall' ) );
31
+ }
32
+
33
+ function page_init() {
34
+ if ( !isset( $this->args['columns'] ) )
35
+ $this->args['columns'] = 2;
36
+
37
+ parent::page_init();
38
+
39
+ add_action( 'load-' . $this->pagehook, array( $this, 'boxes_init' ) );
40
+ add_filter( 'screen_layout_columns', array( $this, 'columns' ) );
41
+ }
42
+
43
+ function default_css() {
44
+ ?>
45
+ <style type="text/css">
46
+ .postbox-container + .postbox-container {margin-left: 18px}
47
+ .postbox-container {padding-right: 0}
48
+
49
+ .inside {clear: both; overflow: hidden; padding: 10px 10px 0 10px !important}
50
+ .inside table {margin: 0 !important; padding: 0 !important}
51
+ .inside table td {vertical-align: middle !important}
52
+ .inside table .regular-text {width: 100% !important}
53
+ .inside .form-table th {width: 30%; max-width: 200px; padding: 10px 0 !important}
54
+ .inside .widefat .check-column {padding-bottom: 7px !important}
55
+ .inside p, .inside table {margin: 0 0 10px 0 !important}
56
+ .inside p.submit {float:left !important; padding: 0 !important}
57
+ </style>
58
+ <?php
59
+ }
60
+
61
+ function page_content() {
62
+ $this->default_css();
63
+
64
+ global $screen_layout_columns;
65
+
66
+ if ( isset( $screen_layout_columns ) ) {
67
+ $hide2 = $hide3 = $hide4 = '';
68
+ switch ( $screen_layout_columns ) {
69
+ case 4:
70
+ $width = 'width:24.5%;';
71
+ break;
72
+ case 3:
73
+ $width = 'width:32.67%;';
74
+ $hide4 = 'display:none;';
75
+ break;
76
+ case 2:
77
+ $width = 'width:49%;';
78
+ $hide3 = $hide4 = 'display:none;';
79
+ break;
80
+ default:
81
+ $width = 'width:98%;';
82
+ $hide2 = $hide3 = $hide4 = 'display:none;';
83
+ }
84
+ }
85
+ ?>
86
+ <div id='<?php echo $this->pagehook ?>-widgets' class='metabox-holder'>
87
+ <?php
88
+ echo "\t<div class='postbox-container' style='$width'>\n";
89
+ do_meta_boxes( $this->pagehook, 'normal', '' );
90
+
91
+ echo "\t</div><div class='postbox-container' style='{$hide2}$width'>\n";
92
+ do_meta_boxes( $this->pagehook, 'side', '' );
93
+
94
+ echo "\t</div><div class='postbox-container' style='{$hide3}$width'>\n";
95
+ do_meta_boxes( $this->pagehook, 'column3', '' );
96
+
97
+ echo "\t</div><div class='postbox-container' style='{$hide4}$width'>\n";
98
+ do_meta_boxes( $this->pagehook, 'column4', '' );
99
+ ?>
100
+ </div></div>
101
+ <?php
102
+ }
103
+
104
+ function page_footer() {
105
+ parent::page_footer();
106
+ $this->_boxes_js_init();
107
+ }
108
+
109
+ function form_handler() {
110
+ if ( empty( $_POST ) )
111
+ return;
112
+
113
+ check_admin_referer( $this->nonce );
114
+
115
+ // Box handler
116
+ foreach ( $this->boxes as $box ) {
117
+ $args = isset( $box[4] ) ? $box[4] : array();
118
+
119
+ $handler = $box[0] . '_handler';
120
+
121
+ if ( method_exists( $this, $handler ) )
122
+ call_user_func_array( array( $this, $handler ), $args );
123
+ }
124
+
125
+ if ( $this->options )
126
+ $this->formdata = $this->options->get();
127
+ }
128
+
129
+ function columns( $columns ) {
130
+ $columns[$this->pagehook] = $this->args['columns'];
131
+
132
+ return $columns;
133
+ }
134
+
135
+ function uninstall() {
136
+ global $wpdb;
137
+
138
+ $hook = str_replace( '-', '', $this->pagehook );
139
+
140
+ foreach ( array( 'metaboxhidden', 'closedpostboxes', 'wp_metaboxorder', 'screen_layout' ) as $option )
141
+ $keys[] = "'{$option}_{$hook}'";
142
+
143
+ $keys = '( ' . implode( ', ', $keys ) . ' )';
144
+
145
+ $wpdb->query( "
146
+ DELETE FROM {$wpdb->usermeta}
147
+ WHERE meta_key IN {$keys}
148
+ " );
149
+ }
150
+
151
+ function boxes_init() {
152
+ wp_enqueue_script( 'common' );
153
+ wp_enqueue_script( 'wp-lists' );
154
+ wp_enqueue_script( 'postbox' );
155
+
156
+ $registered = array();
157
+ foreach( $this->boxes as $box_args ) {
158
+ @list( $name, $title, $context, $priority, $args ) = $box_args;
159
+
160
+ if ( empty( $title ) )
161
+ $title = ucfirst( $name );
162
+ if ( empty( $context ) )
163
+ $context = 'normal';
164
+ if ( empty( $priority ) )
165
+ $priority = 'default';
166
+ if ( empty( $args ) )
167
+ $args = array();
168
+
169
+ if ( isset( $registered[$name] ) ) {
170
+ if ( empty( $args ) )
171
+ trigger_error( "Duplicate box name: $name", E_USER_NOTICE );
172
+
173
+ $name = $this->_increment( $name );
174
+ } else {
175
+ $registered[$name] = true;
176
+ }
177
+
178
+ add_meta_box( $name, $title, array( $this, '_intermediate_callback' ), $this->pagehook, $context, $priority, $args );
179
+ }
180
+ }
181
+
182
+ // Make it so that $args is actually what's passed to the callback
183
+ function _intermediate_callback( $_, $box ) {
184
+ list( $name ) = explode( '-', $box['id'] );
185
+
186
+ call_user_func_array( array( $this, $name . '_box' ), $box['args'] );
187
+ }
188
+
189
+ private function _increment( $name ) {
190
+ $parts = explode( '-', $name );
191
+ if ( isset( $parts[1] ) )
192
+ $parts[1]++;
193
+ else
194
+ $parts[1] = 2;
195
+
196
+ return implode( '-', $parts );
197
+ }
198
+
199
+ // Adds necesary code for JS to work
200
+ function _boxes_js_init() {
201
+ echo $this->js_wrap( <<<EOT
202
+ jQuery( document ).ready( function( $ ){
203
+ // close postboxes that should be closed
204
+ $( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
205
+ // postboxes setup
206
+ postboxes.add_postbox_toggles( '$this->pagehook' );
207
+ } );
208
+ EOT
209
+ );
210
+ ?>
211
+
212
+ <form style='display: none' method='get' action=''>
213
+ <p>
214
+ <?php
215
+ wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
216
+ wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
217
+ ?>
218
+ </p>
219
+ </form>
220
+ <?php
221
+ }
222
+ }
223
+
scb/Cron.php ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class scbCron {
4
+ protected $schedule;
5
+ protected $interval;
6
+ protected $time;
7
+
8
+ protected $hook;
9
+ protected $callback_args;
10
+
11
+ /**
12
+ * Create a new cron job
13
+ *
14
+ * @param string Reference to main plugin file
15
+ * @param array List of args:
16
+ string $action OR callback $callback
17
+ string $schedule OR number $interval
18
+ array $callback_args ( optional )
19
+ * @param bool Debug mode
20
+ */
21
+ function __construct( $file, $args, $debug = false ) {
22
+ $this->_set_args( $args );
23
+
24
+ scbUtil::add_activation_hook( $file, array( $this, 'reset' ) );
25
+ register_deactivation_hook( $file, array( $this, 'unschedule' ) );
26
+
27
+ add_filter( 'cron_schedules', array( $this, '_add_timing' ) );
28
+
29
+ if ( $debug )
30
+ self::debug();
31
+ }
32
+
33
+ /* Change the interval of the cron job
34
+ *
35
+ * @param array List of args:
36
+ string $schedule OR number $interval
37
+ timestamp $time ( optional )
38
+ */
39
+ function reschedule( $args ) {
40
+ extract( $args );
41
+
42
+ if ( $schedule && $this->schedule != $schedule ) {
43
+ $this->schedule = $schedule;
44
+ } elseif ( $interval && $this->interval != $interval ) {
45
+ $this->schedule = $interval . 'secs';
46
+ $this->interval = $interval;
47
+ }
48
+
49
+ $this->time = $time;
50
+
51
+ $this->reset();
52
+ }
53
+
54
+ /**
55
+ * Reset the schedule
56
+ */
57
+ function reset() {
58
+ $this->unschedule();
59
+ $this->schedule();
60
+ }
61
+
62
+ /**
63
+ * Clear the cron job
64
+ */
65
+ function unschedule() {
66
+ # wp_clear_scheduled_hook( $this->hook, $this->callback_args );
67
+ self::really_clear_scheduled_hook( $this->hook );
68
+ }
69
+
70
+ /**
71
+ * Execute the job now
72
+ */
73
+ function do_now() {
74
+ do_action( $this->hook );
75
+ }
76
+
77
+ /**
78
+ * Execute the job with a given delay
79
+ * @param int Delay in seconds
80
+ */
81
+ function do_once( $delay = 0 ) {
82
+ wp_schedule_single_event( time() + $delay, $this->hook, $this->callback_args );
83
+ }
84
+
85
+ /**
86
+ * Display current cron jobs
87
+ */
88
+ function debug() {
89
+ add_action( 'admin_footer', array( __CLASS__, '_debug' ) );
90
+ }
91
+
92
+
93
+ //_____INTERNAL METHODS_____
94
+
95
+
96
+ function _add_timing( $schedules ) {
97
+ if ( isset( $schedules[$this->schedule] ) )
98
+ return $schedules;
99
+
100
+ $schedules[$this->schedule] = array( 'interval' => $this->interval,
101
+ 'display' => $this->interval . ' seconds' );
102
+
103
+ return $schedules;
104
+ }
105
+
106
+ function _debug() {
107
+ if ( ! current_user_can( 'manage_options' ) )
108
+ return;
109
+
110
+ echo "<pre>";
111
+ print_r( get_option( 'cron' ) );
112
+ echo "</pre>";
113
+ }
114
+
115
+ protected function schedule() {
116
+ if ( ! $this->time )
117
+ $this->time = time();
118
+
119
+ wp_schedule_event( $this->time, $this->schedule, $this->hook, $this->callback_args );
120
+ }
121
+
122
+ protected function _set_args( $args ) {
123
+ extract( $args );
124
+
125
+ // Set hook
126
+ if ( isset( $action ) ) {
127
+ $this->hook = $action;
128
+ } elseif ( isset( $callback ) ) {
129
+ $this->hook = self::_callback_to_string( $callback );
130
+
131
+ add_action( $this->hook, $callback );
132
+ } elseif ( method_exists( $this, 'callback' ) ) {
133
+ $this->hook = self::_callback_to_string( $callback );
134
+
135
+ add_action( $this->hook, $callback );
136
+ } else {
137
+ trigger_error( '$action OR $callback not set', E_USER_WARNING );
138
+ }
139
+
140
+ // Set schedule
141
+ if ( isset( $interval ) ) {
142
+ $this->schedule = $interval . 'secs';
143
+ $this->interval = $interval;
144
+ } elseif ( isset( $schedule ) ) {
145
+ $this->schedule = $schedule;
146
+ } else {
147
+ trigger_error( '$schedule OR $interval not set', E_USER_WARNING );
148
+ }
149
+
150
+ if ( isset( $callback_args ) )
151
+ $this->callback_args = ( array ) $callback_args;
152
+ }
153
+
154
+ protected static function really_clear_scheduled_hook( $name ) {
155
+ $crons = _get_cron_array();
156
+
157
+ foreach ( $crons as $timestamp => $hooks ) {
158
+ foreach ( $hooks as $hook => $args )
159
+ if ( $hook == $name )
160
+ unset( $crons[$timestamp][$hook] );
161
+
162
+ if ( empty( $hooks ) )
163
+ unset( $crons[$timestamp] );
164
+ }
165
+
166
+ _set_cron_array( $crons );
167
+ }
168
+
169
+ protected static function _callback_to_string( $callback ) {
170
+ if ( ! is_array( $callback ) )
171
+ $str = $callback;
172
+ elseif ( ! is_string( $callback[0] ) )
173
+ $str = get_class( $callback[0] ) . '_' . $callback[1];
174
+ else
175
+ $str = $callback[0] . '::' . $callback[1];
176
+
177
+ $str .= '_hook';
178
+
179
+ return $str;
180
+ }
181
+ }
182
+
scb/Forms.php CHANGED
@@ -9,24 +9,24 @@ class scbForms {
9
  protected static $args;
10
  protected static $formdata = array();
11
 
12
- static function input($args, $formdata = array()) {
13
- $args = self::validate_data($args);
14
 
15
  $error = false;
16
- foreach ( array('name', 'value') as $key ) {
17
  $old = $key . 's';
18
 
19
- if ( isset($args[$old]) ) {
20
  $args[$key] = $args[$old];
21
- unset($args[$old]);
22
  }
23
  }
24
 
25
- if ( empty($args['name']) )
26
- return trigger_error('Empty name', E_USER_WARNING);
27
 
28
  self::$args = $args;
29
- self::$formdata = self::validate_data($formdata);
30
 
31
  if ( 'select' == $args['type'] )
32
  return self::_select();
@@ -35,82 +35,136 @@ class scbForms {
35
  }
36
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  // Generates a form
39
- static function form($inputs, $formdata = NULL, $nonce) {
40
  $output = '';
41
  foreach ( $inputs as $input )
42
- $output .= self::input($input, $formdata);
 
 
 
 
 
43
 
44
- $output = self::form_wrap($output, $nonce);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  return $output;
47
  }
48
 
49
  // Wraps the given content in a <form> tag
50
- static function form_wrap($content, $nonce = 'update_options') {
51
  $output = "\n<form method='post' action=''>\n";
52
  $output .= $content;
53
- $output .= wp_nonce_field($action = $nonce, $name = "_wpnonce", $referer = true , $echo = false);
54
  $output .= "\n</form>\n";
55
 
56
  return $output;
57
  }
58
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  // ____________PRIVATE METHODS____________
61
 
62
 
63
  // Recursivly transform empty arrays to ''
64
- private static function validate_data($data) {
65
- if ( !is_array($data) )
66
  return $data;
67
 
68
- if ( empty($data) )
69
  return '';
70
 
71
  foreach ( $data as $key => &$value )
72
- $value = self::validate_data($value);
73
 
74
  return $data;
75
  }
76
 
77
  // From multiple inputs to single inputs
78
  private static function _input() {
79
- extract(wp_parse_args(self::$args, array(
80
  'name' => NULL,
81
  'value' => NULL,
82
  'desc' => NULL,
83
  'checked' => NULL,
84
- )));
85
 
86
- $m_name = is_array($name);
87
- $m_value = is_array($value);
88
- $m_desc = is_array($desc);
89
 
90
  // Correct name
91
  if ( !$m_name && $m_value
92
  && 'checkbox' == $type
93
- && false === strpos($name, '[')
94
  )
95
  $args['name'] = $name = $name . '[]';
96
 
97
  // Expand names or values
98
  if ( !$m_name && !$m_value ) {
99
- $a = array($name => $value);
100
  }
101
  elseif ( $m_name && !$m_value ) {
102
- $a = array_fill_keys($name, $value);
103
  }
104
  elseif ( !$m_name && $m_value ) {
105
- $a = array_fill_keys($value, $name);
106
  }
107
  else {
108
- $a = array_combine($name, $value);
109
  }
110
 
111
  // Correct descriptions
112
  $_after = '';
113
- if ( isset($desc) && !$m_desc && false === strpos($desc, self::token) ) {
114
  if ( $m_value ) {
115
  $_after = $desc;
116
  $args['desc'] = $desc = $value;
@@ -130,12 +184,15 @@ class scbForms {
130
  $i2 = 'val';
131
  }
132
 
133
- $func = in_array($type, array('checkbox', 'radio')) ? '_checkbox_single' : '_input_single';
134
 
135
  // Set constant args
136
- $const_args = self::array_extract(self::$args, array('type', 'desc_pos', 'checked'));
137
- if ( isset($extra) )
138
- $const_args['extra'] = explode(' ', $extra);
 
 
 
139
 
140
  $i = 0;
141
  foreach ( $a as $name => $val ) {
@@ -148,120 +205,120 @@ class scbForms {
148
  $cur_args['value'] = $$i2;
149
 
150
  // Set desc
151
- if ( is_array($desc) )
152
  $cur_args['desc'] = $desc[$i];
153
- elseif ( isset($desc) )
154
  $cur_args['desc'] = $desc;
155
 
156
  // Find relevant formdata
157
  $match = NULL;
158
  if ( $checked === NULL ) {
159
- $match = @self::$formdata[str_replace('[]', '', $$i1)];
160
- if ( is_array($match) ) {
161
  $match = $match[$i];
162
  }
163
- } else if ( is_array($checked) ) {
164
- $cur_args['checked'] = isset($checked[$i]) && $checked[$i];
165
  }
166
 
167
- $output[] = self::$func($cur_args, $match);
168
 
169
  $i++;
170
  }
171
 
172
- return implode("\n", $output) . $_after;
173
  }
174
 
175
  // Handle args for checkboxes and radio inputs
176
- private static function _checkbox_single($args, $data) {
177
- $args = wp_parse_args($args, array(
178
  'name' => NULL,
179
  'value' => true,
180
  'desc_pos' => 'after',
181
  'desc' => NULL,
182
  'checked' => NULL,
183
  'extra' => array(),
184
- ));
185
 
186
  foreach ( $args as $key => &$val )
187
  $$key = &$val;
188
- unset($val);
189
 
190
  if ( $checked === NULL && $value == $data )
191
  $checked = true;
192
 
193
  if ( $checked )
194
- $extra[] = 'checked="checked"';
195
 
196
- if ( $desc === NULL && !is_bool($value) )
197
- $desc = str_replace('[]', '', $value);
198
 
199
- return self::_input_gen($args);
200
  }
201
 
202
  // Handle args for text inputs
203
- private static function _input_single($args, $data) {
204
- $args = wp_parse_args($args, array(
205
  'value' => $data,
206
  'desc_pos' => 'after',
207
- 'extra' => array('class="regular-text"'),
208
- ));
209
 
210
  foreach ( $args as $key => &$val )
211
  $$key = &$val;
212
- unset($val);
213
 
214
- if ( FALSE === strpos($name, '[') )
215
- $extra[] = "id='{$name}'";
216
 
217
- return self::_input_gen($args);
218
  }
219
 
220
  // Generate html with the final args
221
- private static function _input_gen($args) {
222
- extract(wp_parse_args($args, array(
223
  'name' => NULL,
224
  'value' => NULL,
225
  'desc' => NULL,
226
  'extra' => array()
227
- )));
228
 
229
- $extra = self::validate_extra($extra, $name);
230
 
231
  if ( 'textarea' == $type ) {
232
- $value = esc_html($value);
233
- $input = "<textarea name='{$name}'{$extra}>\n{$value}\n</textarea>\n";
234
  }
235
  else {
236
- $value = esc_attr($value);
237
  $input = "<input name='{$name}' value='{$value}' type='{$type}'{$extra} /> ";
238
  }
239
 
240
- return self::add_label($input, $desc, $desc_pos);
241
  }
242
 
243
  private static function _select() {
244
- extract(wp_parse_args(self::$args, array(
245
  'name' => '',
246
  'value' => array(),
247
  'text' => '',
248
- 'selected' => array('foo'), // hack to make default blank
249
- 'extra' => '',
250
  'numeric' => false, // use numeric array instead of associative
251
  'desc' => '',
252
  'desc_pos' => '',
253
- )), EXTR_SKIP);
254
 
255
- if ( empty($value) )
256
- $value = array('' => '');
257
 
258
- if ( !is_array($value) )
259
- return trigger_error("'value' argument is expected to be an array", E_USER_WARNING);
260
 
261
- if ( !self::is_associative($value) && !$numeric )
262
- $value = array_combine($value, $value);
263
 
264
- if ( isset(self::$formdata[$name]) )
265
  $cur_val = self::$formdata[$name];
266
  else
267
  $cur_val = $selected;
@@ -269,39 +326,32 @@ class scbForms {
269
  if ( false === $text ) {
270
  $opts = '';
271
  } else {
272
- $opts = "\t<option value=''";
273
- if ( $cur_val === array('foo') )
274
- $opts .= " selected='selected'";
275
- $opts .= ">{$text}</option>\n";
276
  }
277
 
278
  foreach ( $value as $key => $value ) {
279
- if ( empty($key) || empty($value) )
280
  continue;
281
 
282
- $cur_extra = array();
283
- if ( (string) $key == (string) $cur_val )
284
- $cur_extra[] = "selected='selected'";
285
-
286
- $cur_extra = self::validate_extra($cur_extra, $key);
287
-
288
- $opts .= "\t<option value='{$key}'{$cur_extra}>{$value}</option>\n";
289
  }
290
 
291
- $extra = self::validate_extra($extra, $name);
 
 
292
 
293
  $input = "<select name='{$name}'$extra>\n{$opts}</select>";
294
 
295
- return self::add_label($input, $desc, $desc_pos);
296
  }
297
 
298
- private static function add_label($input, $desc, $desc_pos) {
299
- if ( empty($desc_pos) )
300
  $desc_pos = 'after';
301
 
302
  $label = '';
303
- if ( false === strpos($desc, self::token) ) {
304
- switch ($desc_pos) {
305
  case 'before': $label = $desc . ' ' . self::token; break;
306
  case 'after': $label = self::token . ' ' . $desc;
307
  }
@@ -309,9 +359,9 @@ class scbForms {
309
  $label = $desc;
310
  }
311
 
312
- $label = trim(str_replace(self::token, $input, $label));
313
 
314
- if ( empty($desc) )
315
  $output = $input . "\n";
316
  else
317
  $output = "<label>{$label}</label>\n";
@@ -319,31 +369,35 @@ class scbForms {
319
  return $output;
320
  }
321
 
322
- private static function validate_extra($extra, $name, $implode = true) {
323
- if ( !is_array($extra) )
324
- $extra = explode(' ', $extra);
325
 
326
- if ( empty($extra) )
327
- return '';
328
 
329
- return ' ' . ltrim(implode(' ', $extra));
 
 
330
  }
331
 
332
- // Utilities
 
 
 
 
 
 
333
 
334
- private static function is_associative($array) {
335
- if ( !is_array($array) || empty($array) )
336
  return false;
337
 
338
- $keys = array_keys($array);
339
 
340
- return array_keys($keys) !== $keys;
341
  }
342
 
343
- private static function array_extract($array, $keys) {
344
  $r = array();
345
  foreach ( $keys as $key )
346
- if ( isset($array[$key]) )
347
  $r[$key] = $array[$key];
348
 
349
  return $r;
@@ -351,10 +405,10 @@ class scbForms {
351
  }
352
 
353
  // PHP < 5.2
354
- if ( !function_exists('array_fill_keys') ) :
355
- function array_fill_keys($keys, $value) {
356
- if ( !is_array($keys) )
357
- trigger_error('First argument is expected to be an array.' . gettype($keys) . 'given', E_USER_WARNING);
358
 
359
  $r = array();
360
  foreach ( $keys as $key )
9
  protected static $args;
10
  protected static $formdata = array();
11
 
12
+ static function input( $args, $formdata = array() ) {
13
+ $args = self::validate_data( $args );
14
 
15
  $error = false;
16
+ foreach ( array( 'name', 'value' ) as $key ) {
17
  $old = $key . 's';
18
 
19
+ if ( isset( $args[$old] ) ) {
20
  $args[$key] = $args[$old];
21
+ unset( $args[$old] );
22
  }
23
  }
24
 
25
+ if ( empty( $args['name'] ) )
26
+ return trigger_error( 'Empty name', E_USER_WARNING );
27
 
28
  self::$args = $args;
29
+ self::$formdata = self::validate_data( $formdata );
30
 
31
  if ( 'select' == $args['type'] )
32
  return self::_select();
35
  }
36
 
37
 
38
+ // ____________UTILITIES____________
39
+
40
+
41
+ // Generates a table wrapped in a form
42
+ static function form_table( $rows, $formdata = NULL ) {
43
+ $output = '';
44
+ foreach ( $rows as $row )
45
+ $output .= self::table_row( $row, $formdata );
46
+
47
+ $output = self::form_table_wrap( $output );
48
+
49
+ return $output;
50
+ }
51
+
52
  // Generates a form
53
+ static function form( $inputs, $formdata = NULL, $nonce ) {
54
  $output = '';
55
  foreach ( $inputs as $input )
56
+ $output .= self::input( $input, $formdata );
57
+
58
+ $output = self::form_wrap( $output, $nonce );
59
+
60
+ return $output;
61
+ }
62
 
63
+ // Generates a table
64
+ static function table( $rows, $formdata = NULL ) {
65
+ $output = '';
66
+ foreach ( $rows as $row )
67
+ $output .= self::table_row( $row, $formdata );
68
+
69
+ $output = self::table_wrap( $output );
70
+
71
+ return $output;
72
+ }
73
+
74
+ // Generates a table row
75
+ static function table_row( $args, $formdata = NULL ) {
76
+ return self::row_wrap( $args['title'], self::input( $args, $formdata ) );
77
+ }
78
+
79
+
80
+ // ____________WRAPPERS____________
81
+
82
+
83
+ // Wraps the given content in a <form><table>
84
+ static function form_table_wrap( $content, $nonce = 'update_options' ) {
85
+ $output = self::table_wrap( $content );
86
+ $output = self::form_wrap( $output, $nonce );
87
 
88
  return $output;
89
  }
90
 
91
  // Wraps the given content in a <form> tag
92
+ static function form_wrap( $content, $nonce = 'update_options' ) {
93
  $output = "\n<form method='post' action=''>\n";
94
  $output .= $content;
95
+ $output .= wp_nonce_field( $action = $nonce, $name = "_wpnonce", $referer = true , $echo = false );
96
  $output .= "\n</form>\n";
97
 
98
  return $output;
99
  }
100
 
101
+ // Wraps the given content in a <table>
102
+ static function table_wrap( $content ) {
103
+ $output = "\n<table class='form-table'>\n" . $content . "\n</table>\n";
104
+
105
+ return $output;
106
+ }
107
+
108
+ // Wraps the given content in a <tr><td>
109
+ static function row_wrap( $title, $content ) {
110
+ return "\n<tr>\n\t<th scope='row'>" . $title . "</th>\n\t<td>\n\t\t" . $content . "\t</td>\n\n</tr>";
111
+ }
112
+
113
 
114
  // ____________PRIVATE METHODS____________
115
 
116
 
117
  // Recursivly transform empty arrays to ''
118
+ private static function validate_data( $data ) {
119
+ if ( !is_array( $data ) )
120
  return $data;
121
 
122
+ if ( empty( $data ) )
123
  return '';
124
 
125
  foreach ( $data as $key => &$value )
126
+ $value = self::validate_data( $value );
127
 
128
  return $data;
129
  }
130
 
131
  // From multiple inputs to single inputs
132
  private static function _input() {
133
+ extract( wp_parse_args( self::$args, array(
134
  'name' => NULL,
135
  'value' => NULL,
136
  'desc' => NULL,
137
  'checked' => NULL,
138
+ ) ) );
139
 
140
+ $m_name = is_array( $name );
141
+ $m_value = is_array( $value );
142
+ $m_desc = is_array( $desc );
143
 
144
  // Correct name
145
  if ( !$m_name && $m_value
146
  && 'checkbox' == $type
147
+ && false === strpos( $name, '[' )
148
  )
149
  $args['name'] = $name = $name . '[]';
150
 
151
  // Expand names or values
152
  if ( !$m_name && !$m_value ) {
153
+ $a = array( $name => $value );
154
  }
155
  elseif ( $m_name && !$m_value ) {
156
+ $a = array_fill_keys( $name, $value );
157
  }
158
  elseif ( !$m_name && $m_value ) {
159
+ $a = array_fill_keys( $value, $name );
160
  }
161
  else {
162
+ $a = array_combine( $name, $value );
163
  }
164
 
165
  // Correct descriptions
166
  $_after = '';
167
+ if ( isset( $desc ) && !$m_desc && false === strpos( $desc, self::token ) ) {
168
  if ( $m_value ) {
169
  $_after = $desc;
170
  $args['desc'] = $desc = $value;
184
  $i2 = 'val';
185
  }
186
 
187
+ $func = in_array( $type, array( 'checkbox', 'radio' ) ) ? '_checkbox_single' : '_input_single';
188
 
189
  // Set constant args
190
+ $const_args = self::array_extract( self::$args, array( 'type', 'desc_pos', 'checked' ) );
191
+ if ( isset( $extra ) ) {
192
+ if ( !is_array( $extra ) )
193
+ $extra = self::attr_to_array( $extra );
194
+ $const_args['extra'] = $extra;
195
+ }
196
 
197
  $i = 0;
198
  foreach ( $a as $name => $val ) {
205
  $cur_args['value'] = $$i2;
206
 
207
  // Set desc
208
+ if ( is_array( $desc ) )
209
  $cur_args['desc'] = $desc[$i];
210
+ elseif ( isset( $desc ) )
211
  $cur_args['desc'] = $desc;
212
 
213
  // Find relevant formdata
214
  $match = NULL;
215
  if ( $checked === NULL ) {
216
+ $match = @self::$formdata[str_replace( '[]', '', $$i1 )];
217
+ if ( is_array( $match ) ) {
218
  $match = $match[$i];
219
  }
220
+ } else if ( is_array( $checked ) ) {
221
+ $cur_args['checked'] = isset( $checked[$i] ) && $checked[$i];
222
  }
223
 
224
+ $output[] = self::$func( $cur_args, $match );
225
 
226
  $i++;
227
  }
228
 
229
+ return implode( "\n", $output ) . $_after;
230
  }
231
 
232
  // Handle args for checkboxes and radio inputs
233
+ private static function _checkbox_single( $args, $data ) {
234
+ $args = wp_parse_args( $args, array(
235
  'name' => NULL,
236
  'value' => true,
237
  'desc_pos' => 'after',
238
  'desc' => NULL,
239
  'checked' => NULL,
240
  'extra' => array(),
241
+ ) );
242
 
243
  foreach ( $args as $key => &$val )
244
  $$key = &$val;
245
+ unset( $val );
246
 
247
  if ( $checked === NULL && $value == $data )
248
  $checked = true;
249
 
250
  if ( $checked )
251
+ $extra['checked'] = 'checked';
252
 
253
+ if ( is_null( $desc ) && !is_bool( $value ) )
254
+ $desc = str_replace( '[]', '', $value );
255
 
256
+ return self::_input_gen( $args );
257
  }
258
 
259
  // Handle args for text inputs
260
+ private static function _input_single( $args, $data ) {
261
+ $args = wp_parse_args( $args, array(
262
  'value' => $data,
263
  'desc_pos' => 'after',
264
+ 'extra' => array( 'class' => 'regular-text' ),
265
+ ) );
266
 
267
  foreach ( $args as $key => &$val )
268
  $$key = &$val;
269
+ unset( $val );
270
 
271
+ if ( FALSE === strpos( $name, '[' ) )
272
+ $extra['id'] = $name;
273
 
274
+ return self::_input_gen( $args );
275
  }
276
 
277
  // Generate html with the final args
278
+ private static function _input_gen( $args ) {
279
+ extract( wp_parse_args( $args, array(
280
  'name' => NULL,
281
  'value' => NULL,
282
  'desc' => NULL,
283
  'extra' => array()
284
+ ) ) );
285
 
286
+ $extra = self::array_to_attr( $extra );
287
 
288
  if ( 'textarea' == $type ) {
289
+ $value = esc_html( $value );
290
+ $input = "<textarea name='{$name}'{$extra}>{$value}</textarea>\n";
291
  }
292
  else {
293
+ $value = esc_attr( $value );
294
  $input = "<input name='{$name}' value='{$value}' type='{$type}'{$extra} /> ";
295
  }
296
 
297
+ return self::add_label( $input, $desc, $desc_pos );
298
  }
299
 
300
  private static function _select() {
301
+ extract( wp_parse_args( self::$args, array(
302
  'name' => '',
303
  'value' => array(),
304
  'text' => '',
305
+ 'selected' => array( 'foo' ), // hack to make default blank
306
+ 'extra' => array(),
307
  'numeric' => false, // use numeric array instead of associative
308
  'desc' => '',
309
  'desc_pos' => '',
310
+ ) ), EXTR_SKIP );
311
 
312
+ if ( empty( $value ) )
313
+ $value = array( '' => '' );
314
 
315
+ if ( !is_array( $value ) )
316
+ return trigger_error( "'value' argument is expected to be an array", E_USER_WARNING );
317
 
318
+ if ( !self::is_associative( $value ) && !$numeric )
319
+ $value = array_combine( $value, $value );
320
 
321
+ if ( isset( self::$formdata[$name] ) )
322
  $cur_val = self::$formdata[$name];
323
  else
324
  $cur_val = $selected;
326
  if ( false === $text ) {
327
  $opts = '';
328
  } else {
329
+ $opts = "\t<option value=''" . selected( $cur_val, array( 'foo' ), false ) . ">{$text}</option>\n";
 
 
 
330
  }
331
 
332
  foreach ( $value as $key => $value ) {
333
+ if ( empty( $key ) || empty( $value ) )
334
  continue;
335
 
336
+ $opts .= "\t<option value='{$key}'" . selected( (string) $key, (string) $cur_val, false) . '>' . $value . "</option>\n";
 
 
 
 
 
 
337
  }
338
 
339
+ if ( !is_array( $extra ) )
340
+ $extra = self::attr_to_array( $extra );
341
+ $extra = self::array_to_attr( $extra );
342
 
343
  $input = "<select name='{$name}'$extra>\n{$opts}</select>";
344
 
345
+ return self::add_label( $input, $desc, $desc_pos );
346
  }
347
 
348
+ private static function add_label( $input, $desc, $desc_pos ) {
349
+ if ( empty( $desc_pos ) )
350
  $desc_pos = 'after';
351
 
352
  $label = '';
353
+ if ( false === strpos( $desc, self::token ) ) {
354
+ switch ( $desc_pos ) {
355
  case 'before': $label = $desc . ' ' . self::token; break;
356
  case 'after': $label = self::token . ' ' . $desc;
357
  }
359
  $label = $desc;
360
  }
361
 
362
+ $label = trim( str_replace( self::token, $input, $label ) );
363
 
364
+ if ( empty( $desc ) )
365
  $output = $input . "\n";
366
  else
367
  $output = "<label>{$label}</label>\n";
369
  return $output;
370
  }
371
 
 
 
 
372
 
373
+ // Utilities
 
374
 
375
+
376
+ private static function attr_to_array( $html ) {
377
+ return shortcode_parse_atts( $html );
378
  }
379
 
380
+ private static function array_to_attr( $attr ) {
381
+ $out = '';
382
+ foreach ( $attr as $key => $value )
383
+ $out .= ' ' . $key . '=' . '"' . esc_attr( $value ) . '"';
384
+
385
+ return $out;
386
+ }
387
 
388
+ private static function is_associative( $array ) {
389
+ if ( !is_array( $array ) || empty( $array ) )
390
  return false;
391
 
392
+ $keys = array_keys( $array );
393
 
394
+ return array_keys( $keys ) !== $keys;
395
  }
396
 
397
+ private static function array_extract( $array, $keys ) {
398
  $r = array();
399
  foreach ( $keys as $key )
400
+ if ( isset( $array[$key] ) )
401
  $r[$key] = $array[$key];
402
 
403
  return $r;
405
  }
406
 
407
  // PHP < 5.2
408
+ if ( !function_exists( 'array_fill_keys' ) ) :
409
+ function array_fill_keys( $keys, $value ) {
410
+ if ( !is_array( $keys ) )
411
+ trigger_error( 'First argument is expected to be an array.' . gettype( $keys ) . 'given', E_USER_WARNING );
412
 
413
  $r = array();
414
  foreach ( $keys as $key )
scb/Options.php ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Documentation: http://scribu.net/wordpress/scb-framework/scb-options.html
4
+
5
+ class scbOptions {
6
+
7
+ protected $key; // the option name
8
+
9
+ protected $defaults; // the default value( s )
10
+
11
+ public $wp_filter_id; // used by WP hooks
12
+
13
+ /**
14
+ * Create a new set of options
15
+ *
16
+ * @param string $key Option name
17
+ * @param string $file Reference to main plugin file
18
+ * @param array $defaults An associative array of default values ( optional )
19
+ */
20
+ public function __construct( $key, $file, $defaults = '' ) {
21
+ $this->key = $key;
22
+ $this->defaults = $defaults;
23
+
24
+ scbUtil::add_activation_hook( $file, array( $this, '_update_reset' ) );
25
+
26
+ scbUtil::add_uninstall_hook( $file, array( $this, 'delete' ) );
27
+ }
28
+
29
+ /**
30
+ * Get option name
31
+ */
32
+ public function get_key() {
33
+ return $this->key;
34
+ }
35
+
36
+ /**
37
+ * Get option values for one, many or all fields
38
+ *
39
+ * @param string|array $field The field( s ) to get
40
+ * @return mixed Whatever is in those fields
41
+ */
42
+ public function get( $field = '' ) {
43
+ $data = get_option( $this->key );
44
+
45
+ if ( is_array( $this->defaults ) )
46
+ $data = ( array ) $data;
47
+
48
+ return $this->_get( $field, $data );
49
+ }
50
+
51
+ /**
52
+ * Get default values for one, many or all fields
53
+ *
54
+ * @param string|array $field The field( s ) to get
55
+ * @return mixed Whatever is in those fields
56
+ */
57
+ public function get_defaults( $field = '' ) {
58
+ return $this->_get( $field, $this->defaults );
59
+ }
60
+
61
+ /**
62
+ * Set all data fields, certain fields or a single field
63
+ *
64
+ * @param string|array $field The field to update or an associative array
65
+ * @param mixed $value The new value ( ignored if $field is array )
66
+ * @return null
67
+ */
68
+ public function set( $field, $value = '' ) {
69
+ if ( is_array( $field ) )
70
+ $newdata = $field;
71
+ else
72
+ $newdata = array( $field => $value );
73
+
74
+ $this->update( array_merge( $this->get(), $newdata ) );
75
+ }
76
+
77
+ /**
78
+ * Reset option to defaults
79
+ *
80
+ * @return null
81
+ */
82
+ public function reset() {
83
+ $this->update( $this->defaults, false );
84
+ }
85
+
86
+ /**
87
+ * Remove any keys that are not in the defaults array
88
+ *
89
+ * @return bool
90
+ */
91
+ public function cleanup() {
92
+ $this->update( $this->_clean( $this->get() ) );
93
+ }
94
+
95
+ /**
96
+ * Update raw data
97
+ *
98
+ * @param mixed $newdata
99
+ * @param bool $clean wether to remove unrecognized keys or not
100
+ * @return null
101
+ */
102
+ public function update( $newdata, $clean = true ) {
103
+ if ( $clean )
104
+ $newdata = $this->_clean( $newdata );
105
+
106
+ update_option( $this->key, $newdata );
107
+ }
108
+
109
+ /**
110
+ * Delete the option
111
+ *
112
+ * @return null
113
+ */
114
+ public function delete() {
115
+ delete_option( $this->key );
116
+ }
117
+
118
+
119
+ //_____INTERNAL METHODS_____
120
+
121
+
122
+ // Add new fields with their default values
123
+ function _update_reset() {
124
+ if ( is_array( $this->defaults ) )
125
+ $this->update( array_merge( $this->defaults, $this->get() ) );
126
+ else
127
+ add_option( $this->key, $this->defaults );
128
+ }
129
+
130
+ private function _clean( $data ) {
131
+ if ( !is_array( $data ) || !is_array( $this->defaults ) )
132
+ return $data;
133
+
134
+ $r = array();
135
+ foreach ( array_keys( $this->defaults ) as $key )
136
+ $r[$key] = @$data[$key];
137
+
138
+ return $r;
139
+ }
140
+
141
+ // Get one, more or all fields from an array
142
+ private function &_get( $field, $data ) {
143
+ if ( empty( $field ) )
144
+ return $data;
145
+
146
+ if ( is_string( $field ) )
147
+ return $data[$field];
148
+
149
+ foreach ( $field as $key )
150
+ if ( isset( $data[$key] ) )
151
+ $result[] = $data[$key];
152
+
153
+ return $result;
154
+ }
155
+
156
+ // Magic method: $options->field
157
+ function __get( $field ) {
158
+ return $this->get( $field );
159
+ }
160
+
161
+ // Magic method: $options->field = $value
162
+ function __set( $field, $value ) {
163
+ $this->set( $field, $value );
164
+ }
165
+
166
+ // Magic method: isset( $options->field )
167
+ function __isset( $field ) {
168
+ $data = $this->get();
169
+ return isset( $data[$field] );
170
+ }
171
+ }
172
+
scb/Query.php ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // A decorator for the WP_Query class
4
+ class scbQuery {
5
+ protected $wp_query;
6
+
7
+ public function __construct( $qv = '', $debug = false ) {
8
+ if ( $debug )
9
+ add_filter( 'posts_request', array( $this, '_debug' ), 999 );
10
+
11
+ if ( empty( $qv ) ) {
12
+ $this->_add_filters( true );
13
+ } else {
14
+ $this->_add_filters();
15
+ $this->wp_query = new WP_Query( $qv );
16
+ $this->_remove_filters();
17
+ }
18
+ }
19
+
20
+ /*
21
+ final public function __get( $key ) {
22
+ return $this->wp_query->$key;
23
+ }
24
+
25
+ final public function __call( $name, $args ) {
26
+ return call_user_func_array( array( $this->wp_query, $name ), $args );
27
+ }
28
+ */
29
+
30
+ public function _debug( $query ) {
31
+ remove_filter( current_filter(), array( $this, __FUNCTION__ ), 999 );
32
+
33
+ debug( $query );
34
+
35
+ return $query;
36
+ }
37
+
38
+ public function _add_filters( $runonce = false ) {
39
+ if ( $runonce )
40
+ foreach ( $this->_find_filters() as $filter )
41
+ add_filter( $filter, array( $this, '_dispatch' ), 10, 10 );
42
+ else
43
+ foreach ( $this->_find_filters() as $filter )
44
+ add_filter( $filter, array( $this, $filter ), 10, 10 );
45
+ }
46
+
47
+ public function _dispatch( $value ) {
48
+ $filter = current_filter();
49
+
50
+ remove_filter( $filter, array( $this, '_dispatch' ), 10, 10 );
51
+
52
+ return $this->$filter( $value );
53
+ }
54
+
55
+ public function _remove_filters() {
56
+ foreach ( $this->_find_filters() as $filter )
57
+ remove_filter( $filter, array( $this, $filter ), 10, 10 );
58
+ }
59
+
60
+ private function _find_filters() {
61
+ $filters = array();
62
+
63
+ foreach ( _scb_get_public_methods( $this ) as $method )
64
+ if ( '_' != substr( $method, 0, 1 ) )
65
+ $filters[] = $method;
66
+
67
+ return $filters;
68
+ }
69
+ }
70
+
71
+ // Current scope is lost while calling external function
72
+ function _scb_get_public_methods( $class ) {
73
+ return get_class_methods( $class );
74
+ }
75
+
scb/Rewrite.php ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Helper class for modifying the rewrite rules
4
+ abstract class scbRewrite {
5
+
6
+ public function __construct( $plugin_file = '' ) {
7
+
8
+ add_action( 'init', array( $this, 'generate' ) );
9
+ add_action( 'generate_rewrite_rules', array( $this, 'generate' ) );
10
+
11
+ if ( $plugin_file )
12
+ scbUtil::add_activation_hook( $plugin_file, array( __CLASS__, 'flush' ) );
13
+ }
14
+
15
+ // This is where the actual code goes
16
+ abstract public function generate();
17
+
18
+ static public function flush() {
19
+ global $wp_rewrite;
20
+
21
+ $wp_rewrite->flush_rules();
22
+ }
23
+ }
24
+
scb/Table.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Takes care of creating, updating and deleting database tables
4
+ class scbTable {
5
+ protected $name;
6
+ protected $columns;
7
+ protected $upgrade_method;
8
+
9
+ function __construct( $name, $file, $columns, $upgrade_method = 'dbDelta' ) {
10
+ global $wpdb;
11
+
12
+ $this->name = $wpdb->$name = $wpdb->prefix . $name;
13
+ $this->columns = $columns;
14
+ $this->upgrade_method = $upgrade_method;
15
+
16
+ scbUtil::add_activation_hook( $file, array( $this, 'install' ) );
17
+ scbUtil::add_uninstall_hook( $file, array( $this, 'uninstall' ) );
18
+ }
19
+
20
+ function install() {
21
+ global $wpdb;
22
+
23
+ $charset_collate = '';
24
+ if ( $wpdb->has_cap( 'collation' ) ) {
25
+ if ( ! empty( $wpdb->charset ) )
26
+ $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
27
+ if ( ! empty( $wpdb->collate ) )
28
+ $charset_collate .= " COLLATE $wpdb->collate";
29
+ }
30
+
31
+ if ( 'dbDelta' == $this->upgrade_method ) {
32
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
33
+ dbDelta( "CREATE TABLE $this->name ( $this->columns ) $charset_collate" );
34
+ return;
35
+ }
36
+
37
+ if ( 'delete_first' == $this->upgrade_method )
38
+ $wpdb->query( "DROP TABLE IF EXISTS $this->name;" );
39
+
40
+ $wpdb->query( "CREATE TABLE IF NOT EXISTS $this->name ( $this->columns ) $charset_collate;" );
41
+ }
42
+
43
+ function uninstall() {
44
+ global $wpdb;
45
+
46
+ $wpdb->query( "DROP TABLE IF EXISTS $this->name" );
47
+ }
48
+ }
49
+
scb/Util.php CHANGED
@@ -3,73 +3,77 @@
3
  class scbUtil {
4
 
5
  // Force script enqueue
6
- static function do_scripts($handles) {
7
  global $wp_scripts;
8
 
9
- if ( ! is_a($wp_scripts, 'WP_Scripts') )
10
  $wp_scripts = new WP_Scripts();
11
 
12
- $wp_scripts->do_items((array) $handles);
13
  }
14
 
15
  // Force style enqueue
16
- static function do_styles($handles) {
17
- self::do_scripts('jquery');
18
 
19
  global $wp_styles;
20
 
21
- if ( ! is_a($wp_styles, 'WP_Styles') )
22
  $wp_styles = new WP_Styles();
23
 
24
  ob_start();
25
- $wp_styles->do_items((array) $handles);
26
- $content = str_replace(array('"', "\n"), array("'", ''), ob_get_clean());
27
 
28
  echo "<script type='text/javascript'>\n";
29
- echo "jQuery(document).ready(function($) {\n";
30
- echo "$('head').prepend(\"$content\");\n";
31
- echo "});\n";
32
  echo "</script>";
33
  }
34
 
 
 
 
 
35
 
36
  // Have more than one uninstall hooks; also prevents an UPDATE query on each page load
37
- static function add_uninstall_hook($plugin, $callback) {
38
- register_uninstall_hook($plugin, '__return_false'); // dummy
39
 
40
- add_action('uninstall_' . plugin_basename($plugin), $callback);
41
  }
42
 
43
- // Apply a function to each element of a (nested) array recursively
44
- static function array_map_recursive($callback, $array) {
45
- array_walk_recursive($array, array(__CLASS__, 'array_map_recursive_helper'), $callback);
46
 
47
  return $array;
48
  }
49
 
50
- static function array_map_recursive_helper(&$val, $key, $callback) {
51
- $val = call_user_func($callback, $val);
52
  }
53
 
54
  // Extract certain $keys from $array
55
- static function array_extract($array, $keys) {
56
  $r = array();
57
 
58
  foreach ( $keys as $key )
59
- if ( array_key_exists($key, $array) )
60
  $r[$key] = $array[$key];
61
 
62
  return $r;
63
  }
64
 
65
  // Extract a certain value from a list of arrays
66
- static function array_pluck($array, $key) {
67
  $r = array();
68
 
69
  foreach ( $array as $value ) {
70
- if ( is_object($value) )
71
- $value = get_object_vars($value);
72
- if ( array_key_exists($key, $value) )
73
  $r[] = $value[$key];
74
  }
75
 
@@ -77,7 +81,7 @@ class scbUtil {
77
  }
78
 
79
  // Transform a list of objects into an associative array
80
- static function objects_to_assoc($objects, $key, $value) {
81
  $r = array();
82
 
83
  foreach ( $objects as $obj )
@@ -87,24 +91,24 @@ class scbUtil {
87
  }
88
 
89
  // Prepare an array for an IN statement
90
- static function array_to_sql($values) {
91
  foreach ( $values as &$val )
92
- $val = "'" . esc_sql(trim($val)) . "'";
93
 
94
- return implode(',', $values);
95
  }
96
 
97
- // Example: split_at('</', '<a></a>') => array('<a>', '</a>')
98
- static function split_at($delim, $str) {
99
- $i = strpos($str, $delim);
100
 
101
  if ( false === $i )
102
  return false;
103
 
104
- $start = substr($str, 0, $i);
105
- $finish = substr($str, $i);
106
 
107
- return array($start, $finish);
108
  }
109
  }
110
 
@@ -112,21 +116,21 @@ class scbUtil {
112
  //_____Minimalist HTML framework_____
113
 
114
 
115
- if ( ! function_exists('html') ):
116
- function html($tag, $content = '') {
117
- list($closing) = explode(' ', $tag, 2);
118
 
119
  return "<{$tag}>{$content}</{$closing}>";
120
  }
121
  endif;
122
 
123
  // Generate an <a> tag
124
- if ( ! function_exists('html_link') ):
125
- function html_link($url, $title = '') {
126
- if ( empty($title) )
127
  $title = $url;
128
 
129
- return sprintf("<a href='%s'>%s</a>", esc_url($url), $title);
130
  }
131
  endif;
132
 
@@ -135,28 +139,28 @@ endif;
135
 
136
 
137
  // WP < 3.0
138
- if ( ! function_exists('__return_false') ) :
139
  function __return_false() {
140
  return false;
141
  }
142
  endif;
143
 
144
  // WP < ?
145
- if ( ! function_exists('__return_true') ) :
146
  function __return_true() {
147
  return true;
148
  }
149
  endif;
150
 
151
  // WP < ?
152
- if ( ! function_exists('set_post_field') ) :
153
- function set_post_field($field, $value, $post_id) {
154
  global $wpdb;
155
 
156
- $post_id = absint($post_id);
157
- $value = sanitize_post_field($field, $value, $post_id, 'db');
158
 
159
- return $wpdb->update($wpdb->posts, array($field => $value), array('ID' => $post_id));
160
  }
161
  endif;
162
 
3
  class scbUtil {
4
 
5
  // Force script enqueue
6
+ static function do_scripts( $handles ) {
7
  global $wp_scripts;
8
 
9
+ if ( ! is_a( $wp_scripts, 'WP_Scripts' ) )
10
  $wp_scripts = new WP_Scripts();
11
 
12
+ $wp_scripts->do_items( ( array ) $handles );
13
  }
14
 
15
  // Force style enqueue
16
+ static function do_styles( $handles ) {
17
+ self::do_scripts( 'jquery' );
18
 
19
  global $wp_styles;
20
 
21
+ if ( ! is_a( $wp_styles, 'WP_Styles' ) )
22
  $wp_styles = new WP_Styles();
23
 
24
  ob_start();
25
+ $wp_styles->do_items( ( array ) $handles );
26
+ $content = str_replace( array( '"', "\n" ), array( "'", '' ), ob_get_clean() );
27
 
28
  echo "<script type='text/javascript'>\n";
29
+ echo "jQuery( document ).ready( function( $ ) {\n";
30
+ echo "$( 'head' ).prepend( \"$content\" );\n";
31
+ echo "} );\n";
32
  echo "</script>";
33
  }
34
 
35
+ // Enable delayed activation ( to be used with scb_init() )
36
+ static function add_activation_hook( $plugin, $callback ) {
37
+ add_action( 'scb_activation_' . plugin_basename( $plugin ), $callback );
38
+ }
39
 
40
  // Have more than one uninstall hooks; also prevents an UPDATE query on each page load
41
+ static function add_uninstall_hook( $plugin, $callback ) {
42
+ register_uninstall_hook( $plugin, '__return_false' ); // dummy
43
 
44
+ add_action( 'uninstall_' . plugin_basename( $plugin ), $callback );
45
  }
46
 
47
+ // Apply a function to each element of a ( nested ) array recursively
48
+ static function array_map_recursive( $callback, $array ) {
49
+ array_walk_recursive( $array, array( __CLASS__, 'array_map_recursive_helper' ), $callback );
50
 
51
  return $array;
52
  }
53
 
54
+ static function array_map_recursive_helper( &$val, $key, $callback ) {
55
+ $val = call_user_func( $callback, $val );
56
  }
57
 
58
  // Extract certain $keys from $array
59
+ static function array_extract( $array, $keys ) {
60
  $r = array();
61
 
62
  foreach ( $keys as $key )
63
+ if ( array_key_exists( $key, $array ) )
64
  $r[$key] = $array[$key];
65
 
66
  return $r;
67
  }
68
 
69
  // Extract a certain value from a list of arrays
70
+ static function array_pluck( $array, $key ) {
71
  $r = array();
72
 
73
  foreach ( $array as $value ) {
74
+ if ( is_object( $value ) )
75
+ $value = get_object_vars( $value );
76
+ if ( array_key_exists( $key, $value ) )
77
  $r[] = $value[$key];
78
  }
79
 
81
  }
82
 
83
  // Transform a list of objects into an associative array
84
+ static function objects_to_assoc( $objects, $key, $value ) {
85
  $r = array();
86
 
87
  foreach ( $objects as $obj )
91
  }
92
 
93
  // Prepare an array for an IN statement
94
+ static function array_to_sql( $values ) {
95
  foreach ( $values as &$val )
96
+ $val = "'" . esc_sql( trim( $val ) ) . "'";
97
 
98
+ return implode( ',', $values );
99
  }
100
 
101
+ // Example: split_at( '</', '<a></a>' ) => array( '<a>', '</a>' )
102
+ static function split_at( $delim, $str ) {
103
+ $i = strpos( $str, $delim );
104
 
105
  if ( false === $i )
106
  return false;
107
 
108
+ $start = substr( $str, 0, $i );
109
+ $finish = substr( $str, $i );
110
 
111
+ return array( $start, $finish );
112
  }
113
  }
114
 
116
  //_____Minimalist HTML framework_____
117
 
118
 
119
+ if ( ! function_exists( 'html' ) ):
120
+ function html( $tag, $content = '' ) {
121
+ list( $closing ) = explode( ' ', $tag, 2 );
122
 
123
  return "<{$tag}>{$content}</{$closing}>";
124
  }
125
  endif;
126
 
127
  // Generate an <a> tag
128
+ if ( ! function_exists( 'html_link' ) ):
129
+ function html_link( $url, $title = '' ) {
130
+ if ( empty( $title ) )
131
  $title = $url;
132
 
133
+ return sprintf( "<a href='%s'>%s</a>", esc_url( $url ), $title );
134
  }
135
  endif;
136
 
139
 
140
 
141
  // WP < 3.0
142
+ if ( ! function_exists( '__return_false' ) ) :
143
  function __return_false() {
144
  return false;
145
  }
146
  endif;
147
 
148
  // WP < ?
149
+ if ( ! function_exists( '__return_true' ) ) :
150
  function __return_true() {
151
  return true;
152
  }
153
  endif;
154
 
155
  // WP < ?
156
+ if ( ! function_exists( 'set_post_field' ) ) :
157
+ function set_post_field( $field, $value, $post_id ) {
158
  global $wpdb;
159
 
160
+ $post_id = absint( $post_id );
161
+ $value = sanitize_post_field( $field, $value, $post_id, 'db' );
162
 
163
+ return $wpdb->update( $wpdb->posts, array( $field => $value ), array( 'ID' => $post_id ) );
164
  }
165
  endif;
166
 
scb/Widget.php ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Adds compatibility methods between WP_Widget and scbForms
4
+
5
+ abstract class scbWidget extends WP_Widget {
6
+ protected $defaults = array();
7
+
8
+ private static $scb_widgets = array();
9
+
10
+ static function init( $class, $file = '', $base = '' ) {
11
+ self::$scb_widgets[] = $class;
12
+
13
+ add_action( 'widgets_init', array( __CLASS__, '_scb_register' ) );
14
+
15
+ // for auto-uninstall
16
+ if ( $file && $base && class_exists( 'scbOptions' ) )
17
+ new scbOptions( "widget_$base", $file );
18
+ }
19
+
20
+ static function _scb_register() {
21
+ foreach ( self::$scb_widgets as $widget )
22
+ register_widget( $widget );
23
+ }
24
+
25
+ // A pre-filled method, for convenience
26
+ function widget( $args, $instance ) {
27
+ $instance = wp_parse_args( $instance, $this->defaults );
28
+
29
+ extract( $args );
30
+
31
+ echo $before_widget;
32
+
33
+ $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
34
+
35
+ if ( ! empty( $title ) )
36
+ echo $before_title . $title . $after_title;
37
+
38
+ $this->content( $instance );
39
+
40
+ echo $after_widget;
41
+ }
42
+
43
+ // This is where the actual widget content goes
44
+ function content( $instance ) {}
45
+
46
+
47
+ //_____HELPER METHODS_____
48
+
49
+
50
+ // See scbForms::input()
51
+ // Allows extra parameter $args['title']
52
+ protected function input( $args, $formdata = array() ) {
53
+ // Add default class
54
+ if ( !isset( $args['extra'] ) )
55
+ $args['extra'] = 'class="regular-text"';
56
+
57
+ // Add default label position
58
+ if ( !in_array( $args['type'], array( 'checkbox', 'radio' ) ) && empty( $args['desc_pos'] ) )
59
+ $args['desc_pos'] = 'before';
60
+
61
+ // Then add prefix to names and formdata
62
+ $new_formdata = array();
63
+ foreach ( ( array ) $args['name'] as $name )
64
+ $new_formdata[$this->scb_get_field_name( $name )] = @$formdata[$name];
65
+ $new_names = array_keys( $new_formdata );
66
+
67
+ // Finally, replace the old names
68
+ if ( 1 == count( $new_names ) )
69
+ $args['name'] = $new_names[0];
70
+ else
71
+ $args['name'] = $new_names;
72
+
73
+ return scbForms::input( $args, $new_formdata );
74
+ }
75
+
76
+
77
+ //_____INTERNAL METHODS_____
78
+
79
+
80
+ private function scb_get_field_name( $name ) {
81
+ if ( $split = scbUtil::split_at( '[', $name ) )
82
+ list( $basename, $extra ) = $split;
83
+ else
84
+ return $this->get_field_name( $name );
85
+
86
+ return str_replace( '[]', '', $this->get_field_name( $basename ) ) . $extra;
87
+ }
88
+ }
89
+
scb/load.php CHANGED
@@ -1,69 +1,84 @@
1
  <?php
2
- if ( !class_exists('scbLoad3') ) :
3
- class scbLoad3 {
 
 
 
 
 
 
4
 
5
  private static $candidates;
 
 
 
6
  private static $loaded;
7
- private static $initial_load;
8
-
9
- static function init($rev, $file, $classes) {
10
- if ( $path = get_option('scb-framework') && !self::$initial_load ) {
11
- if ( $path != __FILE__ )
12
- include $path;
13
 
14
- self::$initial_load = true;
15
- }
16
 
17
  self::$candidates[$file] = $rev;
 
 
 
 
18
 
19
- self::load(dirname($file) . '/', $classes);
 
20
 
21
- add_action('deactivate_plugin', array(__CLASS__, 'deactivate'));
22
- add_action('update_option_active_plugins', array(__CLASS__, 'reorder'));
23
  }
24
 
25
- static function deactivate($plugin) {
26
- $plugin = dirname($plugin);
27
 
28
- if ( '.' == $plugin )
29
  return;
30
 
31
- foreach ( self::$candidates as $path => $rev )
32
- if ( plugin_basename(dirname(dirname($path))) == $plugin )
33
- unset(self::$candidates[$path]);
 
 
 
 
34
  }
35
 
36
- static function reorder() {
37
- arsort(self::$candidates);
38
 
39
- update_option('scb-framework', key(self::$candidates));
40
- }
41
-
42
- private static function load($path, $classes) {
43
- foreach ( $classes as $class_name ) {
44
- if ( class_exists($class_name) )
45
- continue;
46
 
47
- $fpath = $path . substr($class_name, 3) . '.php';
48
 
49
- if ( file_exists($fpath) ) {
50
- self::$loaded[$class_name] = $fpath;
 
 
 
 
51
  include $fpath;
 
52
  }
53
  }
 
 
 
 
54
  }
55
 
56
  static function get_info() {
57
- arsort(self::$candidates);
58
 
59
- return array(get_option('scb-framework'), self::$loaded, self::$candidates);
60
  }
61
  }
62
  endif;
63
 
64
- scbLoad3::init(14, __FILE__, array(
65
- 'scbUtil', 'scbOptions', 'scbForms', 'scbTable', 'scbDebug',
66
- 'scbWidget', 'scbAdminPage', 'scbBoxesPage',
67
- 'scbQuery', 'scbRewrite', 'scbCron',
68
- ));
69
 
1
  <?php
2
+
3
+ $GLOBALS['_scb_data'] = array( 22, __FILE__, array(
4
+ 'scbUtil', 'scbOptions', 'scbForms', 'scbTable',
5
+ 'scbWidget', 'scbAdminPage', 'scbBoxesPage',
6
+ 'scbQuery', 'scbRewrite', 'scbCron', ) );
7
+
8
+ if ( !class_exists( 'scbLoad4' ) ) :
9
+ class scbLoad4 {
10
 
11
  private static $candidates;
12
+ private static $classes;
13
+ private static $callbacks = array();
14
+
15
  private static $loaded;
 
 
 
 
 
 
16
 
17
+ static function init( $callback = '' ) {
18
+ list( $rev, $file, $classes ) = $GLOBALS['_scb_data'];
19
 
20
  self::$candidates[$file] = $rev;
21
+ self::$classes[$file] = $classes;
22
+
23
+ if ( !empty( $callback ) ) {
24
+ self::$callbacks[$file] = $callback;
25
 
26
+ add_action( 'activate_plugin', array( __CLASS__, 'delayed_activation' ) );
27
+ }
28
 
29
+ // TODO: don't load when activating a plugin ?
30
+ add_action( 'plugins_loaded', array( __CLASS__, 'load' ), 10, 0 );
31
  }
32
 
33
+ static function delayed_activation( $plugin ) {
34
+ $plugin_dir = dirname( $plugin );
35
 
36
+ if ( '.' == $plugin_dir )
37
  return;
38
 
39
+ foreach ( self::$callbacks as $file => $callback )
40
+ if ( plugin_basename( dirname( dirname( $file ) ) ) == $plugin_dir ) {
41
+ self::load( false );
42
+ call_user_func( $callback );
43
+ do_action( 'scb_activation_' . $plugin );
44
+ break;
45
+ }
46
  }
47
 
48
+ static function load( $do_callbacks = true ) {
49
+ arsort( self::$candidates );
50
 
51
+ $file = key( self::$candidates );
 
 
 
 
 
 
52
 
53
+ $path = dirname( $file ) . '/';
54
 
55
+ foreach ( self::$classes[$file] as $class_name ) {
56
+ if ( class_exists( $class_name ) )
57
+ continue;
58
+
59
+ $fpath = $path . substr( $class_name, 3 ) . '.php';
60
+ if ( file_exists( $fpath ) ) {
61
  include $fpath;
62
+ self::$loaded[] = $fpath;
63
  }
64
  }
65
+
66
+ if ( $do_callbacks )
67
+ foreach ( self::$callbacks as $callback )
68
+ call_user_func( $callback );
69
  }
70
 
71
  static function get_info() {
72
+ arsort( self::$candidates );
73
 
74
+ return array( self::$loaded, self::$candidates );
75
  }
76
  }
77
  endif;
78
 
79
+ if ( !function_exists( 'scb_init' ) ) :
80
+ function scb_init( $callback = '' ) {
81
+ scbLoad4::init( $callback );
82
+ }
83
+ endif;
84
 
screenshot-1.png ADDED
Binary file