Advertisement
Guest User

Core File for Plugin

a guest
Jun 29th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Core class for handling functionality requried throught the plugin.
  5. *
  6. * @author Iain Cambridge
  7. * @copyright Fubra Limited 2010-2011 (c)
  8. * @license http://www.gnu.org/licenses/gpl.html GPL v3
  9. */
  10.  
  11. class Wpsqt_Core {
  12.  
  13. protected $_pages = array();
  14.  
  15. /**
  16. * Adds the generic hooks that are required throughout
  17. * the plugin.
  18. *
  19. * @since 2.0
  20. */
  21.  
  22. public function __construct(){
  23.  
  24. $this->_addPage(WPSQT_PAGE_MAIN, "WPSQT", "WPSQT", "wpsqt-manage", "Main")
  25. ->_addPage(WPSQT_PAGE_MAIN.'&type=quiz', "Quizzes", "Quizzes", "wpsqt-manage", "Quizzes", WPSQT_PAGE_MAIN)
  26. ->_addPage(WPSQT_PAGE_MAIN.'&type=survey', "Surveys", "Surveys", "wpsqt-manage", "Surveys", WPSQT_PAGE_MAIN)
  27. ->_addPage(WPSQT_PAGE_MAIN.'&type=poll', "Polls", "Polls", "wpsqt-manage", "Polls", WPSQT_PAGE_MAIN)
  28. ->_addPage(WPSQT_PAGE_OPTIONS, "Options", "Options", "wpsqt-manage", "Options", WPSQT_PAGE_MAIN)
  29. ->_addPage(WPSQT_PAGE_MAINTENANCE, 'Maintenance', 'Maintenance', 'wpsqt-manage', 'Maintenance', WPSQT_PAGE_MAIN)
  30. ->_addPage(WPSQT_PAGE_HELP, "Help", "Help", "wpsqt-manage", "Help",WPSQT_PAGE_MAIN);
  31.  
  32. add_action("init",array($this, "create_nonce" ) );
  33. add_action("wp_footer",array($this,"show_footer"));
  34.  
  35. add_shortcode( 'wpsqt_quiz' , array($this, 'shortcode_quiz') );
  36. add_shortcode( 'wpsqt_survey' , array($this, 'shortcode_survey') );
  37. add_shortcode( 'wpsqt' , array($this, 'shortcode') );
  38. add_shortcode( 'wpsqt_results', array($this, 'shortcode_results') );
  39. add_shortcode( 'wpsqt_survey_results', array($this, 'shortcode_survey_results') );
  40.  
  41. add_action('init', array($this,"init"));
  42. add_action('admin_bar_menu', array($this,"adminbar"),999);
  43. add_action( 'init' , array($this,"enqueue_files"));
  44.  
  45. // Register the top scores widget
  46. require_once WPSQT_DIR.'lib/Wpsqt/Widget.php';
  47. add_action( 'widgets_init', create_function('', 'return register_widget("Wpsqt_Top_Widget");') );
  48. }
  49.  
  50.  
  51. /**
  52. * Quick, easy and neat way to add new pages to
  53. * the plugin without having to edit multiple files
  54. * in multiple places.
  55. *
  56. * @param string $id the page identifier
  57. * @param string $title The menu title
  58. * @param string $pageTitle The Page title.
  59. * @param string $cap The capaiblity required to access the menu item.
  60. * @param string $module The Module that relates to the class that holds the logic for the page.
  61. * @param string|null $parent Parent identifier, if null it is a parent.
  62. * @since 2.0
  63. * @return Wpsqt_Core
  64. */
  65.  
  66. protected function _addPage($id,$title,$pageTitle,$cap,$module,$parent = null){
  67.  
  68. $this->_pages[$id] = array("title" => $title,
  69. "page_title" => $pageTitle,
  70. "capability" => $cap,
  71. "module" => $module,
  72. "parent" => $parent);
  73.  
  74. return $this;
  75.  
  76. }
  77.  
  78. /**
  79. * Hook to allow people to extend the plugin
  80. * using filter to interact with the object.
  81. *
  82. * @since 2.0
  83. */
  84.  
  85. public function init(){
  86.  
  87. apply_filters("wpsqt_init",$this);
  88.  
  89. if ( isset($_SESSION['wpsqt']) ) {
  90. unset($_SESSION['wpsqt']['current_message']);
  91. }
  92. }
  93.  
  94. /**
  95. * Adds the WPSQT Menu items to the admin bar.
  96. * Because we're cool like that.
  97. *
  98. * @param WP_Admin_bar $wp_admin_bar
  99. */
  100.  
  101. public function adminbar( $wp_admin_bar) {
  102.  
  103. if ( current_user_can("manage_options") ) {
  104. foreach ( $this->_pages as $pagevar => $page ){
  105. $wp_admin_bar->add_menu( array( 'title' => $page['title'], 'href' => admin_url('admin.php?page='.$pagevar), 'id' => $pagevar, 'parent' => $page['parent']));
  106. }
  107.  
  108. }
  109. }
  110.  
  111. /**
  112. * Creates the current nonce and checks to see if a nonce field
  113. * has been sent and if so if it is valid.
  114. *
  115. * @since 2.0
  116. */
  117.  
  118. public function create_nonce(){
  119.  
  120. if ( isset($_REQUEST["wpsqt_nonce"]) ){
  121. $validNonce = wp_verify_nonce($_REQUEST["wpsqt_nonce"],'wpsqt_nonce');
  122. } else {
  123. $validNonce = false;
  124. }
  125.  
  126. define( "WPSQT_NONCE_VALID" , $validNonce );
  127. define( "WPSQT_NONCE_CURRENT" , wp_create_nonce('wpsqt_nonce') );
  128.  
  129. }
  130.  
  131. /**
  132. * Checks to see if their is a valid nonce if not
  133. * then calls wp_die();
  134. *
  135. * @since 2.0
  136. */
  137.  
  138. public static function validNonce(){
  139.  
  140. if ( WPSQT_NONCE_VALID != true ){
  141. wp_die("Invalid nonce field, either your session has timed out or someone has tried to trick you with a cross site request.");
  142. }
  143.  
  144. }
  145.  
  146. /**
  147. * Checks to see if the a custom page view exists
  148. * and if so it uses that. Checks the directory of
  149. * the quiz or survey's custom pages. If no file
  150. * exists checks the shared custom directory else
  151. * it returns the location of the default page view.
  152. *
  153. * Note: All plugin page views can be replaced using
  154. * this functionality.
  155. *
  156. * @param string $file the location of the page view file.
  157. * @since 2.0
  158. * @return string the location of the page view.
  159. */
  160.  
  161. public static function pageView($file){
  162.  
  163. global $blog_id;
  164.  
  165. $quizPath = ( isset($_SESSION['wpsqt']['item_id'])
  166. && ctype_digit($_SESSION['wpsqt']['item_id']) ) ?
  167. $blog_id.'/'.$_SESSION['wpsqt']['current_type'].'-'.$_SESSION['wpsqt']['item_id'].'/' : '';
  168.  
  169. if ( file_exists(WPSQT_DIR.'pages/custom/'.$quizPath.$file) ){
  170. return WPSQT_DIR.'pages/custom/'.$quizPath.$file;
  171. } elseif (file_exists(WPSQT_DIR.'pages/custom/'.$blog_id.'/shared/'.$file)) {
  172. return WPSQT_DIR.'pages/custom/'.$blog_id.'/shared/'.$file;
  173. }
  174. return WPSQT_DIR.'pages/'.$file;
  175.  
  176. }
  177.  
  178. /**
  179. * Gets the number of page based eon the number
  180. * of items there are and the number of items
  181. * per page.
  182. *
  183. * @param integer $numberOfItems the total number of items.
  184. * @param integer $itemsPerPage the number of items we want on a page.
  185. * @return integer Returns the number of pages required to display $numberOfItems with $itemsPerPages per page
  186. * @since 2.0
  187. */
  188.  
  189. public static function getPaginationCount($numberOfItems,$itemsPerPage){
  190.  
  191. if ( $numberOfItems > 0 ){
  192. $numberOfPages = intval( $numberOfItems / $itemsPerPage );
  193.  
  194. if ( $numberOfItems % $itemsPerPage ){
  195. $numberOfPages++;
  196. }
  197. } else {
  198. $numberOfPages = 0;
  199. }
  200.  
  201. return $numberOfPages;
  202. }
  203.  
  204. /**
  205. * Generates a usable Uri for adding new get variables. Allows
  206. * for easy page links. Excludes certain get variables from the
  207. * uri if they are present in the $exclude array.
  208. *
  209. * @param array $exclude the items which we want to exclude from the uri.
  210. * @since 2.0
  211. */
  212.  
  213. public static function generateUri( array $exclude = array() ){
  214.  
  215. $returnString = $_SERVER['PHP_SELF'].'?';
  216. if ( !empty($_GET) ){
  217. foreach ( $_GET as $varName => $varValue ){
  218. if ( !in_array($varName, $exclude) ){
  219. $returnString .= $varName.'='.$varValue.'&';
  220. }
  221. }
  222. }
  223.  
  224. return $returnString;
  225. }
  226.  
  227. /**
  228. * Generates the pagination links. Shows 5 links in total. Two on either side
  229. * if possible.
  230. *
  231. * @return $returnString the html with the links.
  232. * @since 2.0
  233. */
  234.  
  235. public static function getPaginationLinks($currentPage,$numberOfPages){
  236.  
  237. $returnString = '';
  238. $pageUri = self::generateUri( array('pageno') );
  239.  
  240. for($i = 1; $i <= $numberOfPages; $i ++) {
  241. if ($i == $currentPage) {
  242. $returnString .= ' <a href="'.$pageUri.'pageno='.$i.'" class="page-numbers current">'.$i.'</a>';
  243. } else {
  244. $returnString .= ' <a href="'.$pageUri.'pageno='.$i.'" class="page-numbers">'.$i.'</a>';
  245. }
  246. }
  247.  
  248. return $returnString;
  249. }
  250.  
  251. /**
  252. * Returns the integer value of $_GET['pageno'] while ensuring
  253. * that it is set and is a number. Otherwise returns 1.
  254. *
  255. * @since 2.0
  256. */
  257.  
  258. public static function getCurrentPageNumber(){
  259.  
  260. if ( isset($_GET['pageno']) && ctype_digit($_GET['pageno']) ){
  261. $pageNumber = (int)$_GET['pageno'];
  262. }
  263. else{
  264. $pageNumber = 1;
  265. }
  266.  
  267. return $pageNumber;
  268.  
  269. }
  270.  
  271. /**
  272. * Adds the CatN's link to the footer if the
  273. * user agrees to it.
  274. *
  275. * @since 2.0
  276. */
  277.  
  278. public function show_footer(){
  279.  
  280. echo '<!-- Survey and Quizzes Powered by WP Survey And Quiz Tool '.WPSQT_VERSION.' iain.cambridge at fubra.com -->';
  281.  
  282. if ( get_option('wpsqt_support_us') == 'yes'){
  283. echo '<p style="text-align: center;"><a href="http://catn.com/">Get Cloud PHP Hosting on CatN</a></p>';
  284. }
  285.  
  286. }
  287.  
  288. /**
  289. * Returns the possible locations of class files like Wpsqt_Page and
  290. * Wpsqt_Question child classes.
  291. *
  292. * @param string $className The name of the class that is to be included.
  293. * @since 2.0
  294. */
  295.  
  296. public static function getObject($className , $fatal = true){
  297.  
  298. $possibleLocations = array();
  299. $possibleLocations[] = WPSQT_DIR.'lib/';
  300. $possibleLocations = apply_filters('wpsqt_plugin_locations',$possibleLocations);
  301.  
  302. foreach ( $possibleLocations as $locus ){
  303.  
  304. $location = $locus.str_replace(" ", "_", str_replace("_","/",$className)).".php";
  305.  
  306. if ( file_exists($location) ){
  307. require_once $location;
  308. break;
  309. }
  310. }
  311.  
  312. if ( !class_exists($className) ){
  313. if ( $fatal === true ){
  314. wp_die("No such ".$className." class");
  315. } else {
  316. return false;
  317. }
  318. }
  319.  
  320. $object = new $className();
  321.  
  322. return $object;
  323. }
  324.  
  325.  
  326. public function shortcode_survey( $atts ){
  327.  
  328. if ( empty($atts) ){
  329. return;
  330. }
  331.  
  332. extract( shortcode_atts( array(
  333. 'name' => false
  334. ), $atts) );
  335.  
  336. return $this->_shortcode($name, 'survey');
  337.  
  338. }
  339.  
  340. public function shortcode_quiz( $atts ) {
  341.  
  342. if ( empty($atts) ){
  343. return;
  344. }
  345.  
  346. extract( shortcode_atts( array(
  347. 'name' => false
  348. ), $atts) );
  349.  
  350. return $this->_shortcode($name, 'quiz');
  351.  
  352. }
  353.  
  354.  
  355. /**
  356. * Method for new shortcode that will allow for type handler option.
  357. *
  358. * @param array $atts
  359. * @since 2.2.2
  360. */
  361. public function shortcode($atts) {
  362. if (empty($atts)) {
  363. return;
  364. }
  365.  
  366. extract( shortcode_atts( array(
  367. 'name' => false,
  368. 'type' => false
  369. ), $atts) );
  370.  
  371. return $this->_shortcode($name, $type);
  372. }
  373.  
  374. /**
  375. * DRY method to show return the quizzes and surveys in the correct location.
  376. *
  377. * @param string $identifer The name or numerical id of the quiz/survey
  378. * @param string $type If it is a quiz or a survey.
  379. * @since 2.2.2
  380. */
  381. protected function _shortcode($identifer,$type) {
  382. ob_start();
  383.  
  384. require_once WPSQT_DIR.'lib/Wpsqt/Shortcode.php';
  385. $objShortcode = new Wpsqt_Shortcode($identifer, $type);
  386. $objShortcode->display();
  387.  
  388. $content = ob_get_contents();
  389. ob_end_clean();
  390.  
  391. return $content;
  392. }
  393.  
  394. public function shortcode_results( $atts ) {
  395. global $wpdb;
  396. extract( shortcode_atts( array(
  397. 'username' => false,
  398. 'accepted' => false
  399. ), $atts) );
  400. if ($username != false) {
  401. $sql = 'SELECT * FROM `'.WPSQT_TABLE_RESULTS.'` WHERE `person_name` = "'.$username.'"';
  402. if ($accepted != false)
  403. $sql .= 'AND `status` = "Accepted"';
  404. $return = $wpdb->get_results($sql, 'ARRAY_A');
  405. if (empty($return))
  406. echo '<p>'.$username.' has no results</p>';
  407. foreach ($return as $result) {
  408. $sql = 'SELECT * FROM `'.WPSQT_TABLE_QUIZ_SURVEYS.'` WHERE `id` = "'.$result['item_id'].'"';
  409. $return = $wpdb->get_results($sql, 'ARRAY_A');
  410. if (empty($return)) {
  411. continue;
  412. }
  413. return "<h3>Results for ".$return[0]['name']."</h3>".
  414. "<p>Score: ".$result['score']."/".$result['total']."</p>".
  415. "<p>Percentage: ".$result['percentage']."</p>".
  416. "<br /><br />";
  417. }
  418. } else {
  419. return 'No username was supplied for this results page. The shortcode should look like [wpsqt_results username="admin"]';
  420. }
  421. }
  422.  
  423. public function shortcode_survey_results( $atts ) {
  424. global $wpdb;
  425. extract( shortcode_atts( array(
  426. 'name' => false
  427. ), $atts) );
  428. if ($name == false) {
  429. echo 'No survey name was supplied.';
  430. } else {
  431. echo 'Results for '.$name;
  432.  
  433. // Get the ID
  434. $surveyId = $wpdb->get_row("SELECT `id` FROM `".WPSQT_TABLE_QUIZ_SURVEYS."` WHERE `name` = '".$name."'", ARRAY_A);
  435. $surveyId = (int) $surveyId['id'];
  436.  
  437. // Get results
  438. $result = $wpdb->get_row("SELECT * FROM `".WPSQT_TABLE_SURVEY_CACHE."` WHERE item_id = '".$surveyId."'", ARRAY_A);
  439. $sections = unserialize($result['sections']);
  440. foreach($sections[0]['questions'] as $questionKey => $question) {
  441. if ($question['type'] == 'Free Text') {
  442. $uncachedResult = $wpdb->get_results(
  443. $wpdb->prepare("SELECT * FROM `".WPSQT_TABLE_RESULTS."` WHERE item_id = %d",
  444. array($_GET['id'])), ARRAY_A
  445. );
  446. $uncachedresults = $uncachedResult;
  447. // Storing all the IDs for free text questions
  448. $freetextq[] = $questionKey;
  449. }
  450. }
  451. // Just reuse the same page view that the admin thing uses
  452. require_once WPSQT_DIR.'pages/admin/surveys/result.total.script.php';
  453. }
  454. }
  455.  
  456. public function enqueue_files() {
  457. wp_enqueue_script("jquery");
  458. wp_enqueue_style("wpsqt-main",plugins_url('/css/main.css',WPSQT_FILE));
  459. }
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement