base-admin.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderBaseAdmin extends RevSliderBase {
  9. protected static $master_view;
  10. protected static $view;
  11. private static $arrSettings = array();
  12. private static $arrMenuPages = array();
  13. private static $arrSubMenuPages = array();
  14. private static $tempVars = array();
  15. private static $startupError = '';
  16. private static $menuRole = 'admin';
  17. private static $arrMetaBoxes = array(); //option boxes that will be added to post
  18. private static $allowed_views = array('master-view', 'system/validation', 'system/dialog-video', 'system/dialog-update', 'system/dialog-global-settings', 'sliders', 'slider', 'slider_template', 'slides', 'slide', 'navigation-editor', 'slide-editor', 'slide-overview', 'slide-editor', 'slider-overview', 'themepunch-google-fonts', 'global-settings');
  19. /**
  20. *
  21. * main constructor
  22. */
  23. public function __construct($t){
  24. parent::__construct($t);
  25. //set view
  26. self::$view = self::getGetVar("view");
  27. if(empty(self::$view))
  28. self::$view = 'sliders';
  29. //add internal hook for adding a menu in arrMenus
  30. add_action('admin_menu', array('RevSliderBaseAdmin', 'addAdminMenu'));
  31. add_action('add_meta_boxes', array('RevSliderBaseAdmin', 'onAddMetaboxes'));
  32. add_action('save_post', array('RevSliderBaseAdmin', 'onSavePost'));
  33. //if not inside plugin don't continue
  34. if($this->isInsidePlugin() == true){
  35. add_action('admin_enqueue_scripts', array('RevSliderBaseAdmin', 'addCommonScripts'));
  36. add_action('admin_enqueue_scripts', array('RevSliderAdmin', 'onAddScripts'));
  37. }else{
  38. add_action('admin_enqueue_scripts', array('RevSliderBaseAdmin', 'addGlobalScripts'));
  39. }
  40. //a must event for any admin. call onActivate function.
  41. $this->addEvent_onActivate();
  42. $this->addAction_onActivate();
  43. self::addActionAjax('show_image', 'onShowImage');
  44. }
  45. /**
  46. *
  47. * add some meta box
  48. * return metabox handle
  49. */
  50. public static function addMetaBox($title,$content = null, $customDrawFunction = null,$location="post"){
  51. $box = array();
  52. $box['title'] = $title;
  53. $box['location'] = $location;
  54. $box['content'] = $content;
  55. $box['draw_function'] = $customDrawFunction;
  56. self::$arrMetaBoxes[] = $box;
  57. }
  58. /**
  59. *
  60. * on add metaboxes
  61. */
  62. public static function onAddMetaboxes(){
  63. foreach(self::$arrMetaBoxes as $index=>$box){
  64. $title = $box['title'];
  65. $location = $box['location'];
  66. $boxID = 'mymetabox_revslider_'.$index;
  67. $function = array(self::$t, "onAddMetaBoxContent");
  68. if(is_array($location)){
  69. foreach($location as $loc)
  70. add_meta_box($boxID,$title,$function,$loc,'normal','default');
  71. }else
  72. add_meta_box($boxID,$title,$function,$location,'normal','default');
  73. }
  74. }
  75. /**
  76. *
  77. * on save post meta. Update metaboxes data from post, add it to the post meta
  78. */
  79. public static function onSavePost(){
  80. //protection against autosave
  81. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
  82. $postID = RevSliderFunctions::getPostVariable("ID");
  83. return $postID;
  84. }
  85. $postID = RevSliderFunctions::getPostVariable("ID");
  86. if(empty($postID))
  87. return(false);
  88. foreach(self::$arrMetaBoxes as $box){
  89. $arrSettingNames = array('slide_template');
  90. foreach($arrSettingNames as $name){
  91. $value = RevSliderFunctions::getPostVariable($name);
  92. update_post_meta( $postID, $name, $value );
  93. } //end foreach settings
  94. } //end foreach meta
  95. }
  96. /**
  97. *
  98. * on add metabox content
  99. */
  100. public static function onAddMetaBoxContent($post,$boxData){
  101. $postID = $post->ID;
  102. $boxID = RevSliderFunctions::getVal($boxData, "id");
  103. $index = str_replace('mymetabox_revslider_',"",$boxID);
  104. $arrMetabox = self::$arrMetaBoxes[$index];
  105. //draw element
  106. $drawFunction = RevSliderFunctions::getVal($arrMetabox, "draw_function");
  107. if(!empty($drawFunction))
  108. call_user_func($drawFunction);
  109. }
  110. /**
  111. *
  112. * set the menu role - for viewing menus
  113. */
  114. public static function setMenuRole($menuRole){
  115. self::$menuRole = $menuRole;
  116. }
  117. /**
  118. * get the menu role - for viewing menus
  119. */
  120. public static function getMenuRole(){
  121. return self::$menuRole;
  122. }
  123. /**
  124. *
  125. * set startup error to be shown in master view
  126. */
  127. public static function setStartupError($errorMessage){
  128. self::$startupError = $errorMessage;
  129. }
  130. /**
  131. *
  132. * tells if the the current plugin opened is this plugin or not
  133. * in the admin side.
  134. */
  135. private function isInsidePlugin(){
  136. $page = self::getGetVar("page");
  137. if($page == 'revslider' || $page == 'themepunch-google-fonts' || $page == 'revslider_navigation' || $page == 'revslider_global_settings')
  138. return(true);
  139. return(false);
  140. }
  141. /**
  142. * add global used scripts
  143. * @since: 5.1.1
  144. */
  145. public static function addGlobalScripts(){
  146. wp_enqueue_script(array('jquery', 'jquery-ui-core', 'jquery-ui-sortable', 'wpdialogs'));
  147. wp_enqueue_style(array('wp-jquery-ui', 'wp-jquery-ui-dialog', 'wp-jquery-ui-core'));
  148. }
  149. /**
  150. * add common used scripts
  151. */
  152. public static function addCommonScripts(){
  153. if(function_exists("wp_enqueue_media"))
  154. wp_enqueue_media();
  155. wp_enqueue_script(array('jquery', 'jquery-ui-core', 'jquery-ui-mouse', 'jquery-ui-accordion', 'jquery-ui-datepicker', 'jquery-ui-dialog', 'jquery-ui-slider', 'jquery-ui-autocomplete', 'jquery-ui-sortable', 'jquery-ui-droppable', 'jquery-ui-tabs', 'jquery-ui-widget', 'wp-color-picker'));
  156. wp_enqueue_style(array('wp-jquery-ui', 'wp-jquery-ui-core', 'wp-jquery-ui-dialog', 'wp-color-picker'));
  157. wp_enqueue_script('unite_settings', RS_PLUGIN_URL .'admin/assets/js/settings.js', array(), RevSliderGlobals::SLIDER_REVISION );
  158. wp_enqueue_script('unite_admin', RS_PLUGIN_URL .'admin/assets/js/admin.js', array(), RevSliderGlobals::SLIDER_REVISION );
  159. wp_enqueue_style('unite_admin', RS_PLUGIN_URL .'admin/assets/css/admin.css', array(), RevSliderGlobals::SLIDER_REVISION);
  160. //add tipsy
  161. wp_enqueue_script('tipsy', RS_PLUGIN_URL .'admin/assets/js/jquery.tipsy.js', array(), RevSliderGlobals::SLIDER_REVISION );
  162. wp_enqueue_style('tipsy', RS_PLUGIN_URL .'admin/assets/css/tipsy.css', array(), RevSliderGlobals::SLIDER_REVISION);
  163. //include codemirror
  164. wp_enqueue_script('codemirror_js', RS_PLUGIN_URL .'admin/assets/js/codemirror/codemirror.js', array(), RevSliderGlobals::SLIDER_REVISION );
  165. wp_enqueue_script('codemirror_js_highlight', RS_PLUGIN_URL .'admin/assets/js/codemirror/util/match-highlighter.js', array(), RevSliderGlobals::SLIDER_REVISION );
  166. wp_enqueue_script('codemirror_js_searchcursor', RS_PLUGIN_URL .'admin/assets/js/codemirror/util/searchcursor.js', array(), RevSliderGlobals::SLIDER_REVISION );
  167. wp_enqueue_script('codemirror_js_css', RS_PLUGIN_URL .'admin/assets/js/codemirror/css.js', array(), RevSliderGlobals::SLIDER_REVISION );
  168. wp_enqueue_script('codemirror_js_html', RS_PLUGIN_URL .'admin/assets/js/codemirror/xml.js', array(), RevSliderGlobals::SLIDER_REVISION );
  169. wp_enqueue_style('codemirror_css', RS_PLUGIN_URL .'admin/assets/js/codemirror/codemirror.css', array(), RevSliderGlobals::SLIDER_REVISION);
  170. }
  171. /**
  172. *
  173. * admin pages parent, includes all the admin files by default
  174. */
  175. public static function adminPages(){
  176. //self::validateAdminPermissions();
  177. }
  178. /**
  179. *
  180. * validate permission that the user is admin, and can manage options.
  181. */
  182. protected static function isAdminPermissions(){
  183. if( is_admin() && current_user_can("manage_options") )
  184. return(true);
  185. return(false);
  186. }
  187. /**
  188. *
  189. * validate admin permissions, if no pemissions - exit
  190. */
  191. protected static function validateAdminPermissions(){
  192. if(!self::isAdminPermissions()){
  193. echo "access denied";
  194. return(false);
  195. }
  196. }
  197. /**
  198. *
  199. * set view that will be the master
  200. */
  201. protected static function setMasterView($masterView){
  202. self::$master_view = $masterView;
  203. }
  204. /**
  205. *
  206. * inlcude some view file
  207. */
  208. protected static function requireView($view){
  209. try{
  210. //require master view file, and
  211. if(!empty(self::$master_view) && !isset(self::$tempVars["is_masterView"]) ){
  212. $masterViewFilepath = self::$path_views.self::$master_view.".php";
  213. RevSliderFunctions::validateFilepath($masterViewFilepath,"Master View");
  214. self::$tempVars["is_masterView"] = true;
  215. require $masterViewFilepath;
  216. }else{ //simple require the view file.
  217. if(!in_array($view, self::$allowed_views)) UniteFunctionsRev::throwError(__('Wrong Request', 'revslider'));
  218. switch($view){ //switch URLs to corresponding php files
  219. case 'slide':
  220. $view = 'slide-editor';
  221. break;
  222. case 'slider':
  223. $view = 'slider-editor';
  224. break;
  225. case 'sliders':
  226. $view = 'slider-overview';
  227. break;
  228. case 'slides':
  229. $view = 'slide-overview';
  230. break;
  231. }
  232. $viewFilepath = self::$path_views.$view.".php";
  233. RevSliderFunctions::validateFilepath($viewFilepath,"View");
  234. require $viewFilepath;
  235. }
  236. }catch (Exception $e){
  237. echo "<br><br>View (".esc_attr($view).") Error: <b>".esc_attr($e->getMessage())."</b>";
  238. }
  239. }
  240. /**
  241. * require some template from "templates" folder
  242. */
  243. protected static function getPathTemplate($templateName){
  244. $pathTemplate = self::$path_templates.$templateName.'.php';
  245. RevSliderFunctions::validateFilepath($pathTemplate,'Template');
  246. return($pathTemplate);
  247. }
  248. /**
  249. *
  250. * add all js and css needed for media upload
  251. */
  252. protected static function addMediaUploadIncludes(){
  253. wp_enqueue_script('thickbox');
  254. wp_enqueue_script('media-upload');
  255. wp_enqueue_style('thickbox');
  256. }
  257. /**
  258. * add admin menus from the list.
  259. */
  260. public static function addAdminMenu(){
  261. global $revslider_screens;
  262. $role = "manage_options";
  263. switch(self::$menuRole){
  264. case 'author':
  265. $role = "edit_published_posts";
  266. break;
  267. case 'editor':
  268. $role = "edit_pages";
  269. break;
  270. default:
  271. case 'admin':
  272. $role = "manage_options";
  273. break;
  274. }
  275. foreach(self::$arrMenuPages as $menu){
  276. $title = $menu["title"];
  277. $pageFunctionName = $menu["pageFunction"];
  278. $revslider_screens[] = add_menu_page( $title, $title, $role, 'revslider', array(self::$t, $pageFunctionName), 'dashicons-update' );
  279. }
  280. foreach(self::$arrSubMenuPages as $menu){
  281. $title = $menu["title"];
  282. $pageFunctionName = $menu["pageFunction"];
  283. $pageSlug = $menu["pageSlug"];
  284. $revslider_screens[] = add_submenu_page( 'revslider', $title, $title, $role, $pageSlug, array(self::$t, $pageFunctionName) );
  285. }
  286. }
  287. /**
  288. *
  289. * add menu page
  290. */
  291. protected static function addMenuPage($title,$pageFunctionName){
  292. self::$arrMenuPages[] = array("title"=>$title,"pageFunction"=>$pageFunctionName);
  293. }
  294. /**
  295. *
  296. * add menu page
  297. */
  298. protected static function addSubMenuPage($title,$pageFunctionName,$pageSlug){
  299. self::$arrSubMenuPages[] = array("title"=>$title,"pageFunction"=>$pageFunctionName,"pageSlug"=>$pageSlug);
  300. }
  301. /**
  302. *
  303. * get url to some view.
  304. */
  305. public static function getViewUrl($viewName,$urlParams=""){
  306. $params = "&view=".$viewName;
  307. if(!empty($urlParams))
  308. $params .= "&".$urlParams;
  309. $link = admin_url( 'admin.php?page=revslider'.$params);
  310. return($link);
  311. }
  312. /**
  313. *
  314. * register the "onActivate" event
  315. */
  316. protected function addEvent_onActivate($eventFunc = "onActivate"){
  317. register_activation_hook( RS_PLUGIN_FILE_PATH, array(self::$t, $eventFunc) );
  318. }
  319. protected function addAction_onActivate(){
  320. register_activation_hook( RS_PLUGIN_FILE_PATH, array(self::$t, 'onActivateHook') );
  321. }
  322. public static function onActivateHook(){
  323. $options = array();
  324. $options = apply_filters('revslider_mod_activation_option', $options);
  325. $operations = new RevSliderOperations();
  326. $options_exist = $operations->getGeneralSettingsValues();
  327. if(!is_array($options_exist)) $options_exist = array();
  328. $options = array_merge($options_exist, $options);
  329. $operations->updateGeneralSettings($options);
  330. }
  331. /**
  332. *
  333. * store settings in the object
  334. */
  335. protected static function storeSettings($key,$settings){
  336. self::$arrSettings[$key] = $settings;
  337. }
  338. /**
  339. *
  340. * get settings object
  341. */
  342. protected static function getSettings($key){
  343. if(!isset(self::$arrSettings[$key]))
  344. RevSliderFunctions::throwError("Settings $key not found");
  345. $settings = self::$arrSettings[$key];
  346. return($settings);
  347. }
  348. /**
  349. *
  350. * add ajax back end callback, on some action to some function.
  351. */
  352. protected static function addActionAjax($ajaxAction,$eventFunction){
  353. add_action('wp_ajax_revslider_'.$ajaxAction, array('RevSliderAdmin', $eventFunction));
  354. }
  355. /**
  356. *
  357. * echo json ajax response
  358. */
  359. private static function ajaxResponse($success,$message,$arrData = null){
  360. $response = array();
  361. $response["success"] = $success;
  362. $response["message"] = $message;
  363. if(!empty($arrData)){
  364. if(gettype($arrData) == "string")
  365. $arrData = array("data"=>$arrData);
  366. $response = array_merge($response,$arrData);
  367. }
  368. $json = json_encode($response);
  369. echo $json;
  370. exit();
  371. }
  372. /**
  373. *
  374. * echo json ajax response, without message, only data
  375. */
  376. protected static function ajaxResponseData($arrData){
  377. if(gettype($arrData) == "string")
  378. $arrData = array("data"=>$arrData);
  379. self::ajaxResponse(true,"",$arrData);
  380. }
  381. /**
  382. *
  383. * echo json ajax response
  384. */
  385. protected static function ajaxResponseError($message,$arrData = null){
  386. self::ajaxResponse(false,$message,$arrData,true);
  387. }
  388. /**
  389. * echo ajax success response
  390. */
  391. protected static function ajaxResponseSuccess($message,$arrData = null){
  392. self::ajaxResponse(true,$message,$arrData,true);
  393. }
  394. /**
  395. * echo ajax success response
  396. */
  397. protected static function ajaxResponseSuccessRedirect($message,$url){
  398. $arrData = array("is_redirect"=>true,"redirect_url"=>$url);
  399. self::ajaxResponse(true,$message,$arrData,true);
  400. }
  401. }
  402. /**
  403. * old classname extends new one (old classnames will be obsolete soon)
  404. * @since: 5.0
  405. **/
  406. class UniteBaseAdminClassRev extends RevSliderBaseAdmin {}
  407. ?>