59*(ALO_EM_INTERVAL_MIN),
'display' => 'EasyMail every ' .ALO_EM_INTERVAL_MIN. ' minutes'
);
return $schedules;
}
add_filter('cron_schedules', 'alo_em_more_reccurences');
/**
* To fix missing cron schedules
*/
function alo_em_check_cron_scheduled() {
if( !wp_next_scheduled( 'alo_em_batch' ) ) {
wp_schedule_event( time() +60, 'alo_em_interval', 'alo_em_batch' );
}
if( !wp_next_scheduled( 'alo_em_schedule' ) ) {
wp_schedule_event(time(), 'twicedaily', 'alo_em_schedule');
}
}
add_action('wp', 'alo_em_check_cron_scheduled');
/**
* On plugin activation
*/
function alo_em_install() {
global $wpdb, $wp_roles;
if (!get_option('alo_em_template')) add_option('alo_em_template', 'Hi [USER-NAME],
I have published a new post [POST-TITLE].
[POST-EXCERPT]
Please visit my site [SITE-LINK] to read it and leave your comment about it.
Hope to see you online!
[SITE-LINK]');
if (!get_option('alo_em_list')) add_option('alo_em_list', '');
if (!get_option('alo_em_lastposts')) add_option('alo_em_lastposts', 10);
if (!get_option('alo_em_dayrate')) add_option('alo_em_dayrate', 2000);
if (!get_option('alo_em_batchrate')) add_option('alo_em_batchrate', 25);
if (!get_option('alo_em_sleepvalue')) add_option('alo_em_sleepvalue', 0);
if (!get_option('alo_em_sender_email')) {
$admin_email = get_option('admin_email');
add_option('alo_em_sender_email', $admin_email);
}
if (!get_option('alo_em_sender_name')) {
$sender_name = get_option('blogname');
add_option('alo_em_sender_name', $sender_name );
}
update_option('alo_em_import_alert', "show" );
update_option('alo_em_timeout_alert', "show" );
if (!get_option('alo_em_delete_on_uninstall')) add_option('alo_em_delete_on_uninstall', 'no');
if (!get_option('alo_em_show_subscripage')) add_option('alo_em_show_subscripage', 'no');
if (!get_option('alo_em_embed_css')) add_option('alo_em_embed_css', 'no');
if (!get_option('alo_em_no_activation_mail')) add_option('alo_em_no_activation_mail', 'no');
if (!get_option('alo_em_show_credit_banners')) add_option('alo_em_show_credit_banners', 'no');
if (!get_option('alo_em_filter_br')) add_option('alo_em_filter_br', 'no');
if (!get_option('alo_em_filter_the_content')) add_option('alo_em_filter_the_content', 'yes');
if (!get_option('alo_em_js_rec_list')) add_option('alo_em_js_rec_list', 'ajax_normal');
if (!get_option('alo_em_use_themes')) add_option('alo_em_use_themes', 'yes');
if (!get_option('alo_em_publish_newsletters')) add_option('alo_em_publish_newsletters', 'yes');
alo_em_setup_predomain_texts( false );
if ( alo_em_db_tables_need_update() ) alo_em_install_db_tables();
//-------------------------------------------------------------------------
// Create/update the page with subscription
// check if page already exists
$my_page_id = get_option('alo_em_subsc_page');
$my_page = array();
$my_page['post_title'] = 'Newsletter';
$my_page['post_content'] = '[ALO-EASYMAIL-PAGE]';
$my_page['post_status'] = 'publish';
$my_page['post_author'] = 1;
$my_page['comment_status'] = 'closed';
$my_page['post_type'] = 'page';
if ( !$my_page_id ) { // insert the post into the database
$my_page_id = wp_insert_post( $my_page );
update_option('alo_em_subsc_page', $my_page_id);
}
// add scheduled cleaner
if( !wp_next_scheduled( 'alo_em_schedule' ) ) wp_schedule_event(time(), 'twicedaily', 'alo_em_schedule');
// add scheduled cron batch
if( !wp_next_scheduled( 'alo_em_batch' ) ) wp_schedule_event( time() +60, 'alo_em_interval', 'alo_em_batch' );
// default permissions
$wp_roles->add_cap( 'administrator', 'manage_newsletter_options');
$wp_roles->add_cap( 'administrator', 'manage_newsletter_subscribers');
$wp_roles->add_cap( 'administrator', 'publish_newsletters');
$wp_roles->add_cap( 'administrator', 'edit_newsletters');
$wp_roles->add_cap( 'administrator', 'edit_others_newsletters');
$wp_roles->add_cap( 'administrator', 'delete_newsletters');
$wp_roles->add_cap( 'administrator', 'delete_others_newsletters');
$wp_roles->add_cap( 'administrator', 'read_private_newsletters');
$wp_roles->add_cap( 'editor', 'publish_newsletters');
$wp_roles->add_cap( 'editor', 'edit_newsletters');
$wp_roles->add_cap( 'editor', 'edit_others_newsletters');
$wp_roles->add_cap( 'editor', 'delete_newsletters');
$wp_roles->add_cap( 'editor', 'delete_others_newsletters');
}
/**
* Since 3.1 the register_activation_hook is not called when a plugin
* is updated, so to run the above code on automatic upgrade you need
* to check the plugin db version on another hook.
*/
function alo_em_check_db_when_loaded() {
if ( alo_em_db_tables_need_update() ) alo_em_install_db_tables();
}
add_action('plugins_loaded', 'alo_em_check_db_when_loaded');
/**
* Install/update database tables
*/
function alo_em_install_db_tables() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
//-------------------------------------------------------------------------
if ( alo_em_db_tables_need_update() ) {
if( defined( 'DB_COLLATE' ) && constant( 'DB_COLLATE' ) != '' ) {
$collate = constant( 'DB_COLLATE' );
} else {
$collate = constant( 'DB_CHARSET' );
}
// Create the table structure
$sql = "CREATE TABLE {$wpdb->prefix}easymail_subscribers (
ID int(11) unsigned NOT NULL auto_increment ,
email varchar(100) NOT NULL ,
name varchar(100) NOT NULL ,
join_date datetime NOT NULL ,
active INT( 1 ) NOT NULL DEFAULT '0' ,
unikey varchar(24) NOT NULL ,
lists varchar(255) DEFAULT '|' ,
lang varchar(5) DEFAULT NULL ,
last_act datetime NULL ,
ip_address varchar(50) NULL ,
PRIMARY KEY (ID)
) DEFAULT CHARSET=".$collate.";
CREATE TABLE {$wpdb->prefix}easymail_recipients (
ID int(11) unsigned NOT NULL auto_increment ,
newsletter int(11) unsigned NOT NULL ,
email varchar(100) NOT NULL ,
result varchar(3) NOT NULL DEFAULT '0' ,
user_id int(11) unsigned DEFAULT NULL ,
PRIMARY KEY (ID)
) DEFAULT CHARSET=".$collate.";
CREATE TABLE {$wpdb->prefix}easymail_stats (
ID int(11) unsigned NOT NULL auto_increment ,
recipient int(11) unsigned NOT NULL ,
newsletter int(11) unsigned NOT NULL ,
added_on datetime NOT NULL ,
request text ,
PRIMARY KEY (ID)
) DEFAULT CHARSET=".$collate.";
CREATE TABLE {$wpdb->prefix}easymail_unsubscribed (
email varchar(100) NOT NULL ,
added_on datetime NOT NULL ,
PRIMARY KEY (email)
) DEFAULT CHARSET=".$collate.";
";
dbDelta($sql);
// Update the old "lists" field if upgrading from v. 1.x
$installed_db = get_option('alo_em_db_version');
if ( $installed_db < 2012 ) {
$wpdb->query( "UPDATE ". $wpdb->prefix."easymail_subscribers SET lists = REPLACE( lists, '_', '|');" );
$wpdb->query( "UPDATE {$wpdb->options} SET option_name = REPLACE( option_name, 'ALO_em_', 'alo_em_');" );
}
// v.2016: Add table indexes
if ( $installed_db < 2016 ) {
$wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_recipients ADD INDEX ( `newsletter` ), ADD INDEX ( `email` )");
$wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_stats ADD INDEX ( `newsletter` ), ADD INDEX ( `recipient` )");
}
// Add 'email' index only if not exists (it exists in plugin versions older than 2.3)
if ( !$wpdb->get_row("SHOW INDEX FROM {$wpdb->prefix}easymail_subscribers WHERE Non_unique = 0 AND Column_name = 'email';" ) ) {
$wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_subscribers ADD UNIQUE ( `email` )");
}
// v.2017: Modify Request column, an index in new 'unsubscribed' table
if ( $installed_db < 2017 ) {
$wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_stats CHANGE `request` `request` text");
}
// v.2019: the new 'last_act' column if empty has the same value of the 'join_date'
if ( $installed_db < 2019 ) {
$wpdb->query("UPDATE ". $wpdb->prefix."easymail_subscribers SET last_act = join_date WHERE last_act IS NULL;");
}
update_option( "alo_em_db_version", ALO_EM_DB_VERSION );
}
}
/**
* Check if plugin tables are already properly installed
*/
function alo_em_db_tables_need_update() {
global $wpdb;
$installed_db = get_option('alo_em_db_version');
$missing_table = false; // Check if tables not yet installed
$tables = array ( $wpdb->prefix."easymail_subscribers", $wpdb->prefix."easymail_recipients", $wpdb->prefix."easymail_stats", $wpdb->prefix."easymail_unsubscribed" );
foreach ( $tables as $table_name ) {
if ( $wpdb->get_var("show tables like '$table_name'") != $table_name ) $missing_table = true;
}
return ( $missing_table || ALO_EM_DB_VERSION != $installed_db ) ? true : false;
}
/**
* Manage plugin activation: on multisite and on standard (thanks to kzyz!)
*/
function alo_em_activate() {
global $wpdb;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
// check if it is a network activation - if so, run the activation function for each blog id
if ( isset( $_GET['networkwide'] ) && ( $_GET['networkwide'] == 1 ) ) {
$old_blog = $wpdb -> blogid;
// Get all blog ids
$blogids = $wpdb -> get_col( $wpdb -> prepare( "SELECT blog_id FROM {$wpdb -> blogs}" ) );
foreach ( $blogids as $blog_id ) {
switch_to_blog( $blog_id );
alo_em_install();
}
switch_to_blog( $old_blog );
return;
}
}
alo_em_install();
}
register_activation_hook(__FILE__,'alo_em_activate');
/**
* Clean the new subscription not yet activated after too much time
*/
function alo_em_clean_no_actived() {
global $wpdb;
// delete subscribes not yet activated after 5 days
$limitdate = date ("Y-m-d",mktime(0,0,0,date("m"),date("d")-5,date("Y")));
$output = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}easymail_subscribers WHERE join_date <= '%s' AND active = '0'", $limitdate ) );
//return $output;.
}
add_action('alo_em_schedule', 'alo_em_clean_no_actived');
add_action( 'alo_em_batch' , 'alo_em_batch_sending');
/**
* On plugin adectivation
*/
function alo_em_uninstall() {
global $wpdb, $wp_roles, $wp_version;
// delete scheduled cleaner
wp_clear_scheduled_hook('alo_em_schedule');
wp_clear_scheduled_hook('ALO_em_schedule'); // old versions
// delete cron batch sending
wp_clear_scheduled_hook('alo_em_batch');
wp_clear_scheduled_hook('ALO_em_batch'); // old versions
// if required delete all plugin data (options, db tables, page)
if ( get_option('alo_em_delete_on_uninstall') == "yes" ) {
$tables = array ( "easymail_recipients", "easymail_subscribers", "easymail_stats", "easymail_sendings", "easymail_trackings", "easymail_unsubscribed" );
foreach ( $tables as $tab ) {
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}$tab");
}
// delete option from db
$wpdb->query( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE 'alo_em_%'" );
// delete subscription page
if ( version_compare ( $wp_version , '2.9', '>=' ) ) {
wp_delete_post( get_option('alo_em_subsc_page'), true ); // skip trash, from wp 2.9
} else {
wp_delete_post( get_option('alo_em_subsc_page') );
}
// and the option with page id
delete_option ('alo_em_subsc_page');
// reset cap
$roles = $wp_roles->get_names(); // get a list of values, containing pairs of: $role_name => $display_name
foreach ( $roles as $rolename => $key) {
$wp_roles->remove_cap( $rolename, 'manage_newsletter_options');
$wp_roles->remove_cap( $rolename, 'manage_newsletter_subscribers');
$wp_roles->remove_cap( $rolename, 'publish_newsletters');
$wp_roles->remove_cap( $rolename, 'edit_newsletters');
$wp_roles->remove_cap( $rolename, 'edit_others_newsletters');
$wp_roles->remove_cap( $rolename, 'delete_newsletters');
$wp_roles->remove_cap( $rolename, 'delete_others_newsletters');
$wp_roles->remove_cap( $rolename, 'read_private_newsletters');
// deprecated old caps to be removed
$wp_roles->remove_cap( $rolename, 'manage_easymail_options');
$wp_roles->remove_cap( $rolename, 'manage_easymail_subscribers');
}
}
}
/**
* Manage plugin de-activation: on multisite and on standard (thanks to kzyz!)
*/
function alo_em_deactivate() {
global $wpdb;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
// check if it is a network activation - if so, run the activation function for each blog id
if ( isset( $_GET['networkwide'] ) && ( $_GET['networkwide'] == 1 ) ) {
$old_blog = $wpdb->blogid;
// Get all blog ids
$blogids = $wpdb -> get_col( $wpdb -> prepare( "SELECT blog_id FROM {$wpdb -> blogs}" ) );
foreach ( $blogids as $blog_id ) {
switch_to_blog( $blog_id );
alo_em_uninstall();
}
switch_to_blog( $old_blog );
return;
}
}
alo_em_uninstall();
}
register_deactivation_hook( __FILE__, 'alo_em_deactivate' );
/**
* Plugin activation whren new blog on multisite (thanks to kzyz!)
*/
function alo_em_new_blog( $blog_id ) {
global $wpdb;
if ( is_plugin_active_for_network( ALO_EM_PLUGIN_DIR. '/'. basename( __FILE__ ) ) ) {
$old_blog = $wpdb -> blogid;
switch_to_blog( $blog_id );
alo_em_install();
switch_to_blog( $old_blog );
}
}
add_action( 'wpmu_new_blog', 'alo_em_new_blog' );
/**
* Add menu pages
*/
function alo_em_add_admin_menu() {
if ( current_user_can('manage_newsletter_subscribers') ) {
add_submenu_page( 'edit.php?post_type=newsletter', __("Subscribers", "alo-easymail"), __("Subscribers", "alo-easymail"), 'manage_newsletter_subscribers', 'alo-easymail/alo-easymail_subscribers.php' );
add_action( 'load-alo-easymail/alo-easymail_subscribers.php', 'alo_em_contextual_help_tabs' );
}
if ( current_user_can('manage_newsletter_options') ) {
add_submenu_page( 'edit.php?post_type=newsletter', __("Settings"), __("Settings"), 'manage_newsletter_options', 'alo-easymail/alo-easymail_options.php' );
add_action( 'load-alo-easymail/alo-easymail_options.php', 'alo_em_contextual_help_tabs' );
}
add_action( 'load-edit.php', 'alo_em_contextual_help_tabs' );
add_action( 'load-post-new.php', 'alo_em_contextual_help_tabs' );
}
add_action('admin_menu', 'alo_em_add_admin_menu');
function alo_em_contextual_help_tabs() {
if ( !class_exists('WP_Screen') ) return;
$screen = get_current_screen();
if ( !is_object($screen) ) return;
if ( $screen->post_type != 'newsletter' ) return;
// Main tab per page
$tab = false;
switch ( $screen->id ) {
case 'alo-easymail/alo-easymail_subscribers':
/* TODO!
$tab = array(
'id' => 'alo-easymail_subscribers',
'title' => __("Subscribers", "alo-easymail"),
'content' => '
This is the content for the tab.
' // TODO // Use 'callback' instead of 'content' for a function callback that renders the tab content. ); */ break; case 'alo-easymail/alo-easymail_options': /* TODO! $tab = array( 'id' => 'alo-easymail_options', 'title' => __("Settings", "alo-easymail"), 'content' => 'This is the content for the tab.
' // TODO ); */ break; case 'edit-newsletter': case 'newsletter': $tab = array( 'id' => 'alo-easymail_newsletter', 'title' => __("Newsletter", "alo-easymail"), 'content' => '' ); break; } if ( is_array($tab) ) $screen->add_help_tab( $tab ); // Common tab $screen->add_help_tab( array( 'id' => 'alo-easymail_links', // This should be unique for the screen. 'title' => __("Links"), 'content' => ''.__("Resources about EasyMail Newsletter", "alo-easymail") . ': '. 'homepage | guide | faq | for developers | forum | news | WP forum'. '
' ) ); // Common sidebar $screen->set_help_sidebar( "". __("If you use this plugin consider the idea of donating and supporting its development", "alo-easymail") ."
". "
" ."" ); if ( $screen->id == 'alo-easymail/alo-easymail_subscribers' ) { $screen->add_option( 'per_page', array( 'label' => __("subscribers per page", "alo-easymail"), 'default' => 20, 'option' => 'edit_per_page' ) ); } } /** * Contextual help */ if ( version_compare ( $wp_version , '3.3', '<' ) ) { function alo_em_contextual_help() { global $hook_suffix; if (function_exists('add_contextual_help')) { $html = __("Resources about EasyMail Newsletter", "alo-easymail") . ': homepage | guide | faq | for developers | forum | news | WP forum'; $html .= " | "; if ( $hook_suffix == 'alo-easymail/alo-easymail_options.php' ) { // extra help } if ( $hook_suffix == 'alo-easymail/alo-easymail_subscribers.php' ) { // extra help } add_contextual_help( $hook_suffix, $html ); } } add_action( 'admin_head-alo-easymail/alo-easymail_options.php', 'alo_em_contextual_help' ); add_action( 'admin_head-alo-easymail/alo-easymail_subscribers.php', 'alo_em_contextual_help' ); } /* * Add some links on the plugin page */ function alo_em_add_plugin_links($links, $file) { if ( $file == plugin_basename(__FILE__) ) { $links[] = 'Guide'; $links[] = 'Faq'; $links[] = 'Forum'; $links[] = 'News'; $links[] = 'WP forum'; } return $links; } add_filter( 'plugin_row_meta', 'alo_em_add_plugin_links', 10, 2 ); /** * On plugin init */ function alo_em_init_method() { // if required, exclude the easymail page from pages' list if ( get_option('alo_em_show_subscripage') == "no" ) add_filter('get_pages','alo_em_exclude_page'); // load localization files load_plugin_textdomain ("alo-easymail", false, "alo-easymail/languages"); // Let's install custom fields, if any global $wpdb; $alo_em_cf = alo_easymail_get_custom_fields(); if ( $alo_em_cf ) { $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}easymail_subscribers" ); $existing = array(); foreach ( $fields as $field ) $existing[] = $field->Field; foreach( $alo_em_cf as $key => $value ) { // Create db column if missing if ( !in_array( $key, $existing ) ) { $wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_subscribers ADD `".$key."` ". $value['sql_attr']); } // Create index if required if ( $value['sql_key'] && !$wpdb->get_row("SHOW INDEX FROM {$wpdb->prefix}easymail_subscribers WHERE Column_name = '".$key."';" ) ) { $wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_subscribers ADD INDEX ( `".$key."` )"); } } // Get fields again, after previpus installation $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}easymail_subscribers" ); $existing = array(); foreach ( $fields as $field ) $existing[] = $field->Field; foreach( $alo_em_cf as $key => $value ) { // Create index if required if ( in_array( $key, $existing ) && $value['sql_key'] && !$wpdb->get_row("SHOW INDEX FROM {$wpdb->prefix}easymail_subscribers WHERE Column_name = '".$key."';" ) ) { $wpdb->query("ALTER TABLE {$wpdb->prefix}easymail_subscribers ADD INDEX ( `".$key."` )"); } } } } add_action( 'init', 'alo_em_init_method' ); /** * New custom post type: Newsletter */ function alo_em_register_newsletter_type () { $labels = array( 'name' => __( 'Newsletters', "alo-easymail" ), 'singular_name' => __( 'Newsletter', "alo-easymail" ), 'add_new' => __( 'Add New', "alo-easymail" ), 'add_new_item' => __( 'Add New Newsletter', "alo-easymail" ), 'edit_item' => __( 'Edit Newsletter', "alo-easymail" ), 'new_item' => __( 'New Newsletter', "alo-easymail" ) , 'view_item' => __( 'View Newsletter', "alo-easymail" ), 'search_items' => __( 'Search Newsletters', "alo-easymail" ), 'not_found' => __( 'No Newsletters found', "alo-easymail" ), 'not_found_in_trash' => __( 'No Newsletters found in Trash', "alo-easymail" ), 'parent_item_colon' => __( 'Parent Newsletter', "alo-easymail" ), 'menu_name' => __( 'Newsletters', "alo-easymail" ), 'parent' => __( 'Parent Newsletter', "alo-easymail" ), ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'exclude_from_search' => false, 'rewrite' => array('slug' => 'newsletters'), //'capability_type' => 'post', // TODO vedi sotto // http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types 'capability_type' => 'newsletter', 'capabilities' => array( 'publish_posts' => 'publish_newsletters', 'edit_posts' => 'edit_newsletters', 'edit_others_posts' => 'edit_others_newsletters', 'delete_posts' => 'delete_newsletters', 'delete_others_posts'=> 'delete_others_newsletters', 'read_private_posts'=> 'read_private_newsletters', // DO not assign the next 3 caps to roles: will be mapped by filter 'edit_post' => 'edit_newsletter', 'delete_post' => 'delete_newsletter', 'read_post' => 'read_newsletter', ), 'has_archive' => true, 'hierarchical' => false, 'menu_position' => false, 'menu_icon' => ALO_EM_PLUGIN_URL.'/images/16-email-letter.png', 'can_export' => true, 'supports' => array( 'title' , 'editor', 'custom-fields', 'thumbnail' ) ); // If it doesn't allow newsletter publication online if ( get_option('alo_em_publish_newsletters') == "no" ) { $args['public'] = false; $args['publicly_queryable'] = false; $args['show_ui'] = true; $args['show_in_menu'] = true; $args['query_var'] = false; $args['exclude_from_search'] = true; // TODO read here: http://jandcgroup.com/2011/09/14/exclude-custom-post-types-from-wordpress-search-do-not-use-exclude_from_search/ } $args = apply_filters ( 'alo_easymail_register_newsletter_args', $args ); register_post_type( 'newsletter', $args ); } add_action('init', 'alo_em_register_newsletter_type'); /** * Filtering the map_meta_cap hook to know if user can do something * * http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types */ function alo_em_map_meta_cap( $caps, $cap, $user_id, $args ) { // If editing, deleting, or reading an item, get the post and post type object. if ( 'edit_newsletter' == $cap || 'delete_newsletter' == $cap || 'read_newsletter' == $cap ) { $post = get_post( $args[0] ); $post_type = get_post_type_object( $post->post_type ); // Set an empty array for the caps. $caps = array(); } // If editing assign the required capability. if ( 'edit_newsletter' == $cap ) { if ( $user_id == $post->post_author ) $caps[] = $post_type->cap->edit_posts; else $caps[] = $post_type->cap->edit_others_posts; } // If deleting, assign the required capability. elseif ( 'delete_newsletter' == $cap ) { if ( $user_id == $post->post_author ) $caps[] = $post_type->cap->delete_posts; else $caps[] = $post_type->cap->delete_others_posts; } // If reading a private item, assign the required capability. elseif ( 'read_newsletter' == $cap ) { if ( 'private' != $post->post_status ) $caps[] = 'read'; elseif ( $user_id == $post->post_author ) $caps[] = 'read'; else $caps[] = $post_type->cap->read_private_posts; } // Return the capabilities required by the user. return $caps; } add_filter( 'map_meta_cap', 'alo_em_map_meta_cap', 10, 4 ); /** * Texts when a Newsletter is updated */ function alo_em_newsletter_updated_messages( $messages ) { global $post, $post_ID; if ( get_option('alo_em_publish_newsletters') == "no" ) { $view_url = ""; $preview_url = ""; } else { $view_url = sprintf( __(' View Newsletter', "alo-easymail" ), esc_url( get_permalink($post_ID) ) ); $preview_url = sprintf( __(' Preview Newsletter', "alo-easymail"), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ); } $messages['newsletter'] = array( 0 => '', // Unused. Messages start at index 1. 1 => __('Newsletter updated.', "alo-easymail" ). $view_url, 2 => __('Custom field updated.', "alo-easymail"), 3 => __('Custom field deleted.', "alo-easymail"), 4 => __('Newsletter updated.', "alo-easymail"), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Newsletter restored to revision from %s', "alo-easymail"), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => __('Newsletter published.', "alo-easymail") . $view_url, 7 => __('Newsletter saved.', "alo-easymail"), 8 => sprintf( __('Newsletter submitted. Preview Newsletter', "alo-easymail"), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Newsletter scheduled for: %1$s.', "alo-easymail"), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'j M Y @ G:i', "alo-easymail" ), strtotime( $post->post_date ) ) ). $preview_url, 10 => __('Newsletter draft updated.', "alo-easymail") . $preview_url , ); return $messages; } add_filter('post_updated_messages', 'alo_em_newsletter_updated_messages'); /** * Adds media upload in thickbox in Newsletter */ function alo_em_newsletter_add_media_upload_scripts() { if ($GLOBALS['post_type'] == 'newsletter') { add_thickbox(); wp_enqueue_script('media-upload'); } } add_action('admin_print_styles-post-new.php', 'alo_em_newsletter_add_media_upload_scripts'); add_action('admin_print_styles-post.php', 'alo_em_newsletter_add_media_upload_scripts'); /** * Dirty hack to hide "Quick edit" button // TODO: add easymail option in Quick edit view: http://shibashake.com/wordpress-theme/expand-the-wordpress-quick-edit-menu */ /* function alo_em_inti_method () { add_action('admin_print_styles-edit.php','alo_em_hide_quick_edit_css'); } add_action('admin_init','alo_em_inti_method'); function alo_em_hide_quick_edit_css() { if ( isset( $_GET['post_type'] ) && $_GET['post_type'] == "newsletter" ) : ?> "; $columns["title"] = __( 'Title' ) ." / " . __( 'Subject', "alo-easymail"); $columns["easymail_recipients"] = __( 'Recipients', "alo-easymail" ); $columns["easymail_status"] = __( 'Newsletter status', "alo-easymail" ); $columns["date"] = __( 'Start', "alo-easymail" ); $columns["author"] = __( 'Author' ); return $columns; } add_filter ('manage_edit-newsletter_columns', 'alo_em_edit_table_columns'); /** * Fills the columns of Newsletter display table */ function alo_em_table_column_value ( $columns ) { global $post, $user_ID; $count_recipients = alo_em_count_recipients_from_meta( $post->ID ); $recipients = alo_em_get_recipients_from_meta( $post->ID ); if ( $columns == "easymail_recipients" ) { if ( $count_recipients == 0 ) { if ( alo_em_user_can_edit_newsletter( $post->ID ) ) echo ''; echo '
' . __( 'No recipients selected yet', "alo-easymail").'';
if ( alo_em_user_can_edit_newsletter( $post->ID ) ) echo '';
} else {
if ( alo_em_user_can_edit_newsletter( $post->ID ) ) echo "";
echo __( 'Total recipients', "alo-easymail") .": ";
echo $count_recipients;
if ( alo_em_user_can_edit_newsletter( $post->ID ) ) {
echo "
';
echo "";
echo '
';
echo "";
}
if ( alo_em_user_can_edit_newsletter( $newsletter ) && current_user_can( "publish_newsletters" )) echo " ". $goto_report;
break;
case "pending":
echo "".__("Pending Review"). "
';
}
break;
case "draft":
echo "".__("Draft"). "
';
echo "";
}
if ( alo_em_user_can_edit_newsletter( $newsletter ) ) echo " ". $goto_report;
break;
case false:
default:
switch ( $post_status ) {
case "pending":
case "draft":
echo "".__("A newsletter cannot be sent if its status is draft or pending review"). "| \n"; $html .= " | \n"; $html .= " \n"; $html .= " | \n"; $html .= "
|---|
| \n"; // edit-by-alo: $html .= " | \n"; // edit-by-alo: added next $input block //$html .= sprintf( $value['edit_html'], $subscriber->ID, $subscriber->ID, format_to_edit( $subscriber->$key ) )."\n"; /*switch( $value['input_type'] ) { case "text": $tpl = ""; } $input = str_replace( '[id]', 'id="'.$field_id.'"', $input); $input = str_replace( '[name]', 'name="'.$field_id.'"', $input); $input = str_replace( '[value]', $subscriber ? format_to_edit( $subscriber->$key ):'', $input); $html .= $input ."\n"; */ $prev = isset($subscriber->$key) ? format_to_edit( $subscriber->$key ) : ''; $html .= alo_easymail_custom_field_html ( $key, $value, $field_id, $prev, true ); $html .= " | \n"; $html .= "
|---|
ID ) == 0 ) ? "class=\"easymail-txtwarning\"" : "" ) ." >"; echo "" .__("Selected recipients", "alo-easymail") .": ". alo_em_count_recipients_from_meta( $post->ID ) ."
"; if ( alo_em_get_newsletter_status ( $post->ID ) == "sent" || alo_em_is_newsletter_recipients_archived ( $post->ID ) ) { echo "". __("This newsletter was already sent", "alo-easymail") .".
"; echo "". __("The creation of the recipients list has already started", "alo-easymail") .".
"; echo ""; echo "
"; echo "";
echo '
';
echo " " . stripslashes ( alo_em___( $newsletter[0]->post_title ) ) ."
";
echo __("Progress", "alo-easymail") .": " . alo_em_newsletter_recipients_percentuage_already_sent( $newsletter[0]->ID ) . " %
" ;
echo "".__("Added on", "alo-easymail") . " ". date_i18n( __( 'j M Y @ G:i', "alo-easymail" ), strtotime( $newsletter[0]->post_date ) ) . " - ";
echo __("Scheduled by", "alo-easymail") . " ". get_user_meta($newsletter[0]->post_author, 'nickname',true). "";
echo "
". __("There are no newsletters in queue", "alo-easymail") . ".
"; } echo "". sprintf( __("There are %d subscribers: %d activated, %d not activated", "alo-easymail"), $total, $active, $noactive ) . ".
"; } else { echo "". __("No subscribers", "alo-easymail") . ".
"; } if ( current_user_can('administrator') ) { echo "'; printf(__('RSS Error: %s'), $rss->get_error_message()); echo '
'; } } } function alo_em_add_dashboard_widgets() { if ( current_user_can ( 'manage_newsletter_subscribers' ) && current_user_can ( 'edit_others_newsletters' ) ) { wp_add_dashboard_widget('alo-easymail-widget', 'EasyMail Newsletter', 'alo_em_dashboard_widget_function'); } } add_action('wp_dashboard_setup', 'alo_em_add_dashboard_widgets' ); /** * Show the optin/optout on Registration Form */ function alo_em_show_registration_optin () { $optin_txt = ( alo_em_translate_option ( alo_em_get_language (), 'alo_em_custom_optin_msg', false) !="") ? alo_em_translate_option ( alo_em_get_language (), 'alo_em_custom_optin_msg', false) : __("Yes, I would like to receive the Newsletter", "alo-easymail"); echo ''; echo '
'; $mailinglists = alo_em_get_mailinglists( 'public' ); if ( $mailinglists ) { $lists_msg = ( alo_em_translate_option ( alo_em_get_language (), 'alo_em_custom_lists_msg',false) !="") ? alo_em_translate_option ( alo_em_get_language (), 'alo_em_custom_lists_msg',false) : __("You can also sign up for specific lists", "alo-easymail"); echo "". $lists_msg .":
\n"; foreach ( $mailinglists as $list => $val ) { echo "\n"; } } echo ' '; } add_action('register_form','alo_em_show_registration_optin'); /** * Save the optin/optout on Registration Form */ function alo_em_save_registration_optin ( $user_id, $password="", $meta=array() ) { $user = get_userdata($user_id); if (!empty($user->first_name) && !empty($user->last_name)) { $name = $user->first_name.' '.$user->last_name; } else { $name = $user->display_name; } if ( isset ($_POST['alo_em_opt']) && $_POST['alo_em_opt'] == "yes" ) { $lang = ( isset($_POST['alo_em_lang']) && in_array ( $_POST['alo_em_lang'], alo_em_get_all_languages( false )) ) ? $_POST['alo_em_lang'] : "" ; $fields['email'] = $user->user_email; //edit : added all this line $fields['name'] = $name; //edit : added all this line //alo_em_add_subscriber( $fields, 1, $lang ); //edit : orig : alo_em_add_subscriber( $user->user_email, $name , 1, $lang ); if ( alo_em_add_subscriber( $fields, 1, $lang ) == "OK" ) { do_action ( 'alo_easymail_new_subscriber_added', alo_em_get_subscriber( $user->user_email ), $user_id ); } // if subscribing, save also lists $mailinglists = alo_em_get_mailinglists( 'public' ); if ($mailinglists) { $subscriber_id = alo_em_is_subscriber( $user->user_email ); foreach ( $mailinglists as $mailinglist => $val) { if ( isset ($_POST['alo_em_register_lists']) && is_array ($_POST['alo_em_register_lists']) && in_array ( $mailinglist, $_POST['alo_em_register_lists'] ) ) { alo_em_add_subscriber_to_list ( $subscriber_id, $mailinglist ); // add to list } } } } else { alo_em_add_email_in_unsubscribed ( $user->user_email ); } } add_action( 'user_register', 'alo_em_save_registration_optin' ); /** * Edit the e-mail message */ function alo_em_handle_email ( $args ) { // $args['to'], $args['subject'], $args['message'], $args['headers'], $args['attachments'] // Check based on $args['subject']; more attrs in $args['message'] global $_config; /* * 1) Activation e-mail */ if ( strpos ( "#_EASYMAIL_ACTIVATION_#", $args['subject'] ) !== false) { // Get the parameters stored as a query in $args['message'] $defaults = array( 'lang' => '', 'email' => '', 'name' => '', 'unikey' => '' ); /* // replaced 'wp_parse_args' because use urlencode and stripslashes, so affect emails with '+' chars $customs = wp_parse_args( $args['message'], $defaults ); extract( $customs, EXTR_SKIP ); */ $pars = array(); $raw = explode('&', $args['message']); foreach ($raw as $section) { if (strpos($section, '=') !== false) { list($key, $value) = explode('=', $section); $pars[$key] = $value; } } $customs = array_merge( $defaults, $pars ); extract( $customs, EXTR_SKIP ); //$subscriber = alo_em_get_subscriber( $email ); // Subject if ( $subject_text = alo_em_translate_option ( $lang, 'alo_em_txtpre_activationmail_subj', true ) ) { $subject = $subject_text; } else { $subject = alo_em___( __("Confirm your subscription to %BLOGNAME% Newsletter", "alo-easymail" ) ); } $blogname = html_entity_decode ( wp_kses_decode_entities ( get_option('blogname') ) ); $subject = str_replace ( "%BLOGNAME%", $blogname, $subject ); $args['subject'] = $subject; // Content if ( $content_txt = alo_em_translate_option ( $lang, 'alo_em_txtpre_activationmail_mail', true ) ) { $content = $content_txt; } else { $content = __("Hi %NAME%\nto complete your subscription to %BLOGNAME% newsletter you need to click on the following link (or paste it in the address bar of your browser):\n", "alo-easymail"); $content .= "%ACTIVATIONLINK%\n\n"; $content .= __("If you did not ask for this subscription ignore this message.", "alo-easymail"). "\n"; $content .= __("Thank you", "alo-easymail")."\n". $blogname ."\n"; } /* $div_email = explode("@", $email); $arr_params = array ('ac' => 'activate', 'em1' => $div_email[0], 'em2' => $div_email[1], 'uk' => $unikey ); $sub_link = add_query_arg( $arr_params, get_page_link (get_option('alo_em_subsc_page')) ); $sub_link = alo_em_translate_url ( $sub_link, $lang ); */ //$div_email = explode("@", $email); $sub_vars = $email ."|" /*$div_email[0] . "|" . $div_email[1] . "|" */ . $unikey . "|" . $lang; //$sub_vars = $subscriber->ID . "|" . $subscriber->unikey; $sub_vars = urlencode( base64_encode( $sub_vars ) ); $sub_link = add_query_arg( 'emact', $sub_vars, alo_em_translate_home_url ( $lang ) /*trailingslashit( get_home_url() )*/ ); //$sub_link = alo_em_translate_url ( $sub_link, $lang /*$subscriber->lang */ ); $content = str_replace ( "%BLOGNAME%", $blogname, $content ); $content = str_replace ( "%NAME%", /* $subscriber->name */ $name, $content ); $content = str_replace ( "%ACTIVATIONLINK%", $sub_link, $content ); $args['message'] = $content; } return $args; } add_filter('wp_mail', 'alo_em_handle_email'); /** * Add Newsletter menu in Toolbar Admin bar (WP 3.1-3.2) */ if ( version_compare ( $wp_version , '3.1', '>=' ) && version_compare ( $wp_version , '3.3', '<' ) ) { function alo_em_add_menu_admin_bar() { global $wp_admin_bar; if ( !$wp_admin_bar ) return; if ( !is_admin_bar_showing() ) return; if ( current_user_can('edit_newsletters') ) { $wp_admin_bar->add_menu( array( 'id' => 'alo_easymail', 'title' =>__( 'Newsletters', "alo-easymail" ), 'href' => admin_url('edit.php')."?post_type=newsletter" ) ); $wp_admin_bar->add_menu( array( 'id' => 'alo_easymail_main', 'parent' => 'alo_easymail', 'title' => __( 'Newsletters', "alo-easymail" ), 'href' => admin_url('edit.php')."?post_type=newsletter" ) ); $wp_admin_bar->add_menu( array( 'parent' => 'alo_easymail_main', 'title' => __( 'Add New Newsletter', "alo-easymail" ), 'href' => admin_url('post-new.php')."?post_type=newsletter" ) ); $wp_admin_bar->add_menu( array( 'parent' => 'alo_easymail_main', 'title' => __( 'Show all', "alo-easymail" ), 'href' => admin_url('edit.php')."?post_type=newsletter" ) ); } if ( current_user_can('manage_newsletter_subscribers') ) { $wp_admin_bar->add_menu( array( 'parent' => 'alo_easymail', 'title' => __( 'Subscribers', "alo-easymail" ), 'href' => admin_url('edit.php')."?post_type=newsletter&page=alo-easymail/alo-easymail_subscribers.php" ) ); } if ( current_user_can('manage_newsletter_options') ) { $wp_admin_bar->add_menu( array( 'parent' => 'alo_easymail', 'title' => __( 'Options', "alo-easymail" ), 'href' => admin_url('edit.php')."?post_type=newsletter&page=alo-easymail/alo-easymail_options.php" ) ); } } add_action( 'admin_bar_menu', 'alo_em_add_menu_admin_bar' , 70); } else if ( version_compare ( $wp_version , '3.3', '>=' ) ) { function alo_em_add_menu_toolbar( $wp_admin_bar ) { if ( current_user_can('edit_newsletters') ) { $ico = ''; $args = array('id' => 'alo_easymail', 'title' => $ico. __( 'Newsletters', "alo-easymail" ), 'parent' => false, 'href' => admin_url('edit.php')."?post_type=newsletter" ); $wp_admin_bar->add_node($args); $args = array('id' => 'alo_easymail-all', 'title' => __( 'Newsletters', "alo-easymail" ), 'parent' => 'alo_easymail', 'href' => admin_url('edit.php')."?post_type=newsletter" ); $wp_admin_bar->add_node($args); $args = array('id' => 'alo_easymail-new', 'title' => __( 'Add New', "alo-easymail" ), 'parent' => 'alo_easymail', 'href' => admin_url('post-new.php')."?post_type=newsletter" ); $wp_admin_bar->add_node($args); if ( current_user_can('manage_newsletter_subscribers') ) { $args = array('id' => 'alo_easymail-subscribers', 'title' => __( 'Subscribers', "alo-easymail" ), 'parent' => 'alo_easymail', 'href' => admin_url('edit.php')."?post_type=newsletter&page=alo-easymail/alo-easymail_subscribers.php" ); $wp_admin_bar->add_node($args); } if ( current_user_can('manage_newsletter_options') ) { $args = array('id' => 'alo_easymail-settings', 'title' => __( 'Options', "alo-easymail" ), 'parent' => 'alo_easymail', 'href' => admin_url('edit.php')."?post_type=newsletter&page=alo-easymail/alo-easymail_options.php" ); $wp_admin_bar->add_node($args); } } } add_action( 'admin_bar_menu', 'alo_em_add_menu_toolbar', 45 ); } /** * Send a newsletter to a test email */ function alo_em_send_mailtest () { $result = "no"; check_ajax_referer( "alo-easymail_recipients-list" ); $newsletter = ( isset( $_POST['newsletter'] ) && is_numeric( $_POST['newsletter'] ) ) ? (int) $_POST['newsletter'] : false; $email = ( isset( $_POST['email'] ) && is_email( $_POST['email'] ) ) ? $_POST['email'] : false; if ( $email && $newsletter && current_user_can( "publish_newsletters" ) ) { $maybe_subscriber = (array)alo_em_get_subscriber( $email ); if ( isset( $maybe_subscriber['ID'] ) ) { $maybe_subscriber['subscriber'] = $maybe_subscriber['ID']; unset( $maybe_subscriber['ID'] ); } else { $maybe_subscriber['subscriber'] = false; } $user_id = ( email_exists( $email ) ) ? email_exists( $email ) : false; $recipient = (object) array_merge( $maybe_subscriber, array ( 'newsletter' => $newsletter, 'email' => $email, 'user_id' => $user_id ) ); //$recipient = (object) array ( 'newsletter' => $newsletter, 'email' => $email ); if ( alo_em_send_newsletter_to ( $recipient, false /*true*/ ) ) $result = "yes"; } usleep( 500000 ); die ( $result ); } add_action('wp_ajax_easymail_send_mailtest', 'alo_em_send_mailtest'); /** * Alert in admin panel */ function alo_em_admin_notice() { global $pagenow; $page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : false; if ( $pagenow == "edit.php" && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'newsletter' && $page != 'alo-easymail/alo-easymail_subscribers.php' ) { /* if ( get_option('alo_em_timeout_alert') != "hide" ) { echo '
'. __("To enable the plugin work better you should increase the wp_cron and php timeouts", "alo-easymail") .". ";
echo __("For more info you can use the Help button or visit the FAQ of the site", "alo-easymail");
echo ' »
'. __("Debug mode is activated", "alo-easymail") .": ";
if ( get_option('ALO_em_debug_newsletters') == "to_author" ) _e("all messages will be sent to the newsletter author", "alo-easymail");
if ( get_option('ALO_em_debug_newsletters') == "to_file" ) _e("all messages will be recorded into a log file", "alo-easymail");
echo ".
'. __("ALO Easymail Newsletter needs attention", "alo-easymail") ."!
";
echo __("The plugin database tables have not properly installed", "alo-easymail") .": " . __("you can try to deactivate and activate the plugin", "alo-easymail").".";
echo "
". __("For more info, visit the FAQ of the site.", "alo-easymail")."";
echo ".
'. esc_js( sprintf( __('Maybe you would like to import subscribers from your blog registered members or an external archive (using CSV). Click the '%s' button', 'alo-easymail'), $impexp_butt) ) .'
'; ?> Easymail | '. esc_js( __( 'No recipients selected yet', "alo-easymail") ) .''; $pointer_content .= ''. esc_js( __('Before sending the newsletter you have to select recipients.', 'alo-easymail')." " .__('Click the link to do it now.', 'alo-easymail') ) .'
'; ?> Easymail | '. esc_js( __( 'Create list of recipients', "alo-easymail") ) .''; $pointer_content .= ''. esc_js( __('You have to prepare the list of recipients to send the newsletter to', 'alo-easymail').". " .__('Click the link to do it now.', 'alo-easymail') ) .'
'; ?>