Changeset 1857199
- Timestamp:
- 04/12/2018 02:08:42 PM (8 years ago)
- Location:
- fotki-media-bot
- Files:
-
- 6 added
- 2 edited
-
tags/0.3 (added)
-
tags/0.3/class.list-table.php (added)
-
tags/0.3/class.telegram.php (added)
-
tags/0.3/fotki-media-bot.php (added)
-
tags/0.3/index.php (added)
-
tags/0.3/readme.txt (added)
-
trunk/fotki-media-bot.php (modified) (10 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
fotki-media-bot/trunk/fotki-media-bot.php
r1855898 r1857199 4 4 Plugin URI: https://agency.fotki.com/wordpress-plugins/fotki-media-bot/ 5 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. 26 Version: 0.3 7 7 Author: FotkiAgency 8 8 Author URI: https://agency.fotki.com/ … … 21 21 22 22 const FOTKI_MEDIA_BOT_PREFIX = 'fotki_media_bot_'; 23 const FOTKI_MEDIA_BOT_SLUG = 'fotki-media-bot'; 23 24 const FOTKI_MEDIA_BOT_DB_VERSION = '1.0'; 24 25 const FOTKI_MEDIA_BOT_BOTS_TABLE_NAME = 'fotki_media_bots'; … … 106 107 public function fmb_page_links() 107 108 { 109 $menu_slug_prefix = plugin_dir_path(__FILE__); 110 108 111 add_menu_page( 109 __("Fotki Media Bot", 'fotki-media-bot'), 110 __("Fotki Media Bot", 'fotki-media-bot'), 112 __('Fotki Media Bot', self::FOTKI_MEDIA_BOT_SLUG), // page title 113 __('Fotki Media Bot', self::FOTKI_MEDIA_BOT_SLUG), // menu title 114 'manage_options', // capability 115 $menu_slug_prefix, // menu slug 116 [$this, 'fmb_main_page'], // function 117 'dashicons-smiley' // icon url 118 ); 119 120 add_submenu_page( 121 $menu_slug_prefix, // parent slug 122 __('Bots', self::FOTKI_MEDIA_BOT_SLUG), // page title 123 __('Bots', self::FOTKI_MEDIA_BOT_SLUG), // menu title 124 'manage_options', // capability 125 $menu_slug_prefix, // menu slug 126 [$this, 'fmb_main_page'] // function 127 ); 128 129 add_submenu_page( 130 $menu_slug_prefix, 131 __('Log', self::FOTKI_MEDIA_BOT_SLUG), 132 __('Log', self::FOTKI_MEDIA_BOT_SLUG), 111 133 'manage_options', 112 plugin_dir_path(__FILE__), 113 [$this, 'fmb_main_page'], 114 'dashicons-smiley' 134 $menu_slug_prefix.'/bot_logs', 135 [$this, 'fmb_bot_logs'] 115 136 ); 116 137 } … … 145 166 $current_callback_id = get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id'); 146 167 168 if (empty($current_callback_id)) { 169 $this->write_incoming_log('Callback ID in plugin is empty'); 170 return; 171 } 172 147 173 if ($current_callback_id !== $callback_id) { 174 $this->write_incoming_log('Callback ID from param not match. Param ID is '.$callback_id); 148 175 return; 149 176 } 150 177 } else { 178 $this->write_incoming_log('Callback ID is empty'); 151 179 return; 152 180 } 153 181 154 if (!empty($response['message']['photo']) && !empty($bot_token)) { 155 list($photo_string, $photo_data) = FotkiMediaBot_Telegram::get_telegram_photo($bot_token, $response); 156 157 $imageurl = $photo_data['photo_path']; 158 $imageurl = stripslashes($imageurl); 159 160 $uploads = wp_upload_dir(); 161 $post_id = isset($_GET['post_id']) ? sanitize_text_field((int)$_GET['post_id']) : 0; 162 163 $ext = pathinfo(basename($imageurl), PATHINFO_EXTENSION); 164 165 $newfilename = isset($_POST['newfilename']) ? sanitize_text_field($_POST['newfilename']) . '.' . $ext : basename($imageurl); 166 167 $filename = wp_unique_filename($uploads['path'], $newfilename, $unique_filename_callback = null); 168 169 $wp_filetype = wp_check_filetype($filename, null); 170 171 $fullpathfilename = $uploads['path'] . '/' . $filename; 172 173 try { 174 if (!substr_count($wp_filetype['type'], 'image')) { 175 throw new Exception(basename($imageurl) . ' is not a valid image. ' . $wp_filetype['type'] . ''); 176 } 177 178 $fileSaved = file_put_contents($uploads['path'] . '/' . $filename, $photo_string); 179 180 if (!$fileSaved) { 181 throw new Exception("The file cannot be saved."); 182 } 183 184 $attachment = [ 185 'post_mime_type' => $wp_filetype['type'], 186 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 187 'post_content' => !empty($photo_data['caption']) ? $photo_data['caption'] : '', 188 'post_status' => 'inherit', 189 'guid' => $uploads['url'] . '/' . $filename 190 ]; 191 192 $attach_id = wp_insert_attachment($attachment, $fullpathfilename, $post_id); 193 194 if (!$attach_id) { 195 throw new Exception("Failed to save record into database."); 196 } 197 198 if (isset($photo_data['sender_id'])) { 199 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'sender_id', $photo_data['sender_id']); 200 } 201 202 if (isset($photo_data['sender_username'])) { 203 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'sender_username', $photo_data['sender_username']); 204 } 205 206 if (isset($photo_data['chat_id'])) { 207 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'chat_id', $photo_data['chat_id']); 208 } 209 210 if (isset($photo_data['chat_title'])) { 211 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'chat_title', $photo_data['chat_title']); 212 } 213 214 require_once(ABSPATH . 'wp-admin' . '/includes/image.php'); 215 $attach_data = wp_generate_attachment_metadata($attach_id, $fullpathfilename); 216 wp_update_attachment_metadata($attach_id, $attach_data); 217 218 $this->fmb_check_create_post_exists(); 219 220 } catch (Exception $e) { 221 $error = '<div id="message" class="error"><p>' . $e->getMessage() . '</p></div>'; 222 } 223 182 if (empty($bot_token)) { 183 $this->write_incoming_log('Bot token is empty'); 184 return; 185 } 186 187 if (empty($response['message']['photo'])) { 188 $this->write_incoming_log('No photo in message'); 189 return; 190 } 191 192 list($photo_string, $photo_data) = FotkiMediaBot_Telegram::get_telegram_photo($bot_token, $response); 193 194 $imageurl = $photo_data['photo_path']; 195 $imageurl = stripslashes($imageurl); 196 197 $uploads = wp_upload_dir(); 198 $post_id = isset($_GET['post_id']) ? sanitize_text_field((int)$_GET['post_id']) : 0; 199 200 $ext = pathinfo(basename($imageurl), PATHINFO_EXTENSION); 201 202 $newfilename = isset($_POST['newfilename']) ? sanitize_text_field($_POST['newfilename']) . '.' . $ext : basename($imageurl); 203 204 $filename = wp_unique_filename($uploads['path'], $newfilename, $unique_filename_callback = null); 205 206 $wp_filetype = wp_check_filetype($filename, null); 207 208 $fullpathfilename = $uploads['path'] . '/' . $filename; 209 210 try { 211 if (!substr_count($wp_filetype['type'], 'image')) { 212 $this->write_incoming_log(basename($imageurl) . ' is not a valid image. ' . $wp_filetype['type']); 213 throw new Exception(basename($imageurl) . ' is not a valid image. ' . $wp_filetype['type']); 214 } 215 216 $fileSaved = file_put_contents($uploads['path'] . '/' . $filename, $photo_string); 217 218 if (!$fileSaved) { 219 $this->write_incoming_log('The file cannot be saved'); 220 throw new Exception("The file cannot be saved."); 221 } 222 223 $attachment = [ 224 'post_mime_type' => $wp_filetype['type'], 225 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 226 'post_content' => !empty($photo_data['caption']) ? $photo_data['caption'] : '', 227 'post_status' => 'inherit', 228 'guid' => $uploads['url'] . '/' . $filename 229 ]; 230 231 $attach_id = wp_insert_attachment($attachment, $fullpathfilename, $post_id); 232 233 if (!$attach_id) { 234 $this->write_incoming_log('Failed to save record into database'); 235 throw new Exception("Failed to save record into database."); 236 } 237 238 if (isset($photo_data['sender_id'])) { 239 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'sender_id', $photo_data['sender_id']); 240 } 241 242 if (isset($photo_data['sender_username'])) { 243 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'sender_username', $photo_data['sender_username']); 244 } 245 246 if (isset($photo_data['chat_id'])) { 247 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'chat_id', $photo_data['chat_id']); 248 } 249 250 if (isset($photo_data['chat_title'])) { 251 update_post_meta($attach_id, '_' . self::FOTKI_MEDIA_BOT_PREFIX . 'chat_title', $photo_data['chat_title']); 252 } 253 254 require_once(ABSPATH . 'wp-admin' . '/includes/image.php'); 255 $attach_data = wp_generate_attachment_metadata($attach_id, $fullpathfilename); 256 wp_update_attachment_metadata($attach_id, $attach_data); 257 258 $this->fmb_check_create_post_exists(); 259 260 } catch (Exception $e) { 261 $this->write_incoming_log('Save image try failed, exception was called'); 224 262 } 225 263 } … … 236 274 ?> 237 275 <div class="wrap"> 238 <h1> Fotki Media Bot</h1>276 <h1><?php echo get_admin_page_title(); ?></h1> 239 277 <?php 240 278 // echo '<div>Callback ID: ' . get_option(self::FOTKI_MEDIA_BOT_PREFIX . 'callback_id') . '</div>'; 241 279 242 if (isset($_POST['bot_submited']) && $_POST['bot_submited'] === 'Add') {280 if (isset($_POST['bot_submited'])) { 243 281 $bot_name = sanitize_text_field($_POST['bot_name']); 244 282 $bot_service = sanitize_text_field($_POST['bot_service']); … … 402 440 } 403 441 442 public function fmb_feedback() 443 { 444 echo '<div class="wrap">'; 445 echo '<h1>' . get_admin_page_title() . '</h1>'; 446 447 echo '</div>'; 448 } 449 450 public function fmb_bot_logs() 451 { 452 echo '<div class="wrap">'; 453 echo '<h1>' . get_admin_page_title() . '</h1>'; 454 455 $wp_debug_log_file_path = plugin_dir_path(__FILE__).'incoming_data_from_bots.log'; 456 457 if (file_exists($wp_debug_log_file_path)) { 458 $wp_debug_log_file = fopen($wp_debug_log_file_path, 'r'); 459 460 echo '<div class="card" style="max-width: none">'; 461 462 while (!feof($wp_debug_log_file)) { 463 echo esc_attr(fgets($wp_debug_log_file)) . "<br />"; 464 } 465 466 fclose($wp_debug_log_file); 467 468 echo '</div>'; 469 } else { 470 echo "There is no $wp_debug_log_file_path file."; 471 } 472 473 echo '</div>'; 474 } 475 404 476 public function bots_exists() 405 477 { … … 428 500 public function show_bots() 429 501 { 502 ?> 503 504 <p> 505 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. 506 </p> 507 508 <?php 430 509 $bots_list = new FotkiMediaBot_Bots_WP_Table(); 431 510 $bots_list->prepare_items(); … … 439 518 } else { 440 519 echo '<form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">'; 520 //echo '<p>'; 521 //echo 'Name<br />'; 522 echo '<input type="hidden" name="bot_name" pattern="[a-zA-Z0-9 ]+" value="Bot #1" size="40" />'; 523 //echo '</p>'; 441 524 echo '<p>'; 442 echo 'Name<br />'; 443 echo '<input type="text" name="bot_name" pattern="[a-zA-Z0-9 ]+" value="' . (isset($_POST["bot_name"]) ? esc_attr($_POST["bot_name"]) : '') . '" size="40" />'; 444 echo '</p>'; 445 echo '<p>'; 446 echo 'Service<br />'; 447 echo '<input type="text" name="bot_service" value="Telegram" size="40" readonly="readonly" />'; 525 //echo 'Service<br />'; 526 echo '<input type="hidden" name="bot_service" value="Telegram" size="40" readonly="readonly" />'; 448 527 echo '<div style="color: dimgray"> 449 528 - chat with <a href="https://telegram.me/botfather">BotFather</a> on device where you have Telegram installed, and follow a few simple steps to create a new Bot. Copy to your clipboard a "token to access the HTTP API" (will be given to you in red color).<br> … … 456 535 echo '</p>'; 457 536 echo '<p>'; 458 echo '<p><input type="submit" class="button action" name="bot_submited" value="Add bot"/></p>';537 echo '<p><input type="submit" class="button button-primary" name="bot_submited" value="Add bot"/></p>'; 459 538 echo '</form>'; 460 539 } 461 540 } 462 463 541 464 542 public function get_callback_url() … … 610 688 return $form_fields; 611 689 } 690 691 public function write_incoming_log($message) { 692 $wp_date_format = get_option('date_format'); 693 694 $date = ''; 695 696 if ($wp_date_format) { 697 $date = date(get_option('date_format') . ' H:i:s', current_time('timestamp', 0)); 698 } else { 699 $date = date('d.m.Y H:i:s', current_time('timestamp', 0)); 700 } 701 702 $log_file = $wp_debug_log_file_path = plugin_dir_path(__FILE__).'incoming_data_from_bots.log'; 703 704 if ($handle = fopen($log_file, 'a')) { 705 fwrite($handle, '[' . $date . '] ' . $message); 706 fwrite($handle, PHP_EOL); 707 } 708 709 fclose($handle); 710 } 612 711 } 613 712 } -
fotki-media-bot/trunk/readme.txt
r1855898 r1857199 4 4 Requires at least: 4.0 5 5 Tested up to: 4.9.4 6 Stable tag: 0. 26 Stable tag: 0.3 7 7 License: GPLv2 or later 8 8 … … 28 28 == Changelog == 29 29 30 = 0.3 = 31 * fixed minor bugs 32 * added log for bots incoming messages, only for errors 33 * improved instruction how to use the bot 34 30 35 = 0.2 = 31 36 * improved instruction how to setup the bot
Note: See TracChangeset
for help on using the changeset viewer.