我正在编写一个插件,用于向邀请对象发送邀请,当单击链接时,该邀请对象会打开一个表单。我按照@toscho在Report Broken Video插件中给出的代码封装了类中的所有函数。相关代码如下:
/*
Plugin Name: Send Invitation
Plugin URI: http://w3boutique.net
Description: Emails a the link of the current page to a friend
Author: Nandakumar Chandrasekhar
Version: 0.1
Author URI: http://w3boutique.net/about-nanda.html
License: GPL2
*/
// include() or require() any necessary files here
// Settings and/or Configuration Details go here
define('SEND_INVITATION_MIN_WORDPRESS_VERSION', '3.1.1');
define('SEND_INVITATION_PLUGIN_URL', plugins_url('', __FILE__));
add_action( 'init', array( 'SendInvitation', 'nc_sendinvitation_init' ) );
class SendInvitation {
protected $nonce_name = 'nc_sendinvitation';
protected $post_url = '';
public static function nc_sendinvitation_init() {
new self;
}
public function __construct() {
add_action( 'init', array(&$this, 'nc_sendinvitation_head' ));
add_action( 'init', array(&$this, 'nc_sendinvitation_check_wordpress_version' ));
add_action( 'init', array(&$this, 'nc_sendinvitation_form_action' ));
//$this->post_url = $this->nc_sendinvitation_get_post_url();
}
public function nc_sendinvitation_head() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'send_invitation_js',
plugins_url( 'js/send-invitation.js', __FILE__ ),
array( 'jquery' ) );
wp_enqueue_style( 'send_invitation_css',
plugins_url( 'css/send-invitation.css', __FILE__ ) );
}
public function nc_sendinvitation_check_wordpress_version() {
global $wp_version;
$exit_msg = 'Send Invitation requires version '
. SEND_INVITATION_MIN_WORDPRESS_VERSION
. 'or newer <a href="http://codex.wordpress.org/Upgrading_WordPress">Please
update!</a>';
if ( version_compare( $wp_version, SEND_INVITATION_MIN_WORDPRESS_VERSION, '<') )
{
exit( $exit_msg );
}
}
public function nc_sendinvitation_form_action() {
$action = '';
if ( $_SERVER['REQUEST_METHOD'] != 'POST' )
{
$action = $this->nc_sendinvitation_get_form();
}
else if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$action = $this->nc_sendinvitation_handle_submit();
}
return $action;
}
public function nc_sendinvitation_get_form() {
// Start the output buffer, prevents content being output directly into
// script, instead it is stored in a buffer which can then be flushed
// to allow the include file to be stored in a variable
// See http://www.codingforums.com/showthread.php?t=124537
ob_start();
include('send-invitation-form.php');
$send_invitation_link = ob_get_clean();
return $send_invitation_link;
}
public function nc_sendinvitation_handle_submit() {
if ( isset( $_POST['form_type'] ) && ( $_POST['form_type'] == 'nc_sendinvitation' ) ) {
$to = 'navanitachora@gamil.com';
$subject = 'Invitation to SwanLotus';
$message = 'Navanitachora invites you to take a look at this link';
wp_mail($to, $subject, $message);
$result = 'Email was sent successfully';
}
else {
$result = $this->nc_sendinvitation_get_form();
}
return $result;
}
public function nc_sendinvitation_get_post_url() {
global $post;
$blog_id = get_option('page_for_posts');
$post_id = '';
if (is_home($blog_id)) {
$post_id = $blog_id;
} else {
$post_id = $post->ID;
}
return get_permalink($post_id);
}
}
/* End of File */
?>
我不知道如何在模板中使用此类,以便显示表单。我知道我需要实例化该类,但是我不确定将代码放在何处以及如何访问该对象,以便可以在模板中使用它。我具有OOP知识,但之前没有在此上下文中使用过它,并且需要一点点逐步指导。
非常感谢。
plugin_action_demo
in 的目的是add_action()
什么?如何do_action( 'print_foo', 50 );
知道该动作plugin_action_demo
与该名称无关?