Completed
Push — master ( a85a3b...a9e055 )
by Justin
11:13
created

wpsc_theming::wpsc_theming()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 4
nop 0
1
<?php
2
function wpsc_move_theme_images(){
3
	$image_dir = WPSC_CORE_THEME_PATH.'wpsc-images';
4
	$end_location = trailingslashit( get_stylesheet_directory() ).'wpsc-images';
5
	@mkdir( $end_location );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using mkdir()
Loading history...
6
	$imgdr = opendir($image_dir);
7
	while ( false !== ( $file = readdir( $imgdr )) ) {
8
		@ copy( $image_dir . '/' . $file, $end_location . '/' . $file );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
9
	}
10
	closedir( $imgdr );
11
}
12
/**
13
 * WP eCommerce theme porting class
14
 *
15
 * This class is responsible for moving all of the core template files from the
16
 * plugin folder to the active theme folder on new installs.
17
 *
18
 * On upgrades, it is responsible for checking appropriate folders for themes,
19
 * converting and porting to the active theme folder.
20
 *
21
 * @package wp-e-commerce
22
 * @since 3.8
23
 */
24
class wpsc_theming {
0 ignored issues
show
Coding Style introduced by
Class name "wpsc_theming" is not in camel caps format
Loading history...
25
26
	var $active_wp_theme;
27
	var $active_wp_style;
28
	var $active_wpsc_theme;
29
	var $theme_file_prefix;
30
	var $templates_to_move;
31
	var $list_of_templates;
32
33
	/**
34
	 * wpsc_theming
35
	 *
36
	 * Construct
37
	 *
38
	 * @return
39
	 */
40
	public function __construct() {
41
		check_admin_referer( 'wpsc_copy_themes' );
42
43
		$this->active_wp_style   = trailingslashit( get_stylesheet_directory() );
44
		$this->templates_to_move = isset( $_POST['wpsc_templates_to_port'] ) ? $_POST['wpsc_templates_to_port'] : array();
45
		$this->list_of_templates = wpsc_list_product_templates( $this->active_wp_style );
46
		$this->theme_file_prefix = 'wpsc-';
47
48
		if ( $this->files_exist() ) {
49
			return;
50
		} else {
51
			$_SESSION['wpsc_themes_copied_results'] = array();
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
52
			// WP-WPSC theme doesn't exist, so let's figure out where we're porting from, either the plugin directory or the wpsc-themes directory
53
			$theme_location        = $this->theme_location();
54
			$this->active_wp_theme = trailingslashit( get_stylesheet_directory() );
55
56
			// Now that we have the theme location, let's copy it over to the themes directory and mod from there.
57
			$this->move_theme( $theme_location, $this->active_wp_style );
58
59
			// The rest of this is ported from the previous copy_theme function
60
			$_SESSION['wpsc_themes_copied'] = true;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
61
62
			$sendback = wp_get_referer();
63
			//$sendback = add_query_arg('tab', $_SESSION['wpsc_settings_curr_page'], remove_query_arg('tab', $sendback));
64
			wp_redirect( $sendback );
65
		}
66
	}
67
68
	/**
69
	 * files_exist()
70
	 *
71
	 * Checks to see which theme files exist in the current WP theme folder
72
	 * and which theme files that have been selected but have not been moved over
73
	 *
74
	 * @access public
75
	 * @since 3.8
76
	 * @param None
77
	 * @return true if no templates need to be moved or false if some templates do need to be moved
78
	 */
79
	function files_exist() {
80
81
		if( empty( $this->templates_to_move ) ) {
82
			$_SESSION['wpsc_theme_empty'] = true;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
83
			wp_redirect( admin_url('options-general.php?page=wpsc-settings&tab=presentation') );
84
85
		}
86
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
87
88
		$results = array_diff( $this->templates_to_move, $this->list_of_templates );
89
		$this->templates_to_move = $results;
90
		// If theme already exists, we're set, do nothing
91
		// This should also indicate a new install
92
		if ( count( $results ) == 0 )
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return count($results) == 0;.
Loading history...
93
			return true;
94
		else
95
			return false;
96
	}
97
98
	/*
99
	 * Determines the current theme location
100
	 * @access public
101
	 *
102
	 * @since 3.8
103
	 * @param None
104
	 * @return None
105
	 */
106
	function theme_location() {
107
108
		$selected_theme         = get_option( 'wpsc_selected_theme' );
109
		$active_wpsc_theme_path = WPSC_OLD_THEMES_PATH . $selected_theme;
110
111
		// Check if theme exists in uploads folder. If so, that's theme location.
112
		if ( file_exists( $active_wpsc_theme_path . '/functions.php' ) )
113
			$theme_location = $active_wpsc_theme_path;
114
115
		// If it's not there, the theme location will be the plugins folder.
116
		else
117
			$theme_location = WPSC_THEMES_PATH;
118
119
		return $theme_location;
120
	}
121
122
	function recursive_copy( $src, $dst ) {
123
124
		if ( $src != WPSC_THEMES_PATH )
125
			$theme_file_prefix = $this->theme_file_prefix;
126
		else
127
			$theme_file_prefix = '';
128
129
		$dir = opendir( $src );
130
		@mkdir( $dst );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using mkdir()
Loading history...
131
132
		while ( false !== ( $file = readdir( $dir )) ) {
133
			if ( in_array( $file, $this->templates_to_move ) ) {
134
				if ( is_dir( $src . '/' . $file ) )
135
					$this->recursive_copy( $src . '/' . $file, $dst . '/' . $file );
136
				else
137
					$_SESSION['wpsc_themes_copied_results'][] = @ copy( $src . '/' . $file, $dst . '/' . $theme_file_prefix . $file );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
138
			}
139
		}
140
		closedir( $dir );
141
	}
142
143
	/* Moves, renames, and appends header and footer functions to theme if they do not currently have it.
144
	 * @access public
145
	 *
146
	 * @since 3.8
147
	 * @param old - Current location of theme
148
	 * @param new -New location for theme
149
	 * @return None
150
	 */
151
	function move_theme( $old, $new ) {
152
153
		if ( $old != WPSC_THEMES_PATH )
154
			$theme_file_prefix = $this->theme_file_prefix;
155
		else
156
			$theme_file_prefix = '';
157
158
		$this->recursive_copy( $old, $new );
159
		$path = $new;
160
		$dh   = opendir( $old );
161
162
		while ( false !== ( $file = readdir( $dh ) ) ) {
163
			if ( $file != "." && $file != ".." && !strstr( $file, ".svn" ) && !strstr( $file, "images" ) && ( strstr( $file, 'wpsc-' ) || strstr($file, '.css') ) ) {
164
				if('wpsc-default.css' == $file)
165
					wpsc_move_theme_images();
166
				if ( in_array( $file, $this->templates_to_move ) ) {
167
					if ( !strstr( $file, "functions" ) && !strstr( $file, 'widget' ) ) {
168
						$file_data = file_get_contents( $old . "/" . $file );
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
169
						$_SESSION['wpsc_themes_copied_results'][] = @file_put_contents( $path . "/" . $file, $file_data );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using file_put_contents()
Loading history...
170
						rename( $path . "/" . $file, $path . "/" . $theme_file_prefix . $file );
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using rename()
Loading history...
171
					}
172
				}
173
			}
174
		}
175
		closedir( $dh );
176
177
		do_action( 'wpsc_move_theme' );
178
	}
179
}
180
/**
181
 * Initializes WPSC_Theming global.
182
 *
183
 * A relic of days gone by, an awkwardly named class intended for use in the migration of theme templates from
184
 * the core theme folders into the currently active theme folder.
185
 *
186
 * @since  3.8.14.4
187
 * @return void
188
 */
189
function wpsc_init_theming_global() {
190
	global $wpsc_theming;
191
	$wpsc_theming = new wpsc_theming();
192
}
193
194
if ( isset( $_REQUEST['wpsc_move_themes'] ) && ! empty( $_REQUEST['wpsc_move_themes'] ) ) {
195
	add_action( 'admin_init', 'wpsc_init_theming_global' );
196
}