Plugin Directory

Changeset 818923


Ignore:
Timestamp:
12/11/2013 02:14:50 PM (12 years ago)
Author:
sydcode
Message:

Added search widget and extra settings.

Location:
pubmed-posts/trunk
Files:
8 added
5 edited

Legend:

Unmodified
Added
Removed
  • pubmed-posts/trunk/admin.css

    r812902 r818923  
    11/**
    2  * Stylesheet for "PubMed Posts" Wordpress plugin
     2 * Admin stylesheet for "PubMed Posts" Wordpress plugin
    33 */
    44 
     
    3232#pubmed-dashboard .pubmed-buttons {
    3333    margin: 10px 0 0 0;
    34     overflow: hidden;
    3534}
    3635
  • pubmed-posts/trunk/class-article.php

    r812902 r818923  
    1010}
    1111
    12 class PubMedArticle {
     12class PubMedPostsArticle {
    1313
    1414    const PUBMED_URL = 'http://www.ncbi.nlm.nih.gov/pubmed/';
     
    317317        $year = $this->getJournalYear();
    318318        $month = $this->getJournalMonth();
    319         $date = $this->getJournalDate();
     319        $medline = $this->getJournalMedlineDate();
    320320        $volume = $this->getJournalVolume();
    321321        $issue = $this->getJournalIssue();
     
    328328            }
    329329        } else {
    330             if (!empty($date)) {
    331                 $citation .= ' ' . $date;
     330            if (!empty($medline)) {
     331                $citation .= ' ' . $medline;
    332332            }
    333333        }
     
    347347     */
    348348    public function getJournalDate() { 
     349        $date = '';
    349350        $year = $this->getJournalYear();
    350351        $month = $this->getJournalMonth();
    351         $day = $this->getJournalDay();
    352         if (!empty($year) && !empty($month) && !empty($day)) {
     352        $day = $this->getJournalDay();         
     353        if (empty($year)) {
     354            // Try to parse MEDLINE date
     355            $medline_date = $this->getJournalMedlineDate();
     356            if (!empty($medline_date)) {
     357                $components = $this->getMedlineDateComponents($medline_date);
     358                $year = empty($components['year']) ? '' : $components['year'];
     359                $month = empty($components['month']) ? '' : $components['month'];
     360                $day = empty($components['day']) ? '' : $components['day'];
     361            }
     362        }
     363        if (!empty($year)) {
     364            $month = empty($month) ? '1' : $month;
     365            $day = empty($day) ? '1' : $day;       
    353366            $time = strtotime($year . '-' . $month . '-' . $day);
    354             $date = date('Y-m-d', $time);
    355         } else {
    356             // Try Medline date
    357             $date = $this->getJournalMedlineDate();
     367            if ($time) {
     368                $date = date('Y-m-d', $time);
     369            }       
    358370        }
    359371        return $date;
     
    385397   
    386398    /**
    387      * Get journal Medline date
     399     * Get journal MEDLINE date
    388400     * @return string
    389401     */
     
    422434    public function getJournalYear() { 
    423435        return (string) $this->xml->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year;
     436    }   
     437   
     438    /**
     439     * Get MEDLINE date components
     440     *
     441     * MEDLINE is compiled by the United States National Library of Medicine.
     442     * It is assumed that seasons are for the northern hemishpere.
     443     * Only gets the first date of a range. Typos are converted as follows:
     444     * "2rd Semest" to "2nd Semest"
     445     * "4th Trimest" to "4th Quart"     
     446     *
     447     * @param string $date
     448     * @return string
     449     */
     450    public function getMedlineDateComponents($date) {
     451        $year = $month = $day = '';
     452        $ordinals = array('1st', '2nd', '2rd', '2d', '3rd', '3rd', '3d', '4th');       
     453        $pattern = '/^(\d{4})([ \-])?([A-Za-z0-9]+)?([ \-])?([A-Za-z0-9]+)?([ \-])?([A-Za-z0-9]+)?([ \-])?([A-Za-z0-9]+)?([ \-])?([A-Za-z0-9]+)?([ \-])?/';
     454        preg_match($pattern, $date, $matches);
     455        // Get year
     456        $year = empty($matches[1]) ? '' : $matches[1];
     457        // Get month and day
     458        $separator = empty($matches[2]) ? '' : $matches[2];
     459        if (' ' == $separator) {
     460            $str1 = empty($matches[3]) ? '' : $matches[3];
     461            $separator = empty($matches[4]) ? '' : $matches[4];
     462            // Get academic term or day
     463            if ($str1 && ' ' == $separator) {
     464                $str2 = empty($matches[5]) ? '' : $matches[5];
     465                if ($str2) {
     466                    if (in_array($str1, $ordinals)) {
     467                        $str1 .= ' ' . $str2;
     468                    } else {
     469                        $day = $str2;
     470                    }
     471                }
     472            }
     473            $month = $this->getMedlineDateMonth($str1);
     474        }
     475        // Output date
     476        $month = empty($month) ? '1' : $month;
     477        $day = empty($day) ? '1' : $day;           
     478        $time = strtotime($year . '-' . $month . '-' . $day);
     479        if ($time) {
     480            return array(
     481                'year' => $year,
     482                'month' => $month,
     483                'day' => $day,
     484            );
     485        } else {
     486            return array();
     487        }
     488    }
     489   
     490    /**
     491     * Get month from Medline date
     492     * @param $str string
     493     * @result string
     494     */
     495    public function getMedlineDateMonth($str) {
     496        $str = strtolower($str);
     497        switch ($str) {
     498            case '1st semester' :
     499            case '1st semest' :
     500            case '1st trimest' :
     501            case '1st quart' :
     502                return '01';
     503           
     504            case 'spring' :
     505                return '03';
     506           
     507            case '2nd quart' :
     508            case '2d quart' :
     509                return '04';
     510           
     511            case '2nd trimest' :
     512            case '2d trimest' :
     513                return '05';
     514           
     515            case 'summer' :
     516                return '06';
     517           
     518            case '2nd semester' :
     519            case '2d semester' :
     520            case '2nd semest' :
     521            case '2rd semest' : // typo found
     522            case '2d semest' :
     523            case '3rd quart' :
     524            case '3d quart' :
     525                return '07';
     526           
     527            case 'autumn' :
     528            case '3rd trimest' :
     529            case '3d trimest' :
     530                return '09';
     531           
     532            case '4th quart' :
     533            case '4th trimest' : // typo found
     534                return '10';
     535           
     536            case 'winter' :
     537            case 'christmas' :
     538                return '12';
     539           
     540            default :
     541                $date = date_parse($str);
     542                return (string) $date['month'];
     543        }
    424544    }   
    425545   
  • pubmed-posts/trunk/languages/pubmed-posts.pot

    r812902 r818923  
    22msgstr ""
    33"Project-Id-Version: PubMed\n"
    4 "POT-Creation-Date: 2013-12-01 01:58+1000\n"
    5 "PO-Revision-Date: 2013-12-01 01:58+1000\n"
     4"POT-Creation-Date: 2013-12-11 13:42+1000\n"
     5"PO-Revision-Date: 2013-12-11 13:42+1000\n"
    66"Last-Translator: \n"
    77"Language-Team: \n"
     
    1414"X-Poedit-SearchPath-0: ..\n"
    1515
    16 #: ../pubmed-posts.php:155 ../template-dashboard.php:15
     16#: ../class-search.php:14
     17msgid "A search form for PubMed posts"
     18msgstr ""
     19
     20#: ../class-search.php:15
     21msgid "PubMed Posts Search"
     22msgstr ""
     23
     24#: ../class-search.php:79
     25msgid "Article Abstract"
     26msgstr ""
     27
     28#: ../class-search.php:83
     29msgid "Article Affiliation"
     30msgstr ""
     31
     32#: ../class-search.php:87
     33msgid "Article Authors"
     34msgstr ""
     35
     36#: ../class-search.php:91
     37msgid "Article Title"
     38msgstr ""
     39
     40#: ../class-search.php:95
     41msgid "Journal Title"
     42msgstr ""
     43
     44#: ../class-search.php:99
     45msgid "Journal Volume"
     46msgstr ""
     47
     48#: ../class-search.php:103
     49msgid "Journal Year"
     50msgstr ""
     51
     52#: ../class-search.php:107
     53msgid "PMID"
     54msgstr ""
     55
     56#: ../class-search.php:112 ../class-search.php:117 ../class-search.php:122
     57msgid "Tags"
     58msgstr ""
     59
     60#: ../class-search.php:113
     61msgid "OR"
     62msgstr ""
     63
     64#: ../class-search.php:118
     65msgid "AND"
     66msgstr ""
     67
     68#: ../class-search.php:123
     69msgid "NOT"
     70msgstr ""
     71
     72#: ../class-search.php:130
     73msgid "Advanced"
     74msgstr ""
     75
     76#: ../class-search.php:131
     77msgid "Hide"
     78msgstr ""
     79
     80#: ../class-search.php:134 ../template-dashboard.php:27
     81msgid "Reset"
     82msgstr ""
     83
     84#: ../class-search.php:136
     85msgid "Search"
     86msgstr ""
     87
     88#: ../class-search.php:151
     89msgid "Title"
     90msgstr ""
     91
     92#: ../class-search.php:157
     93msgid "Show advanced search"
     94msgstr ""
     95
     96#: ../class-search.php:162
     97msgid "Hide reset button"
     98msgstr ""
     99
     100#: ../pubmed-posts.php:138 ../pubmed-posts.php:216
     101msgid "Tags (press enter to select)"
     102msgstr ""
     103
     104#: ../pubmed-posts.php:213 ../template-dashboard.php:15
    17105msgid "PMIDs (separate with commas)"
    18106msgstr ""
    19107
    20 #: ../pubmed-posts.php:156
     108#: ../pubmed-posts.php:214
    21109msgid "Categories (# of # selected)"
    22110msgstr ""
    23111
    24 #: ../pubmed-posts.php:157
     112#: ../pubmed-posts.php:215
    25113msgid "Categories"
    26114msgstr ""
    27115
    28 #: ../pubmed-posts.php:158
    29 msgid "Tags (press enter to select)"
    30 msgstr ""
    31 
    32 #: ../pubmed-posts.php:167 ../pubmed-posts.php:428
     116#: ../pubmed-posts.php:260 ../pubmed-posts.php:666
    33117msgid "PubMed Posts"
    34118msgstr ""
    35119
    36 #: ../pubmed-posts.php:195
     120#: ../pubmed-posts.php:298
    37121msgid "Security check failed"
    38122msgstr ""
    39123
    40 #: ../pubmed-posts.php:215
     124#: ../pubmed-posts.php:318
     125msgid "Invalid identifier"
     126msgstr ""
     127
     128#: ../pubmed-posts.php:420
    41129msgid "Post already exists"
    42130msgstr ""
    43131
    44 #: ../pubmed-posts.php:220
    45 msgid "Invalid identifier"
    46 msgstr ""
    47 
    48 #: ../pubmed-posts.php:242
     132#: ../pubmed-posts.php:427
    49133msgid "Article not found"
    50134msgstr ""
    51135
    52 #: ../pubmed-posts.php:296
     136#: ../pubmed-posts.php:480
     137msgid "Failed to update post"
     138msgstr ""
     139
     140#: ../pubmed-posts.php:497
    53141msgid "View post"
    54142msgstr ""
    55143
    56 #: ../pubmed-posts.php:297
     144#: ../pubmed-posts.php:498
    57145msgid "Edit post"
    58146msgstr ""
    59147
    60 #: ../pubmed-posts.php:299
    61 msgid "Post created"
    62 msgstr ""
    63 
    64 #: ../pubmed-posts.php:301
     148#: ../pubmed-posts.php:501
     149msgid "Post draft created"
     150msgstr ""
     151
     152#: ../pubmed-posts.php:503
     153msgid "Post draft updated"
     154msgstr ""
     155
     156#: ../pubmed-posts.php:507
    65157msgid "Post published"
    66158msgstr ""
    67159
    68 #: ../pubmed-posts.php:427 ../template-settings.php:14
     160#: ../pubmed-posts.php:509
     161msgid "Post updated"
     162msgstr ""
     163
     164#: ../pubmed-posts.php:665 ../template-settings.php:14
    69165msgid "PubMed Posts Settings"
    70166msgstr ""
    71167
    72 #: ../pubmed-posts.php:441
     168#: ../pubmed-posts.php:679
    73169msgid "You do not have sufficient permissions to access this page"
    74170msgstr ""
    75171
    76 #: ../pubmed-posts.php:453
     172#: ../pubmed-posts.php:691
     173msgid "Update Post"
     174msgstr ""
     175
     176#: ../pubmed-posts.php:698
     177msgid "Replace Taxonomy"
     178msgstr ""
     179
     180#: ../pubmed-posts.php:705
    77181msgid "Post Author"
    78182msgstr ""
    79183
    80 #: ../pubmed-posts.php:460
     184#: ../pubmed-posts.php:712
    81185msgid "Post Date"
    82186msgstr ""
    83187
    84 #: ../pubmed-posts.php:467
     188#: ../pubmed-posts.php:719
    85189msgid "Post Template"
    86190msgstr ""
    87191
    88 #: ../pubmed-posts.php:483
    89 msgid "Check to show article authors"
    90 msgstr ""
    91 
    92 #: ../pubmed-posts.php:484
     192#: ../pubmed-posts.php:735
     193msgid "Update existing post"
     194msgstr ""
     195
     196#: ../pubmed-posts.php:736
     197msgid "Content and custom fields will be updated if post exists"
     198msgstr ""
     199
     200#: ../pubmed-posts.php:749
     201msgid "Replace post taxonomy"
     202msgstr ""
     203
     204#: ../pubmed-posts.php:750
     205msgid "New categories and tags will replace existing ones for post"
     206msgstr ""
     207
     208#: ../pubmed-posts.php:763
     209msgid "Show article authors"
     210msgstr ""
     211
     212#: ../pubmed-posts.php:764
    93213msgid "All posts will show the article authors instead of post author"
    94214msgstr ""
    95215
    96 #: ../pubmed-posts.php:497
    97 msgid "Check to show article dates"
    98 msgstr ""
    99 
    100 #: ../pubmed-posts.php:498
    101 msgid "New posts will be backdated to the article date"
    102 msgstr ""
    103 
    104 #: ../pubmed-posts.php:511
     216#: ../pubmed-posts.php:777
     217msgid "Today"
     218msgstr ""
     219
     220#: ../pubmed-posts.php:778
     221msgid "Article Published"
     222msgstr ""
     223
     224#: ../pubmed-posts.php:779
     225msgid "Journal Published"
     226msgstr ""
     227
     228#: ../pubmed-posts.php:780
     229msgid "PubMed Record Created"
     230msgstr ""
     231
     232#: ../pubmed-posts.php:781
     233msgid "PubMed Record Completed"
     234msgstr ""
     235
     236#: ../pubmed-posts.php:782
     237msgid "PubMed Record Revised"
     238msgstr ""
     239
     240#: ../pubmed-posts.php:784
     241msgid "New posts will be set to this date"
     242msgstr ""
     243
     244#: ../pubmed-posts.php:797
    105245msgid "Create a template for new posts using HTML and the following tags"
    106246msgstr ""
     
    110250msgstr ""
    111251
    112 #: ../template-dashboard.php:27
    113 msgid "Reset"
    114 msgstr ""
    115 
    116252#: ../template-dashboard.php:28
    117253msgid "Publish"
  • pubmed-posts/trunk/pubmed-posts.php

    r812902 r818923  
    33Plugin Name: PubMed Posts
    44Plugin URI: http://wordpress.org/plugins/pubmed-posts/
    5 Description: This plugin adds a dashboard widget that creates posts from <a href='http://www.ncbi.nlm.nih.gov/pubmed/'>PubMed</a> articles.
    6 Version: 1.0.0
     5Description: This plugin adds a dashboard widget that creates posts from <a href='http://www.ncbi.nlm.nih.gov/pubmed/'>PubMed</a> articles, plus a search widget that finds posts with specific article data.
     6Version: 1.1.0
    77Author: sydcode
    88Author URI: http://profiles.wordpress.org/sydcode
     
    2525
    2626// Load PubMed article class
    27 if ( !class_exists('PubMedArticle') ) {
     27if ( !class_exists('PubMedPostsArticle') ) {
    2828    require(dirname(__FILE__)  . '/class-article.php');
     29}
     30
     31// Load search widget class
     32if ( !class_exists('PubMedPostsSearch') ) {
     33    require(dirname(__FILE__)  . '/class-search.php');
    2934}
    3035
     
    3843    protected static $instance = null;
    3944    const PLUGIN_SLUG = 'pubmed-posts';
    40     const PLUGIN_VERSION = '1.0.0';
     45    const PLUGIN_VERSION = '1.1.0';
    4146    const DEFAULT_TEMPLATE = <<<HTML
    4247<p>[article_authors]</p>
    4348<p>[journal_citation]</p>
    44 <p>PMID: [pmid]</p>
     49<p>PMID: [pmid_link]</a></p>
    4550<h2>Abstract</h2>
    4651<p>[article_abstract]</p>
     
    6772        // Actions
    6873        add_action('admin_init', array($this, 'admin_init'));
     74        add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
    6975        add_action('admin_menu', array($this, 'settings_menu'));       
    70         add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
     76        add_action('pre_get_posts', array($this, 'search_posts'), 99);
     77        add_action('widgets_init', array($this, 'register_search_widget'));
    7178        add_action('wp_dashboard_setup', array($this, 'dashboard_add_widget'));
    72         add_action('wp_ajax_pubmed-posts', array($this, 'ajax_callback'));
     79        add_action('wp_ajax_pubmed-posts', array($this, 'posts_callback'));
     80        add_action('wp_enqueue_scripts', array($this, 'frontend_scripts'));
     81       
    7382        // Filters
     83        add_filter('author_link', array($this, 'author_link'), 10, 3);
     84        add_filter('get_the_author_display_name', array($this, 'show_authors'));
     85        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
    7486        add_filter('the_author', array($this, 'show_authors'));
    75         add_filter('get_the_author_display_name', array($this, 'show_authors'));
    76         add_filter('author_link', array($this, 'author_link'), 10, 3);
    77         add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
    7887    }   
    79 
    80     /**
    81      * Register settings, stylesheet and script
     88   
     89    /**
     90     * Register settings
    8291     */
    8392    public function admin_init() {
    8493        register_setting('pmp_settings_group', 'pmp_settings');
    8594        add_settings_section('pmp_settings_section', '', array($this, 'settings_section'), 'pmp_settings_page');
     95    }
     96   
     97    /**
     98     * Load front script and stylesheet
     99     */
     100    public function frontend_scripts() {
     101        // jQuery TextExt plugin
     102        $this->load_textext_plugin();
     103       
     104        // PubMed Posts stylesheet
     105        $deps = array(
     106            'textext-core',
     107            'textext-tags',
     108            'textext-arrow',
     109            'textext-prompt',
     110            'textext-autocomplete',
     111        );     
     112        $url = plugins_url('style.css', __FILE__);
     113        wp_enqueue_style('pubmed-front', $url, $deps, self::PLUGIN_VERSION);   
     114       
     115        // PubMed posts script
     116        $deps = array(
     117            'jquery',
     118            'textext-core',
     119            'textext-tags',
     120            'textext-arrow',
     121            'textext-prompt',
     122            'textext-autocomplete',
     123        );     
     124        $url = plugins_url('script.js', __FILE__);
     125        wp_enqueue_script('pubmed-front', $url, $deps, self::PLUGIN_VERSION);   
     126       
     127        // Get current tags for autocomplete
     128        $args = array(
     129            'hide_empty' => false,
     130            'fields' => 'names'
     131        );
     132        $tags = get_tags($args);
     133        $tags = json_encode($tags);
     134       
     135        // Output data
     136        $variables = array(
     137            'tags' => $tags,
     138            'tagsText' => __('Tags (press enter to select)', 'pubmed-posts')           
     139        );
     140        wp_localize_script('pubmed-front', 'pubMedPosts', $variables);     
     141    }       
     142
     143    /**
     144     * Load admin scripts and stylesheets
     145     */
     146    public function admin_scripts() {
    86147        // jQuery MultiSelect plugin
    87         wp_enqueue_style('jquery-smoothness', plugins_url('smoothness/jquery-ui-1.10.3.custom.min.css', __FILE__), array(), '1.10.3'); 
    88         wp_enqueue_style('jquery-multiselect', plugins_url('multiselect/jquery.multiselect.css', __FILE__), array(), '1.13');   
    89         wp_enqueue_script('jquery-multiselect', plugins_url('multiselect/jquery.multiselect.min.js', __FILE__), array('jquery', 'jquery-ui-core', 'jquery-ui-widget'), '1.13');     
     148        $url = plugins_url('smoothness/jquery-ui-1.10.3.custom.min.css', __FILE__);
     149        wp_enqueue_style('jquery-smoothness', $url, array(), '1.10.3');
     150       
     151        $url = plugins_url('multiselect/jquery.multiselect.css', __FILE__);
     152        wp_enqueue_style('jquery-multiselect', $url, array(), '1.13'); 
     153       
     154        $url = plugins_url('multiselect/jquery.multiselect.min.js', __FILE__);
     155        wp_enqueue_script('jquery-multiselect', $url, array('jquery', 'jquery-ui-core', 'jquery-ui-widget'), '1.13');       
     156       
    90157        // jQuery TextExt plugin
    91         wp_register_style('textext-core', plugins_url('textext/css/textext.core.css', __FILE__), array(), '1.3.1');
    92         wp_register_style('textext-tags', plugins_url('textext/css/textext.plugin.tags.css', __FILE__), array(), '1.3.1');
    93         wp_register_style('textext-arrow', plugins_url('textext/css/textext.plugin.arrow.css', __FILE__), array(), '1.3.1');
    94         wp_register_style('textext-prompt', plugins_url('textext/css/textext.plugin.prompt.css', __FILE__), array(), '1.3.1');
    95         wp_register_style('textext-autocomplete', plugins_url('textext/css/textext.plugin.autocomplete.css', __FILE__), array(), '1.3.1');
    96         wp_register_script('textext-core', plugins_url('textext/js/textext.core.js', __FILE__), array('jquery'), '1.3.1'); 
    97         wp_register_script('textext-tags', plugins_url('textext/js/textext.plugin.tags.js', __FILE__), array('jquery'), '1.3.1');   
    98         wp_register_script('textext-arrow', plugins_url('textext/js/textext.plugin.arrow.js', __FILE__), array('jquery'), '1.3.1');
    99         wp_register_script('textext-prompt', plugins_url('textext/js/textext.plugin.prompt.js', __FILE__), array('jquery'), '1.3.1');   
    100         wp_register_script('textext-autocomplete', plugins_url('textext/js/textext.plugin.autocomplete.js', __FILE__), array('jquery'), '1.3.1');   
    101         // PubMed Posts
     158        $this->load_textext_plugin();
     159       
     160        // PubMed Posts stylesheet
    102161        $deps = array(
    103162            'jquery-smoothness',
     
    107166            'textext-arrow',
    108167            'textext-prompt',
    109             'textext-autocomplete'
     168            'textext-autocomplete',
    110169        );
    111         wp_register_style('pubmed-admin', plugins_url('admin.css', __FILE__),$deps, self::PLUGIN_VERSION);
     170        $url = plugins_url('admin.css', __FILE__);
     171        wp_enqueue_style('pubmed-admin', $url, $deps, self::PLUGIN_VERSION);
     172       
     173        // PubMed Posts script
    112174        $deps = array(
    113175            'jquery',
     
    117179            'textext-arrow',
    118180            'textext-prompt',
    119             'textext-autocomplete'
     181            'textext-autocomplete',
    120182        );
    121         wp_register_script('pubmed-admin', plugins_url('admin.js', __FILE__), $deps, self::PLUGIN_VERSION);     
    122     }
    123 
    124     /**
    125      * Enqueue script and stylesheet
    126      */
    127     public function admin_scripts() {
    128         wp_enqueue_style('pubmed-admin');
    129         wp_enqueue_script('pubmed-admin');
     183        $url = plugins_url('admin.js', __FILE__);
     184        wp_enqueue_script('pubmed-admin', $url, $deps, self::PLUGIN_VERSION);       
     185   
    130186        // Get categories for autocomplete list
    131187        $categories = array();
     
    142198        }
    143199        $categories = json_encode($categories);
     200       
    144201        // Get current tags for autocomplete
    145202        $args = array(
     
    149206        $tags = get_tags($args);
    150207        $tags = json_encode($tags);
     208       
    151209        // Output data
    152210        $variables = array(
     
    160218        wp_localize_script('pubmed-admin', 'pubMedPosts', $variables);
    161219    }
     220   
     221    /**
     222     * Load stylesheets and scripts for jQuery TextExt plugin
     223     */
     224    public function load_textext_plugin() {
     225        $url = plugins_url('textext/css/textext.core.css', __FILE__);
     226        wp_enqueue_style('textext-core', $url, array(), '1.3.1');
     227       
     228        $url = plugins_url('textext/css/textext.plugin.tags.css', __FILE__);
     229        wp_enqueue_style('textext-tags', $url, array(), '1.3.1');
     230       
     231        $url = plugins_url('textext/css/textext.plugin.arrow.css', __FILE__);
     232        wp_enqueue_style('textext-arrow', $url, array(), '1.3.1');
     233       
     234        $url = plugins_url('textext/css/textext.plugin.prompt.css', __FILE__);
     235        wp_enqueue_style('textext-prompt', $url, array(), '1.3.1');
     236       
     237        $url = plugins_url('textext/css/textext.plugin.autocomplete.css', __FILE__);
     238        wp_enqueue_style('textext-autocomplete', $url, array(), '1.3.1');
     239       
     240        $url = plugins_url('textext/js/textext.core.js', __FILE__);
     241        wp_enqueue_script('textext-core', $url, array('jquery'), '1.3.1'); 
     242       
     243        $url = plugins_url('textext/js/textext.plugin.tags.js', __FILE__);
     244        wp_enqueue_script('textext-tags', $url, array('jquery'), '1.3.1'); 
     245       
     246        $url = plugins_url('textext/js/textext.plugin.arrow.js', __FILE__);
     247        wp_enqueue_script('textext-arrow', $url, array('jquery'), '1.3.1');
     248       
     249        $url = plugins_url('textext/js/textext.plugin.prompt.js', __FILE__);
     250        wp_enqueue_script('textext-prompt', $url, array('jquery'), '1.3.1');   
     251       
     252        $url = plugins_url('textext/js/textext.plugin.autocomplete.js', __FILE__);
     253        wp_enqueue_script('textext-autocomplete', $url, array('jquery'), '1.3.1'); 
     254    }   
    162255
    163256    /**
     
    185278        ob_end_flush();
    186279    }
    187 
    188     /**
    189      * Handle AJAX callback
     280   
     281    /**
     282     * Create markup for dashboard widget
    190283     * @return string
    191284     */
    192     public function ajax_callback() {
     285    public function register_search_widget() {
     286        if (class_exists('PubMedPostsSearch')) {
     287            register_widget('PubMedPostsSearch');
     288        }
     289    }   
     290   
     291    /**
     292     * Handle AJAX callback for posts
     293     * @return string
     294     */
     295    public function posts_callback() {
    193296        // Security check
    194297        if (!wp_verify_nonce($_POST['nonce'], 'pubmed-submit')) {
     
    211314                $id = trim($id);
    212315                if (is_numeric($id)) {
    213                     $exists = $this->article_exists($id);
    214                     if ($exists) {
    215                         $messages[] = '<p>' . $id . ' - ' . __('Post already exists', 'pubmed-posts') . '.</p>';
    216                     } else {
    217                         $messages[] = '<p>' . $this->create_post($id, $status, $categories, $tags) . '</p>';
    218                     }
     316                    $messages[] = '<p>' . $this->create_post($id, $status, $categories, $tags) . '</p>';
    219317                } else {
    220318                    $messages[] = '<p>' . $id . ' - ' . __('Invalid identifier', 'pubmed-posts') . '.</p>';
     
    227325        die();
    228326    }   
    229 
     327   
     328    /**
     329     * Search for PubMed posts
     330     * @param object $query
     331     * @return object
     332     */
     333    public function search_posts($query) { 
     334        // Check for PubMed search
     335        if (!isset($_POST['pubmed-search'])) {
     336            return $query;
     337        }
     338        if ('simple' == $_POST['pubmed-search']) {
     339            // Simple Search
     340            if (!empty($_POST['pubmed-keyword'])) {
     341                $query->set('s', $_POST['pubmed-keyword']);
     342            }
     343        } else {
     344            // Advanced search
     345           
     346            // Meta query
     347            $meta_query = array();
     348            $meta_fields = array(
     349                'pubmed-pmid' => 'PMID',
     350                'article-abstract' => 'Article Abstract',
     351                'article-affiliation' => 'Article Affiliation',
     352                'article-authors' => 'Article Authors',             
     353                'article-title' => 'Article Title',
     354                'journal-title' => 'Journal Title',
     355                'journal-volume' => 'Journal Volume',
     356                'journal-year' => 'Journal Year',
     357            );     
     358            $like_fields = array(
     359                'article-abstract',
     360                'article-affiliation',
     361                'article-authors',
     362                'article-title',
     363                'journal-title',
     364            ); 
     365                foreach ($meta_fields as $name => $key) {
     366                if (!empty($_POST[$name])) {
     367                    $element = array(
     368                        'key' => $key,
     369                        'value' => $_POST[$name],
     370                    );
     371                    if (in_array($name, $like_fields)) {
     372                        $element['compare'] = 'LIKE';
     373                    }
     374                    $meta_query[] = $element;
     375                }
     376            }   
     377            $query->set('meta_query', $meta_query);
     378           
     379            // Tags query
     380            $tax_query = array();
     381            $tax_fields = array(
     382                'post-tags-or' => 'IN',
     383                'post-tags-and' => 'AND',
     384                'post-tags-not' => 'NOT IN',
     385            );         
     386            foreach ($tax_fields as $name => $operator) {
     387                if (isset($_POST[$name])) {
     388                    $tags = json_decode(stripslashes($_POST[$name]));
     389                    if (!empty($tags)) {
     390                        $tags = array_map('trim', $tags);
     391                        $element = array(
     392                            'taxonomy' => 'post_tag',
     393                            'field' => 'slug',
     394                            'terms' => $tags,           
     395                            'operator' => $operator,
     396                        );
     397                        $tax_query[] = $element;
     398                    }
     399                }
     400            }
     401            $query->set('tax_query', $tax_query);
     402        }
     403        return $query;
     404    }
     405   
    230406    /**
    231407     * Create post from PubMed article
     
    236412     */
    237413    public function create_post($pmid, $status, $categories, $tags) {
     414        // Check if post must be updated
     415        $settings = get_option('pmp_settings');
     416        $post_update = empty($settings['update']) ? false : true;
     417        $post_ID = $this->get_article_post($pmid);
     418        if (!$post_update && $post_ID) {
     419            // Exit if update is off and post exists
     420            return $pmid . ' - ' . __('Post already exists', 'pubmed-posts') . '.';
     421        }
     422
    238423        // Get PubMed article
    239424        try {
    240             $article = new PubMedArticle($pmid);
     425            $article = new PubMedPostsArticle($pmid);
    241426        } catch (Exception $e) {
    242427            return $pmid . ' - ' . __('Article not found', 'pubmed-posts') . '.';
     
    259444       
    260445        // Build post content by parsing template tags
    261         $settings = get_option('pmp_settings');
    262446        $content = empty($settings['template']) ? self::DEFAULT_TEMPLATE : $settings['template'];
    263447        foreach ($data as $key => $value) {
     
    270454            $content = str_replace($key, $value, $content);
    271455        }
    272 
    273         // Create post (change date if required)
     456        if ( empty($data['pmid']) || empty($data['article_url']) ) {
     457            $content = str_replace('[pmid_link]', '', $content);
     458        } else {
     459            $pmid_link = sprintf("<a href='%s' title=''>%s</a>", $data['article_url'], $data['pmid']);
     460            $content = str_replace('[pmid_link]', $pmid_link, $content);
     461        }
     462       
     463        // Create post data
    274464        $post_data = array(
    275465            'post_title' => $data['article_title'],
     
    277467            'post_status' => $status
    278468        );
    279         if (!empty($settings['dates'])) {   
    280             if (!empty($data['article_date'])) {
    281                 $post_data['post_date'] = $data['article_date'];
    282             } else if (!empty($data['date_created'])) {
    283                 $post_data['post_date'] = $data['date_created'];
    284             }
    285         }       
    286         $post_ID = wp_insert_post($post_data);
    287        
     469        $date = $this->post_date($data);       
     470        if (!empty($date)) {   
     471            $post_data['post_date'] = $date;
     472        }
     473       
     474        // Update post or create new post
     475        $append_taxonomy = empty($settings['replace']) ? true : false;             
     476        if ($post_ID) {
     477            $new_post = false;
     478            $post_data['ID'] = $post_ID;
     479            $post_ID = wp_update_post($post_data);
     480            if (empty($post_ID)) {
     481                return $pmid . ' - ' . __('Failed to update post', 'pubmed-posts') . '.';
     482            }
     483            if ($append_taxonomy) {
     484                // Append current categories
     485                $post_categories = wp_get_post_categories($post_ID);
     486                $categories = array_unique( array_merge($categories, $post_categories) );
     487            }
     488        } else {
     489            $new_post = true;
     490            $post_ID = wp_insert_post($post_data);
     491        }
     492
    288493        // Set metadata, categories and tags
    289494        $this->save_metadata($post_ID, $data);
    290495        wp_set_post_categories($post_ID, $categories);
    291         wp_set_post_tags($post_ID, $tags, false);
     496        wp_set_post_tags($post_ID, $tags, $append_taxonomy);
    292497       
    293498        // Create status messsage
     
    297502        $post_links .= "<a href='" . $post_edit_url . "'>" . __('Edit post', 'pubmed-posts') . "</a>";
    298503        if ($status == 'draft') {
    299             $message = $pmid . ' - ' . __('Post created', 'pubmed-posts') . '. ' . $post_links;
     504            if ($new_post) {
     505                $message = __('Post draft created', 'pubmed-posts');
     506            } else {
     507                $message = __('Post draft updated', 'pubmed-posts');
     508            }
    300509        } else {
    301             $message = $pmid . ' - ' . __('Post published', 'pubmed-posts') . '. ' . $post_links;
    302         }
    303         return $message;
     510            if ($new_post) {
     511                $message = __('Post published', 'pubmed-posts');
     512            } else {
     513                $message = __('Post updated', 'pubmed-posts');
     514            }
     515        }
     516        return $pmid . ' - ' . $message  . '. ' . $post_links;
     517    }
     518   
     519    /**
     520     * Get date to use for post date
     521     * @param array $data
     522     * @return string
     523     */
     524    public function post_date($data) {
     525        $settings = get_option('pmp_settings');
     526        $selected = empty($settings['dates']) ? '' : $settings['dates'];
     527        if ('1' == $selected) {
     528            // Support for old date setting
     529            $selected = 'article_date';
     530        }
     531        $date = empty($data[$selected]) ? '' : $data[$selected];
     532        if (!empty($date)) {
     533            // Validate date
     534            $time = strtotime($date);
     535            if ($time === false) {
     536                return '';
     537            }
     538        }
     539        return $date;
    304540    }
    305541   
     
    310546     */
    311547    public function save_metadata($post_ID, $data) {   
     548        delete_post_meta($post_ID, 'Article Abstract');
    312549        foreach ( (array) $data['article_abstract'] as $abstract) {
    313550            add_post_meta($post_ID, 'Article Abstract', $abstract, false);
    314551        }   
    315         add_post_meta($post_ID, 'Article Affiliation', $data['article_affiliation'], true);     
    316         add_post_meta($post_ID, 'Article Authors', $data['article_authors'], true);
    317         add_post_meta($post_ID, 'Article Date', $data['article_date'], true);       
    318         add_post_meta($post_ID, 'Article Pagination', $data['article_pagination'], true);
    319         add_post_meta($post_ID, 'Article URL', $data['article_url'], true);
    320         add_post_meta($post_ID, 'Date Completed', $data['date_completed'], true);
    321         add_post_meta($post_ID, 'Date Created', $data['date_created'], true);
    322         add_post_meta($post_ID, 'Date Revised', $data['date_revised'], true);           
    323         add_post_meta($post_ID, 'Journal Abbreviation', $data['journal_abbreviation'], true);
    324         add_post_meta($post_ID, 'Journal Citation', $data['journal_citation'], true);
    325         add_post_meta($post_ID, 'Journal Date', $data['journal_date'], true);
    326         add_post_meta($post_ID, 'Journal Day', $data['journal_day'], true);
    327         add_post_meta($post_ID, 'Journal Issue', $data['journal_issue'], true);
    328         add_post_meta($post_ID, 'Journal Month', $data['journal_month'], true);
    329         add_post_meta($post_ID, 'Journal Title', $data['journal_title'], true);
    330         add_post_meta($post_ID, 'Journal Volume', $data['journal_volume'], true);
    331         add_post_meta($post_ID, 'Journal Year', $data['journal_year'], true);
    332         add_post_meta($post_ID, 'PMID', $data['pmid'], true);       
    333     }
    334 
    335     /**
    336      * Check if a post exists for a PubMed article
     552        update_post_meta($post_ID, 'Article Affiliation', $data['article_affiliation']);       
     553        update_post_meta($post_ID, 'Article Authors', $data['article_authors']);
     554        update_post_meta($post_ID, 'Article Date', $data['article_date']);     
     555        update_post_meta($post_ID, 'Article Pagination', $data['article_pagination']);
     556        update_post_meta($post_ID, 'Article Title', $data['article_title']);
     557        update_post_meta($post_ID, 'Article URL', $data['article_url']);
     558        update_post_meta($post_ID, 'Date Completed', $data['date_completed']);
     559        update_post_meta($post_ID, 'Date Created', $data['date_created']);
     560        update_post_meta($post_ID, 'Date Revised', $data['date_revised']);         
     561        update_post_meta($post_ID, 'Journal Abbreviation', $data['journal_abbreviation']);
     562        update_post_meta($post_ID, 'Journal Citation', $data['journal_citation']);
     563        update_post_meta($post_ID, 'Journal Date', $data['journal_date']);
     564        update_post_meta($post_ID, 'Journal Day', $data['journal_day']);
     565        update_post_meta($post_ID, 'Journal Issue', $data['journal_issue']);
     566        update_post_meta($post_ID, 'Journal Month', $data['journal_month']);
     567        update_post_meta($post_ID, 'Journal Title', $data['journal_title']);
     568        update_post_meta($post_ID, 'Journal Volume', $data['journal_volume']);
     569        update_post_meta($post_ID, 'Journal Year', $data['journal_year']);
     570        update_post_meta($post_ID, 'PMID', $data['pmid']);
     571    }
     572
     573    /**
     574     * Get ID of post for a PubMed article
    337575     * @param string $pmid
    338      * @return boolean 
    339      */
    340     public function article_exists($pmid) {
     576     * @return integer 
     577     */
     578    public function get_article_post($pmid) {
     579        $pmid = trim($pmid);
    341580        $args = array(
    342581            'posts_per_page' => -1
     
    344583        $posts = get_posts($args);
    345584        foreach($posts as $post) {
    346             $data = get_post_meta($post->ID, 'pubmed-pmid', true);
    347             if ($data == $pmid) {
    348                 return true;
    349             }
    350         }
    351         return false;
     585            $data = get_post_meta($post->ID, 'PMID', true);
     586            if (strtolower($data) == strtolower($pmid)) {
     587                return $post->ID;
     588            }
     589        }
     590        return 0;
    352591    }
    353592   
     
    380619        $settings = get_option('pmp_settings');
    381620        if (!empty($settings['authors'])) {
     621            $authors = get_post_meta($post->ID, 'Article Authors', true);
     622            if (!empty($authors)) {
     623                return $authors;
     624            }
     625            // Support for version 1.0
    382626            $authors = get_post_meta($post->ID, 'pubmed-article-authors', true);
    383             if ($authors) {
    384                 $name = $authors;
    385             } else {
    386                 // Support for old plugin
    387                 $old_authors = get_post_meta($post->ID, 'authors', true);
    388                 if ($old_authors) {
    389                     $name = $old_authors;
    390                 }
    391             }
     627            if (!empty($authors)) {
     628                return $authors;
     629            }
     630            // Support for beta version
     631            $authors = get_post_meta($post->ID, 'authors', true);   
     632            if (!empty($authors)) {
     633                return $authors;
     634            }           
    392635        }
    393636        return $name;
     
    449692     */
    450693    public function settings_section() {
     694        add_settings_field(
     695            'pmp_settings_update',
     696            __('Update Post', 'pubmed-posts'),
     697            array($this, 'settings_update'),
     698            'pmp_settings_page',
     699            'pmp_settings_section'
     700        ); 
     701        add_settings_field(
     702            'pmp_settings_replace',
     703            __('Replace Taxonomy', 'pubmed-posts'),
     704            array($this, 'settings_replace'),
     705            'pmp_settings_page',
     706            'pmp_settings_section'
     707        ); 
    451708        add_settings_field(
    452709            'pmp_settings_authors',
     
    471728        );
    472729    }
     730   
     731    /**
     732     * Settings field for update post
     733     * @return string   
     734     */
     735    public function settings_update() {
     736        $settings = get_option('pmp_settings');
     737        $checked = empty($settings['update']) ? '' : "checked='checked'";
     738        ?>
     739        <input id='pubmed-update' type='checkbox' name='pmp_settings[update]' value='1' <?php echo $checked; ?> />
     740        <label for='pubmed-update'><?php _e('Update existing post', 'pubmed-posts'); ?></label>
     741        <p class='description'><?php _e('Content and custom fields will be updated if post exists', 'pubmed-posts'); ?>.</p>
     742        <?php
     743    }   
     744   
     745    /**
     746     * Settings field for replace taxonomy
     747     * @return string   
     748     */
     749    public function settings_replace() {
     750        $settings = get_option('pmp_settings');
     751        $checked = empty($settings['replace']) ? '' : "checked='checked'";
     752        ?>
     753        <input id='pubmed-replace' type='checkbox' name='pmp_settings[replace]' value='1' <?php echo $checked; ?> />
     754        <label for='pubmed-replace'><?php _e('Replace post taxonomy', 'pubmed-posts'); ?></label>
     755        <p class='description'><?php _e('New categories and tags will replace existing ones for post', 'pubmed-posts'); ?>.</p>
     756        <?php
     757    }       
    473758
    474759    /**
     
    481766        ?>
    482767        <input id='pubmed-authors' type='checkbox' name='pmp_settings[authors]' value='1' <?php echo $checked; ?> />
    483         <label for='pubmed-authors'><?php _e('Check to show article authors', 'pubmed-posts'); ?></label>
    484         <p class='description'><?php _e('All posts will show the article authors instead of post author', 'pubmed-posts'); ?></p>
     768        <label for='pubmed-authors'><?php _e('Show article authors', 'pubmed-posts'); ?></label>
     769        <p class='description'><?php _e('All posts will show the article authors instead of post author', 'pubmed-posts'); ?>.</p>
    485770        <?php
    486771    }
     
    492777    public function settings_dates() {
    493778        $settings = get_option('pmp_settings');
    494         $checked = empty($settings['dates']) ? '' : "checked='checked'";
     779        $selected = empty($settings['dates']) ? '' : $settings['dates'];
    495780        ?>
    496         <input id='pubmed-dates' type='checkbox' name='pmp_settings[dates]' value='1' <?php echo $checked; ?> />
    497         <label for='pubmed-dates'><?php _e('Check to show article dates', 'pubmed-posts'); ?></label>
    498         <p class='description'><?php _e('New posts will be backdated to the article date', 'pubmed-posts'); ?></p>
     781        <select id='pubmed-dates' name='pmp_settings[dates]'>
     782            <option value=''><?php _e('Today', 'pubmed-posts'); ?></option>
     783            <option value='article_date' <?php selected($selected, 'article_date');?>><?php _e('Article Published', 'pubmed-posts'); ?></option>
     784            <option value='journal_date' <?php selected($selected, 'journal_date');?>><?php _e('Journal Published', 'pubmed-posts'); ?></option>
     785            <option value='date_created' <?php selected($selected, 'date_created');?>><?php _e('PubMed Record Created', 'pubmed-posts'); ?></option>
     786            <option value='date_completed' <?php selected($selected, 'date_completed');?>><?php _e('PubMed Record Completed', 'pubmed-posts'); ?></option>
     787            <option value='date_revised' <?php selected($selected, 'date_revised');?>><?php _e('PubMed Record Revised', 'pubmed-posts'); ?></option>
     788        </select>
     789        <p class='description'><?php _e('New posts will be set to this date', 'pubmed-posts'); ?>.</p>
    499790        <?php
    500791    }
     
    509800        ?>
    510801        <textarea id='pubmed-template' type='checkbox' name='pmp_settings[template]'><?php echo $template; ?></textarea>
    511         <p class='description'><?php _e('Create a template for new posts using HTML and the following tags', 'pubmed-posts'); ?></p>
     802        <p class='description'><?php _e('Create a template for new posts using HTML and the following tags', 'pubmed-posts'); ?>.</p>
    512803        <p>
    513             <code>[pmid], [date_created], [date_completed], [date_revised], [article_url], [article_title], [article_date], [article_authors],
    514             [article_abstract], [article_pagination], [article_affiliation], [journal_title], [journal_abbreviation], [journal_volume],
    515             [journal_issue], [journal_date], [journal_citation]</code>
     804            <code>[pmid], [pmid_link], [date_created], [date_completed], [date_revised],    [article_url], [article_title], [article_date],
     805            [article_authors], [article_abstract], [article_pagination], [article_affiliation], [journal_title], [journal_abbreviation],
     806            [journal_volume], [journal_issue], [journal_date], [journal_citation]</code>
    516807        </p>
    517808        <?php
    518     }       
     809    }
    519810
    520811} // End Class
  • pubmed-posts/trunk/readme.txt

    r812902 r818923  
    55Requires at least: 3.3
    66Tested up to: 3.7.1
    7 Stable tag: 1.0.0
     7Stable tag: 1.1.0
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
    1010
    11 This plugin adds a dashboard widget that creates posts from PubMed articles.
     11This plugin adds a dashboard widget that creates posts from PubMed articles, plus a search widget that finds posts with specific article data.
    1212
    1313== Description ==
    1414
    15 This plugin adds a dashboard widget that creates posts from [PubMed](http://www.ncbi.nlm.nih.gov/pubmed/) articles. A basic editor is included for changing the layout of data in the post content.
     15This plugin adds a dashboard widget that creates posts from [PubMed](http://www.ncbi.nlm.nih.gov/pubmed/) articles, plus a search widget that finds posts with specific article data. A basic editor is also included that changes the layout of data in the post content.
    1616
    1717This is not an official [PubMed](http://www.ncbi.nlm.nih.gov/pubmed/) plugin. All questions and suggestions should be posted in the plugin forum.
     
    33333. Settings Page
    3434
     354. Sidebar Widget
     36
    3537== Frequently Asked Questions ==
    3638
     
    3941== Upgrade Notice ==
    4042
    41 No upgrade notices.
     43No upgrade notices
    4244
    4345== Changelog ==
    4446
     47= 1.1.0 =
     48* Added search widget
     49* Added new settings (update posts, replace taxonomy)
     50* Added more options to post date settings
     51* Improved parsing of MEDLINE dates
     52
    4553= 1.0.0 =
    4654* First release
Note: See TracChangeset for help on using the changeset viewer.