Changes between Version 1 and Version 2 of WidgetWritingGuide


Ignore:
Timestamp:
12/02/11 22:15:08 (5 years ago)
Author:
ofer
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WidgetWritingGuide

    v1 v2  
    44All widgets are .php scripts that reside in the widgets subdirectory of the plugin,
    55they should be prefixed with tpw_ in order to be read.
     6
     7Note that since v0.8.0 those sub-widgets are implemented as PHP classes with static functions, this is a change in order to allow
     8multiple instances of different sub-widgets in the same page
    69
    710the first few bytes of the file should contain a plugin header in the same format used in wordpress plugins - for example:
     
    1720*/
    1821}}}
    19 files can also reside in a specific subdirectory of the widgets directory.
     22files should reside in a specific subdirectory of the widgets directory.
    2023
    2124You can also place .css and .js files using the same name, those will be automatically included (and can be overriden, see below).
     25
     26The class should extend the '''''transposh_base_widget''''' class like:
     27{{{
     28class tpw_default extends transposh_base_widget
     29}}}
    2230
    2331
     
    2533Currently there are a few functions one can implement
    2634
    27 == function tp_widgets_do($args) ==
     35== static function tp_widgets_do($args) ==
    2836the most important one is the tp_widget_do($args) which actually renders the html that the widget creates
    2937the args include a list of language records which have the following properties:
    3038||= Parameter =||= Example value =||= Description =||
    31 ||lang"||Hebrew||The name of the language in English||
     39||lang||Hebrew||The name of the language in English||
    3240||langorig||עברית||The name of the language in the language itself||
    3341||flag||il||The name of the flag file provided for the language||
     
    3846Sample implementation: Create a select box
    3947{{{
    40 function tp_widget_do($args) {
    41   echo '<span class="' . NO_TRANSLATE_CLASS . '">';
    42   echo '<select name="lang" id="lang" onchange="Javascript:this.form.submit();">';
    43   echo '<option value="none">[Language]</option>';
     48static function tp_widget_do($args) {
     49  echo '<span class="' . NO_TRANSLATE_CLASS . '">'; // wrapping in no_translate to avoid translation of this list
    4450
     51  echo '<select name="lang" id="lang" onchange="document.location.href=this.options[this.selectedIndex].value;">'; // this is a select box which posts on change
    4552  foreach ($args as $langrecord) {
    4653    $is_selected = $langrecord['active'] ? " selected=\"selected\"" : "";
    47     echo "<option value=\"{$langrecord['isocode']}\"{$is_selected}>{$langrecord['langorig']}</option>";
     54    echo "<option value=\"{$langrecord['url']}\"{$is_selected}>{$langrecord['langorig']}</option>";
    4855  }
    4956  echo "</select><br/>";
     
    5259}}}
    5360
    54 == function tp_widget_needs_post_url() ==
    55 This function hints the widget dispatcher regarding the need for calculating the page urls, which may be a demanding function especially if no caching is present and URL translation is enabled
    56 sample implementation: Instruct not to calculate the urls
     61== static function tp_widget_js($file, $dir, $url) ==
     62There are three Parameters:
     63* $file contains the file name in relation to the widgets sub directory
     64* $dir contains the plugin directory
     65* $url contains the plugin url
     66
     67sample implementation: Include the default .js, but make sure jQuery is also loaded
     68
    5769{{{
    58 function tp_widget_needs_post_url() {
    59   return false;
     70static function tp_widget_js($file, $dir, $url) {
     71  wp_enqueue_script("transposh_widget", "$url/widgets/dropdown/tpw_image_dropdown.js", array('jquery'), TRANSPOSH_PLUGIN_VER);
    6072}
    6173}}}
    6274
    63 == function tp_widget_js() ==
    64 sample implementation: Include the default .js, but make sure jQuery is also loaded
     75== static function tp_widget_css(file, $dir, $url) ==
     76sample implementation: Include a given .css
     77
    6578{{{
    66 function tp_widget_js() {
    67   global $my_transposh_plugin;
    68   wp_enqueue_script("transposh_widget", "{$my_transposh_plugin->transposh_plugin_url}/widgets/{$my_transposh_plugin->widget->base_widget_file_name}.js", array('jquery'), TRANSPOSH_PLUGIN_VER);
    69 }
    70 }}}
    71 
    72 == function tp_widget_css() ==
    73 sample implementation: Include a given .css
    74 {{{
    75 function tp_widget_css() {
    76   global $my_transposh_plugin;
    77   wp_enqueue_style("transposh_widget", "{$my_transposh_plugin->transposh_plugin_url}/widgets/tpw_flags.css", array(), TRANSPOSH_PLUGIN_VER);
     79static function tp_widget_css($file, $dir, $url) {
     80  wp_enqueue_style("flags/tpw_flags", "$url/widgets/flags/tpw_flags.css", array(), TRANSPOSH_PLUGIN_VER);
    7881}
    7982}}}
    8083
    8184= Using global environment =
    82 The complete transposh plugin object can be used within the widget, as can be seen in the above samples, just make sure to include the following line:
     85The complete transposh plugin object can be used within the widget, just make sure to include the following line:
     86
    8387{{{
    84   global $my_transposh_plugin;
     88global $my_transposh_plugin;
    8589}}}
     90Normally there would be reason to do that