Completed
Pull Request — master (#170)
by Stephanie
02:37
created

FrmFormMigratorsHelper::is_dismissed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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