Plugin Directory

Changeset 1861487


Ignore:
Timestamp:
04/20/2018 12:28:04 AM (8 years ago)
Author:
fotkin
Message:

Ver 0.6

Location:
fotki-media-bot
Files:
17 added
5 edited

Legend:

Unmodified
Added
Removed
  • fotki-media-bot/trunk/class.incoming-data.php

    r1859835 r1861487  
    66    class FotkiMediaBot_IncomingData
    77    {
    8         public static function init_data() {
     8        public static function init_data()
     9        {
    910            $entityBody = file_get_contents('php://input');
    1011
     
    3637            }
    3738
    38             if (!empty($response['message']['photo'])) {
     39            $is_store_photos = self::is_store_media_by_media_type('photos', $bot_token);
     40            $is_store_videos = self::is_store_media_by_media_type('videos', $bot_token);
     41
     42            if (!empty($response['message']['photo']) && $is_store_photos) {
    3943                list($photo_string, $photo_data) = FotkiMediaBot_Telegram::get_telegram_photo($bot_token, $response);
    40                 self::savePhotoStringToMediaLibrary($photo_string, $photo_data);
     44                self::saveMediaStringToMediaLibrary($photo_string, $photo_data);
    4145            }
    4246
     
    5660                );
    5761
     62                // if it's gif and option not store photos, then not store
     63                if (!empty($response['message']['document']['file_name']) && strpos($response['message']['document']['file_name'],'.gif.') !== FALSE && !$is_store_photos) {
     64                    return;
     65                }
     66
     67                // if it's photo format and option not store photos, then not store
     68                if (in_array($allowed_file_types, array('jpg', 'jpeg', 'png', 'gif'),true) && !$is_store_photos) {
     69                    return;
     70                }
     71
     72                // if it's video format and option not store videos, then not store
     73                if (in_array($allowed_file_types, array('wmv', 'avi', 'mp4'),true) && !$is_store_videos) {
     74                    return;
     75                }
     76
    5877                if ($allowed_file_types) {
    5978                    list($photo_string, $photo_data) = FotkiMediaBot_Telegram::get_telegram_document($bot_token, $response);
     
    6180                    $photo_data['api_file_name'] = $response['message']['document']['file_name'];
    6281
    63                     self::savePhotoStringToMediaLibrary($photo_string, $photo_data);
    64                 }
    65             }
    66         }
    67 
    68         public static function savePhotoStringToMediaLibrary($photo_string, $photo_data) {
     82                    self::saveMediaStringToMediaLibrary($photo_string, $photo_data);
     83                }
     84            }
     85        }
     86
     87        public static function saveMediaStringToMediaLibrary($photo_string, $photo_data)
     88        {
    6989            $imageurl = $photo_data['photo_path'];
    7090            $imageurl = stripslashes($imageurl);
     
    124144                }
    125145
    126                 if (strpos($photo_data['api_file_name'], '.gif.') !== FALSE) {
    127                     update_post_meta($attach_id, '_' . FotkiMediaBot::FOTKI_MEDIA_BOT_PREFIX . 'is_gif', 1);
     146                if (!empty($photo_data['api_file_name']) && strpos($photo_data['api_file_name'], '.gif.') !== FALSE) {
     147                    update_post_meta($attach_id, '_' . FotkiMediaBot::FOTKI_MEDIA_BOT_PREFIX . 'media_type', 'gif');
    128148                }
    129149
     
    139159        }
    140160
    141         public static function write_incoming_log($message) {
     161        public static function is_store_media_by_media_type($media_type, $bot_token) {
     162            $bots = get_option(FotkiMediaBot::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     163
     164            if (!empty($bots)) {
     165
     166                $i = 0;
     167
     168                foreach ($bots as &$bot) {
     169                    foreach ($bot as $key => $value) {
     170                        if ($key === 'fmb_bot_token' && $value === $bot_token) {
     171                            return (empty($bot['fmb_bot_not_save_media_types'][$media_type]) ? true : false);
     172                        }
     173                    }
     174                    $i++;
     175                }
     176            }
     177
     178            return false;
     179        }
     180
     181        public static function write_incoming_log($message)
     182        {
    142183            $wp_date_format = get_option('date_format');
    143 
    144             $date = '';
    145184
    146185            if ($wp_date_format) {
     
    150189            }
    151190
    152             $log_file = $wp_debug_log_file_path = plugin_dir_path(__FILE__).'incoming_data_from_bots.log';
     191            $log_file = ABSPATH . '/wp-content/fotki-media-bot-' . get_option(FotkiMediaBot::FOTKI_MEDIA_BOT_PREFIX . 'callback_id') . '.log';
    153192
    154193            if ($handle = fopen($log_file, 'a')) {
  • fotki-media-bot/trunk/class.list-table.php

    r1855604 r1861487  
    2424                case 'fmb_bot_token':
    2525                    return esc_attr($item[$column_name]);
     26                case 'fmb_bot_not_save_media_types':
     27                    $not_save_photos = false;
     28                    $not_save_videos = false;
     29
     30                    if (!empty($item[$column_name])) {
     31                        foreach ($item[$column_name] as $key => $value) {
     32                            if ($key === 'photos' && !empty($value) && $value === '1') {
     33                                $not_save_photos = true;
     34                            } elseif ($key === 'videos' && !empty($value) && $value === '1') {
     35                                $not_save_videos = true;
     36                            }
     37                        }
     38                    }
     39
     40                    $html = '<div class="fmb-bot-status-group"><label for="save_photos"><input type="checkbox" class="fmb-bot-status-checkbox" id="save_photos" value="' . $item['fmb_bot_token'] . '" autocomplete="off"' . ($not_save_photos ?  '' : ' checked') . '> Photos</label> <span class="fmb-bot-status-checkbox-message"></span></div>';
     41                    $html .= '<div class="fmb-bot-status-group"><label for="save_videos"><input type="checkbox" class="fmb-bot-status-checkbox" id="save_videos" value="' . $item['fmb_bot_token'] . '" autocomplete="off"' . ($not_save_videos ?  '' : ' checked') . '> Videos</label> <span class="fmb-bot-status-checkbox-message"></span></div>';
     42
     43                    return $html;
    2644                default:
    2745                    return print_r($item, true); //Show the whole array for troubleshooting purposes
     
    3553                'fmb_bot_name' => __('Name', 'fotki-media-bot'),
    3654                'fmb_bot_service' => __('Service', 'fotki-media-bot'),
    37                 'fmb_bot_token' => __('Token', 'fotki-media-bot')
     55                'fmb_bot_token' => __('Token', 'fotki-media-bot'),
     56                'fmb_bot_not_save_media_types' => __('Store media type', 'fotki-media-bot'),
    3857            ];
    3958
     
    4362        public function column_fmb_bot_name($item)
    4463        {
    45             $actions = [
    46                 //'edit'      => sprintf('<a href="?page=%s&action=%s&book=%s">Edit</a>',$_REQUEST['page'],'edit',$item['fmb_bot_token']),
    47                 'delete' => sprintf('<a href="?page=%s&action=%s&token=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', esc_attr($item['fmb_bot_token'])),
    48                 'disable_webhook' => sprintf('<a href="?page=%s&action=%s&token=%s">Disable webhook</a>', esc_attr($_REQUEST['page']), 'disable_webhook', esc_attr($item['fmb_bot_token'])),
    49                 'enable_webhook' => sprintf('<a href="?page=%s&action=%s&token=%s">Enable webhook</a>', esc_attr($_REQUEST['page']), 'enable_webhook', esc_attr($item['fmb_bot_token'])),
    50             ];
     64            $is_bot_active = FotkiMediaBot_Telegram::is_webhook_enabled($item['fmb_bot_token']);
    5165
    52             return sprintf('%1$s %2$s', esc_attr($item['fmb_bot_name']), $this->row_actions($actions));
     66            $actions['delete'] = sprintf('<a href="?page=%s&action=%s&token=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', esc_attr($item['fmb_bot_token']));
     67
     68            if ($is_bot_active) {
     69                $actions['disable_webhook'] = sprintf('<a href="?page=%s&action=%s&token=%s">Disable webhook</a>', esc_attr($_REQUEST['page']), 'disable_webhook', esc_attr($item['fmb_bot_token']));
     70            } else {
     71                $actions['enable_webhook'] = sprintf('<a href="?page=%s&action=%s&token=%s">Enable webhook</a>', esc_attr($_REQUEST['page']), 'enable_webhook', esc_attr($item['fmb_bot_token']));
     72            }
     73
     74            $status_html = '<span class="fmb-bot-status ' . ($is_bot_active ? 'enabled' : 'disabled') . '" title="' . ($is_bot_active ? __('Bot enabled', 'fotki-media-bot') : __('Bot disabled', 'fotki-media-bot')) . '">&nbsp;</span>';
     75
     76            return sprintf('%1$s %2$s', $status_html . " " . esc_attr($item['fmb_bot_name']), $this->row_actions($actions));
    5377        }
    5478
     
    6387            $bots = get_option(FotkiMediaBot::FOTKI_MEDIA_BOT_PREFIX . 'bots');
    6488
    65             if (!empty($bots)) {
    66                 foreach ($bots as &$bot) {
    67                     foreach ($bot as $key => $value) {
    68                         $bot[$key] = esc_attr($value);
    69                     }
    70                 }
    71             }
    72 
    7389            $this->items = $bots;
    7490        }
  • fotki-media-bot/trunk/class.telegram.php

    r1859835 r1861487  
    3838        }
    3939
     40        public static function get_webhook_info($bot_token)
     41        {
     42            $url = 'https://' . self::TELEGRAM_API_URL . '/bot' . $bot_token . '/getWebhookInfo';
     43
     44            $response = wp_remote_get($url);
     45            $result = wp_remote_retrieve_body($response);
     46
     47            if ($result) {
     48                return json_decode($result, true);
     49            }
     50
     51            return false;
     52        }
     53
     54        public static function is_webhook_enabled($bot_token) {
     55            $bot_webhook_info = self::get_webhook_info($bot_token);
     56
     57            if ($bot_webhook_info['ok']) {
     58                return $bot_webhook_info['result']['url'];
     59            }
     60
     61            return false;
     62        }
     63
    4064        public static function get_telegram_photo($bot_token, $data)
    4165        {
     
    4771
    4872            $photo_data['chat_id'] = sanitize_text_field($data['message']['chat']['id']);
    49             $photo_data['chat_title'] = sanitize_text_field($data['message']['chat']['title']);
     73            $photo_data['chat_title'] = !empty($data['message']['chat']['title']) ? sanitize_text_field($data['message']['chat']['title']) : '';
    5074
    5175            // get last photo element (last is most bigger photo)
     
    7498
    7599            $photo_data['chat_id'] = sanitize_text_field($data['message']['chat']['id']);
    76             $photo_data['chat_title'] = sanitize_text_field($data['message']['chat']['title']);
     100            $photo_data['chat_title'] = !empty($data['message']['chat']['title']) ? sanitize_text_field($data['message']['chat']['title']) : '';
    77101
    78             // get last photo element (last is most bigger photo)
    79102            $document = $data['message']['document'];
    80103            $file_id = $document['file_id'];
  • fotki-media-bot/trunk/fotki-media-bot.php

    r1859835 r1861487  
    33Plugin Name: Fotki Media Bot
    44Plugin URI: https://agency.fotki.com/wordpress-plugins/fotki-media-bot/
    5 Description: Fotki Media Bot allows saves your photos from your chats (currently we only support Telegram messenger) to WordPress. It also creates daily posts in your Posts and puts your photos there. This plugin is in beta, please report to us any issues, we will try to take care of them.
    6 Version: 0.5
     5Description: Fotki Media Bot allows saves your media from your chats (currently we only support Telegram messenger) to WordPress. It also creates daily posts in your Posts and puts your media there. This plugin is in beta, please report to us any issues, we will try to take care of them.
     6Version: 0.6
    77Author: FotkiAgency
    88Author URI: https://agency.fotki.com/
     
    1616
    1717if (!class_exists('FotkiMediaBot')) {
     18    register_activation_hook(__FILE__, ['FotkiMediaBot', 'fmb_install']);
     19    //register_deactivation_hook(__FILE__, ['FotkiMediaBot', 'on_deactivation']);
     20    register_uninstall_hook(__FILE__, ['FotkiMediaBot', 'fmb_uninstall']);
     21
    1822    class FotkiMediaBot
    1923    {
     
    3741        public function __construct()
    3842        {
    39             if (is_admin()) {
    40                 register_activation_hook(__FILE__, [$this, 'fmb_install']);
    41                 register_uninstall_hook(__FILE__, 'fmb_uninstall');
    42 
    43                 add_action('admin_menu', [$this, 'fmb_page_links']);
    44 
    45                 add_filter('attachment_fields_to_edit', [$this, 'add_image_attachment_fields_to_edit'], 10, 2);
    46             }
    47 
    4843            add_filter('query_vars', [$this, 'fmb_query_vars']);
    4944            add_action('parse_request', [$this, 'fmb_parse_request']);
    5045
    5146            add_shortcode('fotki_media_bot', [$this, 'fmb_shortcode']);
    52         }
    53 
    54         public function fmb_install()
     47
     48            if (is_admin()) {
     49                add_action('admin_menu', [$this, 'fmb_page_links']);
     50
     51                add_filter('attachment_fields_to_edit', [$this, 'add_image_attachment_fields_to_edit'], 10, 2);
     52
     53                add_action('admin_enqueue_scripts', [$this, 'register_plugin_js_and_styles']);
     54            }
     55
     56            add_action('wp_ajax_fmb_store_media_type_options', [$this, 'store_media_type_options_ajax']);
     57        }
     58
     59        public static function fmb_install()
    5560        {
    5661            global $wpdb;
     
    8186                    dbDelta($sql);
    8287
    83                     add_option('fotki_media_bot_db_version', self::FOTKI_MEDIA_BOT_DB_VERSION);
     88                    add_option(self::FOTKI_MEDIA_BOT_PREFIX . 'db_version', self::FOTKI_MEDIA_BOT_DB_VERSION);
    8489                }
    8590            }
     
    9499        }
    95100
    96         public function fmb_uninstall()
     101        public static function fmb_uninstall()
    97102        {
    98103            global $wpdb;
    99104
     105            // if we have some bots then need disable this bots that they not send to site anything
     106            $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     107
     108            if (!empty($bots)) {
     109                foreach ($bots as &$bot) {
     110                    foreach ($bot as $key => $value) {
     111                        if ($key === 'fmb_bot_token') {
     112                            FotkiMediaBot_Telegram::delete_webhook($value);
     113                        }
     114                    }
     115                }
     116            }
     117
     118            // drop table if exists
    100119            $sql = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . self::FOTKI_MEDIA_BOT_BOTS_TABLE_NAME;
    101120            $wpdb->query($sql);
    102121
    103             delete_option('fotki_media_bot_db_version');
    104             delete_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id');
    105             delete_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     122            // find all options created by plugin and remove them all
     123            $all_options = wp_load_alloptions();
     124
     125            foreach ($all_options as $name => $value) {
     126                if (stristr($name,self::FOTKI_MEDIA_BOT_PREFIX)) {
     127                    delete_option($name);
     128                }
     129            }
    106130        }
    107131
     
    161185        }
    162186
     187        public function register_plugin_js_and_styles() {
     188            wp_register_style(self::FOTKI_MEDIA_BOT_SLUG.'-main-css',plugin_dir_url(__FILE__) . 'admin/css/fotki_media_bot.css');
     189            wp_enqueue_style(self::FOTKI_MEDIA_BOT_SLUG.'-main-css');
     190
     191            wp_register_script(self::FOTKI_MEDIA_BOT_SLUG.'-main-js', plugin_dir_url(__FILE__) . 'admin/js/fotki_media_bot.js', ['jquery'], '', TRUE);
     192            wp_localize_script(self::FOTKI_MEDIA_BOT_SLUG.'-main-js', 'fotki_media_bot_data', ['ajax_url' => admin_url('admin-ajax.php')]);
     193            wp_enqueue_script(self::FOTKI_MEDIA_BOT_SLUG.'-main-js');
     194        }
     195
     196        function store_media_type_options_ajax() {
     197            $debug = [];
     198            $success = 0;
     199
     200            $bot_token = !empty($_POST['bot_token']) ? sanitize_text_field($_POST['bot_token']) : '';
     201            $media_type = !empty($_POST['media_type']) ? sanitize_text_field($_POST['media_type']) : '';
     202            $status = !empty($_POST['status']) ? sanitize_text_field($_POST['status']) : '';
     203
     204            if (!empty($bot_token) && !empty($media_type)) {
     205
     206                if ($media_type === 'photos' || $media_type === 'videos') {
     207
     208                    $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     209
     210                    if (!empty($bots)) {
     211
     212                        $i = 0;
     213
     214                        foreach ($bots as &$bot) {
     215                            foreach ($bot as $key => $value) {
     216                                if ($key === 'fmb_bot_token' && $value === $bot_token) {
     217                                    $bots[$i]['fmb_bot_not_save_media_types'][$media_type] = $status;
     218                                }
     219                            }
     220                            $i++;
     221                        }
     222
     223                        if (update_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots', $bots)) {
     224                            $success = 1;
     225                        } else {
     226                            $success = 0;
     227                        }
     228                    } else {
     229                        $success = 0;
     230                    }
     231                }
     232            } else {
     233                $success = 0;
     234            }
     235
     236            die(json_encode(['success' => $success, 'messages' => $debug]));
     237        }
     238
    163239        public function fmb_main_page()
    164240        {
     
    170246            global $wpdb;
    171247
    172             ?>
    173             <div class="wrap">
    174                 <h1><?php echo get_admin_page_title(); ?></h1>
    175                 <?php
    176 //                echo '<div>Callback ID: ' . get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id') . '</div>';
    177 
    178                 if (isset($_POST['bot_submited'])) {
    179                     $bot_name = sanitize_text_field($_POST['bot_name']);
    180                     $bot_service = sanitize_text_field($_POST['bot_service']);
    181                     $bot_token = sanitize_text_field($_POST['bot_token']);
    182 
    183                     if ($bot_name && $bot_service && $bot_token) {
    184                         $bot_options = [
    185                             'fmb_bot_name' => $bot_name,
    186                             'fmb_bot_service' => $bot_service,
    187                             'fmb_bot_token' => $bot_token,
    188                         ];
    189                     }
    190 
    191                     $set_webhook = FotkiMediaBot_Telegram::set_webhook($bot_token, $this->get_callback_url());
    192 
    193                     if ($set_webhook['ok']) {
    194                         if (update_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots', [$bot_options])) {
    195                             ?>
    196                             <div class="updated"><p><strong><?php _e("Bot added."); ?></strong></p>
    197                             </div>
    198                             <?php
    199                         } else {
    200                             ?>
    201                             <div class="error"><p><strong><?php _e("Bot not saved."); ?></strong></p></div>
    202                             <?php
    203                         }
    204                     } else {
    205                         ?>
    206                         <div class="error"><p>
    207                                 <strong><?php _e("Bot not saved. Message from API: \"" . $set_webhook["description"] . "\""); ?></strong>
    208                             </p></div>
    209                         <?php
    210                     }
    211 
    212                     $this->show_bots();
    213                 } elseif (isset($_GET['action']) && $_GET['action'] === 'delete') {
    214 
    215                     if (isset($_GET['token'])) {
    216                         $token = sanitize_text_field($_GET['token']);
    217                     }
    218 
    219                     if (!empty($token)) {
    220                         $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
    221 
    222                         if (!empty($bots)) {
    223 
    224                             $i = 0;
    225 
    226                             foreach ($bots as &$bot) {
    227                                 foreach ($bot as $key => $value) {
    228                                     if ($key === 'fmb_bot_token' && $value === $token) {
    229                                         unset($bots[$i]);
    230                                     }
    231                                 }
    232                                 $i++;
    233                             }
    234 
    235                             if (update_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots', $bots)) {
     248            if (!empty(get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id'))) {
     249                ?>
     250                <div class="wrap">
     251                    <h1><?php echo get_admin_page_title(); ?></h1>
     252                    <?php
     253
     254
     255                    if (isset($_POST['bot_submited'])) {
     256                        $bot_name = sanitize_text_field($_POST['bot_name']);
     257                        $bot_service = sanitize_text_field($_POST['bot_service']);
     258                        $bot_token = sanitize_text_field($_POST['bot_token']);
     259
     260                        if ($bot_name && $bot_service && $bot_token) {
     261                            $bot_options = [
     262                                'fmb_bot_name' => $bot_name,
     263                                'fmb_bot_service' => $bot_service,
     264                                'fmb_bot_token' => $bot_token,
     265                            ];
     266                        }
     267
     268                        $set_webhook = FotkiMediaBot_Telegram::set_webhook($bot_token, $this->get_callback_url());
     269
     270                        if ($set_webhook['ok']) {
     271                            if (update_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots', [$bot_options])) {
    236272                                ?>
    237                                 <div class="updated"><p><strong><?php _e("Bot removed."); ?></strong></p>
     273                                <div class="updated"><p><strong><?php _e("Bot added."); ?></strong></p>
    238274                                </div>
    239275                                <?php
    240276                            } else {
    241277                                ?>
    242                                 <div class="error"><p><strong><?php _e("Bot not removed."); ?></strong></p></div>
     278                                <div class="error"><p><strong><?php _e("Bot not saved."); ?></strong></p></div>
    243279                                <?php
    244280                            }
    245                         }
    246                     }
    247 
    248                     $this->show_default_main_page();
    249                 } elseif (isset($_GET['action']) && $_GET['action'] === 'disable_webhook') {
    250 
    251                     if (isset($_GET['token'])) {
    252                         $token = sanitize_text_field($_GET['token']);
    253                     }
    254 
    255                     if (!empty($token)) {
    256                         $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
    257 
    258                         if (!empty($bots)) {
    259 
    260                             $i = 0;
    261                             $hook_disabled = false;
    262 
    263                             foreach ($bots as &$bot) {
    264                                 foreach ($bot as $key => $value) {
    265                                     if ($key === 'fmb_bot_token' && $value === $token) {
    266                                         if (FotkiMediaBot_Telegram::delete_webhook($token)) {
    267                                             $hook_disabled = true;
     281                        } else {
     282                            ?>
     283                            <div class="error"><p>
     284                                    <strong><?php _e("Bot not saved. Message from API: \"" . $set_webhook["description"] . "\""); ?></strong>
     285                                </p></div>
     286                            <?php
     287                        }
     288
     289                        $this->show_bots();
     290                    } elseif (isset($_GET['action']) && $_GET['action'] === 'delete') {
     291
     292                        if (isset($_GET['token'])) {
     293                            $token = sanitize_text_field($_GET['token']);
     294                        }
     295
     296                        if (!empty($token)) {
     297                            $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     298
     299                            if (!empty($bots)) {
     300
     301                                $i = 0;
     302
     303                                foreach ($bots as &$bot) {
     304                                    foreach ($bot as $key => $value) {
     305                                        if ($key === 'fmb_bot_token' && $value === $token) {
     306                                            unset($bots[$i]);
    268307                                        }
    269308                                    }
     309                                    $i++;
    270310                                }
    271                                 $i++;
     311
     312                                if (update_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots', $bots)) {
     313                                    ?>
     314                                    <div class="updated"><p><strong><?php _e("Bot removed."); ?></strong></p>
     315                                    </div>
     316                                    <?php
     317                                } else {
     318                                    ?>
     319                                    <div class="error"><p><strong><?php _e("Bot not removed."); ?></strong></p></div>
     320                                    <?php
     321                                }
    272322                            }
    273 
    274                             if ($hook_disabled) {
    275                                 ?>
    276                                 <div class="updated"><p><strong><?php _e("Weebhook disabled."); ?></strong></p>
    277                                 </div>
    278                                 <?php
    279                             } else {
    280                                 ?>
    281                                 <div class="error"><p><strong><?php _e("Weebhook not disabled."); ?></strong></p></div>
    282                                 <?php
    283                             }
    284                         }
    285                     }
    286 
    287                     $this->show_default_main_page();
    288                 } elseif (isset($_GET['action']) && $_GET['action'] === 'enable_webhook') {
    289 
    290                     if (isset($_GET['token'])) {
    291                         $token = sanitize_text_field($_GET['token']);
    292                     }
    293 
    294                     if (!empty($token)) {
    295                         $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
    296 
    297                         if (!empty($bots)) {
    298 
    299                             $i = 0;
    300                             $hook_enabled = false;
    301 
    302                             foreach ($bots as &$bot) {
    303                                 foreach ($bot as $key => $value) {
    304                                     if ($key === 'fmb_bot_token' && $value === $token) {
    305                                         $set_webhook = FotkiMediaBot_Telegram::set_webhook($token, $this->get_callback_url());
    306 
    307                                         if ($set_webhook['ok']) {
    308                                             $hook_enabled = true;
     323                        }
     324
     325                        $this->show_default_main_page();
     326                    } elseif (isset($_GET['action']) && $_GET['action'] === 'disable_webhook') {
     327
     328                        if (isset($_GET['token'])) {
     329                            $token = sanitize_text_field($_GET['token']);
     330                        }
     331
     332                        if (!empty($token)) {
     333                            $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     334
     335                            if (!empty($bots)) {
     336
     337                                $i = 0;
     338                                $hook_disabled = false;
     339
     340                                foreach ($bots as &$bot) {
     341                                    foreach ($bot as $key => $value) {
     342                                        if ($key === 'fmb_bot_token' && $value === $token) {
     343                                            if (FotkiMediaBot_Telegram::delete_webhook($token)) {
     344                                                $hook_disabled = true;
     345                                            }
    309346                                        }
    310347                                    }
     348                                    $i++;
    311349                                }
    312                                 $i++;
     350
     351                                if ($hook_disabled) {
     352                                    ?>
     353                                    <div class="updated"><p><strong><?php _e("Weebhook disabled."); ?></strong></p>
     354                                    </div>
     355                                    <?php
     356                                } else {
     357                                    ?>
     358                                    <div class="error"><p><strong><?php _e("Weebhook not disabled."); ?></strong></p>
     359                                    </div>
     360                                    <?php
     361                                }
    313362                            }
    314 
    315                             if ($hook_enabled) {
    316                                 ?>
    317                                 <div class="updated"><p><strong><?php _e("Weebhook enabled."); ?></strong></p>
    318                                 </div>
    319                                 <?php
    320                             } else {
    321                                 ?>
    322                                 <div class="error"><p><strong><?php _e("Weebhook not enabled."); ?></strong></p></div>
    323                                 <?php
     363                        }
     364
     365                        $this->show_default_main_page();
     366                    } elseif (isset($_GET['action']) && $_GET['action'] === 'enable_webhook') {
     367
     368                        if (isset($_GET['token'])) {
     369                            $token = sanitize_text_field($_GET['token']);
     370                        }
     371
     372                        if (!empty($token)) {
     373                            $bots = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'bots');
     374
     375                            if (!empty($bots)) {
     376
     377                                $i = 0;
     378                                $hook_enabled = false;
     379
     380                                foreach ($bots as &$bot) {
     381                                    foreach ($bot as $key => $value) {
     382                                        if ($key === 'fmb_bot_token' && $value === $token) {
     383                                            $set_webhook = FotkiMediaBot_Telegram::set_webhook($token, $this->get_callback_url());
     384
     385                                            if ($set_webhook['ok']) {
     386                                                $hook_enabled = true;
     387                                            }
     388                                        }
     389                                    }
     390                                    $i++;
     391                                }
     392
     393                                if ($hook_enabled) {
     394                                    ?>
     395                                    <div class="updated"><p><strong><?php _e("Weebhook enabled."); ?></strong></p>
     396                                    </div>
     397                                    <?php
     398                                } else {
     399                                    ?>
     400                                    <div class="error"><p><strong><?php _e("Weebhook not enabled."); ?></strong></p>
     401                                    </div>
     402                                    <?php
     403                                }
    324404                            }
    325405                        }
     406
     407                        $this->show_default_main_page();
     408
     409                    } else {
     410                        $this->show_default_main_page();
    326411                    }
    327 
    328                     $this->show_default_main_page();
    329 
    330                 } else {
    331                     $this->show_default_main_page();
    332                 }
    333                 ?>
    334 
    335 
    336             </div>
    337             <?php
     412                    ?>
     413
     414
     415                </div>
     416                <?php
     417            } else {
     418                echo '<div class="wrap">There is no callback ID. Please try re-activate plugin and if problem still exist please contact us.</div>';
     419            }
    338420        }
    339421
     
    351433            echo '<h1>' . get_admin_page_title() . '</h1>';
    352434
    353             $wp_debug_log_file_path = plugin_dir_path(__FILE__).'incoming_data_from_bots.log';
     435            $wp_debug_log_file_path = get_home_path() . 'wp-content/fotki-media-bot-' . get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id') . '.log';
    354436
    355437            if (file_exists($wp_debug_log_file_path)) {
     
    401483
    402484            <p>
    403                 Now you can chat with your Bot in Telegram Messenger and send photos to it, and Bot will save these photos to your WordPress Media Library. Also, you can add your Bot to any of your group chats and Bot will save to WordPress Media Library all photos you send to each other on Telegram.
     485                Now you can chat with your Bot in Telegram Messenger and send media to it, and Bot will save these media to your WordPress Media Library. Also, you can add your Bot to any of your group chats and Bot will save to WordPress Media Library all media you send to each other on Telegram.
    404486            </p>
    405487
     
    502584
    503585            # if title eq date forma, took date from title, if not - take date from post_date
    504             # if user will create post with own date and with our shortocode, then he wanna show photos for this date
    505             # but post date is today and will show only photos for post creation date
     586            # if user will create post with own date and with our shortocode, then he wanna show media for this date
     587            # but post date is today and will show only media for post creation date
    506588            if ($post->post_title === $this->fmb_validate_date($post->post_title, $date_format)) {
    507589                $parsed_date = date_parse($post->post_title);
     
    565647        {
    566648
    567             $is_photo_by_fmb = get_post_meta($post->ID, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'uploaded_by_fmb', true);
    568             if (!empty($is_photo_by_fmb)) {
     649            $is_media_by_fmb = get_post_meta($post->ID, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'uploaded_by_fmb', true);
     650            if (!empty($is_media_by_fmb)) {
    569651                $form_fields[self::FOTKI_MEDIA_BOT_PREFIX . 'uploaded_by_fmb'] = [
    570652                    'label' => __("By"),
     
    610692            }
    611693
    612             $is_gif = get_post_meta($post->ID, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'is_gif', true);
    613             if (!empty($is_gif)) {
    614                 $form_fields[self::FOTKI_MEDIA_BOT_PREFIX . 'is_gif'] = [
    615                     'label' => __("Is GIF"),
     694            $media_type = get_post_meta($post->ID, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'media_type', true);
     695            if (!empty($media_type)) {
     696                $form_fields[self::FOTKI_MEDIA_BOT_PREFIX . 'media_type'] = [
     697                    'label' => __("Media type"),
    616698                    'input' => 'html',
    617                     'html' => '<input type="text" readonly="readonly" value="yes" />'
     699                    'html' => '<input type="text" readonly="readonly" value="' . esc_attr($media_type) . '" />'
    618700                ];
    619701            }
  • fotki-media-bot/trunk/readme.txt

    r1859835 r1861487  
    33Tags: bot, telegram, api, media, archiving, fotki, chat, messenger, gallery, backup
    44Requires at least: 4.0
    5 Tested up to: 4.9.4
    6 Stable tag: 0.5
     5Tested up to: 4.9.5
     6Stable tag: 0.6
    77License: GPLv2 or later
    88
    9 Fotki Media Bot saves your photos from your chats (currently we only support Telegram messenger) to WordPress.
     9Fotki Media Bot saves your media from your chats (currently we only support Telegram messenger) to WordPress.
    1010
    1111== Description ==
    12 Fotki Media Bot saves your photos from your messenger chats or chat groups to WordPress Media Library.
     12Fotki Media Bot saves your media from your messenger chats or chat groups to WordPress Media Library.
    1313Fotki Media Bot currently supports only [Telegram Messenger](https://telegram.org/).
    14 Also it creates daily posts in your Posts and puts your photos there from your photos which you send and receive on Telegram Messenger.
     14Also it creates daily posts in your Posts and puts your media there from your media which you send and receive on Telegram Messenger.
    1515
    1616This plugin is in beta, please report to us any issues, we will try to take care of them.
     
    24243) Send "/setprivacy" command to BotFather using a Telegram and disable privacy (without this step, Fotki Media Bot will not be able to get media from your chats).
    2525
    26 Now, you can send photos to your Telegram Bot, and all photos will be automatically saved into your WordPress. And you can add your Bot to your group chats as well!
     26Now, you can send media to your Telegram Bot, and all media will be automatically saved into your WordPress. And you can add your Bot to your group chats as well!
    2727
    2828== Changelog ==
     29
     30= 0.6 =
     31* fixed minor bugs
     32* added possibility to save or not save some media types from bot, like photos or video
     33* added small features that plugin was more friendly
    2934
    3035= 0.5 =
Note: See TracChangeset for help on using the changeset viewer.