Пример #1
0
/**
* When the post is published, send the messages.
* @param int $ID
* @param obj $post
*/
function twp_post_published($ID, $post)
{
    global $wpdb;
    global $table_name;
    global $tdata;
    if (isset($_POST['twp_send_to_channel'])) {
        update_post_meta($ID, '_twp_send_to_channel', $_POST['twp_send_to_channel']);
        # Load global options
        # p_ prefix stands for $_POST data
        $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);
        } else {
            update_post_meta($ID, '_twp_meta_pattern', $tcp);
        }
        if ($tst != $_POST['twp_send_thumb']) {
            $p_tst = $_POST['twp_send_thumb'];
            update_post_meta($ID, '_twp_send_thumb', $p_tst);
        } else {
            update_post_meta($ID, '_twp_send_thumb', $tst);
        }
        if (isset($_POST['twp_img_id'])) {
            update_post_meta($ID, '_twp_img_id', $_POST['twp_img_id']);
        }
    } else {
        update_post_meta($ID, '_twp_send_to_channel', 0);
    }
    # 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 = $tdata['twp_channel_pattern']->option_value;
        }
        if (!in_array('_twp_send_thumb', get_post_custom_keys($ID))) {
            $thumb_method = $tdata['twp_send_thumb']->option_value;
        } else {
            $thumb_method = get_post_meta($ID, '_twp_send_thumb', true);
        }
    }
    # If there is no pattern then return!
    if ($pattern == "") {
        return;
    }
    switch ($thumb_method) {
        case 1:
            $method = 'photo';
            $photo = get_attached_file(get_post_thumbnail_id($ID));
            break;
        case 2:
            $method = 'photo';
            $photo = get_attached_file(get_post_meta($ID, '_twp_img_id', true));
            break;
        default:
            $method = false;
            break;
    }
    # Initialize Telegram information
    $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;
    }
    if ($post->post_type == 'product') {
        $_pf = new WC_Product_Factory();
        $product = $_pf->get_product($ID);
        $tags_array = explode(', ', $product->get_tags());
        $categories_array = explode(', ', $product->get_categories());
    } else {
        $tags_array = wp_get_post_tags($ID, array('fields' => 'names'));
        $categories_array = wp_get_post_categories($ID, array('fields' => 'names'));
    }
    foreach ($tags_array as $tag) {
        $tags .= " #" . $tag;
    }
    foreach ($categories_array as $cat) {
        $categories .= "|" . $cat;
    }
    $nt = new Notifcaster_Class();
    switch ($tdata['twp_markdown']->option_value) {
        case 0:
            $format = null;
            break;
        case 1:
            $format = "markdown";
            break;
        case 2:
            $format = "html";
            break;
        default:
            $format = null;
            break;
    }
    $nt->_telegram($token, $format, $web_preview);
    # Preparing message for sending
    #Wordpress default tags and substitutes array
    $wp_tags = array("{title}", "{excerpt}", "{content}", "{author}", "{short_url}", "{full_url}", "{tags}", "{categories}");
    $wp_subs = array($post->post_title, wp_trim_words($post->post_content, 55, "..."), $post->post_content, get_the_author_meta("display_name", $post->post_author), wp_get_shortlink($ID), get_permalink($ID), $tags, $categories);
    #WooCommerce tags and substitutes array
    $wc_tags = array("{width}", "{length}", "{height}", "{weight}", "{price}", "{regular_price}", "{sale_price}", "{sku}", "{stock}", "{downloadable}", "{virtual}", "{sold_indiidually}", "{tax_status}", "{tax_class}", "{stock_status}", "{backorders}", "{featured}", "{visibility}");
    if ($post->post_type == 'product') {
        $p = $product;
        $wc_subs = array($p->width, $p->length, $p->height, $p->weight, $p->price, $p->regular_price, $p->sale_price, $p->sku, $p->stock, $p->downloadable, $p->virtual, $p->sold_individually, $p->tax_status, $p->tax_class, $p->stock_status, $p->backorders, $p->featured, $p->visibility);
    }
    # The variables are case-sensitive.
    $re = array("{title}", "{excerpt}", "{content}", "{author}", "{short_url}", "{full_url}", "{tags}", "{categories}");
    $subst = $wp_subs;
    if ($post->post_type == 'product') {
        $p = $product;
        array_merge($re, $wc_tags);
        array_merge($subst, $wc_subs);
    } else {
        #if it's not a product post then strip out all of the WooCommerce tags
        $strip_wc = true;
    }
    $msg = str_replace($re, $subst, $pattern);
    if ($strip_wc) {
        $msg = str_replace($wc_tags, '', $msg);
    }
    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 = 1;
    } else {
        $sending_result = $r["description"];
    }
    $twp_log = $wpdb->get_row("SELECT * FROM {$table_name} WHERE post_id = {$ID}");
    if ($twp_log == null) {
        $wpdb->replace($table_name, array('time' => $publish_date, 'post_id' => $ID, 'sending_result' => $sending_result));
    } else {
        $wpdb->update($table_name, array('time' => $publish_date, 'post_id' => $ID, 'sending_result' => $sending_result), array('post_id' => $ID));
    }
    update_post_meta($ID, '_twp_send_to_channel', 0);
    //unset($_POST['twp_send_to_channel']);
}