Changeset 336027
- Timestamp:
- 01/23/2011 08:00:53 AM (15 years ago)
- Location:
- wp-dict/trunk
- Files:
-
- 1 added
- 1 edited
-
readme.txt (added)
-
wp-dict.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wp-dict/trunk/wp-dict.php
r335849 r336027 3 3 Plugin Name: WP-Dict 4 4 Plugin URI: http://wordpress.org/extend/plugins/wp-dict/ 5 Description: Widget Plugin. A widget could help to translate.Take advantage of the API from dict.cn. 5 Description: Widget Plugin. A widget could help to translate.Take advantage of the API from dict.cn. 6 6 Version: 1.0.0 7 7 Author: windlx ( Tony Luo ) 8 8 Author URI: http://blog.tech4k.com/about 9 License: MIT 10 */ 11 class WP_Dict_Widget extends WP_Widget { 9 12 10 */ 13 function WP_Dict_Widget() { 14 parent::WP_Widget(false, $name = 'WP Dict'); 15 /* Widget settings. */ 16 $widget_ops = array( 'classname' => 'wp_dict', 17 'description' => 'A widget could help to translate.Take advantage of the API from dict.cn.' ); 18 19 /* Widget control settings. */ 20 $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'wp-dict-widget' ); 21 22 /* Create the widget. */ 23 $this->WP_Widget( 'wp-dict-widget', 'WP Dict Widget', $widget_ops, $control_ops ); 24 25 // Load jQuery 26 wp_enqueue_script('jquery'); 27 } 28 29 function widget( $args, $instance ) { 30 extract( $args ); 31 32 // Get the div id of the widget 33 $widgetid = $args['widget_id']; 34 35 /* User-selected settings. */ 36 $title = apply_filters('widget_title', $instance['title'] ); 37 38 //an option that allows your users to turn on or off AJAX within the widget 39 $useAjax = isset($instance['useAjax']) ? $instance['useAjax'] : false; 40 41 /* Before widget (defined by themes). */ 42 echo $before_widget; 43 44 /* Title of widget (before and after defined by themes). */ 45 if ( $title ) 46 echo $before_title; 47 echo $title; 48 echo '<input type="text" style="margin-left:10px;width:150px" id="wp_dict_query" name="wp_dict_query" />'; 49 echo '<input type="button" style="margin-left:5px;width:30px;text-align:middle" id="wp_dict_submit" name="wp_dict_submit" value="Go"/>'; 50 echo '<div id="wp_dict_result"></div>'; 51 echo $after_title; 52 if ($useAjax) { ?> 53 <script type="text/javascript"> 54 jQuery(document).ready(function($){ 55 $("#wp_dict_submit").click(function(){ 56 $.ajax({ 57 type : "GET", 58 url : "index.php", 59 data : { mywidget_request : "wp_dict_request_handler", 60 query : $("#wp_dict_query").val() }, 61 success : function(response) { 62 // The server has finished executing PHP and has returned something, 63 // so display it! 64 $("#wp_dict_result").html(response); 65 } 66 }); 67 }); 68 }); 69 </script> 70 <?php 71 } 72 73 /* After widget (defined by themes). */ 74 echo $after_widget; 75 } 76 function update( $new_instance, $old_instance ) { 77 $instance = $old_instance; 78 79 /* Strip tags (if needed) and update the widget settings. */ 80 $instance['title'] = strip_tags( $new_instance['title'] ); 81 $instance['useAjax'] = (strip_tags($new_instance['useAjax']) == "Yes" ? true : false); 82 83 return $instance; 84 } 85 function form( $instance ) { 86 87 /* Set up some default widget settings. */ 88 $defaults = array( 'title' => 'Translate', 'useAjax' => true ); 89 $instance = wp_parse_args( (array) $instance, $defaults ); ?> 90 91 <p style="text-align: right;"><label 92 for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> <input 93 id="<?php echo $this->get_field_id( 'title' ); ?>" 94 name="<?php echo $this->get_field_name( 'title' ); ?>" 95 value="<?php echo $instance['title']; ?>" style="width: 200px;" /></p> 96 <p style="text-align: right;"><label 97 for="<?php echo $this->get_field_id('useAjax'); ?>">Use Ajax:</label> <select 98 id="<?php echo $this->get_field_id('useAjax'); ?>" 99 name="<?php echo $this->get_field_name('useAjax'); ?>" 100 style="width: 200px;"> 101 <option 102 <?php if ($instance['useAjax'] == true) echo 'selected="selected"'; ?>>Yes</option> 103 <option 104 <?php if ($instance['useAjax'] == false) echo 'selected="selected"'; ?>>No</option> 105 </select></p> 106 <?php 107 } 108 } 109 function wp_dict_request_handler() { 110 // Check that query have been passed 111 if (isset($_GET['query'])) { 112 $ch = curl_init(); 113 if (defined("CURL_CA_BUNDLE_PATH")) curl_setopt($ch, CURLOPT_CAINFO, CURL_CA_BUNDLE_PATH); 114 curl_setopt($ch, CURLOPT_URL,'http://dict.cn/ws.php?utf8=true&q='.$_GET['query']); 115 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 116 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 117 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 118 ////////////////////////////////////////////////// 119 ///// Set to 1 to verify Douban's SSL Cert ////// 120 ////////////////////////////////////////////////// 121 $response = curl_exec($ch); 122 $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 123 curl_close ($ch); 124 if ($http_status != 200 ) { 125 echo "Query Failed."; 126 } 127 else { 128 echo $response; 129 } 130 exit(); 131 } 132 } 133 134 function wp_dict_load_widget() { 135 register_widget( 'WP_Dict_Widget' ); 136 } 137 /* Add our function to the widgets_init hook. */ 138 add_action('widgets_init', 'wp_dict_load_widget' ); 139 add_action('init', 'wp_dict_request_handler'); 11 140 ?>
Note: See TracChangeset
for help on using the changeset viewer.