Advertisement
aligatro

General plug code

Dec 24th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.00 KB | None | 0 0
  1. <?php
  2. global $wpdb, $wp_version;
  3. define("POOLONETABLEQ", $wpdb->prefix . "pooloneq_wp_plugin");
  4. define("POOLONETABLEA", $wpdb->prefix . "poolonea_wp_plugin");
  5.  
  6. if ( ! defined( 'POOLONE_PLUGIN_BASENAME' ) )
  7.     define( 'POOLONE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
  8.  
  9. if ( ! defined( 'POOLONE_PLUGIN_NAME' ) )
  10.     define( 'POOLONE_PLUGIN_NAME', trim( dirname( POOLONE_PLUGIN_BASENAME ), '/' ) );
  11.  
  12. if ( ! defined( 'POOLONE_PLUGIN_DIR' ) )
  13.     define( 'POOLONE_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . POOLONE_PLUGIN_NAME );
  14.  
  15. if ( ! defined( 'POOLONE_PLUGIN_URL' ) )
  16.     define( 'POOLONE_PLUGIN_URL', WP_PLUGIN_URL . '/' . POOLONE_PLUGIN_NAME );
  17.  
  18. function pool1_plugin_path( $path = '' ) {
  19.     return path_join( POOLONE_PLUGIN_DIR, trim( $path, '/' ) );
  20. }
  21.  
  22. function pool1_plugin_url( $path = '' ) {
  23.     return plugins_url( $path, POOLONE_PLUGIN_BASENAME );
  24. }
  25.  
  26.  
  27. /*Функция голосования и вывода количества голосов*/
  28. function poolone()
  29. {
  30.     global $wpdb;
  31.     $pool1_que_css = "<div class='pool-question'>##QUESTION##</div>";
  32.     $pool1_ans_css = "<div class='pool-answer'>##ANSWER##</div>";
  33.     $pool1_btn_css = "<div class='pool-button'>##BUTTON##</div>";
  34.  
  35.    
  36.     $pool_random = "YES";
  37.     $sSql = "select poolq_id, poolq_question from ". POOLONETABLEQ ." where 1=1";
  38.     $sSql = $sSql . " and (`poolq_start` <= NOW() and `poolq_end` >= NOW())";
  39.     if($pool_random == "YES"){ $sSql = $sSql . " ORDER BY RAND()"; }
  40.     $sSql = $sSql . " limit 0,1;";
  41.     $pool_question = $wpdb->get_results($sSql);
  42.     if ( ! empty($pool_question) )
  43.     {
  44.         // setcookie
  45.         ob_start();
  46.         $cookie_pool_question = $pool_question[0];
  47.         if ( !empty($cookie_pool_question) ) $cookie_poolq_id = $cookie_pool_question->poolq_id;
  48.         // setcookie
  49.         if (isset($_COOKIE["POLLONE-$cookie_poolq_id"]))
  50.         {
  51.             $pool1_que_css = "<div class='pool-question'>##QUESTION##</div>";
  52.             $pool1_ans_css = "<div class='pool-answer'>##ANSWER## <span class='pool-result'>(##RES##)</span></div>";
  53.             $res = '<div id="pool1">';
  54.             foreach ( $pool_question as $question )
  55.             {
  56.                 $poolq_id = $question->poolq_id;
  57.                 $poolq_question = stripslashes($question->poolq_question);
  58.                 $poolq_question = str_replace( "##QUESTION##" , $poolq_question, $pool1_que_css);
  59.                 $res = $res . $poolq_question;
  60.                 $sSql = "select poola_id, poola_answer, poola_vote from ". POOLONETABLEA ." where 1=1 and poolq_id = ". $poolq_id;
  61.                 $pool_answer = $wpdb->get_results($sSql);
  62.                 if ( ! empty($pool_answer) )
  63.                 {
  64.                     foreach ( $pool_answer as $answer )
  65.                     {
  66.                         $poola_id = $answer->poola_id;
  67.                         $poola_answer = stripslashes($answer->poola_answer);
  68.                         $poola_vote = $answer->poola_vote;
  69.                         $poola_answer = str_replace( "##ANSWER##" , $poola_answer, $pool1_ans_css);
  70.                         $poola_answer = str_replace( "##RES##" , $poola_vote, $poola_answer, $pool1_res_css);
  71.                         $res = $res . $poola_answer;
  72.                     }
  73.                 }
  74.             }
  75.             $res = $res . '</div>';
  76.             echo $res;
  77.         }
  78.         else
  79.         {
  80.             foreach ( $pool_question as $question )
  81.             {
  82.                 echo '<div id="pool1"><span id="pool1_msg"></span>';
  83.                 $poolq_id = $question->poolq_id;
  84.                 $poolq_question = $question->poolq_question;
  85.                 $poolq_question = str_replace( "##QUESTION##" , $poolq_question, $pool1_que_css);
  86.                 echo $poolq_question;
  87.                 $sSql = "select poola_id, poola_answer from ". POOLONETABLEA ." where 1=1 and poolq_id = ". $poolq_id;
  88.                 $pool_answer = $wpdb->get_results($sSql);
  89.                 if ( ! empty($pool_answer) )
  90.                 {
  91.                     foreach ( $pool_answer as $answer )
  92.                     {
  93.                         $poola_id = $answer->poola_id;
  94.                         $poola_answer = stripslashes($answer->poola_answer);
  95.                         $poola_answer = "<input type ='Radio' onClick='SetPool(".$poola_id.")' name = 'poolanswer' id = 'poolanswer' value= '".$poola_id."'> ".$poola_answer."<br />";
  96.                         $poola_answer = str_replace( "##ANSWER##" , $poola_answer, $pool1_ans_css);
  97.                         echo $poola_answer;
  98.                     }
  99.                 }
  100.                 echo '<input name="pool1_ans" id="pool1_ans" type="hidden" value="" />';
  101.                 $url = '"' . pool1_plugin_url('') . '"';
  102.                 $pool_button = "<a class='pool1_btn_cls' name='pool1_btn' id='pool1_btn' onClick='return pool1_ajx(".$url.")'>Голосовать! </a>";
  103.                 $pool_button = str_replace( "##BUTTON##" , $pool_button, $pool1_btn_css);
  104.                 echo "<a class='pool-btn' onClick='pool1_ajx_res(".$url.")'> Результаты </a>";
  105.                 echo $pool_button;
  106.                 echo '</div>';
  107.             }
  108.         }
  109.     }
  110.     else
  111.     {
  112.         echo '<div style="text-align:center;">No pool available</div>';
  113.     }
  114. }
  115.  
  116. /*Создаем базу данных*/
  117. function pool1_install()
  118. {
  119.     global $wpdb;
  120.     if($wpdb->get_var("show tables like '". POOLONETABLEQ . "'") != POOLONETABLEQ)
  121.     {
  122.         $sSql = "CREATE TABLE IF NOT EXISTS `". POOLONETABLEQ . "` (";
  123.         $sSql = $sSql . "`poolq_id` INT NOT NULL AUTO_INCREMENT ,";
  124.         $sSql = $sSql . "`poolq_question` VARCHAR( 1024 ) NOT NULL ,";
  125.         $sSql = $sSql . "`poolq_start` datetime NOT NULL default '0000-00-00 00:00:00' ,";
  126.         $sSql = $sSql . "`poolq_end` datetime NOT NULL default '0000-00-00 00:00:00' ,";
  127.         $sSql = $sSql . "`poolq_ext0` TEXT CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,";
  128.         $sSql = $sSql . "`poolq_ext1` VARCHAR( 200 ) NOT NULL ,";
  129.         $sSql = $sSql . "`poolq_ext2` VARCHAR( 200 ) NOT NULL ,";
  130.         $sSql = $sSql . "`poolq_ext3` VARCHAR( 200 ) NOT NULL ,";
  131.         $sSql = $sSql . "PRIMARY KEY ( `poolq_id` )";
  132.         $sSql = $sSql . ")";
  133.         $wpdb->query($sSql);
  134.        
  135.        
  136.         $qid = mysql_insert_id();
  137.        
  138.         $sSql = "CREATE TABLE IF NOT EXISTS `". POOLONETABLEA . "` (";
  139.         $sSql = $sSql . "`poola_id` INT NOT NULL AUTO_INCREMENT ,";
  140.         $sSql = $sSql . "`poola_answer` VARCHAR( 1024 ) NOT NULL ,";
  141.         $sSql = $sSql . "`poola_vote` INT NOT NULL default '0' ,";
  142.         $sSql = $sSql . "`poolq_id` INT NOT NULL ,";
  143.         $sSql = $sSql . "`poola_ext0` VARCHAR( 200 ) NOT NULL ,";
  144.         $sSql = $sSql . "`poola_ext1` VARCHAR( 200 ) NOT NULL ,";
  145.         $sSql = $sSql . "`poola_ext2` VARCHAR( 200 ) NOT NULL ,";
  146.         $sSql = $sSql . "`poola_ext3` VARCHAR( 200 ) NOT NULL ,";
  147.         $sSql = $sSql . "PRIMARY KEY ( `poola_id` )";
  148.         $sSql = $sSql . ")";
  149.         $wpdb->query($sSql);
  150.  
  151.     }
  152. }
  153.  
  154. /*Добавляем админку*/
  155. function pool1_admin_options()
  156. {
  157.     global $wpdb;
  158.     $current_page = isset($_GET['ac']) ? $_GET['ac'] : '';
  159.     switch($current_page)
  160.     {
  161.         case 'edit':
  162.             include('pages/content-management-edit.php');
  163.             break;
  164.         case 'add':
  165.             include('pages/content-management-add.php');
  166.             break;
  167.         case 'ans':
  168.             include('pages/content-management-ans.php');
  169.             break;
  170.         default:
  171.             include('pages/content-management-show.php');
  172.             break;
  173.     }
  174. }
  175.  
  176. /*Удаляем плагин*/
  177. function pool1_deactivation()
  178. {
  179.     // No action required.
  180. }
  181.  
  182. function pool1_add_javascript_files()
  183. {
  184.     if (!is_admin())
  185.     {
  186.         wp_enqueue_script( 'pool1_widget', pool1_plugin_url('js/widget.js'));
  187.         wp_enqueue_script( 'pool1_page', pool1_plugin_url('js/page.js'));
  188.     }  
  189. }
  190.  
  191. function pool1_add_to_menu()
  192. {
  193.     if (is_admin())
  194.     {
  195.         add_menu_page( __( 'Poll one', 'PollOne' ), __( 'Poll one', 'PollOne' ), 'administrator', 'PollOne', 'pool1_admin_options' );
  196.         add_submenu_page( 'PollOne', __( 'Poll One questions', 'PollOne' ), __( 'Poll Questions', 'PollOne' ),'administrator', 'PollOne', 'pool1_admin_options' );
  197.     }
  198. }
  199.  
  200. function pool1_widget($args)
  201. {
  202.     extract($args);
  203.     echo $before_widget . $before_title;
  204.     echo get_option('pool1_title');
  205.     echo $after_title;
  206.     poolone();
  207.     echo $after_widget;
  208. }
  209.  
  210.  
  211.  
  212. function pool1_init()
  213. {
  214.     if(function_exists('wp_register_sidebar_widget'))
  215.     {
  216.         wp_register_sidebar_widget('Pool one wp plugin', 'Poll one wp plugin', 'pool1_widget');
  217.     }
  218.    
  219.     if(function_exists('wp_register_widget_control'))
  220.     {
  221.         wp_register_widget_control('Pool one wp plugin', array('Poll one wp plugin', 'widgets'), 'pool1_control', 'width=750');
  222.     }
  223. }
  224. wp_enqueue_style ( 'pool1_style', pool1_plugin_url('/poolstyle.css'));
  225. add_action('admin_menu', 'pool1_add_to_menu');
  226. add_action("plugins_loaded", "pool1_init");
  227. register_activation_hook(__FILE__, 'pool1_install');
  228. register_deactivation_hook(__FILE__, 'pool1_deactivation');
  229. add_action('wp_enqueue_scripts', 'pool1_add_javascript_files');
  230. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement