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