Completed
Pull Request — master (#151)
by Stephanie
02:30
created

FrmFormMigratorsHelper::install_button()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 2
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormMigratorsHelper {
7
8
	public static function notification_count() {
9
		$forms = self::import_links();
10
		if ( empty( $forms ) ) {
11
			return '';
12
		}
13
14
		$count = count( $forms );
15
		$dismissed = get_option( 'frm_dismissed' );
16
		if ( ! empty( $dismissed ) ) {
17
			foreach ( $forms as $form ) {
18
				if ( self::is_dismissed( $form, $dismissed ) ) {
19
					$count --;
20
				}
21
			}
22
		}
23
24
		if ( $count < 1 ) {
25
			return '';
26
		}
27
28
		return ' <span class="update-plugins"><span class="plugin-count">' . absint( $count ) . '</span></span>';
29
	}
30
31
	private static function is_dismissed( $form, $dismissed = null ) {
32
		if ( $dismissed === null ) {
33
			$dismissed = get_option( 'frm_dismissed' );
34
		}
35
36
		if ( ! empty( $dismissed ) && in_array( $form['class'], $dismissed ) ) {
37
			return true;
38
		}
39
40
		return false;
41
	}
42
43
	public static function maybe_show_download_link() {
44
		$forms = self::import_links();
45
		foreach ( $forms as $form ) {
46
			if ( ! self::is_dismissed( $form ) ) {
47
				self::install_banner( $form );
48
			} else {
49
				echo '<span>';
50
				self::install_button( $form, 'auto' );
51
				echo '</span>';
52
			}
53
		}
54
	}
55
56
	private static function import_links() {
57
		if ( ! current_user_can( 'activate_plugins' ) ) {
58
			return array();
59
		}
60
61
		$forms = array();
62
		foreach ( self::importable_forms() as $form ) {
63
			if ( class_exists( $form['class'] ) || ! is_plugin_active( $form['plugin'] ) ) {
64
				// Either the importer is installed or the source plugin isn't.
65
				continue;
66
			}
67
68
			$installer = new FrmInstallPlugin( array( 'plugin_file' => $form['importer'] ) );
69
			$form['installed'] = $installer->is_installed();
70
			$form['link']      = $installer->get_activate_link();
71
72
			$forms[] = $form;
73
		}
74
		return $forms;
75
	}
76
77
	private static function importable_forms() {
78
		return array(
79
			/*'gf' => array(
80
				'class'    => 'FrmGravityImporter',
81
				'plugin'   => 'gravityforms/gravityforms.php',
82
				'importer' => 'formidable-gravity-forms-importer/formidable-gravity-forms-importer.php',
83
				'name'     => 'Gravity Forms',
84
				'package'  => 'https://downloads.wordpress.org/plugin/formidable-gravity-forms-importer.zip',
85
			),*/
86
			'pf' => array(
87
				'class'    => 'FrmPirateImporter',
88
				'plugin'   => 'pirate-forms/pirate-forms.php',
89
				'importer' => 'formidable-import-pirate-forms/pf-to-frm.php',
90
				'name'     => 'Pirate Forms',
91
				'package'  => 'https://downloads.wordpress.org/plugin/formidable-import-pirate-forms.zip',
92
			),
93
		);
94
	}
95
96
	private static function install_banner( $install ) {
97
		if ( empty( $install['link'] ) ) {
98
			return '';
99
		}
100
101
		?>
102
		<div class="frm-feature-banner">
103
			<a href="#" class="dismiss alignright" id="<?php echo esc_attr( $install['class'] ); ?>" title="<?php esc_attr_e( 'Dismiss this message', 'formidable' ); ?>">
104
				<?php FrmAppHelper::icon_by_class( 'frmfont frm_close_icon', array( 'aria-label' => 'Dismiss' ) ); ?>
105
			</a>
106
			<div class="frm-big-icon">
107
				<?php FrmAppHelper::icon_by_class( 'frmfont frm_cloud_upload_solid_icon', array( 'aria-label' => 'Import' ) ); ?>
108
			</div>
109
			<p>Did you know you can import your forms created in <?php echo esc_html( $install['name'] ); ?>?</p>
110
			<?php self::install_button( $install ); ?>
111
		</div>
112
		<?php
113
	}
114
115
	private static function install_button( $install, $label = '' ) {
116
		$primary = 'button-secondary frm-button-secondary ';
117
118
		if ( empty( $label ) ) {
119
			$label   = __( 'Get Started', 'formidable' );
120
			$primary = 'button-primary frm-button-primary ';
121
		}
122
123
		if ( $install['installed'] ) {
124
			?>
125
			<a rel="<?php echo esc_attr( $install['importer'] ); ?>" class="button frm-activate-addon <?php echo esc_attr( $primary . ( empty( $install['link'] ) ? 'frm_hidden' : '' ) ); ?>">
126
			<?php
127
			if ( $label === 'auto' ) {
128
				/* translators: %s: Name of the plugin */
129
				$label = sprintf( __( 'Activate %s', 'formidable' ), $install['name'] );
130
			}
131
		} else {
132
			?>
133
			<a rel="<?php echo esc_attr( $install['package'] ); ?>" class="frm-install-addon button <?php echo esc_attr( $primary ); ?>" aria-label="<?php esc_attr_e( 'Install', 'formidable' ); ?>">
134
			<?php
135
			if ( $label === 'auto' ) {
136
				/* translators: %s: Name of the plugin */
137
				$label = sprintf( __( 'Install %s Importer', 'formidable' ), $install['name'] );
138
			}
139
		}
140
		?>
141
		<?php echo esc_html( $label ); ?>
142
		</a>
143
		<?php
144
	}
145
146
	public static function dismiss_migrator() {
147
		check_ajax_referer( 'frm_ajax', 'nonce' );
148
		$dismissed = get_option( 'frm_dismissed' );
149
		if ( empty( $dismissed ) ) {
150
			$dismissed = array();
151
		}
152
		$dismissed[] = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
153
		update_option( 'frm_dismissed', array_filter( $dismissed ), 'no' );
154
		wp_die();
155
	}
156
}
157