Advertisement
terorama

WP / Test Gallery Plugin

Apr 12th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.46 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: test88
  4. Plugin URI: http://test.org
  5. Description: test plugin
  6. Version: 4.44
  7. Author: Kitsune
  8. Author URI: http://test.org
  9. */
  10.  
  11.  
  12. add_action('init','zzz_init_func');
  13. add_action('admin_menu', 'zzz_test_menu');
  14. add_action('wp_head','zzz_head_scripts');
  15. add_action('wp_enqueue_scripts', 'zzz_test_scripts');
  16. add_action('admin_enqueue_scripts', 'zzz_admin_scripts');
  17.  
  18. add_shortcode('test88_pic','zzz_shortcode');
  19.  
  20. add_action('wp_ajax_getmessage', 'zzz_ajaxfunc');
  21. add_action('wp_ajax_nopriv_getmessage', 'zzz_ajaxfunc');
  22. add_action('widgets_init','zzz_widgetsinit');
  23.  
  24. add_action('activate_test88/test88.php', 'zzz_activateplugin');
  25.  
  26. //--------------------------------- init
  27. function zzz_init_func()  {
  28.  
  29.    if ((!current_user_can('edit_posts')) && (!current_user_can('edit_pages')))
  30.       return;
  31.      
  32.     if (get_user_option('rich_editing')==true) {
  33.         add_filter('mce_external_plugins', 'zzz_add_mce_plugin');
  34.         add_filter('mce_buttons','zzz_mce_regbutton');
  35.     }
  36. }
  37. //--------------------------------- mce_external_plugins
  38.  
  39. function zzz_add_mce_plugin($plugin_array)  {
  40.    $plugin_array['test88_pics'] = plugins_url('test88/mce_test88.js');
  41.    return $plugin_array;
  42. }
  43. //--------------------------------- mce_buttons
  44.  
  45. function zzz_mce_regbutton($buttons)  {
  46.    array_push($buttons, 'separator', 'test88_pics_button');
  47.    return $buttons;
  48. }
  49. //--------------------------------- admin_menu
  50. function zzz_test_menu() {
  51.    add_menu_page(__('Test88 Pics Page'), __('Test88 Pics Page'), 'activate_plugins', 'test88/main_admin_page.php');
  52.    add_submenu_page('test88/main_admin_page.php', __('Options Page'), __('Options Page'), 'activate_plugins', 'test88/sub_admin_page.php');  
  53. }
  54. //--------------------------------- wp_head
  55.  
  56. function zzz_head_scripts() {
  57.  
  58.    $test_opt = get_option('zzzz_render');
  59.    if ($test_opt['style']=='slide') {
  60.    
  61.    ?>
  62.       <script type="text/javascript">
  63.          jQuery(document).ready(
  64.             function() {
  65.                jQuery('.ajax_button').click(zClick);
  66.             }
  67.          )
  68.       </script>
  69.    
  70.       <style type="text/css">
  71.          .gallery {
  72.             position: relative;
  73.          }
  74.       </style>
  75.      
  76.       <?php
  77.       }
  78. }
  79. //--------------------------------- wp_enqueue_scripts
  80.  
  81. function zzz_test_scripts() {
  82.    
  83.    /*if (@file_exists(TEMPLATEPATH.'/test88.css')) {
  84.       wp_enqueue_style('test88_style', get_stylesheet_directory_uri().'/test88.css');
  85.    }*/
  86.    
  87.    if (@file_exists(plugins_url('test88/base.css')))
  88.       wp_enqueue_style('test88_base_style', plugins_url('test88/base.css'));
  89.    
  90.    $test_opt = get_option('zzzz_render');
  91.    
  92.    if ($test_opt['style']=='slide') {
  93.       wp_enqueue_script('test88_slide_js', plugins_url('test88/slide.js'), array('jquery'));
  94.    
  95.       $ajax_nonce = wp_create_nonce('querty');
  96.    
  97.       wp_localize_script('test88_slide_js', 't88slide',
  98.          array ('ajax_url'=>admin_url('admin-ajax.php',(is_ssl() ? 'https' : 'http')),
  99.         'container'=>'ajax_container', 'ajax_nonce' => $ajax_nonce)
  100.       );
  101.    }
  102. }
  103. //--------------------------------- admin_enqueue_scripts
  104.  
  105. function zzz_admin_scripts() {
  106. }
  107.  
  108. //--------------------------------- shortcode
  109.  
  110. function zzz_shortcode($atts) {
  111.  
  112.    extract (
  113.       shortcode_atts(
  114.          array('title'=>'Test88 Gallery', 'sclass'=>'gallery'))
  115.          );
  116.    
  117.     $test_opt = get_option('zzzz_render');
  118.    
  119.     $out = '<div class="'.$sclass.'" style="background:'.$test_opt['background'].';"><h3>'.$title.'</h3>';
  120.    
  121.     global $wpdb;  
  122.     $pics = $wpdb->get_results("SELECT id, pic_address FROM pics_test88 ORDER BY id DESC LIMIT 12");
  123.  
  124.     foreach ($pics as $pic) {
  125.        $out.='<img src="'.$pic->pic_address.'" />';
  126.     }
  127.  
  128.     if ($test_opt['style']=='slide') {
  129.        $out .= '<div><input type="button" class="ajax_button" value="load" />'.
  130.        '</div><div id="ajax_container">ajax container</div>';
  131.     }
  132.     $out.='</div>';
  133.     return $out;  
  134. }
  135.  
  136. //---------------------------------
  137.  
  138. /*function set_schedule() {
  139.    wp_clear_scheduled_hook('test88_cron');
  140.    
  141.    if (!wp_next_scheduled('test88_cron'))
  142.       wp_schedule_event(time(), 'twicedaily', 'test88_cron');
  143. }
  144. //---------------------------------
  145. add_action('test88_cron', 'zzz_cron_action');
  146.  
  147. //--------------------------------- test88_cron
  148. function zzz_cron_action() {
  149.  
  150.    $tmp = get_option('zzzz_counter');
  151.    $inf4 = update_option('zzzz_counter', $tmp+1);
  152. }*/
  153.  
  154. //--------------------------------- wp_ajax_getmessage / wp_ajax_nopriv_getmessage
  155. function zzz_ajaxfunc() {
  156.  
  157.    global $wpdb, $user_identity, $user_ID;
  158.  
  159.    if (!check_ajax_referer('querty', 'security', false)) {
  160.        _e('Failed to Verify Referer');
  161.          exit();
  162.          }       
  163.    echo 'test ajax return 4';
  164.  
  165. }
  166.  
  167. //---------------------------------
  168. class WP_Widget_test88 extends WP_Widget {
  169.  
  170.    //------------------
  171.    function WP_Widget_test88() {
  172.    
  173.       $widget_opts = array('description'=>__('WP test88 Pics Widget'));
  174.       $this->WP_Widget('test88-widget',__('Tes88'), $widget_opts);
  175.    }
  176.    //------------------
  177.    function widget($args, $instance) {
  178.    
  179.       extract($args);
  180.      
  181.       $title = apply_filters('widget_title', esc_attr($instance['title']));
  182.       $bkg_id = intval($instance['bkg_id']);
  183.      
  184.       echo $before_widget;
  185.       echo $before_title;    
  186.       echo $title;
  187.       echo $after_title;
  188.      
  189.       switch ($bkg_id) {
  190.          case 0: $bkg='#444';break;
  191.          case 4: $bkg='#eee';break;
  192.          case 5: $bkg='red';break;
  193.          default: $bkg='green';
  194.       }
  195.    
  196.       $out = '<div style="background:'.$bkg.';"><h3>'.$title.'</h3>';
  197.    
  198.       global $wpdb;
  199.           $wpdb->show_errors();
  200.       $pics = $wpdb->get_results("SELECT id, pic_address FROM pics_test88 ORDER BY id DESC LIMIT 12");
  201.        foreach ($pics as $pic) {
  202.           $out.='<img src="'.$pic->pic_address.'" />';
  203.       }
  204.       $out.='</div>';
  205.           echo $out;
  206.      
  207.       echo $after_widget;
  208.    }
  209.    //------------------
  210.    function update($new_instance, $old_instance) {
  211.      
  212.       $instance = $old_instance;
  213.       $instance['title'] = strip_tags($new_instance['title']);
  214.       $instance['bkg_id'] = intval($new_instance['bkg_id']);
  215.       return $instance;
  216.    }
  217.    //------------------
  218.    function form($instance) {
  219.    
  220.       $instance =
  221.         wp_parse_args( (array)$instance,
  222.          array('title'=>__('Test88 widget'),
  223.                'test_id'=>0)
  224.          );
  225.          
  226.       $title = esc_attr($instance['title']);
  227.       $test_id = intval($instance['test_id']);
  228.      
  229.       echo '<input id="'.$this->get_field_id('title').'" name="'.
  230.            $this->get_field_name('title').'" type="text" value="'.$title.'" /><br/>';
  231.            
  232.       echo '<select name="'.$this->get_field_name('bkg_id').'" id="'.$this->get_field_id('bkg_id').'">';
  233.       echo '<option value="0" '.selected(0, $test_id).'>'.'Dark background'.'</option>';
  234.       echo '<option value="4" '.selected(4, $test_id).'>'.'Light background'.'</option>';
  235.       echo '<option value="5" '.selected(5, $test_id).'>'.'Red background'.'</option>';
  236.       echo '</select><br/>';
  237.    }
  238. }
  239.  
  240. //--------------------------------- widgets_init
  241. function zzz_widgetsinit() {
  242.    register_widget('WP_Widget_test88');
  243. }
  244.  
  245. //--------------------------------- activate_test88/test88.php
  246. function zzz_activateplugin() {
  247.  
  248.    global $wpdb;
  249.    
  250.    $sq = "CREATE TABLE pics_test88 (
  251.          id int(10) NOT NULL auto_increment,
  252.           pic_address varchar(500) NOT NULL default '',
  253.           PRIMARY KEY (id));";
  254.  
  255.    include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
  256.  
  257.    /*$wpdb->show_errors();       
  258.    maybe_create_table($wpdb->pics_test88, $sq);*/
  259.  
  260.    dbDelta($sq);
  261.    
  262.    $tmp = $wpdb->get_var("SELECT id FROM pics_test88 LIMIT 1");
  263.    if (empty($tmp)) {
  264.       $tmp4 = $wpdb->query("INSERT INTO pics_test88 VALUES(1,
  265.          'http://bigpicture.ru/wp-content/uploads/2012/09/Long-Exposure-Fireworks-12.jpg');");
  266.    }
  267.    
  268.    add_option('zzzz_render',array('style' => 'default', 'background' => '#d8e1eb'));
  269.    add_option('zzzz_latest_id',0);
  270.    
  271.    $role = get_role('administrator');
  272.    if (!$role->has_cap('manage_test88'))
  273.       $role->add_cap('manage_test88');
  274.      
  275.    //set_schedule();     
  276. }
  277.  
  278. ?>
  279.  
  280. <?php
  281. /*------------------------------------------------------------------------------
  282.            main_admin_page.php
  283. -------------------------------------------------------------------------------*/
  284. if (!current_user_can('manage_test88'))
  285.    die('Access denied');
  286.    
  287.    
  288. $base_name = plugin_basename('test88/main_admin_page.php');
  289. $base_page = 'admin.php?page='.$base_name;
  290.  
  291. //----------------------------------
  292. if (!empty($_POST['do'])) {
  293.  
  294.    check_admin_referer('add-test88');
  295.    
  296.    $pic_addr = trim($_POST['pic_addr']);
  297.    $wpdb->show_errors();
  298.    $addpic = $wpdb->query("INSERT INTO pics_test88 VALUES(0, '$pic_addr')");
  299.    if (!$addpic) {
  300.       $tx = '<p style="color:red">error in adding picture</p>';
  301.    } else {
  302.       $tx = '<p style="color:green">picture successfully added</p>';
  303.       $insid = intval($wpdb->insert_id);
  304.      
  305.       update_option('zzzz_latest_id', $insid);
  306.       }
  307.  
  308.   echo $tx;  
  309. }
  310. //----------------------------------
  311.  
  312. ?>
  313. <h3>Add picture</h3>
  314. <form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>">
  315.  
  316.    <?php wp_nonce_field('add-test88'); ?>
  317.    
  318.    <p>Input picture address: <input type="text" name="pic_addr" size="70" value="" /></p>
  319.    <input type="submit" name="do" value="<?php _e('Add pic');?>" />
  320. </form>
  321.  
  322. <?php
  323. //----------------------------------
  324.  
  325.  
  326. ?>
  327.  
  328. <?php
  329. /*------------------------------------------------------------------------------
  330.            sub_admin_page.php
  331. -------------------------------------------------------------------------------*/
  332. if (!current_user_can('manage_test88'))
  333.    die('Access denied');
  334.    
  335.    
  336. $base_name = plugin_basename('test88/sub_admin_page.php');
  337. $base_page = 'admin.php?page='.$base_name;
  338.  
  339. if (isset($_POST['submit'])) {
  340.    
  341.    check_admin_referer('options-test88');
  342.    
  343.    $zz_style = strip_tags(trim($_POST['zz_style']));
  344.    $zz_background = strip_tags(trim($_POST['zz_background']));
  345.    
  346.    $zzzz_render = array('style'=>$zz_style, 'background'=>$zz_background);
  347.    
  348.    $tst = update_option('zzzz_render', $zzzz_render);
  349.    
  350.    if (!$tst) {
  351.       $tx = '<p style="color:red">error updating option</p>';
  352.    } else {
  353.       $tx = '<p style="color:green">option successfully updated</p>';
  354.    }
  355.    
  356.    echo $tx;
  357.  
  358. }
  359. //------------------------------------------
  360. ?>
  361. <form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>">
  362.  
  363. <?php
  364.  
  365.    wp_nonce_field('options-test88');
  366.  
  367.    $zzzz_render = get_option('zzzz_render');  
  368. ?>
  369.  
  370.    <h3>select image box style</h3>
  371.    <p>default style <input type="radio" name="zz_style" value="default" <?php checked('default',$zzzz_render['style']);?> /></p>
  372.    <p>slide style <input type="radio" name="zz_style" value="slide" <?php checked('slide', $zzzz_render['style']);?> /></p>
  373.    
  374.    <h3>select image box background</h3>
  375.    <p><input type="text" name="zz_background" value="<?php echo $zzzz_render['background'];?>" /></p>
  376.    
  377.    <p><input type="submit" name="submit" value="<?php _e('Save options'); ?>" /></p>
  378. </form>
  379.  
  380.  
  381. /*-----------------------------------------------------------
  382.                     slide.js
  383. -----------------------------------------------------------*/
  384.  
  385. function zClick() {
  386.  
  387. jQuery.ajax({
  388.    type:'GET',
  389.    
  390.    url: t88slide.ajax_url,
  391.    
  392.    data: 'action=getmessage&security='+t88slide.ajax_nonce,
  393.    cache: false,
  394.    success: onSuccess})
  395.    
  396.    }
  397.    
  398. function onSuccess(data) {
  399.    var el = document.getElementById(t88slide.container);
  400.    el.innerHTML = data;
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement