/wp-content/plugins/exec-php/includes/option.php

https://gitlab.com/Gashler/sg · PHP · 171 lines · 113 code · 27 blank · 31 comment · 26 complexity · 656c5db824e593bc34c7bfd181ea7587 MD5 · raw file

  1. <?php
  2. require_once(dirname(__FILE__).'/const.php');
  3. // -----------------------------------------------------------------------------
  4. // the ExecPhp_Option class handles the loading and storing of the
  5. // plugin options including all needed conversion routines during upgrade
  6. // -----------------------------------------------------------------------------
  7. if (!class_exists('ExecPhp_Option')) :
  8. define('ExecPhp_OPTION_VERSION', 'version');
  9. define('ExecPhp_OPTION_WIDGET_SUPPORT', 'widget_support');
  10. define('ExecPhp_OPTION_HAS_OLD_STYLE', 'exec-php_has_old_style');
  11. define('ExecPhp_OPTION_IGNORE_OLD_STYLE_WARNING', 'exec-php_ignore_old_style_warning');
  12. class ExecPhp_Option
  13. {
  14. var $m_status = ExecPhp_STATUS_UNINITIALIZED;
  15. var $m_version = ExecPhp_VERSION;
  16. // default option values will be set during load()
  17. var $m_widget_support = true;
  18. // ---------------------------------------------------------------------------
  19. // init
  20. // ---------------------------------------------------------------------------
  21. function ExecPhp_Option()
  22. {
  23. $this->m_status = $this->upgrade();
  24. }
  25. // ---------------------------------------------------------------------------
  26. // option handling
  27. // ---------------------------------------------------------------------------
  28. // Upgrades plugin from previous versions or even installs it
  29. function upgrade()
  30. {
  31. $old_version = $this->detect_plugin_version();
  32. while ($old_version != ExecPhp_VERSION)
  33. {
  34. $this->load();
  35. if (version_compare($old_version, '4.0.dev') < 0)
  36. {
  37. $this->upgrade_to_4_0();
  38. $old_version = '4.0';
  39. }
  40. else if (version_compare($old_version, '4.1.dev') < 0)
  41. $old_version = '4.1';
  42. else if (version_compare($old_version, '4.2.dev') < 0)
  43. {
  44. $this->upgrade_to_4_2();
  45. $old_version = '4.2';
  46. }
  47. else if (version_compare($old_version, '4.3.dev') < 0)
  48. $old_version = '4.3';
  49. else if (version_compare($old_version, '4.4.dev') < 0)
  50. $old_version = '4.4';
  51. else if (version_compare($old_version, '4.5.dev') < 0)
  52. $old_version = '4.5';
  53. else if (version_compare($old_version, '4.6.dev') < 0)
  54. $old_version = '4.6';
  55. else if (version_compare($old_version, '4.7.dev') < 0)
  56. $old_version = '4.7';
  57. else if (version_compare($old_version, '4.8.dev') < 0)
  58. $old_version = '4.8';
  59. else if (version_compare($old_version, '4.9.dev') < 0)
  60. $old_version = '4.9';
  61. else
  62. // we are downgrading to an older version of the plugin by
  63. // resetting the version to 0 and walking up the conversion path
  64. $old_version = '0';
  65. $this->m_version = $old_version;
  66. $this->save();
  67. }
  68. $this->load();
  69. return ExecPhp_STATUS_OKAY;
  70. }
  71. function save()
  72. {
  73. // introduced in 4.0
  74. $option[ExecPhp_OPTION_VERSION] = $this->m_version;
  75. // introduced in 4.0
  76. $option[ExecPhp_OPTION_WIDGET_SUPPORT] = $this->m_widget_support;
  77. update_option(ExecPhp_PLUGIN_ID, $option);
  78. }
  79. function load()
  80. {
  81. $option = get_option(ExecPhp_PLUGIN_ID);
  82. // introduced in 4.0
  83. if (isset($option[ExecPhp_OPTION_WIDGET_SUPPORT]))
  84. $this->m_widget_support = $option[ExecPhp_OPTION_WIDGET_SUPPORT];
  85. else
  86. $this->m_widget_support = true;
  87. }
  88. // ---------------------------------------------------------------------------
  89. // tools
  90. // ---------------------------------------------------------------------------
  91. function detect_plugin_version()
  92. {
  93. $option = get_option(ExecPhp_PLUGIN_ID);
  94. if ($option === false)
  95. $version = '0';
  96. else
  97. $version = $option[ExecPhp_OPTION_VERSION];
  98. return $version;
  99. }
  100. function upgrade_to_4_0()
  101. {
  102. // this is first installation of the plugin or upgrade from a version
  103. // prior to 4.0;
  104. // still needed for deletion from the database - these are obsolete
  105. // since version 3.1
  106. delete_option(ExecPhp_OPTION_HAS_OLD_STYLE);
  107. delete_option(ExecPhp_OPTION_IGNORE_OLD_STYLE_WARNING);
  108. // be sure standard roles are available, these may be deleted or
  109. // renamed by the blog administrator
  110. $role = get_role('administrator');
  111. if ($role !== NULL)
  112. $role->add_cap(ExecPhp_CAPABILITY_EXECUTE_ARTICLES);
  113. }
  114. function upgrade_to_4_2()
  115. {
  116. // be sure standard roles are available, these may be deleted or
  117. // renamed by the blog administrator
  118. $role = get_role('administrator');
  119. if ($role !== NULL)
  120. $role->add_cap(ExecPhp_CAPABILITY_EDIT_OTHERS_PHP);
  121. }
  122. // ---------------------------------------------------------------------------
  123. // access
  124. // ---------------------------------------------------------------------------
  125. function set_from_POST()
  126. {
  127. $this->m_widget_support
  128. = isset($_POST[ExecPhp_POST_WIDGET_SUPPORT]);
  129. }
  130. function get_status()
  131. {
  132. return $this->m_status;
  133. }
  134. function get_version()
  135. {
  136. return $this->m_version;
  137. }
  138. function get_widget_support()
  139. {
  140. return $this->m_widget_support;
  141. }
  142. }
  143. endif;
  144. ?>