Advertisement
Guest User

fr

a guest
Nov 22nd, 2012
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.61 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Feed Refresh
  4. Plugin URI: #
  5. Description: Feed Refresh Beta.
  6. Version: 0.1
  7. Author: Dexter
  8. Author URI: #
  9. */
  10.  
  11. // ------------------------------------------------------------------------
  12. // REQUIRE MINIMUM VERSION OF WORDPRESS:                                              
  13. // ------------------------------------------------------------------------
  14. // THIS IS USEFUL IF YOU REQUIRE A MINIMUM VERSION OF WORDPRESS TO RUN YOUR
  15. // PLUGIN. IN THIS PLUGIN THE WP_EDITOR() FUNCTION REQUIRES WORDPRESS 3.3
  16. // OR ABOVE. ANYTHING LESS SHOWS A WARNING AND THE PLUGIN IS DEACTIVATED.                    
  17. // ------------------------------------------------------------------------
  18.  
  19. function fr_requires_wordpress_version() {
  20.     global $wp_version;
  21.     $plugin = plugin_basename( __FILE__ );
  22.     $plugin_data = get_plugin_data( __FILE__, false );
  23.  
  24.     if ( version_compare($wp_version, "3.3", "<" ) ) {
  25.         if( is_plugin_active($plugin) ) {
  26.             deactivate_plugins( $plugin );
  27.             wp_die( "'".$plugin_data['Name']."' requires WordPress 3.3 or higher, and has been deactivated! Please upgrade WordPress and try again.<br /><br />Back to <a href='".admin_url()."'>WordPress admin</a>." );
  28.         }
  29.     }
  30. }
  31. add_action( 'admin_init', 'fr_requires_wordpress_version' );
  32.  
  33. // ------------------------------------------------------------------------
  34. // PLUGIN PREFIX:                                                          
  35. // ------------------------------------------------------------------------
  36. // A PREFIX IS USED TO AVOID CONFLICTS WITH EXISTING PLUGIN FUNCTION NAMES.
  37. // WHEN CREATING A NEW PLUGIN, CHANGE THE PREFIX AND USE YOUR TEXT EDITORS
  38. // SEARCH/REPLACE FUNCTION TO RENAME THEM ALL QUICKLY.
  39. // ------------------------------------------------------------------------
  40.  
  41. // 'fr_' prefix is derived from [p]plugin [o]ptions [s]tarter [k]it
  42.  
  43. // ------------------------------------------------------------------------
  44. // REGISTER HOOKS & CALLBACK FUNCTIONS:
  45. // ------------------------------------------------------------------------
  46. // HOOKS TO SETUP DEFAULT PLUGIN OPTIONS, HANDLE CLEAN-UP OF OPTIONS WHEN
  47. // PLUGIN IS DEACTIVATED AND DELETED, INITIALISE PLUGIN, ADD OPTIONS PAGE.
  48. // ------------------------------------------------------------------------
  49.  
  50. // Set-up Action and Filter Hooks
  51. register_activation_hook(__FILE__, 'fr_add_defaults');
  52. register_uninstall_hook(__FILE__, 'fr_delete_plugin_options');
  53. add_action('admin_init', 'fr_init' );
  54. add_action('admin_menu', 'fr_add_options_page');
  55. add_filter( 'plugin_action_links', 'fr_plugin_action_links', 10, 2 );
  56.  
  57. // --------------------------------------------------------------------------------------
  58. // CALLBACK FUNCTION FOR: register_uninstall_hook(__FILE__, 'fr_delete_plugin_options')
  59. // --------------------------------------------------------------------------------------
  60. // THIS FUNCTION RUNS WHEN THE USER DEACTIVATES AND DELETES THE PLUGIN. IT SIMPLY DELETES
  61. // THE PLUGIN OPTIONS DB ENTRY (WHICH IS AN ARRAY STORING ALL THE PLUGIN OPTIONS).
  62. // --------------------------------------------------------------------------------------
  63.  
  64. // Delete options table entries ONLY when plugin deactivated AND deleted
  65. function fr_delete_plugin_options() {
  66.     delete_option('fr_options');
  67. }
  68.  
  69. // ------------------------------------------------------------------------------
  70. // CALLBACK FUNCTION FOR: register_activation_hook(__FILE__, 'fr_add_defaults')
  71. // ------------------------------------------------------------------------------
  72. // THIS FUNCTION RUNS WHEN THE PLUGIN IS ACTIVATED. IF THERE ARE NO THEME OPTIONS
  73. // CURRENTLY SET, OR THE USER HAS SELECTED THE CHECKBOX TO RESET OPTIONS TO THEIR
  74. // DEFAULTS THEN THE OPTIONS ARE SET/RESET.
  75. //
  76. // OTHERWISE, THE PLUGIN OPTIONS REMAIN UNCHANGED.
  77. // ------------------------------------------------------------------------------
  78.  
  79. // Define default option settings
  80. function fr_add_defaults() {
  81.     $tmp = get_option('fr_options');
  82.     if(($tmp['chk_default_options_db']=='1')||(!is_array($tmp))) {
  83.         delete_option('fr_options'); // so we don't have to reset all the 'off' checkboxes too! (don't think this is needed but leave for now)
  84.         $arr = array(
  85.             "txt_1" => "Enter whatever you like here..",
  86.             "txt_2" => "Enter whatever you like here..",
  87.             "txt_3" => "Enter whatever you like here..",
  88.             "txt_4" => "Enter whatever you like here..",
  89.             "txt_5" => "Enter whatever you like here.."
  90.         );
  91.         update_option('fr_options', $arr);
  92.     }
  93. }
  94.  
  95. // ------------------------------------------------------------------------------
  96. // CALLBACK FUNCTION FOR: add_action('admin_init', 'fr_init' )
  97. // ------------------------------------------------------------------------------
  98. // THIS FUNCTION RUNS WHEN THE 'admin_init' HOOK FIRES, AND REGISTERS YOUR PLUGIN
  99. // SETTING WITH THE WORDPRESS SETTINGS API. YOU WON'T BE ABLE TO USE THE SETTINGS
  100. // API UNTIL YOU DO.
  101. // ------------------------------------------------------------------------------
  102.  
  103. // Init plugin options to white list our options
  104. function fr_init(){
  105.     register_setting( 'fr_plugin_options', 'fr_options', 'fr_validate_options' );
  106. }
  107.  
  108. // ------------------------------------------------------------------------------
  109. // CALLBACK FUNCTION FOR: add_action('admin_menu', 'fr_add_options_page');
  110. // ------------------------------------------------------------------------------
  111. // THIS FUNCTION RUNS WHEN THE 'admin_menu' HOOK FIRES, AND ADDS A NEW OPTIONS
  112. // PAGE FOR YOUR PLUGIN TO THE SETTINGS MENU.
  113. // ------------------------------------------------------------------------------
  114.  
  115. // Add menu page
  116. function fr_add_options_page() {
  117.     add_menu_page('Feed Refresh Options Page', 'Feed Refresh', 'manage_options', __FILE__, 'fr_render_form');
  118. }
  119.  
  120. // ------------------------------------------------------------------------------
  121. // CALLBACK FUNCTION SPECIFIED IN: add_options_page()
  122. // ------------------------------------------------------------------------------
  123. // THIS FUNCTION IS SPECIFIED IN add_options_page() AS THE CALLBACK FUNCTION THAT
  124. // ACTUALLY RENDER THE PLUGIN OPTIONS FORM AS A SUB-MENU UNDER THE EXISTING
  125. // SETTINGS ADMIN MENU.
  126. // ------------------------------------------------------------------------------
  127.  
  128. // Render the Plugin options form
  129. function fr_render_form() {
  130.     ?>
  131.     <div class="wrap">
  132.        
  133.         <!-- Display Plugin Icon, Header, and Description -->
  134.         <div class="icon32" id="icon-options-general"><br></div>
  135.         <h2>Plugin Options Starter Kit</h2>
  136.         <p>Below is a collection of sample controls you can use in your own Plugins. Or, you can analyse the code and learn how all the most common controls can be added to a Plugin options form. See the code for more details, it is fully commented.</p>
  137.  
  138.         <!-- Beginning of the Plugin Options Form -->
  139.         <form method="post" action="options.php">
  140.             <?php settings_fields('fr_plugin_options'); ?>
  141.             <?php $options = get_option('fr_options'); ?>
  142.  
  143.             <!-- Table Structure Containing Form Controls -->
  144.             <!-- Each Plugin Option Defined on a New Table Row -->
  145.             <table class="form-table">
  146.  
  147.                 <!-- Textbox Control -->
  148.                 <tr><th scope="row">Track 1</th><td><input type="text" size="200" name="fr_options[txt_1]" value="<?php echo $options['txt_1']; ?>" /></td></tr>
  149.                 <tr><th scope="row">Track 2</th><td><input type="text" size="200" name="fr_options[txt_2]" value="<?php echo $options['txt_2']; ?>" /></td></tr>
  150.                 <tr><th scope="row">Track 3</th><td><input type="text" size="200" name="fr_options[txt_3]" value="<?php echo $options['txt_3']; ?>" /></td></tr>
  151.                 <tr><th scope="row">Track 4</th><td><input type="text" size="200" name="fr_options[txt_4]" value="<?php echo $options['txt_4']; ?>" /></td></tr>
  152.                 <tr><th scope="row">Track 5</th><td><input type="text" size="200" name="fr_options[txt_5]" value="<?php echo $options['txt_5']; ?>" /></td></tr>
  153.  
  154.                 <tr><td colspan="2"><div style="margin-top:10px;"></div></td></tr>
  155.                 <tr valign="top" style="border-top:#dddddd 1px solid;">
  156.                     <th scope="row">Database Options</th>
  157.                     <td>
  158.                         <label><input name="fr_options[chk_default_options_db]" type="checkbox" value="1" <?php if (isset($options['chk_default_options_db'])) { checked('1', $options['chk_default_options_db']); } ?> /> Restore defaults upon plugin deactivation/reactivation</label>
  159.                         <br /><span style="color:#666666;margin-left:2px;">Only check this if you want to reset plugin settings upon Plugin reactivation</span>
  160.                     </td>
  161.                 </tr>
  162.             </table>
  163.             <p class="submit">
  164.             <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  165.             </p>
  166.         </form>
  167.  
  168.         <p style="margin-top:15px;">
  169.             <p style="font-style: italic;font-weight: bold;color: #26779a;">If you have found this starter kit at all useful, please consider making a <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XKZXD2BHQ5UB2" target="_blank" style="color:#72a1c6;">donation</a>. Thanks.</p>
  170.             <span><a href="http://www.facebook.com/PressCoders" title="Our Facebook page" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/facebook-icon.png" /></a></span>
  171.             &nbsp;&nbsp;<span><a href="http://www.twitter.com/dgwyer" title="Follow on Twitter" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/twitter-icon.png" /></a></span>
  172.             &nbsp;&nbsp;<span><a href="http://www.presscoders.com" title="PressCoders.com" target="_blank"><img style="border:1px #ccc solid;" src="<?php echo plugins_url(); ?>/feed-refresh/images/pc-icon.png" /></a></span>
  173.         </p>
  174.  
  175.     </div>
  176.     <?php  
  177. }
  178.  
  179. // Sanitize and validate input. Accepts an array, return a sanitized array.
  180. function fr_validate_options($input) {
  181.      // strip html from textboxes
  182.     $input['txt_1'] =  wp_filter_nohtml_kses($input['txt_1']); // Sanitize textbox input (strip html tags, and escape characters)
  183.     $input['txt_2'] =  wp_filter_nohtml_kses($input['txt_2']); // Sanitize textbox input (strip html tags, and escape characters)
  184.     $input['txt_3'] =  wp_filter_nohtml_kses($input['txt_3']); // Sanitize textbox input (strip html tags, and escape characters)
  185.     $input['txt_4'] =  wp_filter_nohtml_kses($input['txt_4']); // Sanitize textbox input (strip html tags, and escape characters)
  186.     $input['txt_5'] =  wp_filter_nohtml_kses($input['txt_5']); // Sanitize textbox input (strip html tags, and escape characters)
  187.     return $input;
  188. }
  189.  
  190. // Display a Settings link on the main Plugins page
  191. function fr_plugin_action_links( $links, $file ) {
  192.  
  193.     if ( $file == plugin_basename( __FILE__ ) ) {
  194.         $fr_links = '<a href="'.get_admin_url().'admin.php?page=feed-refresh/feed-refresh.php">'.__('Settings').'</a>';
  195.         // make the 'Settings' link appear first
  196.         array_unshift( $links, $fr_links );
  197.     }
  198.  
  199.     return $links;
  200. }
  201.  
  202. // ------------------------------------------------------------------------------
  203. // SAMPLE USAGE FUNCTIONS:
  204. // ------------------------------------------------------------------------------
  205. // THE FOLLOWING FUNCTIONS SAMPLE USAGE OF THE PLUGINS OPTIONS DEFINED ABOVE. TRY
  206. // CHANGING THE DROPDOWN SELECT BOX VALUE AND SAVING THE CHANGES. THEN REFRESH
  207. // A PAGE ON YOUR SITE TO SEE THE UPDATED VALUE.
  208. // ------------------------------------------------------------------------------
  209.  
  210.  
  211. add_action('admin_init', 'feeds');
  212. function feeds() { 
  213.     $options = get_option('fr_options');
  214.     $arr = array(
  215.             $options['txt_1'],
  216.             $options['txt_2'],
  217.             $options['txt_3'],
  218.             $options['txt_4'],
  219.             $options['txt_5']
  220.         );
  221.        
  222.     $filename = plugin_dir_path(__FILE__) . "test.php";
  223.     $text = "";
  224.     foreach($arr as $value)
  225.     {
  226.         $text .= $value."\n";
  227.     }
  228.     $fh = fopen($filename, "w") or die("Could not open file.");
  229.     fwrite($fh, $text) or die("Could not write file!");
  230.     fclose($fh);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement