Skip to content

Commit

Permalink
Optimize db transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerxes Parsi authored and Xerxes Parsi committed Mar 10, 2016
1 parent b6c16a4 commit 82da532
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 35 deletions.
27 changes: 16 additions & 11 deletions admin/settings-page.php
@@ -1,6 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! is_admin() ) die;
global $tdata;
?>
<style type="text/css">
label {vertical-align: middle;}
Expand Down Expand Up @@ -51,6 +52,7 @@
</style>
';
}

?>
<div id="twp-wrap" class="wrap">
<h1><?php echo __("Telegram for WordPress", "twp-plugin") ?></h1>
Expand Down Expand Up @@ -96,7 +98,7 @@
<tr>
<th scope="row"><h3>API Token</h3></th>
<td>
<input id="twp_api_token" type="text" name="twp_api_token" maxlength="34" size="32" value="<?php echo get_option('twp_api_token'); ?>" dir="auto"/>
<input id="twp_api_token" type="text" name="twp_api_token" maxlength="34" size="32" value="<?php echo $tdata['twp_api_token']->option_value; ?>" dir="auto"/>
</td>
</tr>
<tr>
Expand All @@ -105,7 +107,7 @@
</tr>
<tr>
<th scope="row"><h3><?php echo __("Hashtag (optional)", "twp-plugin") ?></h3></th>
<td><input id="twp_hashtag" type="text" name="twp_hashtag" size="32" value="<?php echo get_option('twp_hashtag'); ?>" dir="auto" />
<td><input id="twp_hashtag" type="text" name="twp_hashtag" size="32" value="<?php echo $tdata['twp_hashtag']->option_value; ?>" dir="auto" />
<p class="howto">
<?php echo __("Insert a custom hashtag at the beginning of the messages.", "twp-plugin") ?><br>
<?php echo __("Don't forget <code>#</code> at the beginning", "twp-plugin") ?>
Expand Down Expand Up @@ -138,7 +140,7 @@
<tr>
<th scope="row"><h3>Bot Token</h3></th>
<td>
<input id="twp_bot_token" type="text" name="twp_bot_token" size="32" value="<?php echo get_option('twp_bot_token'); ?>" dir="auto" />
<input id="twp_bot_token" type="text" name="twp_bot_token" size="32" value="<?php echo $tdata['twp_bot_token']->option_value; ?>" dir="auto" />
<button id="checkbot" class="button-secondary" type="button" onclick="botTest()"><?php echo __("Check bot token", "twp-plugin") ?></button>
<p class="howto">
<?php echo __("Bot Info: ", "twp-plugin") ?>
Expand All @@ -150,18 +152,18 @@
<tr>
<th scope="row"><h3><?php echo __("Channel Username", "twp-plugin") ?></h3></th>
<td>
<input id="twp_channel_username" type="text" name="twp_channel_username" size="32" value="<?php echo get_option('twp_channel_username'); ?>" dir="auto" />
<input id="twp_channel_username" type="text" name="twp_channel_username" size="32" value="<?php echo $tdata['twp_channel_username']->option_value; ?>" dir="auto" />
<button id="channelbtn" type="button" class="button-secondary" onclick="channelTest();"> <?php echo __("Send now!", "twp-plugin") ?></button>
<p class="howto"><?php echo __("Don't forget <code>@</code> at the beginning", "twp-plugin") ?></p>
</td>
</tr>
<tr>
<?php
$preview = get_option('twp_web_preview');
$sc = get_option('twp_send_to_channel');
$cp = get_option( 'twp_channel_pattern');
$s = get_option('twp_send_thumb');
$m = get_option( 'twp_markdown' );
$preview = $tdata['twp_web_preview']->option_value;
$sc = $tdata['twp_send_to_channel']->option_value;
$cp = $tdata['twp_channel_pattern']->option_value;
$s = $tdata['twp_send_thumb']->option_value;
$m = $tdata['twp_markdown']->option_value;
?>
<th scope="row"><h3><?php echo __("Always send to Telegram", "twp-plugin") ?></h3></th>
<td>
Expand Down Expand Up @@ -221,7 +223,7 @@ function sendTest() {
jQuery('#sendbtn').prop('disabled', true);
jQuery('#sendbtn').text('<?php echo __("Please wait...", "twp-plugin") ?> ');
if(jQuery("#twp_hashtag").val() != ''){
var h = '<?php echo get_option("twp_hashtag"); ?>';
var h = '<?php echo $tdata["twp_hashtag"]->option_value; ?>';
}
var msg = h +'\n'+'<?php echo __("This is a test message", "twp-plugin") ?>'+ '\n' + document.URL;
jQuery.post(ajaxurl,
Expand All @@ -244,7 +246,10 @@ function channelTest() {
if( c == true ){
jQuery('#channelbtn').prop('disabled', true);
jQuery('#channelbtn').text('<?php echo __("Please wait...", "twp-plugin") ?> ');
var msg = '<?php echo __("This is a test message", "twp-plugin") ?>'+'\n'+ pattern;
var msg = '<?php echo __("This is a test message", "twp-plugin") ?>'+'\n';
if (pattern != null || pattern != ''){
msg += pattern;
}
jQuery.post(ajaxurl,
{
channel_username: channel_username, msg: msg , bot_token: bot_token, markdown: jQuery('input[name=twp_markdown]:checked').attr("data-markdown"), web_preview: jQuery('#twp_web_preview').prop('checked'), subject: 'c', action:'twp_ajax_test'
Expand Down
73 changes: 50 additions & 23 deletions functions.php
Expand Up @@ -50,6 +50,18 @@ function twp_check_db_when_loaded() {
add_action('plugins_loaded', 'twp_check_db_when_loaded');
#End forked functions

/**
* An optimized function for getting our options from db
*
* @since 1.5
*/
function twp_get_option() {
global $wpdb;
$query = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'twp_%%'";
$twp_data = $wpdb->get_results($query, OBJECT_K);
return $twp_data;
}

/**
* Print admin settings page
*/
Expand Down Expand Up @@ -121,6 +133,7 @@ function twp_sanitize_text_field($str) {
*/
return apply_filters( 'twp_sanitize_text_field', $filtered, $str );
}

/**
* Load plugin textdomain.
*
Expand All @@ -147,11 +160,12 @@ function twp_plugin_action_links($links) {
* This will get information about sent mail from PHPMailer and send it to user
*/
function twp_mail_action($result, $to, $cc, $bcc, $subject, $body){
global $tdata;
$nt = new Notifcaster_Class();
$_apitoken = get_option('twp_api_token');
$_apitoken = $tdata['twp_api_token']->option_value;
$_msg = $body;
if(get_option('twp_hashtag') != '') {
$_msg = get_option('twp_hashtag')."\n".$_msg;
if($tdata['twp_hashtag']->option_value != '') {
$_msg = $tdata['twp_hashtag']->option_value."\n".$_msg;
}
$nt->Notifcaster($_apitoken);
$nt->notify($_msg);
Expand All @@ -173,7 +187,7 @@ function twp_api_admin_notice($message) {
echo"<div class=\"$class\"> <p>$message</p></div>";
}

add_action( 'add_meta_boxes', 'twp_add_meta_box' );

/**
* Adds a box to the main column on the Post and Page edit screens.
*/
Expand All @@ -190,6 +204,8 @@ function twp_add_meta_box() {
);
}
}
add_action( 'add_meta_boxes', 'twp_add_meta_box' );

/*
* Gets the excerpt of a specific post ID or object
* @param - $post - object - the object of the post to get the excerpt of
Expand Down Expand Up @@ -220,6 +236,7 @@ function excerpt_by_id($post, $length = 55, $tags = '<a><em><strong>', $extra =
function twp_meta_box_callback( $post ) {
global $wpdb;
global $table_name;
global $tdata;
// Add a nonce field so we can check for it later.
$ID = $post->ID;
wp_nonce_field( 'twp_save_meta_box_data', 'twp_meta_box_nonce' );
Expand All @@ -228,19 +245,19 @@ function twp_meta_box_callback( $post ) {
$check_state = "";
$is_product = false;
$twp_log = $wpdb->get_row( "SELECT * FROM $table_name WHERE post_id = $ID", ARRAY_A );
if (get_option('twp_channel_username') == "" || get_option('twp_bot_token') == "")
if ($tdata['twp_channel_username']->option_value == "" || $tdata['twp_bot_token']->option_value == "")
{
$dis = "disabled=disabled";
$error = "<span style='color:red;font-weight:700;'>".__("Bot token or Channel username aren't set!", "twp-plugin")."</span><br>";
}
#Experimental feature : Always check the the Send to channel option
#Experimental feature : Always check Send to channel option
// $sc = get_post_meta($ID, '_twp_send_to_channel', true);
// $sc = $sc != "" ? $sc : get_option( 'twp_send_to_channel');
$sc = get_option( 'twp_send_to_channel');
$sc = $tdata['twp_send_to_channel']->option_value;
$cp = get_post_meta($ID, '_twp_meta_pattern', true);
$cp = $cp != "" ? $cp : get_option( 'twp_channel_pattern');
$cp = $cp != "" ? $cp : $tdata['twp_channel_pattern']->option_value;
$s = get_post_meta($ID, '_twp_send_thumb', true);
$s = $s != "" ? $s : get_option( 'twp_send_thumb');
$s = $s != "" ? $s : $tdata[ 'twp_send_thumb']->option_value;
if ($post->post_type == 'product'){
$is_product = true;
}
Expand All @@ -264,7 +281,18 @@ function twp_meta_box_callback( $post ) {
</fieldset>
</table>
<hr>
<p><?php echo __("Sending result: ", "twp-plugin") ?></p><span id="twp_last_publish" style="font-weight:700"><?php echo $twp_log['sending_result'].' || '.__("Date: ", "twp-plugin").$twp_log['time'] ?></span>
<p><?php echo __("Sending result: ", "twp-plugin") ?></p>
<span id="twp_last_publish" style="font-weight:700">
<?php
if($twp_log['sending_result'] == 1){
# This prevents adding a repetitive phrase to db.
$sending_result = __("Published successfully", "twp-plugin");
} else {
$sending_result = $twp_log['sending_result'];
}
echo $sending_result.' || '.__("Date: ", "twp-plugin").$twp_log['time']
?>
</span>
</div>

<?php
Expand All @@ -276,6 +304,7 @@ function twp_meta_box_callback( $post ) {
* @param int $ID The ID of the post being saved.
*/
function twp_save_meta_box_data( $ID, $post ) {
global $tdata;
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
Expand Down Expand Up @@ -315,8 +344,8 @@ function twp_save_meta_box_data( $ID, $post ) {
update_post_meta( $ID, '_twp_send_to_channel', $_POST['twp_send_to_channel']);
# Load global options
# p_ prefix stands for $_POST data
$tcp = get_option( 'twp_channel_pattern');
$tst = get_option('twp_send_thumb');
$tcp = $tdata['twp_channel_pattern']->option_value;
$tst = $tdata['twp_send_thumb']->option_value;
if ( $tcp != $_POST['twp_channel_pattern']) {
$p_tcp = $_POST['twp_channel_pattern'];
update_post_meta( $ID, '_twp_meta_pattern', $p_tcp);
Expand Down Expand Up @@ -346,14 +375,15 @@ function twp_save_meta_box_data( $ID, $post ) {
function twp_post_published ( $ID, $post ) {
global $wpdb;
global $table_name;
global $tdata;
# Checks whether user wants to send this post to channel.
if(get_post_meta($ID, '_twp_send_to_channel', true) == 1){
$pattern = get_post_meta($ID, '_twp_meta_pattern', true);
if ($pattern == "" || $pattern == false){
$pattern = get_option( 'twp_channel_pattern');
$pattern = $tdata['twp_channel_pattern']->option_value;
}
if( ! in_array( '_twp_send_thumb', get_post_custom_keys( $ID ) ) ) {
$thumb_method = get_option( 'twp_send_thumb');
$thumb_method = $tdata['twp_send_thumb']->option_value;
} else {
$thumb_method = get_post_meta($ID, '_twp_send_thumb', true);
}
Expand All @@ -369,9 +399,9 @@ function twp_post_published ( $ID, $post ) {
$method = false;
}
# Initialize Telegram information
$ch_name = get_option('twp_channel_username');
$token = get_option('twp_bot_token');
$web_preview = get_option('twp_web_preview');
$ch_name = $tdata['twp_channel_username']->option_value;
$token = $tdata['twp_bot_token']->option_value;
$web_preview = $tdata['twp_web_preview']->option_value;
if ($token == "" || $ch_name == ""){
update_post_meta( $ID, '_twp_meta_data', __('Bot token or Channel username aren\'t set!', 'twp-plugin') );
return;
Expand All @@ -393,7 +423,7 @@ function twp_post_published ( $ID, $post ) {
}

$nt = new Notifcaster_Class();
switch (get_option('twp_markdown')) {
switch ($tdata['twp_markdown']->option_value) {
case 0:
$format = null;
break;
Expand Down Expand Up @@ -444,18 +474,15 @@ function twp_post_published ( $ID, $post ) {
if ($strip_wc){
$msg = str_replace($wc_tags, '', $msg);
}
# Applying Telegram markdown format (bold, italic, inline-url)
// if (get_option('twp_markdown') == 1){
// $msg = $nt->markdown($msg, 1, 1, 1 );
// }

if ($method == 'photo' && $photo != false ) {
$r = $nt->channel_photo($ch_name, $msg, $photo);
} else {
$r = $nt->channel_text($ch_name, $msg);
}
$publish_date = current_time( "mysql", $gmt = 0 );
if ($r["ok"] == true){
$sending_result = __('Published succesfully on ', 'twp-plugin');
$sending_result = 1;
} else {
$sending_result = $r["description"];
}
Expand Down
6 changes: 5 additions & 1 deletion twp.php
Expand Up @@ -27,6 +27,9 @@

require_once(TWP_PLUGIN_DIR."/inc/Notifcaster.class.php");
require_once("functions.php");

// Get all of our plugin options in just one query :)
$tdata = twp_get_option();
$twp_settings =
// create custom plugin settings menu
add_action('admin_menu', 'twp_create_menu');
Expand All @@ -51,8 +54,9 @@ function register_twp_settings() {
register_setting( 'twp-settings-group', 'twp_web_preview', 'sanitize_text_field' );
}


// If api_token has been set, then add our hook to phpmailer.
if (get_option('twp_api_token') != null ) {
if ($tdata['twp_api_token'] != null ) {
add_action( 'phpmailer_init', 'twp_phpmailer_hook' );
}

Expand Down
9 changes: 9 additions & 0 deletions uninstall.php
@@ -0,0 +1,9 @@
<?php
//if uninstall not called from WordPress exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}
//drop our twp_log table and delete all of our options from WordPress options table.
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}twp_logs" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}options WHERE option_name = 'twp_%'" );

0 comments on commit 82da532

Please sign in to comment.