{ return 'import-code-snippets'; } elseif ( in_array( $menu, $settings, true ) ) { return 'snippets-settings'; } elseif ( in_array( $menu, $welcome, true ) ) { return 'code-snippets-welcome'; } else { return 'snippets'; } } /** * Fetch the URL to a snippets admin menu. * * @param string $menu Name of menu to retrieve the URL to. * @param string $context URL scheme to use. * * @return string The menu's URL. */ public function get_menu_url( string $menu = '', string $context = 'self' ): string { $slug = $this->get_menu_slug( $menu ); if ( $this->is_compact_menu() && 'network' !== $context ) { $base_slug = $this->get_menu_slug(); $url = 'tools.php?page=' . $base_slug; if ( $slug !== $base_slug ) { $url .= '&sub=' . $slug; } } else { $url = 'admin.php?page=' . $slug; } if ( 'network' === $context || 'snippets-settings' === $slug ) { return network_admin_url( $url ); } elseif ( 'admin' === $context ) { return admin_url( $url ); } else { return self_admin_url( $url ); } } /** * Fetch the admin menu slug for a snippets admin menu. * * @param integer $snippet_id Snippet ID. * @param string $context URL scheme to use. * * @return string The URL to the edit snippet page for that snippet. */ public function get_snippet_edit_url( int $snippet_id, string $context = 'self' ): string { return add_query_arg( 'id', absint( $snippet_id ), $this->get_menu_url( 'edit', $context ) ); } /** * Allow redirecting to the Code Snippets site. * * @param array $hosts Allowed hosts. * * @return array Modified allowed hosts. */ public function allow_code_snippets_redirect( array $hosts ): array { $hosts[] = 'codesnippets.pro'; $hosts[] = 'snipco.de'; return $hosts; } /** * Determine whether the current user can perform actions on snippets. * * @return boolean Whether the current user has the required capability. * * @since 2.8.6 */ public function current_user_can(): bool { return current_user_can( $this->get_cap() ); } /** * Retrieve the name of the capability required to manage sub-site snippets. * * @return string */ public function get_cap_name(): string { return apply_filters( 'code_snippets_cap', 'manage_options' ); } /** * Retrieve the name of the capability required to manage network snippets. * * @return string */ public function get_network_cap_name(): string { return apply_filters( 'code_snippets_network_cap', 'manage_network_options' ); } /** * Get the required capability to perform a certain action on snippets. * Does not check if the user has this capability or not. * * If multisite, checks if *Enable Administration Menus: Snippets* is active * under the *Settings > Network Settings* network admin menu * * @return string The capability required to manage snippets. * * @since 2.0 */ public function get_cap(): string { if ( is_multisite() ) { $menu_perms = get_site_option( 'menu_items', array() ); // If multisite is enabled and the snippet menu is not activated, restrict snippet operations to super admins only. if ( empty( $menu_perms['snippets'] ) ) { return $this->get_network_cap_name(); } } return $this->get_cap_name(); } /** * Inject the safe mode query var into URLs * * @param string $url Original URL. * * @return string Modified URL. */ public function add_safe_mode_query_var( string $url ): string { return isset( $_REQUEST['snippets-safe-mode'] ) ? add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url ) : $url; } /** * Retrieve a list of available snippet types and their labels. * * @return array Snippet types. */ public static function get_types(): array { return apply_filters( 'code_snippets_types', array( 'php' => __( 'Functions', 'code-snippets' ), 'html' => __( 'Content', 'code-snippets' ), 'cloud_search' => __( 'Cloud Search', 'code-snippets' ), 'css' => __( 'Styles', 'code-snippets' ), 'js' => __( 'Scripts', 'code-snippets' ), 'cloud' => __( 'Codevault', 'code-snippets' ), 'bundles' => __( 'Bundles', 'code-snippets' ), ) ); } /** * Determine whether a snippet type is Pro-only. * * @param string $type Snippet type name. * * @return bool */ public static function is_pro_type( string $type ): bool { return 'css' === $type || 'js' === $type || 'cloud' === $type || 'bundles' === $type; } /** * Localise a plugin script to provide the CODE_SNIPPETS object. * * @param string $handle Script handle. * * @return void */ public function localize_script( string $handle ) { wp_localize_script( $handle, 'CODE_SNIPPETS', [ 'isLicensed' => false, 'restAPI' => [ 'base' => esc_url_raw( rest_url() ), 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), ], 'urls' => [ 'plugin' => plugins_url( '', PLUGIN_FILE ), 'manage' => $this->get_menu_url(), 'edit' => $this->get_menu_url( 'edit' ), 'addNew' => $this->get_menu_url( 'add' ), ], ] ); } }