PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / class-export.php

class-export.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at admin/class-export.php

150 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin\Export
6 */
7
8 /**
9 * Class WPSEO_Export.
10 *
11 * Class with functionality to export the WP SEO settings.
12 */
13 class WPSEO_Export {
14
15 /**
16 * Holds the nonce action.
17 *
18 * @var string
19 */
20 const NONCE_ACTION = 'wpseo_export';
21
22 /**
23 * Holds the export data.
24 *
25 * @var string
26 */
27 private $export = '';
28
29 /**
30 * Holds whether the export was a success.
31 *
32 * @var bool
33 */
34 public $success;
35
36 /**
37 * Handles the export request.
38 */
39 public function export() {
40 check_admin_referer( self::NONCE_ACTION );
41 $this->export_settings();
42 $this->output();
43 }
44
45 /**
46 * Outputs the export.
47 */
48 public function output() {
49 if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
50 esc_html_e( 'You do not have the required rights to export settings.', 'wordpress-seo' );
51 return;
52 }
53
54 echo '<p id="wpseo-settings-export-desc">';
55 printf(
56 /* translators: %1$s expands to Import settings */
57 esc_html__(
58 'Copy all these settings to another site\'s %1$s tab and click "%1$s" there.',
59 'wordpress-seo'
60 ),
61 esc_html__(
62 'Import settings',
63 'wordpress-seo'
64 )
65 );
66 echo '</p>';
67 /* translators: %1$s expands to Yoast SEO */
68 echo '<label for="wpseo-settings-export" class="yoast-inline-label">' . sprintf( __( 'Your %1$s settings:', 'wordpress-seo' ), 'Yoast SEO' ) . '</label><br />';
69 echo '<textarea id="wpseo-settings-export" rows="20" cols="100" aria-describedby="wpseo-settings-export-desc">' . esc_textarea( $this->export ) . '</textarea>';
70 }
71
72 /**
73 * Exports the current site's WP SEO settings.
74 */
75 private function export_settings() {
76 $this->export_header();
77
78 foreach ( WPSEO_Options::get_option_names() as $opt_group ) {
79 $this->write_opt_group( $opt_group );
80 }
81 }
82
83 /**
84 * Writes the header of the export.
85 */
86 private function export_header() {
87 $header = sprintf(
88 /* translators: %1$s expands to Yoast SEO, %2$s expands to Yoast.com */
89 esc_html__( 'These are settings for the %1$s plugin by %2$s', 'wordpress-seo' ),
90 'Yoast SEO',
91 'Yoast.com'
92 );
93 $this->write_line( '; ' . $header );
94 }
95
96 /**
97 * Writes a line to the export.
98 *
99 * @param string $line Line string.
100 * @param bool $newline_first Boolean flag whether to prepend with new line.
101 */
102 private function write_line( $line, $newline_first = false ) {
103 if ( $newline_first ) {
104 $this->export .= PHP_EOL;
105 }
106 $this->export .= $line . PHP_EOL;
107 }
108
109 /**
110 * Writes an entire option group to the export.
111 *
112 * @param string $opt_group Option group name.
113 */
114 private function write_opt_group( $opt_group ) {
115
116 $this->write_line( '[' . $opt_group . ']', true );
117
118 $options = get_option( $opt_group );
119
120 if ( ! is_array( $options ) ) {
121 return;
122 }
123
124 foreach ( $options as $key => $elem ) {
125 if ( is_array( $elem ) ) {
126 $count = count( $elem );
127 for ( $i = 0; $i < $count; $i++ ) {
128 $this->write_setting( $key . '[]', $elem[ $i ] );
129 }
130 }
131 else {
132 $this->write_setting( $key, $elem );
133 }
134 }
135 }
136
137 /**
138 * Writes a settings line to the export.
139 *
140 * @param string $key Key string.
141 * @param string $val Value string.
142 */
143 private function write_setting( $key, $val ) {
144 if ( is_string( $val ) ) {
145 $val = '"' . $val . '"';
146 }
147 $this->write_line( $key . ' = ' . $val );
148 }
149 }
150