-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-connector-example.php
146 lines (133 loc) · 4.4 KB
/
stream-connector-example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Plugin Name: Stream Connector - Example
* Depends: Stream
* Plugin URI: http://x-team.com
* Description: This plugin adds a random post generator which logs entries in your Stream.
* Version: 0.1.0
* Author: X-Team
* Author URI: http://wp-stream.com/
* License: GPLv2+
* Text Domain: stream-connector-example
* Domain Path: /languages
*/
/**
* How to use this plugin
*
* This plugin has been made to be copied and changed to suit your own needs.
* To get started, copy connectors/example.php into your own plugin, then copy
* the register_stream_connector() function from stream-connector-example.php
* and hook it into your own plugin via the plugins_loaded action.
*
* The good stuff is in connectors/example.php - once you've copied out the
* register_stream_connector() function, you can ignore the rest of this file.
*
* To see this connector in action, activate it from your Plugins screen, and
* choose Stream Example from the admin menu
*/
class Stream_Example_Plugin {
/**
* Class constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'register_stream_connector' ) );
add_action( 'admin_menu', array( $this, 'register_menu' ) );
}
/**
* If Stream is active, register the Stream Connector
*
* @action plugins_loaded
*/
public function register_stream_connector() {
if ( ! class_exists( 'WP_Stream' ) ) {
return;
}
add_filter(
'wp_stream_connectors',
function( $classes ) {
include dirname( __FILE__ ) . '/connectors/example.php';
$classes[] = 'WP_Stream_Connector_Example';
return $classes;
}
);
}
/**
* Register menu item
*
* @action admin_menu
*/
public function register_menu() {
add_menu_page(
__( 'Random Post Generator', 'stream-connector-example' ),
__( 'Stream Example', 'stream-connector-example' ),
WP_Stream_Admin::VIEW_CAP,
'example',
array( $this, 'page' ),
'',
3
);
}
/**
* Render Random Number Generator Page
*/
public function page() {
?>
<div class="wrap">
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<?php
if ( isset( $_GET['type'] ) && isset( $_GET['id'] ) ) {
$type = 'page' === $_GET['type'] ? 'page' : 'post';
if ( 'random' === $_GET['id'] ) {
$post_rand = get_posts(
array(
'posts_per_page' => 1,
'post_type' => $type,
'orderby' => 'rand'
)
);
$post_id = $post_rand[0]->ID;
} else {
$post_id = (int) filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
}
$post_id = apply_filters( 'example_post_id', $post_id, $type );
do_action( 'example_after_generate_post_id', $post_id, $type );
echo '<div id="message" class="updated below-h2"><p>' . sprintf( __( 'The %s is', 'stream-connector-example' ), $type ) . ' <strong>' . get_the_title( $post_id ) . '</strong></p></div>';
}
$post_choices = get_posts(
array(
'posts_per_page' => 3,
'post_type' => 'post',
'orderby' => 'rand'
)
);
$page_choices = get_posts(
array(
'posts_per_page' => 3,
'post_type' => 'page',
'orderby' => 'rand'
)
);
?>
<div class="posts">
<h3><?php _e( 'Posts', 'stream-connector-example' ); ?></h3>
<p class="description"><?php _e( 'Choose a post.', 'stream-connector-example' ); ?></p>
<a href="<?php echo add_query_arg( array( 'type' => 'post', 'id' => 'random' ) ); ?>" class="button button-primary"><?php _e( 'Random', 'stream-connector-example' ); ?></a>
<?php foreach ( $post_choices as $post ): ?>
<a href="<?php echo add_query_arg( array( 'type' => 'post', 'id' => $post->ID ) ); ?>" class="button"><?php echo get_the_title( $post->ID ); ?></a>
<?php endforeach; ?>
</div>
<br />
<div class="pages">
<h3><?php _e( 'Pages', 'stream-connector-example' ); ?></h3>
<p class="description"><?php _e( 'Choose a page.', 'stream-connector-example' ); ?></p>
<a href="<?php echo add_query_arg( array( 'type' => 'page', 'id' => 'random' ) ); ?>" class="button button-primary"><?php _e( 'Random', 'stream-connector-example' ); ?></a>
<?php foreach ( $page_choices as $page ): ?>
<a href="<?php echo add_query_arg( array( 'type' => 'page', 'id' => $page->ID ) ); ?>" class="button"><?php echo get_the_title( $page->ID ); ?></a>
<?php endforeach; ?>
</div>
<br />
</div>
<?php
}
}
$GLOBALS['stream_example_plugin'] = new Stream_Example_Plugin;