admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('assoc-dict-nonce'), 'startword' => $assocdict_startword, 'txt' => array( 'word_not_in_dict' => __( "Words fail me -- you can add a description if you want.", 'assoc-dict' ), 'button_add' => __( "add", 'assoc-dict' ), 'button_cancel' => __( "back", 'assoc-dict' ) ) ); wp_localize_script( 'assocdictjs', 'assoc', $_js ); wp_print_scripts('assocdictjs'); wp_print_styles('assocdictcss'); } /* ajax requests */ // get word and description, wether logged in or not add_action( 'wp_ajax_get_word', 'assocdict_get_word' ); add_action( 'wp_ajax_nopriv_get_word', 'assocdict_get_word' ); function assocdict_get_word() { // replace everything that's not alphanumerical // $_w = ereg_replace( "[^[:alpha:]]", "", $_GET['word'] ); // deprecated $_w = preg_replace( '/[^[:alpha:]]/', "", $_GET['word'] ); $_result = array( 'status' => 404, 'error' => 'Word not found', 'word' => $_w ); if ('' != trim( $_w ) ) { global $wpdb; $woord = $wpdb->get_row("select * from widget_assoc_dict where woord = '" . $_w . "';"); if ( $woord ) { $_result = array( 'status' => 200, 'id' => $woord->woordid, 'word' => $woord->woord, 'desc' => $woord->omschrijving ); } } echo json_encode( $_result ); exit; } // add word with description, wether logged in or not add_action( 'wp_ajax_add_word', 'assocdict_add_word' ); add_action( 'wp_ajax_nopriv_add_word', 'assocdict_add_word' ); function assocdict_add_word() { check_ajax_referer('assoc-dict-nonce', 'nonce'); // replace everything that's not alphanumerical $_w = preg_replace( '/[^[:alpha:]]/', "", $_GET['addword'] ); $_result = array( 'status' => 404, 'error' => 'Word could not be added', 'word' => $_w ); if ('' != trim( $_w ) ) { // already in database? global $wpdb; $woord = $wpdb->get_row("select * from widget_assoc_dict where woord = '" . $_w . "';"); error_log( var_export( $woord, true ) ); if ( !$woord ) { // clean up description // no HTML or scripts in description, and $wpdb will escape it for us $_cleandescription = wp_kses( stripslashes( $_GET['adddescr'] ), array() ); // insert in database $wpdb->insert( 'widget_assoc_dict', array( 'woord' => $_w, 'omschrijving' => $_cleandescription, 'date_time' => date("Y-m-d G:i:s"), 'ip' => '', 'useragent' => '' ), array( '%s', '%s', '%s', '%s', '%s') ); // did we succeed? if (false !== $wpdb->insert_id ) { // yes, return 200 and new id $_result = array( 'status' => 200, 'id' => $wpdb->insert_id, 'word' => $_w, 'desc' => $_cleandescription ); wp_mail( get_option( 'admin_email' ), __( 'mail_subject', 'assoc-dict') . ' (' . $_w . ')', $_w . ":\n\n" . $_cleandescription . "\n\n\n\n" . "(This mail has been generated by the Associative Dictionary for Wordpress)", "From: woorden@dirkie.nu\r\n" . "X-Mailer: PHP/" . phpversion() ); } else { error_log( $wpdb->last_error ); } } } echo json_encode( $_result ); exit; } //////////// // Widget // //////////// function register_assocdict_widget() { register_widget("AssocDict_Widget"); } add_action( 'widgets_init', 'register_assocdict_widget' ); class AssocDict_Widget extends WP_Widget { function __construct() { parent::__construct( 'assocdict', // Base ID __('Associative Dictionary', 'assoc_dict'), // Name array( 'description' => __( 'A widget without use.', 'assoc_dict' ), ) // Args ); } public function widget($args, $instance) { global $wpdb; extract($args); $options = get_option('widget_assoc_dict'); $startword = $instance['start']; # start with random word? if ( "yes" == $instance['random'] ) { $woord = $wpdb->get_row("select woordid as id from widget_assoc_dict order by rand() limit 1;"); if ( $woord ) { $startword = $woord->id; } } echo $before_widget . $before_title . $instance['title'] . $after_title . "\n"; // as this widget gets displayed, we make sure the js and css file gets added in the footer global $assocdict_startword; # do we have a word to start things off with? if ( '' != trim( $startword ) ) { $assocdict_startword = $startword; echo "